package com.sunxung.factoring.service.biddingManagement.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.exception.ValidatorException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.component.util.CollectionUtil;
import com.sunxung.factoring.component.util.ConstantUtil;
import com.sunxung.factoring.dict.impl.BusinessStageDict;
import com.sunxung.factoring.dict.impl.FileModuleDict;
import com.sunxung.factoring.dict.impl.ProjectInitiationStatusDict;
import com.sunxung.factoring.entity.biddingManagement.BiddingGuaranteeMethod;
import com.sunxung.factoring.entity.biddingManagement.BiddingRefundDepositConfirmation;
import com.sunxung.factoring.entity.biddingManagement.CBiddingBasicInfo;
import com.sunxung.factoring.entity.biddingManagement.vo.BiddingBasicInfoResultVo;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.mapper.biddingManagement.BiddingRefundDepositConfirmationMapper;
import com.sunxung.factoring.service.biddingManagement.IBiddingGuaranteeMethodService;
import com.sunxung.factoring.service.biddingManagement.IBiddingRefundDepositConfirmationService;
import com.sunxung.factoring.service.biddingManagement.ICBiddingBasicInfoService;
import com.sunxung.factoring.service.sys.ISysAttachmentRefService;
import com.sunxung.factoring.service.sys.dto.AttachmentDto;
import com.sunxung.factoring.service.sys.flowable.FlowableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 招投标管理-保证金退款确认 服务实现类
 * </p>
 *
 * @author yafan
 * @since 2023-11-21
 */
@Service("refund_deposit_confirmService")
public class BiddingRefundDepositConfirmationServiceImpl extends ServiceImpl<BiddingRefundDepositConfirmationMapper,
        BiddingRefundDepositConfirmation> implements IBiddingRefundDepositConfirmationService {

    @Autowired
    private ICBiddingBasicInfoService cBiddingBasicInfoService;
    @Autowired
    private FlowableService flowableService;
    @Autowired
    private ISysAttachmentRefService sysAttachmentRefService;
    @Autowired
    private IBiddingGuaranteeMethodService guaranteeMethodService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BiddingRefundDepositConfirmation biddingRefundDepositConfirmation) {
        //基础信息
        biddingRefundDepositConfirmation.setBusinessKey(biddingRefundDepositConfirmation.getBiddingBasicInfoId());
        save(biddingRefundDepositConfirmation);
        CBiddingBasicInfo biddingBasicInfo = cBiddingBasicInfoService.get(biddingRefundDepositConfirmation.getBusinessKey());
        biddingBasicInfo.setStage(BusinessStageDict.ChildEnum.EMPTY.getCode());
        biddingBasicInfo.setStatusCode(ProjectInitiationStatusDict.ChildEnum.TERMINATED.getCode());
        cBiddingBasicInfoService.updateById(biddingBasicInfo);
        //处理文件
        List<Long> fileIds = biddingRefundDepositConfirmation.getFileIds();
//        if (CollectionUtil.isEmpty(fileIds)) {
//            throw new ValidatorException(CodeUtil.FAIL, "退款凭证不能为空！");
//        }
        AttachmentDto upstreamPurchDto = AttachmentDto.builder().setEntityId(biddingRefundDepositConfirmation.getId())
                .setChildEnum(FileModuleDict.ChildEnum.BIDDING_REFUND_FILE).build();
        sysAttachmentRefService.saveRef(fileIds, upstreamPurchDto);

        //完成审核流程流转到下一流程或者完成流程
        //查询项目
        BiddingBasicInfoResultVo basicInfoResultVo = cBiddingBasicInfoService.getCreditProjectByBiddingId(biddingBasicInfo.getId());
        Map<String, Object> msgVariables = new HashMap<>();
        msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "业务编号为：" + basicInfoResultVo.getBusinessNumber());
        msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                "业务编号为：" + basicInfoResultVo.getBusinessNumber() + "”的保证金退款确认已完成。“");
        flowableService.completeMyTask(biddingRefundDepositConfirmation.getTaskId(), biddingBasicInfo, null,
                null, biddingRefundDepositConfirmation.getId(), msgVariables, ConstantUtil.BIDDING_MANAGEMENT);
    }

    @Override
    public BiddingRefundDepositConfirmation getView(Long formBusinessId) {
        BiddingRefundDepositConfirmation refundDepositConfirmation = getById(formBusinessId);
        //填充文件
        AttachmentDto upstreamPurchDto = AttachmentDto.builder().setEntityId(formBusinessId)
                .setChildEnum(FileModuleDict.ChildEnum.BIDDING_REFUND_FILE).build();
        List<FileStorage> files = sysAttachmentRefService.getFiles(upstreamPurchDto);
        refundDepositConfirmation.setFileStorageList(files);
        //从保证方式中查找保证金金额
        BiddingGuaranteeMethod guaranteeMethod = guaranteeMethodService.getOne(new QueryWrapper<BiddingGuaranteeMethod>()
                .eq("bidding_basic_info_id", refundDepositConfirmation.getBiddingBasicInfoId()));
        guaranteeMethod.setDepositAmount(guaranteeMethod.getDepositAmount());
        return refundDepositConfirmation;
    }
}
