package com.sqx.modules.timedtask;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.DateUtils;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.interviewRecord.entity.InterviewRecord;
import com.sqx.modules.interviewRecord.service.InterviewRecordService;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import com.sqx.modules.resumes.entity.Company;
import com.sqx.modules.resumes.service.CompanyService;
import com.sqx.modules.utils.SenInfoCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import weixin.popular.bean.user.User;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class AutoSendOrder {
    @Autowired
    private UserService userService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private InterviewRecordService recordService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private CompanyService companyService;

    /**
     * 每天凌晨执行一次 初始化是否已增加次数字段
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void refreshAdd() {
        UserEntity userEntity = new UserEntity();
        userEntity.setIsUserAdd(0);
        userEntity.setIsCompanyAdd(0);
        userService.update(userEntity,new QueryWrapper<UserEntity>().eq("is_user_add", 1).eq("is_company_add", 1));
    }

    /**
     * 添加刷新次数
     */
    @Scheduled(cron = "0/2 * * * * ?", zone = "Asia/Shanghai")
    public void refreshUserCount() {
        //普通用户的次数
        String ordinaryUserCount = commonInfoService.findOne(327).getValue();
        //会员用户的次数
        String vipUserCount = commonInfoService.findOne(328).getValue();

        //普通企业的次数
        String ordinaryCompanyCount = commonInfoService.findOne(329).getValue();
        //会员企业的次数
        String vipCompanyCount = commonInfoService.findOne(330).getValue();

        List<UserEntity> userEntityList = userService.list(new QueryWrapper<UserEntity>().eq("is_user_add", 0).or().eq("is_company_add", 0));
        for (UserEntity userEntity : userEntityList) {
            if (userEntity.getIsUserVip() == 1) {
                userEntity.setUserRefreshCount(Integer.parseInt(vipUserCount));
            } else {
                userEntity.setUserRefreshCount(Integer.parseInt(ordinaryUserCount));
            }

            if (userEntity.getIsCompanyAdd() == 1) {
                userEntity.setCompanyRefreshCount(Integer.parseInt(vipCompanyCount));
            } else {
                userEntity.setCompanyRefreshCount(Integer.parseInt(ordinaryCompanyCount));
            }
            userEntity.setIsCompanyAdd(1);
            userEntity.setIsUserAdd(1);
            userService.updateById(userEntity);
        }
    }

    /**
     * 会员过期检测
     */
    @Scheduled(cron = "0/2 * * * * ?", zone = "Asia/Shanghai")
    public void checkUserOrCompany() {
        List<UserEntity> entityList = userService.list(new QueryWrapper<UserEntity>().eq("is_user_vip", 1).or().eq("is_company_vip", 1));
        for (UserEntity userEntity : entityList) {
            if (userEntity.getUserMonthTime() != null && LocalDateTime.now().isAfter(userEntity.getUserMonthTime())) {
                userEntity.setIsUserVip(0);
            }
            if (userEntity.getCompanyMonthTime() != null && LocalDateTime.now().isAfter(userEntity.getCompanyMonthTime())) {
                userEntity.setIsCompanyVip(0);
            }
            userService.updateById(userEntity);
        }
    }

    /**
     * 面试提醒
     */
    @Scheduled(cron = "0/3 * * * * ?", zone = "Asia/Shanghai")
    public void remind() {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String one = commonInfoService.findOne(334).getValue();
        String two = commonInfoService.findOne(335).getValue();
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setTitle("面试提醒");
        messageInfo.setIsSee("0");
        messageInfo.setState(String.valueOf(5));
        List<InterviewRecord> recordListOne = recordService.getRecordList(Integer.valueOf(one));
        List<InterviewRecord> recordListTwo = recordService.getRecordList(Integer.valueOf(two));
        for (InterviewRecord interviewRecord : recordListTwo) {
            UserEntity userEntity = userService.getById(interviewRecord.getUserId());
            messageInfo.setUserName(userEntity.getUserName());
            messageInfo.setUserId(String.valueOf(interviewRecord.getUserId()));
            messageInfo.setCreateAt(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
            if (interviewRecord.getIsSendDay() == 0) {
                interviewRecord.setIsSendDay(1);
                messageInfo.setContent("您在"+interviewRecord.getInterviewDateTime().format(fmt)+"有一场面试,请按时到场");
                messageService.save(messageInfo);
                interviewRecord.setIsSendHour(1);
                recordService.updateById(interviewRecord);
                if (StringUtils.isNotEmpty(userEntity.getClientid())) {
                    userService.pushToSingle("系统通知", "您在"+interviewRecord.getInterviewDateTime().format(fmt)+"有一场面试,请按时到场", userEntity.getClientid());
                }
                if (StringUtils.isNotBlank(userEntity.getOpenId())) {
                    CommonInfo make = commonInfoService.findOne(342);
                    Company company = companyService.getById(interviewRecord.getCompanyId());
                    UserEntity companyEntity = userService.getById(company.getUserId());
                    List<String> msgList = new ArrayList<>();
                    msgList.add(company.getCompanyName());
                    msgList.add("点击后查看");
                    msgList.add(companyEntity.getPhone());
                    msgList.add(interviewRecord.getInterviewDateTime().format(fmt));
                    msgList.add(interviewRecord.getRemarks());
                    SenInfoCheckUtil.sendMsg(userEntity.getOpenId(), make.getValue(), msgList, 4, null);
                }
            }
        }
        for (InterviewRecord interviewRecord : recordListOne) {
            UserEntity userEntity = userService.getById(interviewRecord.getUserId());
            messageInfo.setUserName(userEntity.getUserName());
            messageInfo.setUserId(String.valueOf(interviewRecord.getUserId()));
            messageInfo.setCreateAt(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
            if (interviewRecord.getIsSendDay() == 0 && interviewRecord.getIsSendHour()==0) {
                interviewRecord.setIsSendDay(1);
                messageInfo.setContent("您在"+interviewRecord.getInterviewDateTime().format(fmt)+"有一场面试,请按时到场");
                messageService.save(messageInfo);
                interviewRecord.setIsSendDay(1);
                recordService.updateById(interviewRecord);
                if (StringUtils.isNotEmpty(userEntity.getClientid())) {
                    userService.pushToSingle("系统通知", "您在"+interviewRecord.getInterviewDateTime().format(fmt)+"有一场面试,请按时到场", userEntity.getClientid());
                }

                if (StringUtils.isNotBlank(userEntity.getOpenId())) {
                    CommonInfo make = commonInfoService.findOne(342);
                    Company company = companyService.getById(interviewRecord.getCompanyId());
                    UserEntity companyEntity = userService.getById(company.getUserId());
                    List<String> msgList = new ArrayList<>();
                    msgList.add(company.getCompanyName());
                    msgList.add("点击后查看");
                    msgList.add(companyEntity.getPhone());
                    msgList.add(interviewRecord.getInterviewDateTime().format(fmt));
                    msgList.add(interviewRecord.getRemarks());;
                    SenInfoCheckUtil.sendMsg(userEntity.getOpenId(), make.getValue(), msgList, 4, null);
                }
            }
        }

    }

    @Scheduled(cron = "0/2 * * * * ?", zone = "Asia/Shanghai")
    public void viewOverdue() {
        List<InterviewRecord> recordList = recordService.list(new QueryWrapper<InterviewRecord>().eq("status", 1));
        String overTime = commonInfoService.findOne(336).getValue();
        for (InterviewRecord interviewRecord : recordList) {
            Duration between = Duration.between(LocalDateTime.now(), interviewRecord.getCreateTime());
            if (between.toHours() >= Integer.parseInt(overTime)) {
                interviewRecord.setStatus(4);
                recordService.updateById(interviewRecord);
            }
        }
    }
}
