package com.pnkx.quartz.task;

import com.pnkx.common.constant.RedisConstants;
import com.pnkx.common.constant.WebsiteAddressConstants;
import com.pnkx.common.core.domain.AjaxResult;
import com.pnkx.common.core.domain.entity.SysUser;
import com.pnkx.common.utils.SecurityUtils;
import com.pnkx.common.utils.StringUtils;
import com.pnkx.common.utils.reflect.ReflectUtils;
import com.pnkx.common.utils.template.TemplateUtils;
import com.pnkx.domain.po.PxChatMessage;
import com.pnkx.domain.po.PxLeaveMessage;
import com.pnkx.domain.po.PxToDo;
import com.pnkx.mapper.PxCardUserMapper;
import com.pnkx.mapper.PxToDoMapper;
import com.pnkx.system.domain.SysEmail;
import com.pnkx.system.mapper.SysUserMapper;
import com.pnkx.system.service.ISysEmailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author phy
 */
@Component("pxTask")
public class PxTask {
    @Resource
    private PxCardUserMapper pxCardUserMapper;
    @Resource
    private PxToDoMapper pxToDoMapper;
    @Resource
    private SysUserMapper userMapper;
    @Resource
    private ISysEmailService sysEmailService;
    @Resource
    private RedisTemplate redisTemplate;

    private static Logger logger = LoggerFactory.getLogger(ReflectUtils.class);

    /**
     * 定时清理在线聊天室缓存
     */
    public void cleanChat() {
        ValueOperations<String, List<PxChatMessage>> operations = redisTemplate.opsForValue();
        operations.set(RedisConstants.PX_CHAT_MEMBER, new ArrayList<>());
        operations.set(RedisConstants.PX_CHAT_MESSAGE, new ArrayList<>());
    }

    /**
     * 定时发放卡券
     */
    public void grantCard() {
        pxCardUserMapper.regularGrantCard();
    }

    /**
     * 待办提醒
     */
    public void toDoReminder() {
        PxToDo pxToDo = new PxToDo();
        pxToDo.setStatus(false);
        // 未完成的待办列表
        List<PxToDo> pxToDoList = pxToDoMapper.selectPxToDoList(pxToDo);
        for (PxToDo toDo : pxToDoList) {
            LocalDateTime planEndTime = LocalDateTime.parse(toDo.getPlanEndTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            // 判断计划结束时间距离当前是否小于4个小时
            if (LocalDateTime.now().isAfter(planEndTime.minusHours(4))) {
                // 负责人
                String performer = toDo.getPerformer();
                if (StringUtils.isNotEmpty(performer)) {
                    String[] performers = performer.split(",");
                    for (String userId : performers) {
                        SysEmail sysEmail = new SysEmail();
                        SysUser sysUser = userMapper.selectUserById(Long.valueOf(userId));
                        String activationTemplate = TemplateUtils.getTemplate("todo");
                        activationTemplate = activationTemplate.replace("template-nickName", sysUser.getNickName());
                        activationTemplate = activationTemplate.replace("template-todoTitle", toDo.getContent());
                        StringBuilder todoRemind = new StringBuilder();
                        Duration duration;
                        if (LocalDateTime.now().isAfter(planEndTime)) {
                            duration = Duration.between(planEndTime, LocalDateTime.now());
                            todoRemind.append("已超时");
                        } else {
                            duration = Duration.between(LocalDateTime.now(), planEndTime);
                            todoRemind.append("还有");
                        }
                        long hours = duration.toHours();
                        long minutes = duration.toMinutes() % 60;
                        if (hours > 0) {
                            todoRemind.append(hours).append("个小时");
                        }
                        todoRemind.append(minutes).append("分钟,请及时处理~");
                        activationTemplate = activationTemplate.replace("template-todoRemind", todoRemind);
                        sysEmail.setReceiverEmail(sysUser.getEmail());
                        sysEmail.setSubject("\uD83D\uDC49 叮咚！「Pei你看雪博客」您的待办提醒~");
                        sysEmail.setContent(activationTemplate);
                        try {
                            sysEmailService.sendMail(sysEmail);
                        } catch (Exception e) {
                            logger.error("发送待办提醒邮件异常，异常信息为：{}", e.getMessage());
                        }
                    }
                }
            }
        }
    }
}
