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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.util.CollectionUtil;
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.BusinessRefundDepositConfirmation;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.mapper.businessManagement.BusinessRefundDepositConfirmationMapper;
import com.sunxung.factoring.service.businessmanagement.BusinessBasicInfoService;
import com.sunxung.factoring.service.businessmanagement.IBusinessRefundDepositConfirmationService;
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-28
 */
@Service
public class BusinessRefundDepositConfirmationServiceImpl extends ServiceImpl<BusinessRefundDepositConfirmationMapper, BusinessRefundDepositConfirmation> implements IBusinessRefundDepositConfirmationService {

//    @Autowired
//    private ISysAttachmentRefService sysAttachmentRefService;
//
//    @Autowired
//    private BusinessBasicInfoService businessBasicInfoService;
//
//    @Autowired
//    private DictionaryService dictionaryService;
//
//    @Autowired
//    private FlowableService flowableService;
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void add(BusinessRefundDepositConfirmation businessRefundDepositConfirmation) {
//        //基础信息
//        businessRefundDepositConfirmation.setBusinessKey(businessRefundDepositConfirmation.getBusinessBasicInfoId());
//        save(businessRefundDepositConfirmation);
//        BusinessBasicInfo businessBasicInfo = businessBasicInfoService.get(businessRefundDepositConfirmation.getBusinessKey());
//        businessBasicInfo.setStage(dictionaryService.getByCode(BusinessStageDict.ChildEnum.EMPTY.getCode()).getId());
//        businessBasicInfo.setStatusDic(dictionaryService.getByCode(ProjectInitiationStatusDict.ChildEnum.TERMINATED.getCode()).getId());
//        businessBasicInfoService.updateById(businessBasicInfo);
//        //退还凭证
//        if (CollectionUtil.isNotEmpty(businessRefundDepositConfirmation.getReturnVoucher())) {
//            List<Long> returnVoucher = businessRefundDepositConfirmation.getReturnVoucher().stream().map(r -> r.getId()).collect(Collectors.toList());
//            AttachmentDto returnVoucherAttachmentDto = AttachmentDto.builder()
//                    .setEntityId(businessRefundDepositConfirmation.getId())
//                    .setChildEnum(FileModuleDict.ChildEnum.RETURN_VOUCHER)
//                    .build();
//            sysAttachmentRefService.saveRef(returnVoucher, returnVoucherAttachmentDto);
//        }
//        //完成审核流程流转到下一流程或者完成流程
//        Map<String, Object> msgVariables = new HashMap<>();
//        msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "业务编号为：" + businessBasicInfo.getBusinessNumber());
//        msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
//                "业务编号为：" + businessBasicInfo.getBusinessNumber() + "”的业务保证金退款确认已完成。“");
//        flowableService.completeMyTask(businessRefundDepositConfirmation.getTaskId(), businessBasicInfo, null, null, businessRefundDepositConfirmation.getId(), msgVariables, ConstantUtil.BUSINESS_MANAGEMENT);
//    }
//
//    @Override
//    public BusinessRefundDepositConfirmation getView(Long formBusinessId) {
//        //基础信息
//        BusinessRefundDepositConfirmation businessRefundDepositConfirmation = getById(formBusinessId);
//        //退还凭证
//        AttachmentDto returnVoucherAttachmentDto = AttachmentDto.builder()
//                .setEntityId(businessRefundDepositConfirmation.getId())
//                .setChildEnum(FileModuleDict.ChildEnum.RETURN_VOUCHER)
//                .build();
//        List<FileStorage> files = sysAttachmentRefService.getFiles(returnVoucherAttachmentDto);
//        businessRefundDepositConfirmation.setReturnVoucher(files);
//        return businessRefundDepositConfirmation;
//    }
}
