package cn.iocoder.yudao.module.system.job.attendance;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.Constants;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.system.api.subscribe.SubscribeMessageSendApi;
import cn.iocoder.yudao.module.system.api.subscribe.dto.MsgData;
import cn.iocoder.yudao.module.system.api.subscribe.dto.SubscribeMessageReqDTO;
import cn.iocoder.yudao.module.system.controller.admin.attendance.dto.AttendanceWorkDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.punchrecord.AttendancePunchRecordDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.attendance.punchrecord.AttendancePunchRecordService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class RemindJob {
    @Resource
    private AttendancePunchRecordService attendancePunchRecordService;
    @Resource
    private SubscribeMessageSendApi subscribeMessageSendApi;
    @Resource
    private AdminUserService adminUserService;

    // TODO: 2024/4/22  - 考勤提醒 每10分钟执行一次
    @XxlJob("remindJob")
    @TenantJob // --- ⚠️ 这个注解 会将租户列表拉出来 完了后逐个租户执行 定时任务需要注意
    public ReturnT<String> execute() throws Exception {
        // -- 获取当天以及明天的未打卡并且未提醒记录
        //未提醒列表
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime futureTime = localDateTime.plusMinutes(15);

        List<AttendancePunchRecordDO> attendancePunchRecordDOS = attendancePunchRecordService.getNotReminded(localDateTime);
        if (attendancePunchRecordDOS.isEmpty()) {
            return ReturnT.SUCCESS;
        }

        //还有10分钟就要上班了，别忘记打卡哦～
        //幸苦一天了，研发部全体人员提醒您，记得打卡哦～

        Map<Long, AttendanceWorkDTO> map = new HashMap<>();
        //打卡时间 - 15分钟之后是否是 大于打卡时间 - 是的话 - 把用户id拿出来 组装好消息发送
        List<AttendancePunchRecordDO> editList = new ArrayList<>();
        for (AttendancePunchRecordDO attendancePunchRecordDO : attendancePunchRecordDOS) {
            if (attendancePunchRecordDO.getWorkType().equals(Constants.ZERO) && futureTime.isAfter(attendancePunchRecordDO.getShouldPunchTime())) {
                AttendanceWorkDTO dto = new AttendanceWorkDTO();
                dto.setWorkTypeStr("上班打卡");
                dto.setData(String.format("还有%s钟就要上班了，别忘记打卡哦～",
                        DateUtil.formatBetween(LocalDateTimeUtil.between(localDateTime, attendancePunchRecordDO.getShouldPunchTime()).toMillis(), BetweenFormatter.Level.MINUTE)));
                map.put(attendancePunchRecordDO.getUserId(), dto);
                attendancePunchRecordDO.setRemindFlag(Constants.TRUE);
                editList.add(attendancePunchRecordDO);
            } else if (attendancePunchRecordDO.getWorkType().equals(Constants.ONE) && localDateTime.isAfter(attendancePunchRecordDO.getShouldPunchTime())) {
                AttendanceWorkDTO dto = new AttendanceWorkDTO();
                dto.setWorkTypeStr("下班打卡");
                dto.setData("工作幸苦了，记得打卡哦～");
                map.put(attendancePunchRecordDO.getUserId(), dto);
                attendancePunchRecordDO.setRemindFlag(Constants.TRUE);
                editList.add(attendancePunchRecordDO);
            }
        }
        //获取到所有的用户id
        if (MapUtil.isEmpty(map)) {
            return ReturnT.SUCCESS;
        }
        List<Long> userIds = new ArrayList<>(map.keySet());
        List<AdminUserDO> userList = adminUserService.getUserList(userIds);
        List<AdminUserDO> users = userList.stream().filter(a -> StrUtil.isNotEmpty(a.getOpenId())).collect(Collectors.toList());
        String templateId = "5yHyXhrsyqXi8s4hYhvaeMtz9kT5OPvDCe3h6G1T-OE";
        List<SubscribeMessageReqDTO> dtos = new ArrayList<>();
        for (AdminUserDO user : users) {
            AttendanceWorkDTO attendanceWorkDTO = map.get(user.getId());
            SubscribeMessageReqDTO dto = new SubscribeMessageReqDTO();
            dto.setToUser(user.getOpenId());
            dto.setTemplateId(templateId);
            MsgData data = new MsgData();
            data.setName("thing9");
            data.setValue(attendanceWorkDTO.getWorkTypeStr());
            dto.addData(data);
            data = new MsgData();
            data.setName("thing6");
            data.setValue(attendanceWorkDTO.getData());
            dto.addData(data);
            dto.setPage("subPages/attendance/index");
            dtos.add(dto);
        }
        if (CollectionUtil.isNotEmpty(dtos)) {
            for (SubscribeMessageReqDTO dto : dtos) {
                subscribeMessageSendApi.sendPunchReminder(dto);
            }
        }
        if (CollectionUtil.isNotEmpty(editList)) {
            attendancePunchRecordService.batchUpdate(editList);
        }
        // 返回执行成功
        return ReturnT.SUCCESS;
    }


}













