package com.pqs9900.controller.configuration;

import com.njcn.enums.LogTypeEnum;
import com.njcn.mapper.configuration.SysMapper;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.configuration.SysSet;
import com.njcn.pojo.data.FunctionPloyEntity;
import com.njcn.pojo.data.MenuEntity;
import com.njcn.pojo.data.TimerManage;
import com.njcn.sso.pojo.user.User;
import com.njcn.service.configuration.SysService;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.AppConfig;
import com.njcn.utils.PubUtils;
import com.njcn.utils.UserUtil;
import com.njcn.utils.XssFilterUtil;
import com.pqs9900.task.MyJobDetail;
import com.pqs9900.task.MySchudler;
import com.pqs9900.task.MyTrigger;
import net.sf.json.JSONArray;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.security.auth.login.AccountException;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping(value = "/sysset")
public class SysController {
    @Resource
    private SysService sys;
    @Resource
    UserLogDetailService userLogservice;
    @Autowired
    AppConfig appConfig;

    @Autowired
    SysMapper sm;

    @Autowired
    UserUtil uu;

    @Autowired
    MySchudler mySchudler;

    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(SysController.class);

    @ResponseBody
    @RequestMapping(value = "selectSys", method = RequestMethod.POST)
    public HttpResult selectAll(HttpServletRequest request) {
        HttpResult result = new HttpResult();
        try {
            List<List> set = sys.getSysList();
            if (set.isEmpty()) {
                result.setCode(500);
                result.setMessage("没有获取到系统配置列表");
                userLogservice.getLog("查询系统配置", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 0);
                logger.error("获取系统配置失败");
            } else {
                result.setCode(200);
                result.setBody(set);
                result.setMessage("获取系统配置列表成功");
                userLogservice.getLog("查询系统配置", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 0);
                logger.info("获取系统配置成功");
            }
        } catch (Exception e) {
            logger.error("查询系统配置异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "查询系统配置失败");
        }
        return result;
    }

    //通过ID查询该信息
    @ResponseBody
    @RequestMapping(value = "selectId", method = RequestMethod.POST)
    public List<List> selectId(@Param(value = "sysSetIndex") String sysSetIndex) {
        List<List> list = new ArrayList<>();
        try {
            Example example = new Example(SysSet.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("sysSetIndex", sysSetIndex);
            List<SysSet> sysSets = sm.selectByExample(example);
            if (!sysSets.isEmpty()) {
                for (SysSet sysSet : sysSets) {
                    List<String> temp = new ArrayList<>();
                    temp.add(sysSet.getLimitDate().toString());
                    temp.add(sysSet.getLimitTimes().toString());
                    temp.add(sysSet.getLimitNumber().toString());
                    temp.add(String.valueOf(sysSet.getLimitIdle()));
                    temp.add(String.valueOf(sysSet.getLimitLogout()));
                    temp.add(sysSet.getCasualUserTime().toString());
                    temp.add(sysSet.getLogOutTime().toString());
                    temp.add(sysSet.getLogSize().toString());
                    temp.add(sysSet.getLockTime().toString());
                    list.add(temp);
                }
            }
        } catch (Exception e) {
            logger.error("查询定值信息异常，异常是" + e.toString());
        }
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public HttpResult update(HttpServletRequest request, @Param(value = "sysSetIndex") String sysSetIndex, @Param(value = "limitDate") Integer limitDate, @Param(value = "limitTimes") Integer limitTimes, @Param(value = "limitNumber") Integer limitNumber
            , @Param(value = "limitLogOut") Integer limitLogOut, @Param(value = "limitIdle") Integer limitIdle, @Param(value = "casualUserTime") Integer casualUserTime,
                             @Param(value = "logOutTime") Integer logOutTime, @Param(value = "logSize") Integer logSize, @Param(value = "locktime") Integer locktime) throws AccountException {

        SysSet set = new SysSet();
        HttpResult result ;
        //正则表达
        if (limitDate != null) {
            if (limitTimes != null) {
                if (limitNumber != null) {
                    if (limitLogOut != null) {
                        if (limitIdle != null) {
                            if (casualUserTime != null) {
                                if (logOutTime != null) {
                                    if (logSize != null) {
                                        if (locktime != null) {
                                            if (PubUtils.patternPMonth(limitDate.toString())) {
                                                if (PubUtils.patternPERROR(limitTimes.toString())) {
                                                    if (PubUtils.patternMaxNum(limitNumber.toString())) {
                                                        if (PubUtils.patternPMonth(limitIdle.toString())) {
                                                            if (PubUtils.patterPUpw(limitLogOut.toString())) {
                                                                if (PubUtils.patternPMonth(casualUserTime.toString())) {
                                                                    if (PubUtils.patternFREE(logOutTime.toString())) {
                                                                        if (PubUtils.patterLOG(logSize.toString())) {
                                                                            if (PubUtils.patternUSERLOCK(locktime.toString())) {
                                                                                //参数合法化校验
                                                                                if (limitDate < 1 || limitDate > 3) {
                                                                                    result = PubUtils.assignmentResult(null, 500, "密码有效期值非法");
                                                                                    return result;
                                                                                }
                                                                                if (limitTimes < 1 || limitTimes > 10) {
                                                                                    result = PubUtils.assignmentResult(null, 500, "密码错误次数值非法");
                                                                                    return result;
                                                                                }
                                                                                if (limitNumber < 1 || limitNumber > 100) {
                                                                                    result = PubUtils.assignmentResult(null, 500, "最大并发数值非法");
                                                                                    return result;
                                                                                }
                                                                                if (limitIdle < 1) {
                                                                                    result = PubUtils.assignmentResult(null, 500, "闲置用户修改密码值非法");
                                                                                    return result;
                                                                                }
                                                                                //注销数值需要控制的比修改密码大
                                                                                if (limitLogOut < 1) {
                                                                                    result = PubUtils.assignmentResult(null, 500, "闲置用户注销值非法");
                                                                                    return result;
                                                                                }
                                                                                if (casualUserTime < 1 || casualUserTime > 3) {
                                                                                    return PubUtils.assignmentResult(null, 500, "临时账号休眠值非法");
                                                                                }
                                                                                if (logOutTime < 1 || logOutTime > 12) {
                                                                                    return PubUtils.assignmentResult(null, 500, "休眠账号注销值非法");
                                                                                }
                                                                                if (logSize < 1 || logSize > 2) {
                                                                                    return PubUtils.assignmentResult(null, 500, "审计日志容量值非法");
                                                                                }
                                                                                if (locktime < 20 || locktime > 60) {
                                                                                    return PubUtils.assignmentResult(null, 500, "用户锁定时长值非法");
                                                                                }
                                                                                if (sysSetIndex.equals("0")) {
                                                                                    sysSetIndex = java.util.UUID.randomUUID().toString();
                                                                                    set.setSysSetIndex(sysSetIndex);//guid
                                                                                    set.setLimitDate(limitDate);
                                                                                    set.setLimitTimes(limitTimes);
                                                                                    set.setLimitNumber(limitNumber);
                                                                                    set.setLimitIdle(limitIdle);
                                                                                    set.setLimitLogout(limitLogOut);
                                                                                    User user = TokenManager.getToken();
                                                                                    String userIndex = user.getUserIndex();//当前登录用户
                                                                                    set.setUserIndex(userIndex);
                                                                                    set.setUpdatetime(new Date());
                                                                                    set.setState(1);
                                                                                    set.setCasualUserTime(casualUserTime);
                                                                                    set.setLogOutTime(logOutTime);
                                                                                    set.setLogSize(logSize);
                                                                                    set.setLockTime(locktime);
                                                                                    try {
                                                                                        sm.insert(set);
                                                                                        userLogservice.saveUserLog(TokenManager.getToken().getUserIndex(), TokenManager.getToken().getLoginName(), "系统配置修改", "成功", appConfig.getSystemFlag(), PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                                                                        logger.info("成功更改系统配置");
                                                                                        result = PubUtils.assignmentResult(null, 200, "成功更改系统配置");
                                                                                    } catch (Exception e) {
                                                                                        userLogservice.saveUserLog(TokenManager.getToken().getUserIndex(), TokenManager.getToken().getLoginName(), "系统配置修改", "失败", appConfig.getSystemFlag(), PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                                                                        logger.error("更改配置失败");
                                                                                        result = PubUtils.assignmentResult(null, 500, "更改配置失败");
                                                                                    }
                                                                                } else {
                                                                                    set.setSysSetIndex(sysSetIndex);
                                                                                    set.setLimitDate(limitDate);
                                                                                    set.setLimitTimes(limitTimes);
                                                                                    set.setLimitNumber(limitNumber);
                                                                                    set.setLimitIdle(limitIdle);
                                                                                    set.setLimitLogout(limitLogOut);
                                                                                    User user = TokenManager.getToken();
                                                                                    String userIndex = user.getUserIndex();//当前登录用户
                                                                                    set.setUserIndex(userIndex);
                                                                                    set.setUpdatetime(new Date());
                                                                                    set.setState(1);
                                                                                    set.setCasualUserTime(casualUserTime);
                                                                                    set.setLogOutTime(logOutTime);
                                                                                    set.setLogSize(logSize);
                                                                                    set.setLockTime(locktime);
                                                                                    try {
                                                                                        String detail = userLogservice.updateSysSet(set);
                                                                                        userLogservice.getLogSys("系统配置修改", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1,detail);
                                                                                        logger.info("成功更改系统配置");
                                                                                        result = PubUtils.assignmentResult(null, 200, "成功更改系统配置");
                                                                                    } catch (Exception e) {
                                                                                        // TODO: handle exception
                                                                                        userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                                                                        logger.error("更改配置失败");
                                                                                        result = PubUtils.assignmentResult(null, 500, "更改配置失败");
                                                                                    }
                                                                                }
                                                                            } else {
                                                                                userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                                                                return PubUtils.assignmentResult(null, 500, "用户锁定时长值非法");
                                                                            }
                                                                        } else {
                                                                            userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                                                            return PubUtils.assignmentResult(null, 500, "审计日志容量值非法");
                                                                        }
                                                                    } else {
                                                                        userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                                                        return PubUtils.assignmentResult(null, 500, "休眠账号注销值非法");
                                                                    }
                                                                } else {
                                                                    userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                                                    return PubUtils.assignmentResult(null, 500, "临时账号休眠值非法");
                                                                }
                                                            } else {
                                                                userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                                                return PubUtils.assignmentResult(null, 500, "闲置用户注销值非法");
                                                            }
                                                        } else {
                                                            userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                                            return PubUtils.assignmentResult(null, 500, "闲置用户修改密码值非法");
                                                        }
                                                    } else {
                                                        userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                                        return PubUtils.assignmentResult(null, 500, "最大并发数值非法");
                                                    }
                                                } else {
                                                    userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                                    return PubUtils.assignmentResult(null, 500, "密码错误次数值非法");
                                                }
                                            } else {
                                                userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                                return PubUtils.assignmentResult(null, 500, "密码有效期值非法");
                                            }
                                        } else {
                                            userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                            return PubUtils.assignmentResult(null, 500, "用户锁定时长值为空");
                                        }
                                    } else {
                                        userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                        return PubUtils.assignmentResult(null, 500, "审计日志容量值为空");
                                    }
                                } else {
                                    userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                    return PubUtils.assignmentResult(null, 500, "休眠账号注销值为空");
                                }
                            } else {
                                userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                return PubUtils.assignmentResult(null, 500, "临时账号休眠值为空");
                            }
                        } else {
                            userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                            return PubUtils.assignmentResult(null, 500, "闲置用户注销值为空");
                        }
                    } else {
                        userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                        return PubUtils.assignmentResult(null, 500, "闲置用户修改密码值为空");
                    }
                } else {
                    userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                    return PubUtils.assignmentResult(null, 500, "最大并发数值为空");
                }
            } else {
                userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                return PubUtils.assignmentResult(null, 500, "密码错误次数值为空");
            }
        } else {
            userLogservice.getLog("系统配置修改", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            return PubUtils.assignmentResult(null, 500, "密码有效期值为空");
        }
        return result;
    }

    @ResponseBody
    @PostMapping("menuList")
    public HttpResult listMenu(HttpServletRequest request) {
        HttpResult httpResult = new HttpResult();
        try {
            List<MenuEntity> list = sys.listMenu();
            MenuEntity menuEntity = new MenuEntity();
            menuEntity.setName("菜单资源");
            menuEntity.setGuid("0");
            menuEntity.setPath("#");
            menuEntity.setMenuType("#");
            menuEntity.setMenuName("");
            menuEntity.setUpdateTime("");
            menuEntity.setMenuDes("");
            menuEntity.setParentNode("");
            menuEntity.setParentNode(null);
            list.add(menuEntity);
            httpResult.setCode(200);
            httpResult.setBody(list);
            httpResult.setMessage("获取菜单列表成功.");
            userLogservice.getLog("查询菜单列表", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setBody(null);
            httpResult.setMessage("获取菜单列表失败.");
            userLogservice.getLog("查询菜单列表", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
        }

        return httpResult;
    }

    @ResponseBody
    @PostMapping("/menuTypeList")
    public HttpResult listType() {
        HttpResult httpResult = new HttpResult();
        try {
            List<MenuEntity> list = sys.menuTypeList();
            httpResult.setCode(200);
            httpResult.setBody(list);
            httpResult.setMessage("获取资源类型成功.");
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setBody(null);
            httpResult.setMessage("获取资源类型失败.");
        }

        return httpResult;
    }

    @ResponseBody
    @PostMapping("/parentMenuList")
    public HttpResult parentMenuList() {
        HttpResult httpResult = new HttpResult();
        try {
            List<MenuEntity> list = sys.parentMenuList();
            httpResult.setCode(200);
            httpResult.setBody(list);
            httpResult.setMessage("获取上级节点成功.");
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setBody(null);
            httpResult.setMessage("获取上级节点失败.");
        }

        return httpResult;
    }

    @ResponseBody
    @PostMapping("addConfig")
    public HttpResult addConfig(HttpServletRequest request, String id, String roleId, String name, String menuIds, String des) {
        HttpResult httpResult = new HttpResult();
        id = XssFilterUtil.dealString(id);
        roleId = XssFilterUtil.dealString(roleId);
        name = XssFilterUtil.dealString(name);
        menuIds = XssFilterUtil.dealString(menuIds);
        des = XssFilterUtil.dealString(des);
        JSONArray jsonArray = JSONArray.fromObject(menuIds);
        List<String> list = new ArrayList<>();

        for (int i = 0; i < jsonArray.size(); i++) {
            list.add(jsonArray.get(i).toString());
        }

        if (id.equals("")) {
            try {//新增配置数据
                httpResult.setCode(200);
                sys.insertRoleMenuData(UUID.randomUUID().toString(), roleId, name, list, des, new Date(), TokenManager.getToken().getUserIndex(), TokenManager.getSysType());
                userLogservice.getLog("角色菜单资源配置", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            } catch (Exception e) {
                httpResult.setCode(500);
                httpResult.setBody(null);
                httpResult.setMessage("新增失败");
                userLogservice.getLog("角色菜单资源配置", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            }
        } else {
            try { //更新配置数据
                httpResult.setCode(200);
                sys.updateRoleMenuData(id, roleId, name, list, des, new Date(), TokenManager.getToken().getUserIndex());
                userLogservice.getLog("角色菜单资源配置", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            } catch (Exception e) {
                httpResult.setCode(500);
                httpResult.setBody(null);
                httpResult.setMessage("更新失败");
                userLogservice.getLog("角色菜单资源配置", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            }
        }

        return httpResult;
    }

    @ResponseBody
    @PostMapping("addMenu")
    public HttpResult addMenu(HttpServletRequest request, String id, String name, String path, String parentSelect, String typeSelect, String des) {
        HttpResult httpResult = new HttpResult();
        //xss处理
        id = XssFilterUtil.dealString(id);
        name = XssFilterUtil.dealString(name);
        path = XssFilterUtil.dealString(path);
        parentSelect = XssFilterUtil.dealString(parentSelect);
        typeSelect = XssFilterUtil.dealString(typeSelect);
        des = XssFilterUtil.dealString(des);

        if (judgeParam(name)) {//非空判断
            if (judgeParam(path)) {
                if (judgeParam(parentSelect)) {
                    if (judgeParam(typeSelect)) {
                        try {
                            if (PubUtils.patternTF(name)) { //正则校验
                                if (PubUtils.patterUrl(path)) {
                                    try {
                                        MenuEntity menuEntity = new MenuEntity();
                                        menuEntity.setName(name);
                                        menuEntity.setPath(path);
                                        menuEntity.setParentNode(parentSelect);
                                        menuEntity.setMenuType(typeSelect);
                                        menuEntity.setMenuDes(des);
                                        menuEntity.setDateUpdateTime(new Date());
                                        String userIndex = TokenManager.getToken().getUserIndex();
                                        String sysType = TokenManager.getSysType();

                                        if (judgeParam(id)) { //更新
                                            menuEntity.setGuid(id);
                                            sys.updateData(menuEntity, userIndex);
                                        } else {    //新增
                                            String guid = UUID.randomUUID().toString();
                                            menuEntity.setGuid(guid);
                                            sys.insertData(menuEntity, userIndex, sysType);
                                        }

                                        httpResult.setCode(200);
                                        httpResult = PubUtils.assignmentResult(null, 200, "操作菜单资源基础项配置成功");
                                    } catch (Exception e) {
                                        userLogservice.getLog("菜单资源基础项配置", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                        return PubUtils.assignmentResult(null, 500, "操作菜单资源基础项配置失败");
                                    }
                                } else {
                                    userLogservice.getLog("菜单资源基础项配置", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                    return PubUtils.assignmentResult(null, 500, "资源路径非法");
                                }
                            } else {
                                userLogservice.getLog("菜单资源基础项配置", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                                return PubUtils.assignmentResult(null, 500, "资源名称非法");
                            }
                        } catch (Exception e) {
                            userLogservice.getLog("菜单资源基础项配置", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                            return PubUtils.assignmentResult(null, 500, "操作菜单资源基础项配置失败");
                        }
                    } else {
                        userLogservice.getLog("菜单资源基础项配置", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                        return PubUtils.assignmentResult(null, 500, "资源类型为空");
                    }
                } else {
                    userLogservice.getLog("菜单资源基础项配置", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                    return PubUtils.assignmentResult(null, 500, "父节点为空");
                }
            } else {
                userLogservice.getLog("菜单资源基础项配置", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                return PubUtils.assignmentResult(null, 500, "资源路径为空");
            }
        } else {
            userLogservice.getLog("菜单资源基础项配置", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            return PubUtils.assignmentResult(null, 500, "资源名称为空");
        }

        return httpResult;
    }

    private boolean judgeParam(String s) {
        if (!s.equals("") && s != null) {
            return true;
        }

        return false;
    }

    @ResponseBody
    @PostMapping("deleteMenu")
    public HttpResult deleteMenu(String id,HttpServletRequest request) {
        HttpResult httpResult = new HttpResult();
        try {
            String userIndex = TokenManager.getToken().getUserIndex();
            sys.deleteMenu(id, userIndex, new Date());
            httpResult.setCode(200);
            httpResult.setMessage("删除成功");
            userLogservice.getLog("删除菜单", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("删除失败");
            userLogservice.getLog("删除菜单", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
        }

        return httpResult;
    }

    @ResponseBody
    @PostMapping("getTimerManage")
    public HttpResult getTimerManage() {
        HttpResult httpResult = new HttpResult();

        try {
            List<TimerManage> list = sys.getTimerManage();
            httpResult.setCode(200);
            httpResult.setBody(list);
            httpResult.setMessage("获取任务定时器状态成功.");
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setBody(null);
            httpResult.setMessage("获取任务定时器状态失败.");
        }

        return httpResult;
    }

    @ResponseBody
    @PostMapping("setTimerState")
    public HttpResult setTimerState(String guid, Integer state) {
        HttpResult httpResult = new HttpResult();
        Map<MyTrigger, MyJobDetail> map = mySchudler.getMap();
        String str = sys.getTimerManageByGuid(guid);

        Iterator<Map.Entry<MyTrigger, MyJobDetail>> iterator = map.entrySet().iterator();

        httpResult.setCode(500);
        httpResult.setBody(null);
        httpResult.setMessage("设置任务定时器失败.");

        while (iterator.hasNext()) {
            Map.Entry<MyTrigger, MyJobDetail> m = iterator.next();

            if (m.getKey().getCronTrigger().getKey().getGroup().equals(str)) {
                if (state == 1)
                    mySchudler.stopJob(m.getKey());
                else
                    mySchudler.startJob(m.getValue(), m.getKey(), state);

                try {
                    Integer result = sys.setTimerState(guid, state);

                    if (result == 0) {
                        httpResult.setCode(500);
                        httpResult.setBody(null);
                        httpResult.setMessage("设置任务定时器失败.");
                    } else {
                        httpResult.setCode(200);
                        httpResult.setBody(null);
                        httpResult.setMessage("设置任务定时器成功.");
                    }
                } catch (Exception e) {
                    httpResult.setCode(500);
                    httpResult.setBody(null);
                    httpResult.setMessage("设置任务定时器失败.");
                }
            }
        }

        return httpResult;
    }

    @ResponseBody
    @PostMapping("menuPloyList")
    public HttpResult menuPloyList(HttpServletRequest request) {
        HttpResult httpResult = new HttpResult();

        try {
            String sysType = TokenManager.getSysType();
            List<FunctionPloyEntity> list = sys.menuPloyList(sysType);
            httpResult.setCode(200);
            httpResult.setBody(list);
            httpResult.setMessage("获取角色菜单配置模板策略成功.");
            userLogservice.getLog("查询角色菜单配置模板策略", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setBody(null);
            httpResult.setMessage("获取角色菜单配置模板策略失败.");
            userLogservice.getLog("查询角色菜单配置模板策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
        }

        return httpResult;
    }

    @ResponseBody
    @PostMapping("roleList")
    public HttpResult roleList() {
        HttpResult httpResult = new HttpResult();

        try {
            List<MenuEntity> list = sys.roleList();
            MenuEntity menuEntity = new MenuEntity();
            menuEntity.setGuid("0");
            menuEntity.setName("用户角色");
            menuEntity.setParentNode(null);
            list.add(menuEntity);
            httpResult.setCode(200);
            httpResult.setBody(list);
            httpResult.setMessage("获取角色成功.");
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setBody(null);
            httpResult.setMessage("获取角色失败.");
        }

        return httpResult;
    }

    @ResponseBody
    @PostMapping("roleStateList")
    public HttpResult roleStateList() {
        HttpResult httpResult = new HttpResult();
        try {
            List<String> list = sys.roleStateList(TokenManager.getSysType());

            if (list == null || list.isEmpty() || list.size() == 0) {
                httpResult.setCode(500);
            } else {
                httpResult.setCode(200);
                httpResult.setBody(list);
            }
        } catch (Exception e) {
            httpResult.setCode(500);
        }
        return httpResult;
    }

    @ResponseBody
    @PostMapping("deleteMenuPloy")
    public HttpResult deleteMenuPloy(String id, String roleId) {
        HttpResult httpResult = new HttpResult();
        try {
            httpResult.setCode(200);
            sys.deleteFunctionRoleData(id, new Date(), TokenManager.getToken().getUserIndex(), roleId);
        } catch (Exception e) {
            httpResult.setCode(500);
        }

        return httpResult;
    }

    @ResponseBody
    @PostMapping("getRoleFunction")
    public HttpResult getRoleFunction(String roleId, String id) {
        HttpResult httpResult = new HttpResult();
        try {
            List<String> list = sys.getRoleFunction(roleId, id);

            if (list == null || list.isEmpty() || list.size() == 0) {
                httpResult.setCode(500);
            } else {
                httpResult.setCode(200);
                httpResult.setBody(list);
            }
        } catch (Exception e) {
            httpResult.setCode(500);
        }

        return httpResult;
    }
}
