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

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.businessmanagement.BusinessBasicInfo;
import com.sunxung.factoring.entity.businessmanagement.BusinessBondCollectionConfirmation;
import com.sunxung.factoring.mapper.businessManagement.BusinessBondCollectionConfirmationMapper;
import com.sunxung.factoring.service.businessmanagement.BusinessBasicInfoService;
import com.sunxung.factoring.service.businessmanagement.IBusinessBondCollectionConfirmationService;
import com.sunxung.factoring.service.project.BusinessAssigneeService;
import com.sunxung.factoring.service.sys.DictionaryService;
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.Map;

/**
 * <p>
 * 业务管理 保证金收款确认 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2023-02-27
 */
@Service
public class BusinessBondCollectionConfirmationServiceImpl extends ServiceImpl<BusinessBondCollectionConfirmationMapper, BusinessBondCollectionConfirmation> implements IBusinessBondCollectionConfirmationService {

    @Autowired
    private BusinessBasicInfoService businessBasicInfoService;

    @Autowired
    private FlowableService flowableService;

    @Autowired
    private BusinessAssigneeService assigneeService;

    @Autowired
    private DictionaryService dictionaryService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BusinessBondCollectionConfirmation businessBondCollectionConfirmation) {
        businessBondCollectionConfirmation.setBusinessKey(businessBondCollectionConfirmation.getBusinessBasicInfoId());
        save(businessBondCollectionConfirmation);

        BusinessBasicInfo businessBasicInfo = businessBasicInfoService.getById(businessBondCollectionConfirmation.getBusinessKey());

        businessBasicInfo.setStage(dictionaryService.getByCode(BusinessStageDict.ChildEnum.AWARD.getCode()).getId());
        businessBasicInfoService.updateById(businessBasicInfo);

        // 中标确认 执行人为项目经理
        Map<String, Object> procVariables = new HashMap<>();
        procVariables.put(ConstantUtil.AWARD_CONFIRM_ASSIGNEE,
                assigneeService.getIdByBusiness(businessBasicInfo.getId(), ConstantUtil.BUSINESS_MANAGEMENT, ConstantUtil.ASSIGNEE_PROJECT_MANAGER));
        //完成审核流程流转到下一流程或者完成流程
        Map<String, Object> msgVariables = new HashMap<>();
        msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "业务编号为：" + businessBasicInfo.getBusinessNumber());
        msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                "业务编号为：" + businessBasicInfo.getBusinessNumber() + "”的业务保证金收款确认完成，进入“"
                        + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.BUSINESS_MANAGEMENT) + "—中标确认”阶段，"
                        + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
        flowableService.completeMyTask(businessBondCollectionConfirmation.getTaskId(), businessBasicInfo, procVariables, null, businessBondCollectionConfirmation.getId(), msgVariables, ConstantUtil.BUSINESS_MANAGEMENT);
    }

    @Override
    public BusinessBondCollectionConfirmation getView(Long formBusinessId) {
        BusinessBondCollectionConfirmation businessBondCollectionConfirmation = getById(formBusinessId);
        return businessBondCollectionConfirmation;
    }
}
