package com.pqs9900.controller.business;

import com.njcn.enums.LogTypeEnum;
import com.njcn.mapper.configuration.RptAssMapper;
import com.njcn.mapper.configuration.RptDataMapper;
import com.njcn.mapper.configuration.RptPloyMapper;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.commons.PloyInfo;
import com.njcn.pojo.configuration.RPTree;
import com.njcn.pojo.configuration.RptAss;
import com.njcn.pojo.configuration.RptData;
import com.njcn.pojo.configuration.RptPloy;
import com.njcn.service.configuration.ReportService;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;
import com.njcn.utils.UserUtil;
import com.njcn.utils.XssFilterUtil;
import net.sf.json.JSONArray;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("report")
public class ReportController {
    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(DeviceController.class);

    @Resource
    private ReportService getreportInfo;

    @Resource
    private RptDataMapper rptDataMapper;

    @Resource
    private RptPloyMapper rptPloyMapper;

    @Resource
    private RptAssMapper rptAssMapper;

    @Autowired
    private UserLogDetailService userLog;

    /**
     * 报告基础项树
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "rptree", method = RequestMethod.POST)
    @ResponseBody
    public List<RPTree> getTree(HttpServletRequest request, HttpSession session) {
        List<RPTree> result = new ArrayList();
        result = getreportInfo.getTreeData(Integer.valueOf("0"));

        return result;
    }

    /**
     * 区域报告基础项树
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "arearptree", method = RequestMethod.POST)
    @ResponseBody
    public List<RPTree> getAreaTree(HttpServletRequest request, HttpSession session) {
        List<RPTree> result = new ArrayList();
        result = getreportInfo.getTreeData(Integer.valueOf("1"));

        return result;
    }

    @RequestMapping(value = "addNode", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult addNode(HttpServletRequest request, HttpSession session, String name, String describe, Integer first, Integer second) {
        HttpResult result = new HttpResult();

        name = XssFilterUtil.dealString(name);
        describe = XssFilterUtil.dealString(describe);
        if (!PubUtils.patternDevname(name)) {
            result.setCode(500);
            result.setMessage("新增失败!名称违规");
            logger.error("新增配置项失败!");
            userLog.getLog("新增配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        } else if (!PubUtils.patternDes64(describe)) {
            result.setCode(500);
            result.setMessage("新增配置项失败!描述违规");
            logger.error("新增配置项失败!");
            userLog.getLog("新增配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        } else {
            try {
                String message = getreportInfo.saveNode(name, describe, first, second, 0);
                if (message.equalsIgnoreCase("成功")) {
                    result.setCode(200);
                    result.setMessage(message);
                    logger.info("新增配置项成功!");
                    userLog.getLog("新增配置项", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                } else if (message.equalsIgnoreCase("失败a")) {
                    result.setCode(400);
                    result.setMessage(message);
                    logger.error("新增配置项失败!名称已存在");
                    userLog.getLog("新增配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                } else {
                    result.setCode(500);
                    result.setMessage(message);
                    logger.error("新增配置项失败!");
                    userLog.getLog("新增配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                }

            } catch (Exception e) {
                result.setCode(500);
                result.setMessage("新增配置项失败!");
                logger.error("新增配置项失败!");
                userLog.getLog("新增配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
            }
        }
        return result;
    }

    @RequestMapping(value = "modNode", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult modNode(HttpServletRequest request, HttpSession session, String rdIndex, String name, String describe) {
        HttpResult result = new HttpResult();

        name = XssFilterUtil.dealString(name);
        describe = XssFilterUtil.dealString(describe);
        if (!PubUtils.patternDevname(name)) {
            result.setCode(500);
            result.setMessage("更新失败!名称违规");
            logger.error("更新配置项失败!");
            userLog.getLog("修改配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        } else if (!PubUtils.patternDes64(describe)) {
            result.setCode(500);
            result.setMessage("更新配置项失败!描述违规");
            logger.error("更新配置项失败!");
            userLog.getLog("修改配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        } else {
            try {
                String message = getreportInfo.updateNode(rdIndex, name, describe);
                if (message.equalsIgnoreCase("更新成功")) {
                    result.setCode(200);
                    result.setMessage(message);
                    logger.info("更新配置项成功!");
                    userLog.getLog("修改配置项", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                } else if (message.equalsIgnoreCase("更新失败a")) {
                    result.setCode(400);
                    result.setMessage(message);
                    logger.error("更新配置项失败!名称已存在");
                    userLog.getLog("修改配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                } else {
                    result.setCode(500);
                    result.setMessage(message);
                    logger.error("更新配置项失败!");
                    userLog.getLog("修改配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                }

            } catch (Exception e) {
                result.setCode(500);
                result.setMessage("更新配置项失败!");
                logger.error("更新配置项失败!");
                userLog.getLog("修改配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
            }
        }
        return result;
    }

    /**
     * 加载一级节点
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "getfirst", method = RequestMethod.POST)
    @ResponseBody
    public List<RptData> getfirst(HttpServletRequest request, HttpSession session) {
        List<RptData> result = new ArrayList();
        RptData tmp = new RptData();

        result = rptDataMapper.getNodeData(0, 0);

        return result;
    }

    /**
     * 加载二级节点
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "getsecond", method = RequestMethod.POST)
    @ResponseBody
    public List<RptData> getsecond(HttpServletRequest request, HttpSession session, Integer first) {
        List<RptData> result = new ArrayList();
        RptData tmp = new RptData();

        try {
            result = rptDataMapper.getNodeData(first, 0);
        } catch (Exception e) {
            result = null;
        }

        return result;
    }

    /**
     * 加载配置项信息
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "getInfo", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getInfo(HttpServletRequest request, HttpSession session, Integer index) {
        HttpResult result = new HttpResult();
        List<List> datalist;

        try {
            if (index == 0) {
                datalist = getreportInfo.getAllData(Integer.valueOf("0"));
            } else {
                datalist = getreportInfo.getData(index);
            }
            if (datalist.isEmpty()) {
                result.setCode(500);
                result.setMessage("没有配置信息");
                logger.error("获取配置项信息失败");
                userLog.getLog("查询配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
            } else {
                result.setCode(200);
                result.setMessage("获取配置信息成功");
                result.setBody(datalist);
                logger.info("获取配置项信息成功");
                userLog.getLog("查询配置项", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
            }
        } catch (Exception e) {
            result.setCode(500);
            result.setMessage("获取配置项信息失败");
            logger.error("获取配置项信息失败");
            userLog.getLog("查询配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        }

        return result;
    }

    /**
     * 保存报告模板
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "addPloy", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult addPloy(HttpServletRequest request, HttpSession session, String name, String describe, String treecheck) {
        HttpResult result = new HttpResult();
        //后台Xss攻击处理、包括html/js/sql
        name = XssFilterUtil.dealString(name);
        describe = XssFilterUtil.dealString(describe);
        JSONArray ja = JSONArray.fromObject(treecheck);
        List<String> index = (List<String>) JSONArray.toCollection(ja, String.class);

        if (!PubUtils.patternDevname(name)) {
            result.setCode(500);
            result.setMessage("新增失败!名称违规");
            logger.error("新增模板失败!");
            userLog.getLog("新增模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        } else if (!PubUtils.patternDes64(describe)) {
            result.setCode(500);
            result.setMessage("新增模板失败!描述违规");
            logger.error("新增模板失败!");
            userLog.getLog("新增模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        } else {
            try {
                String message = getreportInfo.savePloy(name, describe, index, Integer.valueOf("0"));

                if (message.equalsIgnoreCase("成功")) {
                    result.setCode(200);
                    result.setMessage(message);
                    logger.info("新增模板成功!");
                    userLog.getLog("新增模板", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                } else if (message.equalsIgnoreCase("失败a")) {
                    result.setCode(400);
                    result.setMessage(message);
                    logger.error("新增模板失败!名称已存在");
                    userLog.getLog("新增模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                } else {
                    result.setCode(500);
                    result.setMessage(message);
                    logger.error("新增模板失败!");
                    userLog.getLog("新增模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                }

            } catch (Exception e) {
                result.setCode(500);
                result.setMessage("新增模板失败!");
                logger.error("新增模板失败!");
                userLog.getLog("新增模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
            }
        }
        return result;
    }

    /**
     * 修改报告模板
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "modPloy", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult modPloy(HttpServletRequest request, HttpSession session, String rpIndex, String name, String describe, String treecheck) {
        HttpResult result = new HttpResult();

        JSONArray ja = JSONArray.fromObject(treecheck);
        List<String> index = (List<String>) JSONArray.toCollection(ja, String.class);

        name = XssFilterUtil.dealString(name);
        describe = XssFilterUtil.dealString(describe);
        if (!PubUtils.patternDevname(name)) {
            result.setCode(500);
            result.setMessage("更新失败!名称违规");
            logger.error("更新模板失败!");
            userLog.getLog("修改模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        } else if (!PubUtils.patternDes64(describe)) {
            result.setCode(500);
            result.setMessage("更新模板失败!描述违规");
            logger.error("更新模板失败!");
            userLog.getLog("修改模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        } else {
            try {
                String message = getreportInfo.updatePloy(rpIndex, name, describe, index);

                if (message.equalsIgnoreCase("成功")) {
                    result.setCode(200);
                    result.setMessage(message);
                    logger.info("更新模板成功!");
                    userLog.getLog("修改模板", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                } else if (message.equalsIgnoreCase("失败a")) {
                    result.setCode(400);
                    result.setMessage(message);
                    logger.error("更新模板失败!名称已存在");
                    userLog.getLog("修改模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                } else {
                    result.setCode(500);
                    result.setMessage(message);
                    logger.error("更新模板失败!");
                    userLog.getLog("修改模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                }

            } catch (Exception e) {
                result.setCode(500);
                result.setMessage("更新模板失败!");
                logger.error("更新模板失败!");
                userLog.getLog("修改模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
            }
        }
        return result;
    }

    /**
     * 加载模板列表
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "queryPloy", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult queryPloy(HttpServletRequest request, HttpSession session) {
        HttpResult result = new HttpResult();
        List<List> list = new ArrayList<>();
        List<RptPloy> rpp = new ArrayList<>();
        RptPloy aa = new RptPloy();
        aa.setState(Integer.valueOf("1"));
        aa.setPloyType(Integer.valueOf("0"));

        rpp = rptPloyMapper.select(aa);

        for (int i = 0; i < rpp.size(); i++) {
            List<String> tmp = new ArrayList<>();
            tmp.add(rpp.get(i).getRpName());
            tmp.add(rpp.get(i).getRpDescribe());

            tmp.add(
                    "<button type='button' class='btn themeBGColor' style='margin-right:10px;' href='javascript:void(0);' onclick='updateRpPloy(\"" + rpp.get(i).getRpIndex() + "\")' ><i class='glyphicon glyphicon-edit'></i>修改</button>"
                            + "<button type='button' class='btn  btn-danger' style='margin-right:10px;' href='javascript:void(0);' onclick='deleteRpPloy(\"" + rpp.get(i).getRpIndex() + "\")' ><i class='glyphicon glyphicon-trash'></i>删除</button>"
            );

            list.add(tmp);
        }

        if (list.isEmpty()) {
            result.setCode(500);
            result.setMessage("没有报告模板信息");
            logger.error("获取报告模板失败");
            userLog.getLog("查询报告模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        } else {
            result.setCode(200);
            result.setMessage("获取报告模板成功");
            result.setBody(list);
            logger.info("获取报告模板成功");
            userLog.getLog("查询报告模板", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        }

        return result;
    }

    @RequestMapping(value = "delRdInfo", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult delRdInfo(HttpServletRequest request, String rdIndex) {
        HttpResult result = new HttpResult();

        RptData rpt = new RptData();
        rpt = rptDataMapper.selectByPrimaryKey(rdIndex);
        rpt.setState(Integer.valueOf("0"));

        try {
            int flag = rptDataMapper.updateByPrimaryKey(rpt);
            result.setCode(200);
            logger.info("删除报表配置项成功!");
            userLog.getLog("删除配置项", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        } catch (Exception e) {
            result.setCode(500);
            logger.error("删除报表配置项失败!");
            userLog.getLog("删除配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        }

        return result;
    }

    @RequestMapping(value = "loadRpInfo", method = RequestMethod.POST)
    @ResponseBody
    public RptData loadRpInfo(HttpServletRequest request, String rdIndex) {
        RptData result = new RptData();

        try {
            result = rptDataMapper.selectByPrimaryKey(rdIndex);
            userLog.getLog("查询报告", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        } catch (Exception e) {
            result = null;
            userLog.getLog("查询报告", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        }

        return result;
    }

    @RequestMapping(value = "loadPloy", method = RequestMethod.POST)
    @ResponseBody
    public PloyInfo loadPloy(HttpServletRequest request, String rpIndex) {
        PloyInfo result = new PloyInfo();

        try {
            result = getreportInfo.loadPloy(rpIndex);
            userLog.getLog("查询报告模板", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        } catch (Exception e) {
            result = null;
            userLog.getLog("查询报告模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        }

        return result;
    }

    @RequestMapping(value = "delRdPloy", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult delRdPloy(HttpServletRequest request, String rpIndex) {
        HttpResult result = new HttpResult();

        RptPloy rpp = new RptPloy();
        rpp = rptPloyMapper.selectByPrimaryKey(rpIndex);
        rpp.setState(Integer.valueOf("0"));

        RptAss ass = new RptAss();
        ass.setRpIndex(rpIndex);

        try {
            int flag = rptPloyMapper.updateByPrimaryKey(rpp);
            int flag1 = rptAssMapper.delete(ass);
            result.setCode(200);
            logger.info("删除报表模板成功!");
            userLog.getLog("删除报告模板", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        } catch (Exception e) {
            result.setCode(500);
            logger.error("删除报表模板失败!");
            userLog.getLog("删除报告模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        }

        return result;
    }

    //excel模板设置
    @PostMapping("excelsetting")
    @ResponseBody
    public HttpResult getExcelSetting(HttpServletRequest request) {
        HttpResult httpResult = new HttpResult();
        List<RptData> list;

        try {
            list = getreportInfo.getExcelSetting();
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("excel报表配置获取失败.");
            httpResult.setBody(null);
            userLog.getLog("获取excel报表配置", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            return httpResult;
        }

        httpResult.setCode(200);
        httpResult.setMessage("excel报表配置获取成功.");
        httpResult.setBody(list);
        userLog.getLog("获取excel报表配置", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        return httpResult;
    }

    //新增配置项
    @PostMapping("createData")
    @ResponseBody
    public HttpResult createData(String shortName, String fullName, String parentID, HttpServletRequest request) {
        HttpResult httpResult = new HttpResult();

        int count;

        //xss过滤
        shortName = XssFilterUtil.dealString(shortName);
        fullName = XssFilterUtil.dealString(fullName);
        if (!PubUtils.patternDevname(shortName) || !PubUtils.patternDevname(fullName)) {
            httpResult.setCode(500);
            httpResult.setMessage("新增失败!名称违规");
            logger.error("新增配置项失败!");
            userLog.getLog("新增配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
        } else {
            try {
                count = getreportInfo.createData(shortName, fullName, parentID);
            } catch (Exception e) {
                httpResult.setCode(1);
                httpResult.setBody("新增配置项失败.");
                userLog.getLog("新增配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
                return httpResult;
            }

            if (1 == count) {
                httpResult.setCode(0);
                httpResult.setBody("新增配置项成功.");
                userLog.getLog("新增配置项", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
            } else {
                httpResult.setCode(1);
                httpResult.setBody("新增配置项失败.");
                userLog.getLog("新增配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
            }
        }
        return httpResult;
    }

    //更新配置项
    @PostMapping("updateData")
    @ResponseBody
    public HttpResult updateData(String shortName, String fullName, String rdIndex, HttpServletRequest request) {
        HttpResult httpResult = new HttpResult();

        //xss过滤
        shortName = XssFilterUtil.dealString(shortName);
        fullName = XssFilterUtil.dealString(fullName);

        int count;

        if (!PubUtils.patternDevname(shortName) || !PubUtils.patternDevname(fullName)) {
            httpResult.setCode(500);
            httpResult.setMessage("更新失败!名称违规");
            logger.error("更新配置项失败!");
            userLog.getLog("更新配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
        } else {
            try {
                count = getreportInfo.updateData(shortName, fullName, rdIndex);
            } catch (Exception e) {
                httpResult.setCode(1);
                httpResult.setBody("更新配置项失败.");
                userLog.getLog("更新配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
                return httpResult;
            }

            if (1 == count) {
                httpResult.setCode(0);
                httpResult.setBody("更新配置项成功.");
                userLog.getLog("更新配置项", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
            } else {
                httpResult.setCode(1);
                httpResult.setBody("更新配置项失败.");
                userLog.getLog("更新配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
            }
        }
        return httpResult;
    }

    //删除配置项
    @PostMapping("deleteData")
    @ResponseBody
    public HttpResult deleteData(String rdIndex, HttpServletRequest request) {
        HttpResult httpResult = new HttpResult();

        int count;

        try {
            count = getreportInfo.deleteData(rdIndex);
        } catch (Exception e) {
            httpResult.setCode(1);
            httpResult.setBody("删除配置项失败.");
            userLog.getLog("删除配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
            return httpResult;
        }

        if (0 < count) {
            httpResult.setCode(0);
            httpResult.setBody("删除配置项成功.");
            userLog.getLog("删除配置项", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
        } else {
            httpResult.setCode(1);
            httpResult.setBody("删除配置项失败.");
            userLog.getLog("删除配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
        }

        return httpResult;
    }

    //新增配置模板
    @PostMapping("addexcelploy")
    @ResponseBody
    public HttpResult addExcelPloy(String name, String describe, String rdIndex, String globalRpIndex, HttpServletRequest request) {
        HttpResult httpResult = new HttpResult();
        //xss过滤
        name = XssFilterUtil.dealString(name);
        describe = XssFilterUtil.dealString(describe);

        int count;

        if (!PubUtils.patternDevname(name)) {
            httpResult.setCode(500);
            httpResult.setMessage("新增失败!名称违规");
            logger.error("新增配置模板失败!");
            userLog.getLog("新增配置模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
        } else if (!PubUtils.patternDes64(describe)) {
            httpResult.setCode(500);
            httpResult.setMessage("新增失败!描述违规");
            logger.error("新增配置模板失败!");
            userLog.getLog("新增配置模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
        } else {
            try {
                count = getreportInfo.addExcelPloy(name, describe, rdIndex);
            } catch (Exception e) {
                httpResult.setCode(1);
                httpResult.setBody("新增配置模板失败.");
                userLog.getLog("新增配置模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
                return httpResult;
            }

            if (0 < count) {
                httpResult.setCode(0);
                httpResult.setBody("新增配置模板成功.");
                userLog.getLog("新增配置模板", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
            } else {
                httpResult.setCode(1);
                httpResult.setBody("新增配置模板失败.");
                userLog.getLog("新增配置模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
            }
        }
        return httpResult;
    }

    //获取excel报表模板信息
    @PostMapping("getPloyData")
    @ResponseBody
    public HttpResult getPloyData(HttpServletRequest request) {
        HttpResult httpResult = new HttpResult();
        RptPloy rptPloy = new RptPloy();
        rptPloy.setState(new Integer(1));
        rptPloy.setPloyType(new Integer(2));

        List<RptPloy> list;

        try {
            list = rptPloyMapper.select(rptPloy);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("excel报表模板获取失败.");
            httpResult.setBody(null);
            userLog.getLog("获取excel报表模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            return httpResult;
        }

        httpResult.setCode(200);
        httpResult.setMessage("excel报表模板获取成功.");
        httpResult.setBody(list);
        userLog.getLog("获取excel报表模板", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);

        return httpResult;
    }

    //获取excel报表模板配置信息
    @PostMapping("getTreeState")
    @ResponseBody
    public HttpResult getTreeState(String rpIndex, HttpServletRequest request) {
        HttpResult httpResult = new HttpResult();

        List<String> list;

        try {
            list = getreportInfo.getTreeState(rpIndex);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("excel报表模板配置获取失败.");
            httpResult.setBody(null);
            userLog.getLog("获取excel报表模板配置", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            return httpResult;
        }

        httpResult.setCode(200);
        httpResult.setMessage("excel报表模板配置获取成功.");
        httpResult.setBody(list);
        userLog.getLog("获取excel报表模板配置", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);

        return httpResult;
    }

    //编辑excel报表模板
    @PostMapping("updateexcelploy")
    @ResponseBody
    public HttpResult updateExcelPloy(String name, String describe, String rdIndex, String globalrpIndex, HttpServletRequest request) {
        HttpResult httpResult = new HttpResult();

        int count;
        //xss过滤
        name = XssFilterUtil.dealString(name);
        describe = XssFilterUtil.dealString(describe);

        if (!PubUtils.patternDevname(name)) {
            httpResult.setCode(500);
            httpResult.setMessage("修改失败!名称违规");
            logger.error("修改配置模板失败!");
            userLog.getLog("修改配置模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
        } else if (!PubUtils.patternDes64(describe)) {
            httpResult.setCode(500);
            httpResult.setMessage("修改失败!描述违规");
            logger.error("修改配置模板失败!");
            userLog.getLog("修改配置模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
        } else {
            try {
                count = getreportInfo.updateExcelPloy(name, describe, rdIndex, globalrpIndex);
            } catch (Exception e) {
                httpResult.setCode(1);
                httpResult.setBody("修改配置模板失败.");
                userLog.getLog("修改配置模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
                return httpResult;
            }

            if (0 < count) {
                httpResult.setCode(0);
                httpResult.setBody("修改配置模板成功.");
                userLog.getLog("修改配置模板", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
            } else {
                httpResult.setCode(1);
                httpResult.setBody("修改配置模板失败.");
                userLog.getLog("修改配置模板", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
            }
        }
        return httpResult;
    }


    //删除excel报表模板
    @PostMapping("deleteexcelploy")
    @ResponseBody
    public HttpResult deleteExcelPloy(String rpIndex, HttpServletRequest request) {
        HttpResult httpResult = new HttpResult();

        int count;

        try {
            count = getreportInfo.deleteExcelPloy(rpIndex);
        } catch (Exception e) {
            httpResult.setCode(1);
            httpResult.setBody("删除配置模板失败.");
            userLog.getLog("删除配置模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
            return httpResult;
        }

        if (0 < count) {
            httpResult.setCode(0);
            httpResult.setBody("删除配置模板成功.");
            userLog.getLog("删除配置模板", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
        } else {
            httpResult.setCode(1);
            httpResult.setBody("删除配置模板失败.");
            userLog.getLog("删除配置模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 1);
        }

        return httpResult;
    }

    /**
     * 加载区域模板列表
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "areaPloy", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult areaPloy(HttpServletRequest request, HttpSession session) {
        HttpResult result = new HttpResult();
        List<List> list = new ArrayList<>();
        List<RptPloy> rpp;
        RptPloy aa = new RptPloy();
        aa.setState(Integer.valueOf("1"));
        aa.setPloyType(Integer.valueOf("1"));

        rpp = rptPloyMapper.select(aa);

        for (int i = 0; i < rpp.size(); i++) {
            List<String> tmp = new ArrayList<>();
            tmp.add(rpp.get(i).getRpName());
            tmp.add(rpp.get(i).getRpDescribe());

            tmp.add(
                    "<button type='button' class='btn themeBGColor' style='margin-right:10px;' href='javascript:void(0);' onclick='updateRpPloy(\"" + rpp.get(i).getRpIndex() + "\")' ><i class='glyphicon glyphicon-edit'></i>修改</button>"
                            + "<button type='button' class='btn  btn-danger' style='margin-right:10px;' href='javascript:void(0);' onclick='deleteRpPloy(\"" + rpp.get(i).getRpIndex() + "\")' ><i class='glyphicon glyphicon-trash'></i>删除</button>"
            );

            list.add(tmp);
        }

        if (list.isEmpty()) {
            result.setCode(500);
            result.setMessage("没有配置信息");
            logger.error("获取配置项信息失败");
            userLog.getLog("查询区域模板", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        } else {
            result.setCode(200);
            result.setMessage("获取配置信息成功");
            result.setBody(list);
            logger.info("获取配置项信息成功");
            userLog.getLog("查询区域模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        }

        return result;
    }

    /**
     * 加载区域配置项信息
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "getAreaInfo", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getAreaInfo(HttpServletRequest request, HttpSession session, Integer index) {
        HttpResult result = new HttpResult();
        List<List> datalist;

        try {
            if (index == 0) {
                datalist = getreportInfo.getAllData(Integer.valueOf("1"));
            } else {
                datalist = getreportInfo.getData(index);
            }
            if (datalist.isEmpty()) {
                result.setCode(500);
                result.setMessage("没有配置信息");
                logger.error("获取配置项信息失败");
                userLog.getLog("查询区域配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
            } else {
                result.setCode(200);
                result.setMessage("获取配置信息成功");
                result.setBody(datalist);
                logger.info("获取配置项信息成功");
                userLog.getLog("查询区域配置项", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
            }
        } catch (Exception e) {
            result.setCode(500);
            result.setMessage("获取配置项信息失败");
            logger.error("获取配置项信息失败");
            userLog.getLog("查询区域配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        }
        return result;
    }

    /**
     * 加载区域一级节点
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "getareafirst", method = RequestMethod.POST)
    @ResponseBody
    public List<RptData> getareafirst(HttpServletRequest request, HttpSession session) {
        List<RptData> result = new ArrayList();
        RptData tmp = new RptData();

        result = rptDataMapper.getNodeData(0, 1);

        return result;
    }

    /**
     * 加载区域二级节点
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "getareasecond", method = RequestMethod.POST)
    @ResponseBody
    public List<RptData> getareasecond(HttpServletRequest request, HttpSession session, Integer first) {
        List<RptData> result = new ArrayList();
        RptData tmp = new RptData();

        try {
            result = rptDataMapper.getNodeData(first, 1);
        } catch (Exception e) {
            result = null;
        }

        return result;
    }

    @RequestMapping(value = "addareaNode", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult addareaNode(HttpServletRequest request, HttpSession session, String name, String describe, Integer first, Integer second) {
        HttpResult result = new HttpResult();

        //xss过滤
        name = XssFilterUtil.dealString(name);
        describe = XssFilterUtil.dealString(describe);

        if (!PubUtils.patternDevname(name)) {
            result.setCode(500);
            result.setMessage("新增失败!名称违规");
            logger.error("新增配置项失败!");
            userLog.getLog("新增区域配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        } else if (!PubUtils.patternDes64(describe)) {
            result.setCode(500);
            result.setMessage("新增失败!描述违规");
            logger.error("新增配置项失败!");
            userLog.getLog("新增区域配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        } else {
            try {
                String message = getreportInfo.saveNode(name, describe, first, second, 1);
                if (message.equalsIgnoreCase("成功")) {
                    result.setCode(200);
                    result.setMessage(message);
                    logger.info("新增配置项成功!");
                    userLog.getLog("新增区域配置项", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                } else if (message.equalsIgnoreCase("失败a")) {
                    result.setCode(400);
                    result.setMessage(message);
                    logger.error("新增配置项失败!名称已存在");
                    userLog.getLog("新增区域配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                } else {
                    result.setCode(500);
                    result.setMessage(message);
                    logger.error("新增配置项失败!");
                    userLog.getLog("新增区域配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                }

            } catch (Exception e) {
                result.setCode(500);
                result.setMessage("新增配置项失败!");
                logger.error("新增配置项失败!");
                userLog.getLog("新增区域配置项", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
            }
        }
        return result;
    }

    /**
     * 保存区域报告模板
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "addareaPloy", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult addareaPloy(HttpServletRequest request, HttpSession session, String name, String describe, String treecheck) {
        HttpResult result = new HttpResult();
        //后台Xss攻击处理、包括html/js/sql
        name = XssFilterUtil.dealString(name);
        describe = XssFilterUtil.dealString(describe);
        JSONArray ja = JSONArray.fromObject(treecheck);
        List<String> index = (List<String>) JSONArray.toCollection(ja, String.class);

        if (!PubUtils.patternDevname(name)) {
            result.setCode(500);
            result.setMessage("新增失败!名称违规");
            logger.error("新增模板失败!");
            userLog.getLog("新增区域模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        } else if (!PubUtils.patternDes64(describe)) {
            result.setCode(500);
            result.setMessage("新增失败!描述违规");
            logger.error("新增模板失败!");
            userLog.getLog("新增区域模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
        } else {
            try {
                String message = getreportInfo.savePloy(name, describe, index, Integer.valueOf("1"));

                if (message.equalsIgnoreCase("成功")) {
                    result.setCode(200);
                    result.setMessage(message);
                    logger.info("新增模板成功!");
                    userLog.getLog("新增区域模板", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                } else if (message.equalsIgnoreCase("失败a")) {
                    result.setCode(400);
                    result.setMessage(message);
                    logger.error("新增模板失败!名称已存在");
                    userLog.getLog("新增区域模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                } else {
                    result.setCode(500);
                    result.setMessage(message);
                    logger.error("新增模板失败!");
                    userLog.getLog("新增区域模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                }

            } catch (Exception e) {
                result.setCode(500);
                result.setMessage("新增模板失败!");
                logger.error("新增模板失败!");
                userLog.getLog("新增区域模板", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
            }
        }
        return result;
    }
}