package com.yeejoin.amos.boot.module.cylinder.biz.job;

import com.yeejoin.amos.boot.module.cylinder.api.entity.CylinderQuestionInfo;
import com.yeejoin.amos.boot.module.cylinder.api.enums.QuestionTypeEnum;
import com.yeejoin.amos.boot.module.cylinder.biz.service.impl.CylinderQuestionInfoServiceImpl;
import com.yeejoin.amos.boot.module.cylinder.flc.api.dto.CylinderUnitDto;
import com.yeejoin.amos.boot.module.cylinder.flc.api.mapper.CylinderUnitMapper;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Component
@EnableScheduling
public class CompanyQuestionJob {


    private CylinderUnitMapper cylinderUnitMapper;

    private CylinderQuestionInfoServiceImpl questionInfoService;

    public CompanyQuestionJob(CylinderUnitMapper cylinderUnitMapper,
                              CylinderQuestionInfoServiceImpl questionInfoService) {
        this.cylinderUnitMapper = cylinderUnitMapper;
        this.questionInfoService = questionInfoService;
    }


    /**
     * 资质超期
     */
    @SchedulerLock(name = "genCompanyQuestionOfPermissionExpireJob", lockAtMostFor = "PT4M")
    @Scheduled(cron = "${inspection.cert.expire.job.cron:0 0/2 * * * ?}")
    @Transactional(rollbackFor = Exception.class)
    public void genCompanyQuestionOfPermissionExpire() {
        List<CylinderUnitDto> cylinderUnitDtoList = cylinderUnitMapper.queryCylinderUnitOfPermissionExpire(QuestionTypeEnum.XKCQ.getCode());
        List<CylinderQuestionInfo> cylinderQuestionInfos = cylinderUnitDtoList.stream().map(cylinderUnitDto -> {
            CylinderQuestionInfo cylinderQuestionInfo = new CylinderQuestionInfo();
            cylinderQuestionInfo.setQuestionType(QuestionTypeEnum.XKCQ.getCode());
            cylinderQuestionInfo.setQuestionTypeName(QuestionTypeEnum.XKCQ.getName());
            return this.fillCommonField(cylinderUnitDto, cylinderQuestionInfo);
        }).collect(Collectors.toList());
        if (cylinderQuestionInfos.size() > 0) {
            questionInfoService.saveBatch(cylinderQuestionInfos);
        }
    }

    @NotNull
    private CylinderQuestionInfo fillCommonField(CylinderUnitDto cylinderUnitDto, CylinderQuestionInfo cylinderQuestionInfo) {
        cylinderQuestionInfo.setHappenDate(new Date());
        cylinderQuestionInfo.setRecDate(new Date());
        cylinderQuestionInfo.setRegionCode(cylinderUnitDto.getRegionCode());
        cylinderQuestionInfo.setQuestionObjectId(cylinderUnitDto.getCreditCode());
        cylinderQuestionInfo.setQuestionObjectName(cylinderUnitDto.getUnitName());
        cylinderQuestionInfo.setQuestionAttributionId(cylinderUnitDto.getAppId());
        cylinderQuestionInfo.setQuestionAttributionName(cylinderUnitDto.getUnitName());
        return cylinderQuestionInfo;
    }


}
