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

import com.sunxung.factoring.component.exception.ValidatorException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.component.util.ConstantUtil;
import com.sunxung.factoring.dict.impl.BusinessStageDict;
import com.sunxung.factoring.entity.biddingManagement.BiddingTemplateReview;
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.BiddingTemplateReviewMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.service.biddingManagement.IBiddingTemplateReviewService;
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.component.util.ConstantConversionUtil;
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("template_review_approvalService")
public class BiddingTemplateReviewServiceImpl extends ServiceImpl<BiddingTemplateReviewMapper, BiddingTemplateReview>
        implements IBiddingTemplateReviewService {

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


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BiddingTemplateReview biddingTemplateReview) {
        //保存审核信息
        biddingTemplateReview.setBusinessKey(biddingTemplateReview.getBiddingBasicInfoId());
        save(biddingTemplateReview);
        CBiddingBasicInfo biddingBasicInfo = cBiddingBasicInfoService.getById(biddingTemplateReview.getBusinessKey());
        Map<String, Object> procVariables = new HashMap<>();
        switch (biddingTemplateReview.getConclusion()) {
            case 1:
                procVariables.put("result", ConstantUtil.APPROVAL);
                biddingBasicInfo.setStage(BusinessStageDict.ChildEnum.GUARANTEE_MODE.getCode());
                cBiddingBasicInfoService.updateById(biddingBasicInfo);
                break;
            case 0:
                procVariables.put("result",ConstantUtil.OVERRULE);
                biddingBasicInfo.setStage(BusinessStageDict.ChildEnum.CREATE.getCode());
                cBiddingBasicInfoService.updateById(biddingBasicInfo);
                break;
            default:
                throw new ValidatorException(CodeUtil.FAIL,"结论错误！");

        }
        //3.开启流程实例   保证方式确认操作岗  业务经理
        //获取项目经理
        ProjectInformation projectInformation = projectInformationService.get(biddingBasicInfo.getProjectInformationId());
        SupplierInfo supplierInfo = supplierInfoService.get(projectInformation.getcSupplierInfoId());
        UserDO cUser = cUserService.getById(supplierInfo.getSysUserId());
        User user = bUserService.get(cUser.getProjectManager());

        procVariables.put(ConstantUtil.GUARANTEE_METHOD_CONFIRM_ASSIGNEE, user.getId());
        //完成审核流程流转到下一流程或者完成流程
        Map<String, Object> msgVariables = new HashMap<>();
        //查询项目
        BiddingBasicInfoResultVo basicInfoResultVo = cBiddingBasicInfoService.getCreditProjectByBiddingId(biddingBasicInfo.getId());
        msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "业务编号为：" + basicInfoResultVo.getBusinessNumber());
        if (biddingTemplateReview.getConclusion().equals(0)) {
            //驳回 释放项目
            projectInformationService.lambdaUpdate()
                    .set(ProjectInformation::getAlreadyBidding,0)
                    .eq(ProjectInformation::getBusinessNumber,biddingBasicInfo.getBusinessNumber())
                    .update();

            msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                    "业务编号为：" + basicInfoResultVo.getBusinessNumber() + "”的业务模板审核已驳回，进入“"
                            + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.BIDDING_MANAGEMENT) + "—退回修改”阶段，"
                            + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
        } else if (biddingTemplateReview.getConclusion().equals(1)) {
            msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                    "业务编号为：" + basicInfoResultVo.getBusinessNumber() + "”的业务模板审核已通过,进入“"
                            + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.BIDDING_MANAGEMENT) + "—保证方式确认”阶段，"
                            + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
        }
        flowableService.completeMyTask(biddingTemplateReview.getTaskId(), biddingBasicInfo, procVariables, null,
                biddingTemplateReview.getId(), msgVariables, ConstantUtil.BIDDING_MANAGEMENT);
    }

    @Override
    public BiddingTemplateReview getView(Long formBusinessId) {
        BiddingTemplateReview biddingTemplateReview = getById(formBusinessId);
        return biddingTemplateReview;
    }

}
