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

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.ConstantConversionUtil;
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.businessmanagement.BusinessBasicInfo;
import com.sunxung.factoring.entity.businessmanagement.BusinessGuaranteeMode;
import com.sunxung.factoring.entity.project.BusinessAssignee;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.mapper.businessManagement.BusinessGuaranteeModeMapper;
import com.sunxung.factoring.service.businessmanagement.BusinessBasicInfoService;
import com.sunxung.factoring.service.businessmanagement.IBusinessGuaranteeModeService;
import com.sunxung.factoring.service.project.BusinessAssigneeService;
import com.sunxung.factoring.service.salescontractmanagement.ICSalesContractBasicInfoService;
import com.sunxung.factoring.service.sys.DictionaryService;
import com.sunxung.factoring.service.sys.ISysAttachmentRefService;
import com.sunxung.factoring.service.sys.UserService;
import com.sunxung.factoring.service.sys.dto.AttachmentDto;
import com.sunxung.factoring.service.sys.flowable.FlowableService;
import org.apache.commons.lang3.StringUtils;
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;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务管理 保证方式信息 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2023-02-27
 */
@Service
public class BusinessGuaranteeModeServiceImpl extends ServiceImpl<BusinessGuaranteeModeMapper, BusinessGuaranteeMode> implements IBusinessGuaranteeModeService {

//    @Autowired
//    private BusinessBasicInfoService businessBasicInfoService;
//
//    @Autowired
//    private FlowableService flowableService;
//
//    @Autowired
//    private BusinessAssigneeService assigneeService;
//
//    @Autowired
//    private UserService userService;
//
//    @Autowired
//    private SalesContractBasicInfoService salesContractBasicInfoService;
//
//    @Autowired
//    private ISysAttachmentRefService sysAttachmentRefService;
//
//    @Autowired
//    private DictionaryService dictionaryService;
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void add(BusinessGuaranteeMode businessGuaranteeMode) {
//        //保存保证方式确认
//        businessGuaranteeMode.setBusinessKey(businessGuaranteeMode.getBusinessBasicInfoId());
//        save(businessGuaranteeMode);
//        //保存文件 保函文件
//        if(CollectionUtil.isNotEmpty(businessGuaranteeMode.getGuaranteeFiles())){
//            AttachmentDto guaranteeFilesAttachmentDto = AttachmentDto.builder()
//                    .setEntityId(businessGuaranteeMode.getId())
//                    .setChildEnum(FileModuleDict.ChildEnum.GUARANTEE_FILE)
//                    .build();
//            List<Long> guaranteeFiles = businessGuaranteeMode.getGuaranteeFiles().stream().map(r->r.getId()).collect(Collectors.toList());
//            sysAttachmentRefService.saveRef(guaranteeFiles, guaranteeFilesAttachmentDto);
//        }
//        //中标通知书文件
//        if(CollectionUtil.isNotEmpty(businessGuaranteeMode.getBidWinningNotice())){
//            AttachmentDto bidWinningNoticeAttachmentDto = AttachmentDto.builder()
//                    .setEntityId(businessGuaranteeMode.getId())
//                    .setChildEnum(FileModuleDict.ChildEnum.BID_WINNING_NOTICE)
//                    .build();
//            List<Long> bidWinningNotice = businessGuaranteeMode.getBidWinningNotice().stream().map(r->r.getId()).collect(Collectors.toList());
//            sysAttachmentRefService.saveRef(bidWinningNotice, bidWinningNoticeAttachmentDto);
//        }
//        BusinessBasicInfo businessBasicInfo = businessBasicInfoService.getById(businessGuaranteeMode.getBusinessKey());
//        Map<String, Object> procVariables = new HashMap<>();
//        switch (businessGuaranteeMode.getBidGuaranteeMethod()) {
//            case 1: ;
//                procVariables.put("result",ConstantUtil.GUARANTEE);
//                businessBasicInfo.setStage(dictionaryService.getByCode(BusinessStageDict.ChildEnum.EMPTY.getCode()).getId());
//                businessBasicInfo.setStatusDic(dictionaryService.getByCode(ProjectInitiationStatusDict.ChildEnum.COMPLETED.getCode()).getId());
//                businessBasicInfoService.updateById(businessBasicInfo);
//                break;
//            case 0:
//                procVariables.put("result",ConstantUtil.BOND);
//                businessBasicInfo.setStage(dictionaryService.getByCode(BusinessStageDict.ChildEnum.BOND_COLLECTION.getCode()).getId());
//                businessBasicInfoService.updateById(businessBasicInfo);
//                break;
//            default:
//                throw new ValidatorException(CodeUtil.FAIL,"保证方式不正确！");
//
//        }
//
//        //保证方式确认操作岗  会计
//        List<Long> accountantLists = userService.findUserIdByRole(ConstantUtil.ACCOUNTANT);
//        if (accountantLists == null || accountantLists.size() == 0) {
//            throw new ValidatorException(CodeUtil.FAIL, ConstantUtil.ACCOUNTANT + "角色内无任何用户！");
//        }
//        BusinessAssignee accountantList = new BusinessAssignee(businessBasicInfo.getId(), ConstantUtil.BUSINESS_MANAGEMENT);
//        accountantList.setCandidateId(StringUtils.join(accountantLists, ","));
//        accountantList.setAssigneeType(ConstantUtil.ASSIGNEE_ACCOUNTANT);
//        assigneeService.add(accountantList);
//
//        //3.开启流程实例   保证方式确认操作岗  业务经理
//        procVariables.put(ConstantUtil.MARGIN_COLLECTION_CONFIRM_CANDIDATE,
//                assigneeService.getCandidateByBusiness(businessBasicInfo.getId(), ConstantUtil.BUSINESS_MANAGEMENT, ConstantUtil.ASSIGNEE_ACCOUNTANT));
//        //完成审核流程流转到下一流程或者完成流程
//        Map<String, Object> msgVariables = new HashMap<>();
//        msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "业务编号为：" + businessBasicInfo.getBusinessNumber());
//        if (businessGuaranteeMode.getBidGuaranteeMethod().equals(1)) {
//            msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
//                    "业务编号为：" + businessBasicInfo.getBusinessNumber() + "”的业务保证金方式为" + ConstantUtil.GUARANTEE + "，进入“"
//                            + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.SALES_CONTRACT) + "—录入”阶段，"
//                            + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
//            //开启销售合同流程
//            salesContractBasicInfoService.startSalesContractProcess(businessBasicInfo, businessBasicInfo.getBusinessNumber());
//        } else if (businessGuaranteeMode.getBidGuaranteeMethod().equals(0)) {
//            msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
//                    "业务编号为：" + businessBasicInfo.getBusinessNumber() + "”的业务保证金方式为" + ConstantUtil.BOND + "，进入“"
//                            + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.BUSINESS_MANAGEMENT) + "—保证金收款确认”阶段，"
//                            + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
//        }
//        flowableService.completeMyTask(businessGuaranteeMode.getTaskId(), businessBasicInfo, procVariables, null, businessGuaranteeMode.getId(), msgVariables, ConstantUtil.BUSINESS_MANAGEMENT, accountantLists);
//    }
//
//    @Override
//    public BusinessGuaranteeMode getView(Long formBusinessId) {
//        //基础信息
//        BusinessGuaranteeMode businessGuaranteeMode = getById(formBusinessId);
//        //文件信息
//        AttachmentDto guaranteeFilesAttachmentDto = AttachmentDto.builder()
//                .setEntityId(businessGuaranteeMode.getId())
//                .setChildEnum(FileModuleDict.ChildEnum.GUARANTEE_FILE)
//                .build();
//        List<FileStorage> guaranteeFiles = sysAttachmentRefService.getFiles(guaranteeFilesAttachmentDto);
//        businessGuaranteeMode.setGuaranteeFiles(guaranteeFiles);
//        //中标通知书文件
//        AttachmentDto bidWinningNoticeAttachmentDto = AttachmentDto.builder()
//                .setEntityId(businessGuaranteeMode.getId())
//                .setChildEnum(FileModuleDict.ChildEnum.BID_WINNING_NOTICE)
//                .build();
//        List<FileStorage> bidWinningNotice = sysAttachmentRefService.getFiles(bidWinningNoticeAttachmentDto);
//        businessGuaranteeMode.setBidWinningNotice(bidWinningNotice);
//        return businessGuaranteeMode;
//    }
}
