package com.pqs9900.controller.user;

import com.njcn.enums.LogTypeEnum;
import com.njcn.mapper.user.TfLgPloyAssMapper;
import com.njcn.mapper.user.TfLgPloyMapper;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.user.TfLgPloy;
import com.njcn.pojo.user.TfLgPloyAss;
import com.njcn.pojo.user.TfLgPloyInfo;
import com.njcn.pojo.user.TfLgTree;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.service.user.TfLgPloyService;
import com.njcn.service.user.TfLgTreeService;
import com.njcn.utils.PubUtils;
import com.njcn.utils.XssFilterUtil;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.PUBLIC_MEMBER;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.security.auth.login.AccountException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("tflp")
public class TfLgPloyController {
	// 日志记录
    private static final Logger logger = LoggerFactory.getLogger(TfLgPloyController.class);
    
    @Resource
    TfLgPloyService lgPloyService;
    
    @Resource
	private UserLogDetailService userLog;
    
    @Resource
    private TfLgTreeService getDeviceTree;
    
    @Resource
    private TfLgPloyMapper tflgMapper;
    
    @Resource
    private TfLgPloyAssMapper tflgAssMapper;
    
    @Autowired
    private TfLgPloyService tflgService;
    
    @ResponseBody
	@RequestMapping(value="selectAll",method=RequestMethod.POST)
	public HttpResult selectAll(HttpServletRequest request) throws AccountException {
		HttpResult result=new HttpResult();
		try {
			List<List> deviceList=lgPloyService.selectAll();
			if(deviceList.isEmpty()){
				result.setCode(500);
				result.setMessage("没有配置信息");
                userLog.getLog("查询变压器策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
				logger.info("获取变压器策略信息失败");
			}else{
				result.setCode(200);
				result.setMessage("获取配置信息成功");
				result.setBody(deviceList);
                userLog.getLog("查询变压器策略", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
				logger.info("获取变压器策略信息成功");
			}
		}catch (Exception e){
			logger.error("获取变压器策略信息异常，异常为"+e.toString());
            userLog.getLog("查询变压器策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
			result=PubUtils.assignmentResult(null,500,"获取变压器策略信息失败");
		}
		return result;
	}
    
    /**
	 * 变压器策略树
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value="tflgstree",method=RequestMethod.POST)
	@ResponseBody
	public List<TfLgTree> tflgstree(HttpServletRequest request,HttpSession session){
		if(session.getAttribute("line_index")!=null||session.getAttribute("line_index")!=""){
			session.removeAttribute("line_index");
		}
		List<TfLgTree> result=new ArrayList();
		try{
			result=getDeviceTree.getTfLgTree();
		}catch (Exception e){
			logger.error("获取变压器策略树异常，异常为"+e.toString());
		}
		return result;
	}
	//新增策略
	@RequestMapping(value = "addTfLgPloy",method=RequestMethod.POST)
	@ResponseBody
	public HttpResult addTfLgPloy(HttpServletRequest request,HttpSession session,String tPName,String describe,String treecheck) throws AccountException {
		HttpResult result=new HttpResult();
		//后台Xss攻击处理、包括html/js/sql
		tPName= XssFilterUtil.dealString(tPName);
		describe= XssFilterUtil.dealString(describe);
		if(!StringUtils.isBlank(tPName)) {
		    if(PubUtils.patternTF(tPName)) {
		        if (PubUtils.patternDes64(describe)) {
                    JSONArray ja = JSONArray.fromObject(treecheck);
                    List<String> index = (List<String>) JSONArray.toCollection(ja, String.class);
                    try {
                        String message = lgPloyService.savePloy(tPName, describe, index);
                        if (message.equalsIgnoreCase("新增策略成功")) {
                            result.setCode(200);
                            result.setMessage(message);
                            userLog.getLog("新增变压器策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                            logger.info("新增策略成功!");
                        } else if (message.equalsIgnoreCase("新增策略失败")) {
                            result.setCode(400);
                            result.setMessage(message);
                            userLog.getLog("新增变压器策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                            logger.error("新增策略失败!名称已存在");
                        } else {
                            result.setCode(500);
                            result.setMessage(message);
                            userLog.getLog("新增变压器策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                            logger.error("新增模板失败!");
                        }
                    } catch (Exception e) {
                        logger.error("新增变压器策略信息异常，异常为" + e.toString());
                        userLog.getLog("新增变压器策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                        result = PubUtils.assignmentResult(null, 500, "新增变压器策略信息失败");
                    }
                }else {
                    userLog.getLog("新增变压器策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                    result = PubUtils.assignmentResult(null, 500, "描述长度为64内");
                }
            }else {
                userLog.getLog("新增变压器策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                result = PubUtils.assignmentResult(null, 500, "策略名称非法");
            }
        }else {
            userLog.getLog("新增变压器策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
            result = PubUtils.assignmentResult(null, 500, "策略名称不可为空");
        }
		return result;
	}
    
	//获取节点上的树
	@RequestMapping(value="loadPloy",method=RequestMethod.POST)
	@ResponseBody
	public TfLgPloyInfo loadPloy(HttpServletRequest request,String rpIndex){
		TfLgPloyInfo result=new TfLgPloyInfo();
		
		result = lgPloyService.loadPloy(rpIndex);
		
		return result;
	}
	
	//修改变压器策略
	@RequestMapping(value="updatePloy",method=RequestMethod.POST)
	@ResponseBody
	public HttpResult updatePloy(HttpServletRequest request,HttpSession session,String rpIndex,String tPName,String describe,String treecheck) throws AccountException {
		HttpResult result=new HttpResult();
		//后台Xss攻击处理、包括html/js/sql
		tPName= XssFilterUtil.dealString(tPName);
		describe= XssFilterUtil.dealString(describe);
		if(!StringUtils.isBlank(tPName)) {
		    if(PubUtils.patternTrans(tPName)) {
                JSONArray ja = JSONArray.fromObject(treecheck);
                List<String> index = (List<String>) JSONArray.toCollection(ja, String.class);
                try {
                    String message = lgPloyService.updatePloy(rpIndex, tPName, describe, index);
                    if (message.equalsIgnoreCase("修改变压器策略成功")) {
                        result.setCode(200);
                        result.setMessage(message);
                        userLog.getLog("修改变压器策略", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                        logger.info("更新变压器策略成功!");
                    } else if (message.equalsIgnoreCase("策略名称已存在")) {
                        result.setCode(400);
                        result.setMessage(message);
                        userLog.getLog("修改变压器策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                        logger.error("更新变压器策略失败!名称已存在");
                    } else {
                        result.setCode(500);
                        result.setMessage(message);
                        userLog.getLog("修改变压器策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                        logger.error("更新变压器策略失败!");
                    }
                } catch (Exception e) {
                    logger.error("修改变压器策略信息异常，异常为" + e.toString());
                    userLog.getLog("修改变压器策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                    result = PubUtils.assignmentResult(null, 500, "修改变压器策略信息失败");
                }
            }else {
                userLog.getLog("新增变压器策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
                result = PubUtils.assignmentResult(null, 500, "修改变压器策略信息失败");
            }
        }else {
            userLog.getLog("新增变压器策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
            result = PubUtils.assignmentResult(null, 500, "修改变压器策略信息失败");
        }
		return result;
	}
	
	//删除变压器策略
	@RequestMapping(value="deltePloy",method=RequestMethod.POST)
	@ResponseBody
	public HttpResult deltePloy(HttpServletRequest request,String rpIndex) throws AccountException {
		HttpResult result=new HttpResult();
		
		TfLgPloy rpp = new TfLgPloy();
		rpp.settPIndex(rpIndex);
		rpp.setState(1);
		TfLgPloyAss ass = new TfLgPloyAss();
		ass.settPIndex(rpIndex);
		
		try{
			int flag=tflgMapper.delete(rpp);
			int flag1 = tflgAssMapper.delete(ass);
			result.setCode(200);
            userLog.getLog("删除变压器策略", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
			logger.info("删除变压器策略成功!");
		}catch (Exception e) {
        	result.setCode(500);
            userLog.getLog("删除变压器策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
			logger.error("删除变压器策略失败!");
		}
		
		return result;
	}
	
	//自检功能·
	@RequestMapping(value="selfcheck",method=RequestMethod.POST)
	@ResponseBody
	public int selfcheck(HttpServletRequest request) throws AccountException {
		int result = 0;
		try {
			result = tflgService.selfcheck();
            userLog.getLog("自检", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
		}catch (Exception e){
            userLog.getLog("自检", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),1);
			logger.error("自检失败，异常为"+e.toString());
		}
	   return result;
	}
	
}
