package cn.iocoder.yudao.module.system.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyContractDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyContractMapper;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class ElderContractUpdateJob implements JobHandler {
    private final static Logger logger = LoggerFactory.getLogger(ElderContractUpdateJob.class);

    @Resource
    private ElderlyContractMapper elderlyContractMapper;

    @Override
    @TenantJob
    public String execute(String param) throws Exception {
        Long tenantId = TenantContextHolder.getTenantId();
        logger.info("租户{},=====================合同更新===================", tenantId);
        updateElderContractStatus(tenantId);
        return String.format("租户{}合同更新情况", tenantId);
    }

    private void updateElderContractStatus(Long tenantId){
        // 获取该租户所有的长者合同
        List<ElderlyContractDO> elderlyContractDOList = elderlyContractMapper.selectList(new LambdaQueryWrapperX<ElderlyContractDO>()
                .eq(ElderlyContractDO::getTenantId, tenantId)
                .eq(ElderlyContractDO::getStatus, 1));
        for (ElderlyContractDO elderlyContractDO : elderlyContractDOList) {
            LocalDate nowLocalDate = LocalDate.now();
            LocalDate expireLocalDate = elderlyContractDO.getExpireTime().toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDate();
            // 判断合同今天是否是到期时间
            if(expireLocalDate.isBefore(nowLocalDate)){   // 当天不属于过期，过了一天才算，所以+1
                // 查询未开始的合同，并且开始时间大于等于今天
                List<ElderlyContractDO> futureContractList = elderlyContractMapper.selectList(new LambdaQueryWrapperX<ElderlyContractDO>()
                        .eq(ElderlyContractDO::getElderId, elderlyContractDO.getElderId())
                        .eq(ElderlyContractDO::getStatus, 2)
                        .ge(ElderlyContractDO::getExpireTime, nowLocalDate));
                if(CollectionUtil.isEmpty(futureContractList)){
                    continue;
                }
                ElderlyContractDO futureContract = futureContractList.get(0);
                // 更新新合同状态
                futureContract.setStatus(1);
                // 更新旧合同状态
                elderlyContractDO.setStatus(0);
                elderlyContractMapper.updateById(elderlyContractDO);
                elderlyContractMapper.updateById(futureContract);
            }
        }
    }
}
