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.service.BlackListService;
import com.saye.hrs.service.system.ServiceParamsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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;
import java.util.List;

/**
 * @description: 黑名单管理
 * @author: Mr.zs
 * @create: 2024-04-11 19:53
 **/
@Controller
@RequestMapping("/blackList")
public class BlackListController {

    @Autowired
    BlackListService blackListService;

    @Autowired
    ServiceParamsService serviceParamsService;

    @RequestMapping("toBlackList")
    public String toBlackList(ModelMap modelMap, Model model) {

        List<HashMap<Object, Object>> is_blackList = serviceParamsService.findParamValByParamCode("is_blackList");
        String isBlackList = "";
        for (HashMap<Object, Object> objectObjectHashMap : is_blackList) {
            isBlackList = objectObjectHashMap.get("PARAM_VAL").toString();
        }
        model.addAttribute("isBlackList", isBlackList);
        return "/blacklist/blackList";
    }

    @RequestMapping("/findAllBlackList")
    @ResponseBody
    public TemplatePage findAllBlackList(Integer page, Integer limit, String cardNo) {

        HashMap<Object, Object> searchMap = new HashMap<>();
        searchMap.put("cardNo", cardNo);
//        searchMap.put("isThird", isThird);
        PageHelper.startPage(page, limit);
        PageInfo<HashMap<Object, Object>> appsPageInfo = new PageInfo<>(blackListService.findAllBlackList(searchMap));
        return PageUtil.loadJsonPage(appsPageInfo);
    }

    @RequestMapping("/modifyBlackList")
    @ResponseBody
    public HashMap<Object, Object> modifyBlackList(@RequestBody HashMap<Object, Object> map) {
        String errCode = "0";
        String errMsg = "";

        try {
            blackListService.modifyBlackList(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;
    }

    @RequestMapping("/delBlackList")
    @ResponseBody
    public HashMap<Object, Object> delBlackList(@RequestBody HashMap<Object, Object> map) {
        String errCode = "0";
        String errMsg = "";

        try {
            blackListService.delBlackList(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;
    }

    @RequestMapping("/changeBlackListStatus")
    @ResponseBody
    public HashMap<Object, Object> changeBlackListStatus(@RequestBody HashMap<String, String> map) {
        String errCode = "0";
        String errMsg = "";
//        HashMap<String, String> operateMap = new HashMap<>();
//        operateMap.put("param_code", map.get("param_code").toString());
//        operateMap.put("param_val", map.get("param_val").toString());
        try {
            serviceParamsService.updateServiceParamsValBycode(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;
    }

}
