package com.pqs9900.controller.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.user.*;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.njcn.mapper.user.TfLgPloyAssMapper;
import com.njcn.pojo.configuration.SubVoltage;
import com.njcn.service.configuration.ISubVoltageService;
import com.njcn.service.user.TfLgAssService;
import com.njcn.service.user.TransFormerService;
import com.njcn.service.user.UserService;


@Controller
@RequestMapping("tflg")
public class TfLgAssController {
    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(TfLgAssController.class);
    @Resource
    private TransFormerService tfs;

    @Resource
    TfLgPloyAssMapper tf;

    @Resource
    private TfLgAssService tflg;

    @Resource
    private ISubVoltageService subv;

    @Resource
    private UserService us;

    @Autowired
    UserLogDetailService userLogDetailService;

    @ResponseBody
    @RequestMapping(value = "selectTwoName", method = RequestMethod.POST)
    public List<TransFormer> selectTwoName(@Param(value = "tfIndex") String tfIndex) {
        List<TransFormer> list = new ArrayList<>();
        try {
            list = tfs.selectTwoName(tfIndex);
        } catch (Exception e) {
            logger.error("产生异常，异常是" + e.toString());
        }
        return list;
    }

    //查询当前变压器的上下节点
    @ResponseBody
    @RequestMapping(value = "selectByMe", method = RequestMethod.POST)
    public List<TfLgAssByMe> selectByMe(@Param(value = "tfIndex") String tfIndex) {
        List<TfLgAssByMe> list = new ArrayList<>();
        try {
            list = tflg.selectByMe(tfIndex);
        } catch (Exception e) {
            logger.error("产生异常，异常是" + e.toString());
        }
        return list;
    }

    //查询所有的节点
    @ResponseBody
    @RequestMapping(value = "selectIndexAll", method = RequestMethod.POST)
    public TfLgAssDetail selectIndexAll(int subIndex, String tfIndex) {
        TfLgAssDetail list = new TfLgAssDetail();
        try {
            list = tflg.getBeforeNext(subIndex, tfIndex);
        } catch (Exception e) {
            logger.error("产生异常，异常是" + e.toString());
        }
        return list;
    }

    //查询所有的节点
    @ResponseBody
    @RequestMapping(value = "selectIndexKAll", method = RequestMethod.POST)
    public TfLgAssDetail selectIndexKAll(int subIndex) {
        TfLgAssDetail list = new TfLgAssDetail();
        try {
            list = tflg.getKBeforeNext(subIndex);
        } catch (Exception e) {
            logger.error("产生异常，异常是" + e.toString());
        }
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "getSubvIndex", method = RequestMethod.POST)
    public int getSubvIndex(int subIndex) {
        int num = 0;
        try {
            num = tflg.getSubvIndex(subIndex);
        } catch (Exception e) {
            logger.error("产生异常，异常是" + e.toString());
        }
        return num;
    }

    //根据subvId查询除此之外的
    @ResponseBody
    @RequestMapping(value = "selectExAll", method = RequestMethod.POST)
    public List<LogicNext> selectExAll(@Param("subvIndex") int subvIndex, @Param("subIndex") int subIndex, @Param(value = "tfIndex") String tfIndex) {
        List<LogicNext> list = new ArrayList<>();
        try {
            list = subv.selectExAll(subvIndex, subIndex, tfIndex);
        } catch (Exception e) {
            logger.error("产生异常，异常是" + e.toString());
        }
        return list;
    }

    //根据id查询所有
    @ResponseBody
    @RequestMapping(value = "selectIdAll", method = RequestMethod.POST)
    public List<TfLgAss> selectIdAll(@Param(value = "tfIndex") String tfIndex) {
        List<TfLgAss> list = new ArrayList<>();
        try {
            list = tflg.selectIdAll(tfIndex);
        } catch (Exception e) {
            logger.error("产生异常，异常是" + e.toString());
        }
        return list;
    }

    //根据userIndex查询更新人
    @ResponseBody
    @RequestMapping(value = "selectIdName", method = RequestMethod.POST)
    public String selectIdName(@Param(value = "userIndex") String userIndex) {
        String logname = "";
        try {
            logname = us.selectIdName(userIndex);
        } catch (Exception e) {
            logger.error("产生异常，异常是" + e.toString());
        }
        return logname;
    }

    //选择节点
    @ResponseBody
    @RequestMapping(value = "updateTfLgAss", method = RequestMethod.POST)
    public int updateTfLgAss(HttpServletRequest request, @Param(value = "tfLgAss_Index") String tfLgAss_Index, @Param(value = "updatetime") Date updatetime,
                             @Param(value = "tfIndex") String tfIndex, @Param(value = "userIndex") String userIndex,
                             @Param(value = "logicBefore") int logicBefore, @Param(value = "logicNext") int logicNext, @Param(value = "state") Integer state) throws AccountException {
        List<TfLgAss> list = tflg.selectIdAll(tfIndex);
        int flag = 0;
        if (!list.isEmpty()) {
            try {
                flag = tflg.updateTfLgAss(tfIndex, userIndex, logicBefore, logicNext);
                userLogDetailService.getLog("节点维护", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            } catch (Exception e) {
                userLogDetailService.getLog("节点维护", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                logger.error("产生异常，异常是" + e.toString());
            }
        } else {
            try {
                flag = tflg.insertTfLgAss(tfLgAss_Index, updatetime, tfIndex, userIndex, logicBefore, logicNext, state);
                userLogDetailService.getLog("节点维护", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            } catch (Exception e) {
                userLogDetailService.getLog("节点维护", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                logger.error("产生异常，异常是" + e.toString());
            }
        }
        return flag;
    }
}
