package com.yida.data.attendance.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.attendance.EduAttendanceStaffRule;
import com.yida.data.common.core.entity.attendance.EduAttendanceStaffRuleRemind;
import com.yida.data.common.core.entity.attendance.EduAttendanceStaffRuleStaff;
import com.yida.data.common.core.entity.attendance.EduAttendanceStaffRuleTime;
import com.yida.data.common.core.entity.constant.AppConstant;
import com.yida.data.common.core.entity.notice.qywx.BaseStaffNotice;
import com.yida.data.common.core.entity.notice.qywx.inside.Text;
import com.yida.data.common.core.entity.system.EduApp;
import com.yida.data.common.core.entity.user.EduStaff;
import com.yida.data.common.core.utils.WxUtil;
import com.yida.data.common.service.CommonService;
import com.yida.data.attendance.mapper.EduAttendanceStaffRuleMapper;
import com.yida.data.attendance.mapper.EduAttendanceStaffRuleRemindMapper;
import com.yida.data.attendance.service.EduAttendanceStaffRuleRemindService;
import com.yida.data.attendance.service.EduAttendanceStaffRuleStaffService;
import com.yida.data.attendance.service.EduAttendanceStaffRuleTimeService;
import com.yida.data.user.feign.RemoteStaffService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 教职工考勤-打卡提醒 Service实现
 *
 * @author ZYJ
 * @date 2021-09-27 18:56:14
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EduAttendanceStaffRuleRemindServiceImpl extends ServiceImpl
        <EduAttendanceStaffRuleRemindMapper, EduAttendanceStaffRuleRemind> implements EduAttendanceStaffRuleRemindService {

    private final EduAttendanceStaffRuleMapper eduAttendanceStaffRuleMapper;
    private final EduAttendanceStaffRuleStaffService eduAttendanceStaffRuleStaffService;
    private final EduAttendanceStaffRuleTimeService eduAttendanceStaffRuleTimeService;

    private final CommonService commonService;
    private final RemoteStaffService remoteStaffService;
    private final WxUtil wxUtil;

    @Override
    public void remindStaff() {
        LocalDateTime now = LocalDateTime.now();
        LocalDate today = LocalDate.now();

        //查询所有需要提醒的规则id
        List<EduAttendanceStaffRuleRemind> remindList = list();
        if (CollectionUtil.isEmpty(remindList)) {
            return;
        }
        for (EduAttendanceStaffRuleRemind remind : remindList) {

            //查询对应规则
            EduAttendanceStaffRule rule = eduAttendanceStaffRuleMapper.selectById(remind.getRuleId());
            List<EduAttendanceStaffRuleStaff> staffList = eduAttendanceStaffRuleStaffService.list(Wrappers.lambdaQuery(new EduAttendanceStaffRuleStaff())
                    .eq(EduAttendanceStaffRuleStaff::getRuleId, rule.getId()));
            //遍历员工
            for (EduAttendanceStaffRuleStaff staff : staffList) {

                List<EduAttendanceStaffRuleTime> timeList = eduAttendanceStaffRuleTimeService.list(Wrappers.lambdaQuery(new EduAttendanceStaffRuleTime())
                        .eq(EduAttendanceStaffRuleTime::getRuleId, rule.getId()));
                for (EduAttendanceStaffRuleTime ruleTime : timeList) {
                    //如果打卡时间小于现在的时间，则提醒
                    LocalDateTime remindTime = ruleTime.getStartTime().atDate(today).plusMinutes(remind.getMinute());

                    if (remindTime.isBefore(now)&&remindTime.plusMinutes(5).isAfter(now)){

                        EduApp app = commonService.getAppByCodeAndSchool(AppConstant.ATTENDANCE_STAFF, null,
                                rule.getSchoolId());
                        String accessToken = wxUtil.getAccessToken(app.getWxCorpId(), app.getWxSecret());

                        //职工wxId
                        EduStaff data = remoteStaffService.getStaffNoPermission(staff.getStaffId()).getData();

                        Text text = new Text();
                        String content = remind.getMessage();
                        if(remind.getIncludeReceiver()==1){

                            content = data.getName()+remind.getMessage();
                        }
                        text.setContent(content);
                        BaseStaffNotice textSchoolNotice = new BaseStaffNotice();
                        textSchoolNotice.setText(text);
                        textSchoolNotice.setAgentid(app.getWxAgentId());
                        textSchoolNotice.setTouser(data.getWxId());
                        //发送微信通知
                        wxUtil.pushStaffNotice(accessToken, textSchoolNotice);
                    }
                }
            }
        }
    }
}
