package com.iformall.schedule;

import com.iformall.mapper.WxCUserMapper;
import com.iformall.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;


@Component
public class WxCUserCheckSchedule {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private boolean isFmException;

    @Autowired
    private String fmExceptionEmails;

    @Autowired
    private MailService mailService;

    @Autowired
    private WxCUserMapper cUserMapper;

    @Scheduled(cron = "0 0 8 * * ?") // 每天凌晨08:00对c_user扫描查重
    // @Scheduled(cron = "0 */1 * * * ?") // 测试1分钟一次
    public void cuserCheckDuplicate() {

        List<Map<String, Object>> mapList = cUserMapper.checkCountByTenantOpenId();
        if(mapList.size()> 0) {
            String message = "wx_c_user异常，有重复数据";
            logger.error(message);
            if(isFmException) {
                String[] receivers = fmExceptionEmails.split(",");
                //发送邮件
                mailService.sendSimpleMail(receivers, message, message);
            }
        }
    }
}