package com.pqs9900.controller.user;

import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.service.user.DeptsLineService;
import com.njcn.utils.PubUtils;
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.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.List;

@Controller
@RequestMapping(value = "/deptsline")
public class DeptsLineController {

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

    @Resource
    private UserLogDetailService userLog;

    @Autowired
    DeptsLineService lineService;

    //绑定监测点
    @RequestMapping(value = "getBindLine", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getBindLine(HttpServletRequest request,  String deptsIndex,String treecheck) throws AccountException {
        HttpResult result = new HttpResult();
        JSONArray jsonArray = JSONArray.fromObject(treecheck);
        List<String> index = (List<String>) JSONArray.toCollection(jsonArray, String.class);
        try {
            String message = lineService.getBindLine(deptsIndex,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, "绑定监测点失败");
        }
        return result;
    }
    //换绑监测点
    @RequestMapping(value = "changeBindLine", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult changeBindLine(HttpServletRequest request,  String deptsIndex,String treecheck) throws AccountException {
        HttpResult result = new HttpResult();
        JSONArray jsonArray = JSONArray.fromObject(treecheck);
        List<String> index = (List<String>) JSONArray.toCollection(jsonArray, String.class);
        try {
            String message = lineService.changeBindLine(deptsIndex,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, "监测点换绑失败");
        }
        return result;
    }
    //解除绑定
    @RequestMapping(value = "deleteBindLine", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult deleteBindLine(HttpServletRequest request,  String deptsIndex,String treecheck) throws AccountException {
        HttpResult result = new HttpResult();
        JSONArray jsonArray = JSONArray.fromObject(treecheck);
        List<String> index = (List<String>) JSONArray.toCollection(jsonArray, String.class);
        try {
            String message = lineService.deleteBindLine(deptsIndex,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, "解绑监测点失败");
        }
        return result;
    }

    /**
     * 查询是否绑定了监测点
     */
    @RequestMapping(value = "selectLine", method = RequestMethod.POST)
    @ResponseBody
    public int selectLine(HttpServletRequest request, String parentNode) throws AccountException {
        int flag = 0;
        try{
            flag = lineService.selectLine(parentNode);
        }catch (Exception e){
            flag = 0;
            return flag;
        }
        return flag;
    }

    /****
     * 解除父节点的所有绑定
     */
    @RequestMapping(value = "removeBind", method = RequestMethod.POST)
    @ResponseBody
    public int removeBind(HttpServletRequest request, String parentNode) throws AccountException {
        int flag = 0;
        try{
            flag = lineService.removeBind(parentNode);
        }catch (Exception e){
            flag = 0;
            return flag;
        }
        return flag;
    }
}
