package com.sunxung.factoring.service.financing.contract.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.util.CollectionUtil;
import com.sunxung.factoring.component.util.ElectronicSignatureUtil;
import com.sunxung.factoring.component.util.StringUtil;
import com.sunxung.factoring.entity.financing.contract.ContractElectronicCustomerInfo;
import com.sunxung.factoring.mapper.financing.contract.ContractElectronicCustomerInfoMapper;
import com.sunxung.factoring.service.financing.contract.IContractElectronicCustomerInfoService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 合同电子签信息客户信息 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2022-06-29
 */
@Service
public class ContractElectronicCustomerInfoServiceImpl extends ServiceImpl<ContractElectronicCustomerInfoMapper, ContractElectronicCustomerInfo>
        implements IContractElectronicCustomerInfoService {

    @Override
    public void urlJob() {
        Date date = new Date();
        Date dateTime = DateUtil.offsetDay(date, -6);
        List<ContractElectronicCustomerInfo> list = lambdaQuery().eq(ContractElectronicCustomerInfo::getSignStatus, false).isNotNull(ContractElectronicCustomerInfo::getContractLink).list();
        if(CollectionUtil.isNotEmpty(list)){
            for (ContractElectronicCustomerInfo contractElectronicCustomerInfo : list) {
                if(StringUtil.isEmpty(contractElectronicCustomerInfo.getShortContractLink()) || contractElectronicCustomerInfo.getShortContractLinkCreate() == null){
                    String shortUrl = ElectronicSignatureUtil.shortUrl(null, contractElectronicCustomerInfo.getContractLink());
                    contractElectronicCustomerInfo.setShortContractLink(shortUrl);
                    contractElectronicCustomerInfo.setShortContractLinkCreate(new Date());
                }else if(contractElectronicCustomerInfo.getShortContractLinkCreate().compareTo(dateTime) < 1){
                    String shortUrl = ElectronicSignatureUtil.shortUrl(null, contractElectronicCustomerInfo.getContractLink());
                    contractElectronicCustomerInfo.setShortContractLink(shortUrl);
                    contractElectronicCustomerInfo.setShortContractLinkCreate(new Date());
                }
                updateById(contractElectronicCustomerInfo);
            }
        }
    }
}
