package com.inkasso.factoring.settlement.service.Impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.adapter.exception.BusinessException;
import com.inkasso.adapter.util.CodeUtil;
import com.inkasso.factoring.common.constant.ConstantUtil;
import com.inkasso.factoring.common.dict.impl.SettlementDict;
import com.inkasso.factoring.common.enums.ProjectInitiationStatusEnum;
import com.inkasso.factoring.common.util.ElectronicSignatureUtil;
import com.inkasso.factoring.dict.service.DictionaryService;
import com.inkasso.factoring.settlement.dao.SettlementElectronicInfoMapper;
import com.inkasso.factoring.settlement.entity.SettlementBasicInfo;
import com.inkasso.factoring.settlement.entity.SettlementElectronicInfo;
import com.inkasso.factoring.settlement.service.SettlementBasicInfoService;
import com.inkasso.factoring.settlement.service.SettlementElectronicInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

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 DictionaryService dictService;
    @Autowired
    private RestTemplate restTemplate;
    @Value("${trade.finalStatementsign}")
    private String finalStatementsign;
    @Override
    public void sign(SettlementElectronicInfo settlementElectronicInfo, String taskId) {
        SettlementBasicInfo settlementBasicInfo = settlementBasicInfoService.getById(settlementElectronicInfo.getSettlementBasicInfoId());
        //调用B端-完成流程
        ResponseEntity<String> response = restTemplate.postForEntity(finalStatementsign,
                settlementBasicInfo,String.class);
        if (response.getStatusCode().value() != HttpStatus.OK.value()) {
            throw new BusinessException(CodeUtil.FAIL, "结算单新增异常！");
        }
        //改变主表的状态
        settlementBasicInfo.setSettlementStatus(ProjectInitiationStatusEnum.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);
                   }
               }
            }
        }
    }
}
