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.BusinessAwardConfirm;
import com.sunxung.factoring.entity.businessmanagement.BusinessBasicInfo;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.mapper.businessManagement.BusinessAwardConfirmMapper;
import com.sunxung.factoring.service.businessmanagement.BusinessBasicInfoService;
import com.sunxung.factoring.service.businessmanagement.IBusinessAwardConfirmService;
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.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;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务管理 中标确认 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2023-02-27
 */
@Service
public class BusinessAwardConfirmServiceImpl extends ServiceImpl<BusinessAwardConfirmMapper, BusinessAwardConfirm> implements IBusinessAwardConfirmService {

//    @Autowired
//    private ISysAttachmentRefService sysAttachmentRefService;
//
//    @Autowired
//    private BusinessBasicInfoService businessBasicInfoService;
//
//    @Autowired
//    private FlowableService flowableService;
//
//    @Autowired
//    private BusinessAssigneeService assigneeService;
//
//    @Autowired
//    private ICSalesContractBasicInfoService salesContractBasicInfoService;
//
//    @Autowired
//    private DictionaryService dictionaryService;
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void add(BusinessAwardConfirm businessAwardConfirm) {
//        //基础信息
//        businessAwardConfirm.setBusinessKey(businessAwardConfirm.getBusinessBasicInfoId());
//        save(businessAwardConfirm);
//        //保存附件 支付凭证
//        if (CollectionUtil.isNotEmpty(businessAwardConfirm.getPaymentVoucher())) {
//            AttachmentDto paymentVoucherAttachmentDto = AttachmentDto.builder()
//                    .setEntityId(businessAwardConfirm.getId())
//                    .setChildEnum(FileModuleDict.ChildEnum.PAYMENT_VOUCHER)
//                    .build();
//            List<Long> paymentVoucher = businessAwardConfirm.getPaymentVoucher().stream().map(r -> r.getId()).collect(Collectors.toList());
//            sysAttachmentRefService.saveRef(paymentVoucher, paymentVoucherAttachmentDto);
//        }
//        //保存附件 投标凭证
//        if (CollectionUtil.isNotEmpty(businessAwardConfirm.getTenderDocuments())) {
//            AttachmentDto tenderDocumentsAttachmentDto = AttachmentDto.builder()
//                    .setEntityId(businessAwardConfirm.getId())
//                    .setChildEnum(FileModuleDict.ChildEnum.TENDER_DOCUMENTS)
//                    .build();
//            List<Long> tenderDocuments = businessAwardConfirm.getTenderDocuments().stream().map(r -> r.getId()).collect(Collectors.toList());
//            sysAttachmentRefService.saveRef(tenderDocuments, tenderDocumentsAttachmentDto);
//        }
//        //保存附件 中标通知书
//        if (CollectionUtil.isNotEmpty(businessAwardConfirm.getBidWinningNotice())) {
//            AttachmentDto bidWinningNoticeAttachmentDto = AttachmentDto.builder()
//                    .setEntityId(businessAwardConfirm.getId())
//                    .setChildEnum(FileModuleDict.ChildEnum.BIDDING_BID_WINNING_NOTICE)
//                    .build();
//            List<Long> bidWinningNotice = businessAwardConfirm.getBidWinningNotice().stream().map(r -> r.getId()).collect(Collectors.toList());
//            sysAttachmentRefService.saveRef(bidWinningNotice, bidWinningNoticeAttachmentDto);
//        }
//        BusinessBasicInfo businessBasicInfo = businessBasicInfoService.getById(businessAwardConfirm.getBusinessKey());
//        Map<String, Object> procVariables = new HashMap<>();
//        switch (businessAwardConfirm.getWinTheBidding()) {
//            case 1:
//                procVariables.put("result",ConstantUtil.AWARD);
//                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.NOT_AWARD);
//                businessBasicInfo.setStage(dictionaryService.getByCode(BusinessStageDict.ChildEnum.REFUND_DEPOSIT.getCode()).getId());
//                businessBasicInfoService.updateById(businessBasicInfo);
//                break;
//            default:
//                throw new ValidatorException(CodeUtil.FAIL,"是否中标不明确！");
//
//        }
//        //保证金退款确认操作岗  业务经理
//        procVariables.put(ConstantUtil.REFUND_DEPOSIT_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());
//        if (businessAwardConfirm.getWinTheBidding().equals(1)) {
//            msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
//                    "业务编号为：" + businessBasicInfo.getBusinessNumber() + "”的业务中标确认已完成，进入“"
//                            + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.SALES_CONTRACT) + "—录入”阶段，"
//                            + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
//            //开启销售合同流程
//            salesContractBasicInfoService.startSalesContractProcess(businessBasicInfo, businessBasicInfo.getBusinessNumber());
//        } else if (businessAwardConfirm.getWinTheBidding().equals(0)) {
//            msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
//                    "业务编号为：" + businessBasicInfo.getBusinessNumber() + "”的业务中标确认已完成，进入“"
//                            + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.BUSINESS_MANAGEMENT) + "—保证金退款确认”阶段，"
//                            + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
//        }
//        flowableService.completeMyTask(businessAwardConfirm.getTaskId(), businessBasicInfo, procVariables, null, businessAwardConfirm.getId(), msgVariables, ConstantUtil.BUSINESS_MANAGEMENT);
//
//
//    }
//
//    @Override
//    public BusinessAwardConfirm getView(Long formBusinessId) {
//        //基础信息
//        BusinessAwardConfirm businessAwardConfirm = getById(formBusinessId);
//        // 支付凭证
//        AttachmentDto paymentVoucherAttachmentDto = AttachmentDto.builder()
//                .setEntityId(businessAwardConfirm.getId())
//                .setChildEnum(FileModuleDict.ChildEnum.PAYMENT_VOUCHER)
//                .build();
//        List<FileStorage> paymentVoucher = sysAttachmentRefService.getFiles(paymentVoucherAttachmentDto);
//        businessAwardConfirm.setPaymentVoucher(paymentVoucher);
//        // 投标凭证
//        AttachmentDto tenderDocumentsAttachmentDto = AttachmentDto.builder()
//                .setEntityId(businessAwardConfirm.getId())
//                .setChildEnum(FileModuleDict.ChildEnum.TENDER_DOCUMENTS)
//                .build();
//        List<FileStorage> tenderDocuments = sysAttachmentRefService.getFiles(tenderDocumentsAttachmentDto);
//        businessAwardConfirm.setTenderDocuments(tenderDocuments);
//        // 中标通知书
//        AttachmentDto bidWinningNoticeAttachmentDto = AttachmentDto.builder()
//                .setEntityId(businessAwardConfirm.getId())
//                .setChildEnum(FileModuleDict.ChildEnum.BIDDING_BID_WINNING_NOTICE)
//                .build();
//        List<FileStorage> bidWinningNotice = sysAttachmentRefService.getFiles(bidWinningNoticeAttachmentDto);
//        businessAwardConfirm.setBidWinningNotice(bidWinningNotice);
//        return businessAwardConfirm;
//    }
}
