package com.pqs9900.controller.configuration;

import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.configuration.ProtectControlWord;
import com.njcn.service.configuration.ProtectControlWordService;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;
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 java.util.List;

/**
 * @Author denghuajun
 * @Description //TODO 内部定值控制字
 * @Date 2018/10/24 15:07
 **/
@Controller
@RequestMapping(value = "protectcw")
public class ProtectControlWordController {
    @Autowired
    ProtectControlWordService pcws;
    @Resource
    UserLogDetailService userLog;
    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(ConstantValueController.class);

    @RequestMapping(value = "getAll", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getAll(HttpServletRequest request, String pvIndex) throws AccountException {
        HttpResult result = new HttpResult();
        try {
            List<ProtectControlWord> list = pcws.getAll(pvIndex);
            if (list.isEmpty()) {
                result.setCode(500);
                userLog.getLog("查询定值信息", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
                result.setBody(null);
                result.setMessage("没有配置信息");
            } else {
                result.setCode(200);
                result.setMessage("获取定值信息成功");
                userLog.getLog("查询定值信息", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
                result.setBody(list);
            }
        } catch (Exception e) {
            logger.error("查询定值信息异常，异常是" + e.toString());
            userLog.getLog("查询定值信息", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            result = PubUtils.assignmentResult(null, 500, "查询定值信息失败");
        }
        return result;
    }

    @RequestMapping(value = "getIsFT", method = RequestMethod.POST)
    @ResponseBody
    public int getIsFT(HttpServletRequest request, String pvIndex) throws AccountException {
        int isft = 0;
        try {
            List<ProtectControlWord> list = pcws.getAll(pvIndex);
            if (list.isEmpty()) {
                isft = 0;
            } else {
                isft = 1;
            }
        } catch (Exception e) {
            logger.error("查询定值信息异常，异常是" + e.toString());
            userLog.getLog("查询定值信息", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            isft = 0;
        }
        return isft;
    }
}
