package jnpf.attendance.service.handle.notice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jnpf.attendance.antifreeze.UserAntifreeze;
import jnpf.attendance.mapper.AttendanceDailyRuleMapper;
import jnpf.constants.AttendanceConstant;
import jnpf.entity.attendance.FtbAttendanceDailyRule;
import jnpf.enums.attendance.NoticeModuleEnum;
import jnpf.enums.attendance.UserSettingEnum;
import jnpf.model.attendance.model.AttendanceChangeModel;
import jnpf.model.attendance.model.AttendanceChangeNoticeModel;
import jnpf.model.attendance.model.JumpUrl;
import jnpf.model.attendance.model.SystemNoticeStrategy;
import jnpf.permission.model.user.PartUserInfoVo;
import jnpf.util.ConstantUtil;
import jnpf.util.DateDetail;
import jnpf.util.StringUtil;
import org.springframework.stereotype.Service;

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

/**
 * 考勤变更(不审批)
 *
 * @author yanwenfu
 * @create 2024-08-16
 */
@Service(value = AttendanceConstant.CHECK_RESULT_CHANGE)
public class AttendanceChangeNotice implements AttendanceNotice<AttendanceChangeModel, AttendanceChangeNoticeModel> {

    @Resource
    private AttendanceNoticeHandler attendanceNoticeHandler;
    @Resource
    private AttendanceDailyRuleMapper attendanceDailyRuleMapper;
    @Resource
    private UserAntifreeze userAntifreeze;

    @Override
    public Boolean isPass(AttendanceChangeNoticeModel model) {

        return attendanceNoticeHandler.checkBeforeCondition(model.getUserId(), UserSettingEnum.ATTENDANCE_SETTING_MESSAGE_RECEIVE,
                UserSettingEnum.ATTENDANCE_SETTING_ATTENDANCE_RESULT_CHANGE_REMINDER);
    }

    @Override
    public List<AttendanceChangeModel> generatingData(AttendanceChangeNoticeModel model) {

        List<PartUserInfoVo> list = userAntifreeze.getAllByIds(List.of(model.getHandleUserId()), model.getTenantId());
        PartUserInfoVo user = list.stream().findFirst().orElse(null);
        StringBuilder handelUser = new StringBuilder();
        if (Objects.isNull(user)) {
            handelUser.append("无");
        } else {
            handelUser.append(user.getRealName()).append("(").append(
                    StringUtil.isEmpty(user.getOrganizeName()) ? "暂无" : user.getOrganizeName()).append("-").append(
                            StringUtil.isEmpty(user.getPositionName()) ? "暂无" : user.getPositionName()).append(")");
        }
        FtbAttendanceDailyRule dailyRule = attendanceDailyRuleMapper.selectById(model.getRuleId());
        String changeTime;
        if (null == dailyRule) {
            changeTime = "--";
        } else {
            changeTime = model.getClockInType().equals(ConstantUtil.ON_WORK) ? DateDetail.getDate2Str(dailyRule.getInPoint(), DateDetail.DF9) : DateDetail.getDate2Str(dailyRule.getOutPoint(), DateDetail.DF9);
        }
        AttendanceChangeModel attendanceChangeModel = AttendanceChangeModel.builder()
                .handelUser(handelUser.toString())
                .changeTime(null == dailyRule ? "--" : changeTime)
                .changeResult(getResult(model.getChangeType()))
                .build();
        return List.of(attendanceChangeModel);
    }

    private String getResult(Integer changeType) {

        // 变更类型(-1: 缺卡, 1: 正常, 2: 迟到, 3: 早退, 99: 撤回变更)
        switch (changeType) {
            case -1:
                return "缺卡";
            case 1:
                return "正常";
            case 2:
                return "迟到";
            case 3:
                return "早退";
            case 99:
                return "撤回变更";
            default:
                return "--";
        }
    }

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

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

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

        List<JumpUrl> list = new ArrayList<>();
        list.add(new JumpUrl(String.format(AttendanceConstant.GROUP_LOCK_URL1, model.getHandleUserId()), AttendanceConstant.GROUP_LOCK_BUTTON_NAME1, AttendanceConstant.COLOR_BLACK,3,"contactInfo", JSON.toJSONString(new JSONObject(){{
            put("contactId", model.getHandleUserId());
        }})));

        return SystemNoticeStrategy.builder()
                .title(AttendanceConstant.TITLE_CLOCK_IN_CHANGE)
                .noticeModuleEnum(NoticeModuleEnum.CQJG_BD)
                .jumpUrls(list)
                .build();
    }

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

        if (null == dataList || dataList.isEmpty()) {
            return "";
        }
        AttendanceChangeModel model = dataList.get(0);
        return "操作人：" + model.getHandelUser() + "</br>"
                + "变更记录：" + model.getChangeTime() + "</br>"
                + "变更结果：" + model.getChangeResult();
    }
}
