package com.pqs9900.controller.user;

import com.njcn.pojo.configuration.GDInformation;
import com.njcn.pojo.user.DeptsGd;
import com.njcn.service.configuration.IGdInfoService;
import com.njcn.service.user.DeptGDService;
import com.njcn.shiro.token.TokenManager;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 java.util.List;

@Controller
@RequestMapping(value = "/deptsGd")
public class DeptsGdController {
    @Resource
    DeptGDService dgd;

    @Resource
    IGdInfoService ig;
    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(DeptsGdController.class);

    @ResponseBody
    @RequestMapping(value = "selectAllGd", method = RequestMethod.POST)
    public List<DeptsGd> selectAllGd(@Param(value = "deptsIndex") String deptsIndex) throws Exception {
        List<DeptsGd> list = dgd.selectAllGd(deptsIndex);

        return list;
    }

    @ResponseBody
    @RequestMapping(value = "selectNoGd", method = RequestMethod.POST)
    public List<GDInformation> selectNoGd() {
        try {
            List<GDInformation> list = ig.selectNoGd(TokenManager.getSysType());
            return list;
        } catch (Exception e) {

            List<GDInformation> listG = dgd.selectAll();
            return listG;

        }

    }

    @ResponseBody
    @RequestMapping(value = "selectOthersNoGd", method = RequestMethod.POST)
    public List<GDInformation> selectOthersNoGd(@Param("deptsIndex") String deptsIndex) {
        List<GDInformation> list = ig.selectOthersNoGd(deptsIndex, TokenManager.getSysType());
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "insertDeptGd", method = RequestMethod.POST)
    public int insertDeptGd(@Param(value = "deptsIndex") String deptsIndex, @Param(value = "GDIndex") Long GDIndex) {
        int falg = 0;
        DeptsGd deptsGd = new DeptsGd();
        try {
            deptsGd.setDeptsIndex(deptsIndex);
            deptsGd.setGDIndex(GDIndex);
            falg = dgd.insertDeptGd(deptsGd);
            logger.info("绑定供电公司成功");
        } catch (Exception e) {
            // TODO: handle exception
            falg = 0;
            logger.error("绑定供电公司失败");
        }

        return falg;
    }

    //先解绑
    @ResponseBody
    @RequestMapping(value = "deleteNowDeptGd", method = RequestMethod.POST)
    public int deleteNowDeptGd(@Param(value = "GDIndex") Long GDIndex) {
        int falg = 0;
        try {
            falg = dgd.deleteNowDeptGd(GDIndex,TokenManager.getSysType());
            logger.info("解绑供电公司成功");
        } catch (Exception e) {
            // TODO: handle exception
            falg = 0;
            logger.error("解绑供电公司失败");
        }

        return falg;
    }

    @ResponseBody
    @RequestMapping(value = "deleteDeptGd", method = RequestMethod.POST)
    public int deleteDeptGd(@Param(value = "deptsIndex") String deptsIndex, @Param(value = "GDIndex") Long GDIndex) {
        int falg = 0;
        DeptsGd deptsGd = new DeptsGd();
        try {
            deptsGd.setDeptsIndex(deptsIndex);
            deptsGd.setGDIndex(GDIndex);
            falg = dgd.deleteDeptGd(deptsGd);
            logger.info("解绑供电公司成功");
        } catch (Exception e) {
            // TODO: handle exception
            falg = 0;
            logger.error("解绑供电公司失败");
        }

        return falg;
    }
}
