package com.openhis.administration.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.core.common.core.domain.entity.SysDictData;
import com.openhis.common.constant.CommonConstants;
import com.openhis.workflow.domain.SupplyRequest;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.core.common.utils.DateUtils;
import com.core.common.utils.SecurityUtils;
import com.core.system.mapper.SysDictDataMapper;
import com.openhis.administration.domain.ChangePriceRecord;
import com.openhis.administration.dto.ChangePriceDataDto;
import com.openhis.administration.mapper.ChangePriceRecordMapper;
import com.openhis.administration.service.IChangePriceRecordService;
import com.openhis.common.enums.SupplyStatus;

/**
 * 修改价格记录Service业务层处理
 *
 * @author system
 * @date 2025-10-15
 */
@Service
public class ChangePriceRecordServiceImpl extends ServiceImpl<ChangePriceRecordMapper, ChangePriceRecord>
    implements IChangePriceRecordService {

    @Resource
    private SysDictDataMapper sysDictDataMapper;
    @Override
    public boolean submitApproval(String busNo) {
        int updateCount = baseMapper.update(null,
            new LambdaUpdateWrapper<ChangePriceRecord>().eq(ChangePriceRecord::getBusNo, busNo)
                .set(ChangePriceRecord::getApplicantTime, DateUtils.getNowDate())
                .set(ChangePriceRecord::getStatusEnum, SupplyStatus.APPROVAL.getValue()));
        return updateCount > 0;
    }

    @Override
    public List<ChangePriceRecord> searchChangePriceDataForBusNo(String busNo) {
        return this.baseMapper.selectList(new LambdaUpdateWrapper<ChangePriceRecord>().eq(ChangePriceRecord::getBusNo,busNo));
    }

    @Override
    public List<ChangePriceDataDto> searchMedChangePriceByBusNo(String busNo, Integer categoryEnum,Integer statusEnum) {
        return this.baseMapper.searchMedChangePriceByBusNo(busNo, categoryEnum,statusEnum);
    }

    @Override
    public List<ChangePriceDataDto> searchMedChangePriceByItemId(Long itemId, Integer categoryEnum, Integer statusEnum) {
        return this.baseMapper.searchMedChangePriceByItemId(itemId,categoryEnum,statusEnum);
    }

    @Override
    public List<ChangePriceDataDto> searchDeviceChangePriceByBusNo(String busNo, Integer categoryEnum,Integer statusEnum) {
        return this.baseMapper.searchDeviceChangePriceByBusNo(busNo,categoryEnum, statusEnum);
    }

    @Override
    public List<ChangePriceDataDto> searchDeviceChangePriceByItemId(Long itemId, Integer categoryEnum, Integer statusEnum) {
        return this.baseMapper.searchDeviceChangePriceByItemId(itemId,categoryEnum,statusEnum);
    }

    @Override
    public List<ChangePriceDataDto> searchActivityChangePriceByBusNo(String busNo, Integer categoryEnum,Integer statusEnum) {
        return this.baseMapper.searchActivityChangePriceByBusNo(busNo,categoryEnum, statusEnum);
    }

    @Override
    public List<ChangePriceDataDto> searchActivityChangePriceByItemId(Long itemId, Integer categoryEnum, Integer statusEnum) {
        return this.baseMapper.searchActivityChangePriceByItemId(itemId,categoryEnum,statusEnum);
    }

    @Override
    public List<ChangePriceDataDto> searchHealthChangePriceByBusNo(String busNo, Integer categoryEnum,Integer statusEnum) {
        return this.baseMapper.searchHealthChangePriceByBusNo(busNo,categoryEnum,statusEnum);
    }

    @Override
    public List<ChangePriceDataDto> searchHealthChangePriceByItemId(Long itemId, Integer categoryEnum, Integer statusEnum) {
        return this.baseMapper.searchHealthChangePriceByItemId(itemId,categoryEnum,statusEnum);
    }

    @Override
    public void agreeRequest(String busNo, Date now) {
        baseMapper.update(null,
                new LambdaUpdateWrapper<ChangePriceRecord>().eq(ChangePriceRecord::getBusNo, busNo)
                        .set(ChangePriceRecord::getApprovalTime, now)
                        .set(ChangePriceRecord::getApproverId, SecurityUtils.getLoginUser().getPractitionerId())
                        .set(ChangePriceRecord::getStatusEnum, SupplyStatus.AGREE.getValue()));
    }

    @Override
    public boolean rejectRequest(String busNo) {
        int updateCount = baseMapper.update(null,
                new LambdaUpdateWrapper<ChangePriceRecord>().eq(ChangePriceRecord::getBusNo, busNo)
                        .set(ChangePriceRecord::getApprovalTime, DateUtils.getNowDate())
                        .set(ChangePriceRecord::getApproverId, SecurityUtils.getLoginUser().getPractitionerId())
                        .set(ChangePriceRecord::getStatusEnum, SupplyStatus.REJECT.getValue()));
        return updateCount > 0;
    }

    @Override
    public List<ChangePriceDataDto> searchChangePriceRecordDataByBusNo(String busNo, Integer statusEnum) {
        return this.baseMapper.searchChangePriceRecordDataByBusNo(busNo,statusEnum);
    }

    @Override
    public List<ChangePriceRecord> searchChangePriceDataGroupByBusNo(String busNo) {
        return this.baseMapper.searchChangePriceDataGroupByBusNo(busNo);
    }

}