package com.jpsoft.shinestar.scheduled;

import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.common.utils.CheckIdCard;
import com.jpsoft.shinestar.modules.common.utils.IdCardUtils;
import com.jpsoft.shinestar.modules.outapi.service.OutApiService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 墨鱼_mo
 * @date 2020-9-27 14:28
 */
@Component
@Slf4j
@Profile(value = {"deprecated"})
public class UpdateHealthCodeTask {
    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private OutApiService outApiService;

//    @Scheduled(cron = "0 0/30 * * * ?")
    public void run() {
        //查询超过1天未更新健康码的记录,每30分钟查询500条
        List<PersonInfo> personInfoList = personInfoService.findByUpdateHealthyTime(DateTime.now().withTimeAtStartOfDay().plusDays(-1).toDate(),100);

        log.warn("更新健康码：{}条数",personInfoList.size());

        for (PersonInfo personInfo : personInfoList) {
            try {
                if (StringUtils.isNotBlank(personInfo.getName()) && StringUtils.isNotBlank(personInfo.getIdCard())){
                    String idCard = personInfo.getIdCard().toUpperCase().trim();

                    CheckIdCard cic = new CheckIdCard(idCard);

                    if(cic.validate()) {
                        outApiService.getGreenCode(personInfo.getName(), idCard);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
