package com.saye.hrs.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saye.hrs.commons.log.ExceptionDUtil;
import com.saye.hrs.commons.log.LogUtil;
import com.saye.hrs.commons.page.PageUtil;
import com.saye.hrs.commons.page.TemplatePage;
import com.saye.hrs.commons.string.StringDUtil;
import com.saye.hrs.service.BlackListService;
import com.saye.hrs.service.MissedAppointRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * @description: 爽约记录管理
 * @author: Mr.zs
 * @create: 2024-04-11 19:47
 **/
@Controller
@RequestMapping("/missedAppointRecord")
public class MissedAppointRecordController {


    @Autowired
    BlackListService blackListService;

    @Autowired
    MissedAppointRecordService missedAppointRecordService;


    @RequestMapping("/toMissedAppointRecord")
    public String toMissedAppointRecord(ModelMap modelMap) {


        return "/blacklist/missedAppointRecord";
    }

    @RequestMapping("/findAllMissedAppointRecord")
    @ResponseBody
    public TemplatePage findAllMissedAppointRecord(Integer page, Integer limit, String cardNo, String startTime, String endTime, String isThird) {
        HashMap<Object, Object> searchMap = new HashMap<>();
        searchMap.put("cardNo", cardNo);
        if (StringDUtil.isNotBlank(startTime)) {
            searchMap.put("startTime", startTime.replace("-", ""));
        }
        if (StringDUtil.isNotBlank(endTime)) {
            searchMap.put("endTime", endTime.replace("-", ""));
        }
        searchMap.put("isThird", isThird);
        PageHelper.startPage(page, limit);
        PageInfo<HashMap<Object, Object>> appsPageInfo = new PageInfo<>(missedAppointRecordService.findAllMissedAppointRecord(searchMap));
        return PageUtil.loadJsonPage(appsPageInfo);
    }

    @RequestMapping("/addBlackList")
    @ResponseBody
    public HashMap<Object, Object> addBlackList(@RequestBody HashMap<Object, Object> map) {
        String errCode = "0";
        String errMsg = "";
        try {
            Integer i = blackListService.fetchCountByCardNo(map);
            if (i > 0) {
                errCode = "999";
                errMsg = "该人员已被添加";
            } else {
                blackListService.addBlackList(map);
            }
        } catch (Exception e) {
            errCode = "999";
            errMsg = "添加黑名单失败";
            LogUtil.error(this.getClass(), "添加黑名单失败，原因:" + ExceptionDUtil.getDetailExceptionMsg(e));
        }
        HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
        resultMap.put("errCode", errCode);
        resultMap.put("errMsg", errMsg);
        return resultMap;
    }

}
