package com.shining.cloud.controller.report;

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

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.njcn.enums.app.ReturnCode;
import com.njcn.pojo.commons.ResponseData;
import com.njcn.pojo.configuration.GDInformation;
import com.njcn.pojo.configuration.Line;
import com.njcn.pojo.configuration.Province;
import com.njcn.pojo.configuration.SubStation;
import com.njcn.utils.PubUtils;
import com.shining.cloud.enums.information.DevMsgCodeEnum;
import com.shining.cloud.service.report.ShiningDeviceService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/device")
@Api(tags = "监测点信息接口")
public class ShiningDeviceController {
	@Autowired
	private ShiningDeviceService deviceService;
	
    private class ProInfo {
    	Long provinceIndex;
        String provinceName;
		public Long getProvinceIndex() {
			return provinceIndex;
		}
		public String getProvinceName() {
			return provinceName;
		}
		public void setProvinceIndex(Long provinceIndex) {
			this.provinceIndex = provinceIndex;
		}
		public void setProvinceName(String provinceName) {
			this.provinceName = provinceName;
		}
    } 

    private class GDInfo {
    	Long gdIndex;
    	String gdName;
		public Long getGdIndex() {
			return gdIndex;
		}
		public String getGdName() {
			return gdName;
		}
		public void setGdIndex(Long gdIndex) {
			this.gdIndex = gdIndex;
		}
		public void setGdName(String gdName) {
			this.gdName = gdName;
		}
    }

    private class SubInfo {
    	Long subIndex;
    	String subName;
		public Long getSubIndex() {
			return subIndex;
		}
		public String getSubName() {
			return subName;
		}
		public void setSubIndex(Long subIndex) {
			this.subIndex = subIndex;
		}
		public void setSubName(String subName) {
			this.subName = subName;
		}
    }

    private class LineInfo {
    	Long lineIndex;
    	String lineName;
		public Long getLineIndex() {
			return lineIndex;
		}
		public String getLineName() {
			return lineName;
		}
		public void setLineIndex(Long lineIndex) {
			this.lineIndex = lineIndex;
		}
		public void setLineName(String lineName) {
			this.lineName = lineName;
		}
    }
    
	/**
	 * @description: 获取省
	 * @author gbl
	 * @param userId 用户ID
	 */
	@PostMapping("getProvince")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query")
	})
    @ApiOperation(value = "获取省", notes = "获取省", response = ResponseData.class)
    public ResponseData getProvince(String userId, HttpServletRequest request) {
		ResponseData responseData = new ResponseData();

        try {
    		List<Province> msgs = new ArrayList<>();
    		msgs = deviceService.getProvince(userId);
    		List<ProInfo> result = new ArrayList<>();
    		for(Province msg : msgs){
    			ProInfo tmp = new ProInfo();
    			tmp.setProvinceIndex(msg.getProvinceIndex());
    			tmp.setProvinceName(msg.getName());
    			result.add(tmp);
    		}

            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,DevMsgCodeEnum.GETPROVINCE_SUCCESS.getCode(), DevMsgCodeEnum.GETPROVINCE_SUCCESS.getMsg(), result);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,DevMsgCodeEnum.GETPROVINCE_FALL.getCode(), DevMsgCodeEnum.GETPROVINCE_FALL.getMsg(), null);
        }
		return responseData;
	}
	
	/**
	 * @description: 获取供电公司
	 * @author gbl
	 * @param userId 用户ID
	 * @param provinceIndex 省份ID
	 */
	@PostMapping("getGDInfo")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query"),
        @ApiImplicitParam(name = "provinceIndex", value = "省份ID", required = true, paramType = "query")
	})
    @ApiOperation(value = "获取供电公司", notes = "获取供电公司", response = ResponseData.class)
    public ResponseData getGDInfo(String userId, Long provinceIndex, HttpServletRequest request) {
		ResponseData responseData = new ResponseData();

        try {
    		List<GDInformation> msgs = new ArrayList<>();
    		msgs = deviceService.getGdInfo(userId, provinceIndex);
    		List<GDInfo> result = new ArrayList<>();
    		for(GDInformation msg : msgs){
    			GDInfo tmp = new GDInfo();
    			tmp.setGdIndex(msg.getGdIndex());
    			tmp.setGdName(msg.getName());
    			result.add(tmp);
    		}

            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,DevMsgCodeEnum.GETGDINFO_SUCCESS.getCode(), DevMsgCodeEnum.GETGDINFO_SUCCESS.getMsg(), result);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,DevMsgCodeEnum.GETGDINFO_FALL.getCode(), DevMsgCodeEnum.GETGDINFO_FALL.getMsg(), null);
        }
		return responseData;
	}

	/**
	 * @description: 获取变电站
	 * @author gbl
	 * @param userId 用户ID
	 * @param gdIndex 供电公司ID
	 */
	@PostMapping("getSubInfo")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query"),
        @ApiImplicitParam(name = "gdIndex", value = "供电公司ID", required = true, paramType = "query")
	})
    @ApiOperation(value = "获取变电站", notes = "获取变电站", response = ResponseData.class)
    public ResponseData getSubInfo(String userId, Long gdIndex, HttpServletRequest request) {
		ResponseData responseData = new ResponseData();

        try {
    		List<SubStation> msgs = new ArrayList<>();
    		msgs = deviceService.getSubInfo(userId, gdIndex);
    		List<SubInfo> result = new ArrayList<>();
    		for(SubStation msg : msgs){
    			SubInfo tmp = new SubInfo();
    			tmp.setSubIndex(msg.getSubIndex());
    			tmp.setSubName(msg.getName());
    			result.add(tmp);
    		}

            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,DevMsgCodeEnum.GETSUBINFO_SUCCESS.getCode(), DevMsgCodeEnum.GETSUBINFO_SUCCESS.getMsg(), result);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,DevMsgCodeEnum.GETSUBINFO_FALL.getCode(), DevMsgCodeEnum.GETSUBINFO_FALL.getMsg(), null);
        }
		return responseData;
	}

	/**
	 * @description: 获取监测点
	 * @author gbl
	 * @param userId 用户ID
	 * @param subIndex 变电站ID
	 */
	@PostMapping("getLineInfo")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query"),
        @ApiImplicitParam(name = "subIndex", value = "变电站ID", required = true, paramType = "query"),
        @ApiImplicitParam(name = "type", value = "排序标志", required = true, paramType = "query")
	})
    @ApiOperation(value = "获取监测点", notes = "获取监测点", response = ResponseData.class)
    public ResponseData getLineInfo(String userId, Long subIndex,int type, HttpServletRequest request) {
		ResponseData responseData = new ResponseData();

        try {
    		List<Line> msgs = new ArrayList<>();
    		msgs = deviceService.getLineInfo(userId, subIndex,type);
    		List<LineInfo> result = new ArrayList<>();
    		for(Line msg : msgs){
    			LineInfo tmp = new LineInfo();
    			tmp.setLineIndex(msg.getLineIndex());
    			tmp.setLineName(msg.getName());
    			result.add(tmp);
    		}

            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,DevMsgCodeEnum.GETLINEINFO_SUCCESS.getCode(), DevMsgCodeEnum.GETLINEINFO_SUCCESS.getMsg(), result);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,DevMsgCodeEnum.GETLINEINFO_FALL.getCode(), DevMsgCodeEnum.GETLINEINFO_FALL.getMsg(), null);
        }
		return responseData;
	}
	
	/**
	 * @description: 监测点模糊查询
	 * @author gbl
	 * @param userId
	 * @param lineName
	 */
	@PostMapping("queryByName")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query"),
        @ApiImplicitParam(name = "lineName", value = "监测点名称", required = false , paramType = "query")
	})
    @ApiOperation(value = "监测点模糊查询", notes = "监测点模糊查询", response = ResponseData.class)
    public ResponseData queryByName(String userId,String lineName, HttpServletRequest request) {
		ResponseData responseData = new ResponseData();

        try {
    		List<Line> msgs ;
    		msgs = deviceService.queryByName(userId,lineName);
    		List<LineInfo> result = new ArrayList<>();
    		for(Line msg : msgs){
    			LineInfo tmp = new LineInfo();
    			tmp.setLineIndex(msg.getLineIndex());
    			tmp.setLineName(msg.getName());
    			result.add(tmp);
    		}

            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,DevMsgCodeEnum.QUERYLINE_SUCCESS.getCode(), DevMsgCodeEnum.QUERYLINE_SUCCESS.getMsg(), result);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,DevMsgCodeEnum.QUERYLINE_FALL.getCode(), DevMsgCodeEnum.QUERYLINE_FALL.getMsg(), null);
        }
		return responseData;
	}
}