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

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
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.component.util.ElectronicSignatureUtil;
import com.sunxung.factoring.component.util.ThreadLocalUtil;
import com.sunxung.factoring.dict.impl.ProjectInitiationStatusDict;
import com.sunxung.factoring.dict.impl.SettlementDict;
import com.sunxung.factoring.entity.settlement.SettlementBasicInfo;
import com.sunxung.factoring.entity.settlement.SettlementElectronicInfo;
import com.sunxung.factoring.entity.supplier.SupplierInfo;
import com.sunxung.factoring.entity.sys.User;
import com.sunxung.factoring.mapper.settlement.SettlementElectronicInfoMapper;
import com.sunxung.factoring.service.settlement.SettlementBasicInfoService;
import com.sunxung.factoring.service.settlement.SettlementElectronicInfoService;
import com.sunxung.factoring.service.supplier.ISupplierInfoService;
import com.sunxung.factoring.service.sys.DictionaryService;
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 javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liutao
 * @date 2022.3.15
 */
@Service
public class SettlementElectronicInfoServiceImpl extends ServiceImpl<SettlementElectronicInfoMapper, SettlementElectronicInfo> implements SettlementElectronicInfoService {

    @Autowired
    private SettlementBasicInfoService settlementBasicInfoService;

    @Autowired
    private FlowableService flowableService;

    @Autowired
    private DictionaryService dictService;
    @Resource(name = "SupplierInfoService")
    private ISupplierInfoService iSupplierInfoService;
    @Autowired
    private UserService userService;


    @Override
    public void sign(SettlementElectronicInfo settlementElectronicInfo, String taskId) {
        User user = userService.getByLoginName("admin");
        ThreadLocalUtil.setUser(user);
        SettlementBasicInfo settlementBasicInfo = settlementBasicInfoService.getById(settlementElectronicInfo.getSettlementBasicInfoId());
        //完成流程
        Map<String, Object> msgVariables = new HashMap<>();
        msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "结算单编号：" + settlementBasicInfo.getSettlementNumber());
        msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                "结算单编号为“" + settlementBasicInfo.getSettlementNumber() + "”的结算单流程已完成签署");
        //2.新增处理人-- C端用户
//        SupplierInfo supplierInfo = iSupplierInfoService.get(settlementBasicInfo.getSupplierId());
        flowableService.completeMyTaskCustomer(taskId, settlementBasicInfo, null, ConstantConversionUtil.processApprovalResult.get("pass"), settlementElectronicInfo.getId(), msgVariables, ConstantUtil.BILL_BASIC_INFORMATION,user.getId());
        //改变主表的状态
        settlementBasicInfo.setSettlementStatus(ProjectInitiationStatusDict.ChildEnum.COMPLETED.getCode());
        settlementBasicInfoService.updateById(settlementBasicInfo);
    }

    @Override
    public void updateShortUrl(Date date) {
        List<SettlementElectronicInfo> list = list();
        if(list!=null && list.size()>0){
            for (SettlementElectronicInfo electronicInfo : list) {
               if(electronicInfo.getSignStatus().equals(0)){
                   Date shortContractLinkCreate = electronicInfo.getShortContractLinkCreate();
                   long between = DateUtil.between(shortContractLinkCreate, date, DateUnit.DAY);
                   if(between>=6){
                       String newShortUrl = ElectronicSignatureUtil.shortUrl(null, electronicInfo.getContractLink());
                       electronicInfo.setShortContractLink(newShortUrl);
                       electronicInfo.setShortContractLinkCreate(date);
                       updateById(electronicInfo);
                   }
               }
            }
        }
    }
}
