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.util.ConstantConversionUtil;
import com.sunxung.factoring.component.util.ConstantUtil;
import com.sunxung.factoring.dict.impl.BusinessStageDict;
import com.sunxung.factoring.entity.biddingManagement.BiddingBondCollectionConfirmation;
import com.sunxung.factoring.entity.biddingManagement.BiddingGuaranteeMethod;
import com.sunxung.factoring.entity.biddingManagement.CBiddingBasicInfo;
import com.sunxung.factoring.entity.biddingManagement.vo.BiddingBasicInfoResultVo;
import com.sunxung.factoring.entity.project.ProjectInformation;
import com.sunxung.factoring.entity.supplier.SupplierInfo;
import com.sunxung.factoring.entity.sys.User;
import com.sunxung.factoring.entity.sys.UserDO;
import com.sunxung.factoring.mapper.biddingManagement.BiddingBondCollectionConfirmationMapper;
import com.sunxung.factoring.service.biddingManagement.IBiddingBondCollectionConfirmationService;
import com.sunxung.factoring.service.biddingManagement.IBiddingGuaranteeMethodService;
import com.sunxung.factoring.service.biddingManagement.ICBiddingBasicInfoService;
import com.sunxung.factoring.service.project.BusinessAssigneeService;
import com.sunxung.factoring.service.project.IProjectInformationService;
import com.sunxung.factoring.service.supplier.ISupplierInfoService;
import com.sunxung.factoring.service.sys.CUserService;
import com.sunxung.factoring.service.sys.UserService;
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 javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 招投标管理-保证金收款确认表 服务实现类
 * </p>
 *
 * @author yafan
 * @since 2023-11-21
 */
@Service("margin_collection_confirmService")
public class BiddingBondCollectionConfirmationServiceImpl extends ServiceImpl<BiddingBondCollectionConfirmationMapper, BiddingBondCollectionConfirmation> implements IBiddingBondCollectionConfirmationService {

    @Autowired
    private ICBiddingBasicInfoService cBiddingBasicInfoService;
    @Autowired
    private BusinessAssigneeService assigneeService;
    @Autowired
    private FlowableService flowableService;
    @Resource(name = "supplierInfoService")
    private ISupplierInfoService supplierInfoService;
    @Resource(name = "projectInformationService")
    private IProjectInformationService projectInformationService;
    @Autowired
    private CUserService cUserService;
    @Autowired
    private UserService bUserService;
    @Autowired
    private IBiddingGuaranteeMethodService guaranteeMethodService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BiddingBondCollectionConfirmation biddingBondCollectionConfirmation) {
        biddingBondCollectionConfirmation.setBusinessKey(biddingBondCollectionConfirmation.getBiddingBasicInfoId());
        save(biddingBondCollectionConfirmation);

        CBiddingBasicInfo biddingBasicInfo = cBiddingBasicInfoService.getById(biddingBondCollectionConfirmation.getBusinessKey());

        biddingBasicInfo.setStage(BusinessStageDict.ChildEnum.AWARD.getCode());
        cBiddingBasicInfoService.updateById(biddingBasicInfo);

        // 中标确认 执行人为项目经理
        //获取项目经理
        ProjectInformation projectInformation = projectInformationService.get(biddingBasicInfo.getProjectInformationId());
        SupplierInfo supplierInfo = supplierInfoService.get(projectInformation.getcSupplierInfoId());
        UserDO cUser = cUserService.getById(supplierInfo.getSysUserId());
        User user = bUserService.get(cUser.getProjectManager());
        Map<String, Object> procVariables = new HashMap<>();
        procVariables.put(ConstantUtil.AWARD_CONFIRM_ASSIGNEE, user.getId());
        //完成审核流程流转到下一流程或者完成流程
        //查询项目
        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() + "”的保证金收款确认完成，进入“"
                        + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.BIDDING_MANAGEMENT) + "—中标确认”阶段，"
                        + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
        flowableService.completeMyTask(biddingBondCollectionConfirmation.getTaskId(), biddingBasicInfo, procVariables, null,
                biddingBondCollectionConfirmation.getId(), msgVariables, ConstantUtil.BIDDING_MANAGEMENT);
    }

    @Override
    public BiddingBondCollectionConfirmation getView(Long formBusinessId) {
        BiddingBondCollectionConfirmation biddingBondCollectionConfirmation = getById(formBusinessId);
        //从保证方式中查找保证金金额
        BiddingGuaranteeMethod guaranteeMethod = guaranteeMethodService.getOne(new QueryWrapper<BiddingGuaranteeMethod>()
                .eq("bidding_basic_info_id", biddingBondCollectionConfirmation.getBiddingBasicInfoId()));
        biddingBondCollectionConfirmation.setDepositAmount(guaranteeMethod.getDepositAmount());
        return biddingBondCollectionConfirmation;
    }
}
