package jnpf.attendance.service.handle.notice;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import jnpf.constants.AttendanceConstant;
import jnpf.engine.DataStatisticsApi;
import jnpf.engine.model.flowstatistics.vo.TemplatePagesVo;
import jnpf.enums.attendance.NoticeModuleEnum;
import jnpf.enums.attendance.UserSettingEnum;
import jnpf.model.attendance.model.JumpUrl;
import jnpf.model.attendance.model.NoClockInModel;
import jnpf.model.attendance.model.NoClockInNoticeModel;
import jnpf.model.attendance.model.SystemNoticeStrategy;
import jnpf.util.ConstantUtil;
import jnpf.util.DateDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 缺卡/旷工消息服务
 *
 * @author yanwenfu
 * @create 2024-08-13
 */
@Service(value = AttendanceConstant.WORK_MISSING)
public class NoClockInNotice implements AttendanceNotice<NoClockInModel, NoClockInNoticeModel> {

    @Resource
    private AttendanceNoticeHandler attendanceNoticeHandler;

    @Autowired
    private DataStatisticsApi dataStatisticsApi;

    @Override
    public Boolean isPass(NoClockInNoticeModel model) {

        // 上班缺卡  下班缺卡  旷工
        if (ConstantUtil.NUM_TRUE == model.getAbsence()) {
            return attendanceNoticeHandler.checkBeforeCondition(model.getUserId(), UserSettingEnum.ATTENDANCE_SETTING_MESSAGE_RECEIVE,
                    UserSettingEnum.ATTENDANCE_SETTING_CLASS_ABSENCE_REMINDER);
        } else if (ConstantUtil.ON_WORK.equals(model.getClockInType())) {
            return attendanceNoticeHandler.checkBeforeCondition(model.getUserId(), UserSettingEnum.ATTENDANCE_SETTING_MESSAGE_RECEIVE,
                    UserSettingEnum.ATTENDANCE_SETTING_MISSING_CHECKIN_REMINDER);
        } else {
            return attendanceNoticeHandler.checkBeforeCondition(model.getUserId(), UserSettingEnum.ATTENDANCE_SETTING_MESSAGE_RECEIVE,
                    UserSettingEnum.ATTENDANCE_SETTING_MISSING_END_WORK_REMINDER);
        }
    }

    @Override
    public List<NoClockInModel> generatingData(NoClockInNoticeModel model) {

        String workTimeStr;
        if (ConstantUtil.NUM_TRUE == model.getAbsence()) {
            workTimeStr = DateDetail.getDate2Str(model.getOnWorkTime(), DateDetail.DF9)+ "至" + DateDetail.getDate2Str(model.getOffWorkTime(), DateDetail.DF10);
        } else if (ConstantUtil.ON_WORK.equals(model.getClockInType())) {
            workTimeStr = DateDetail.getDate2Str(model.getOnWorkTime(), DateDetail.DF9);
        } else {
            workTimeStr = DateDetail.getDate2Str(model.getOffWorkTime(), DateDetail.DF9);
        }
        NoClockInModel noClockInModel = NoClockInModel.builder()
                .workTimeStr(workTimeStr)
                .remark(getTitle(model.getAbsence(), model.getClockInType()))
                .absence(model.getAbsence())
                .clockInType(model.getClockInType())
                .build();
        return List.of(noClockInModel);
    }

    private String getTitle(Integer absence, Integer clockInType) {

        if (ConstantUtil.NUM_TRUE == absence) {
            return AttendanceConstant.TITLE_ABSENCE;
        } else if (ConstantUtil.ON_WORK.equals(clockInType)) {
            return AttendanceConstant.TITLE_ON_WORK;
        } else {
            return AttendanceConstant.TITLE_OFF_WORK;
        }
    }

    @Override
    public List<String> getRecipient(NoClockInNoticeModel model) {

        return List.of(model.getUserId());
    }

    @Override
    public SystemNoticeStrategy setSystemNoticeStrategy(NoClockInNoticeModel model, String noticeId) {

        // 查询url需要的参数(补卡审批)
        List<TemplatePagesVo> list = dataStatisticsApi.getFeignTemplatePagesByBusinessType(ConstantUtil.REPAIR_TYPE);
        String url = "";
        if (null != list && !list.isEmpty()) {
            TemplatePagesVo template = list.get(0);
            JSONObject json = new JSONObject();
            // 前端需要回显补卡的记录 所需的json格式
            JSONObject data = new JSONObject();
            data.set("clockInResultId", model.getClockInResultId());
            json.set("interfaceData", data);
            json.set("extraData", new JSONObject());
            json.set("paramsData", new JSONObject());
            JSONObject sourceData = new JSONObject();
            sourceData.set("text", model.getClockInType().equals(ConstantUtil.ON_WORK) ? DateDetail.getDateTime2Str(model.getOnWorkTime()) : DateDetail.getDateTime2Str(model.getOffWorkTime()));
            json.set("sourceData", sourceData);
            json.set("tableData", new JSONArray());
            url = String.format(AttendanceConstant.BTN_REPAIR_URL, template.getFlowId(), template.getTemplateId(), json);
        }
        return SystemNoticeStrategy.builder()
                .title(getTitle(model.getAbsence(), model.getClockInType()))
                .noticeModuleEnum(NoticeModuleEnum.KQ_DK)
                .jumpUrls(List.of(new JumpUrl(url, AttendanceConstant.BTN_NAME_REPAIR, AttendanceConstant.COLOR_BLACK, "__UNI__65CF373")))
                .build();
    }

    @Override
    public String getContext(List<NoClockInModel> dataList) {

        if (null == dataList || dataList.isEmpty()) {
            return "";
        }
        NoClockInModel model = dataList.get(0);

        if (ConstantUtil.NUM_TRUE == model.getAbsence()) {
            return model.getWorkTimeStr() + " 上下班均未打卡，计为旷工！";
        } else if (ConstantUtil.ON_WORK.equals(model.getClockInType())) {
            return model.getWorkTimeStr() + " 上班已缺卡！";
        } else {
            return model.getWorkTimeStr() + " 下班已缺卡！";
        }
    }
}
