package com.chinaitop.depot.intelligent.grainsituation.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.chinaitop.depot.intelligent.grainsituation.model.TTestdata;
import com.chinaitop.depot.intelligent.grainsituation.model.TTestdataLayer;
import com.chinaitop.depot.intelligent.grainsituation.service.TPointsService;
import com.chinaitop.depot.intelligent.grainsituation.service.TemperatureRecordService;
import com.chinaitop.depot.intelligent.socket.SocketClient;
import com.chinaitop.depot.intelligent.utils.ConstUtils;
import com.chinaitop.depot.intelligent.utils.RemoteControlUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unissoft.model.ResponseEntity;

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

@RestController
@RequestMapping(value = "/intelligents/temperatureRecord")
@Api(value = "TemperatureRecordController", description = "测温记录控制类")
@SuppressWarnings("all")
public class TemperatureRecordController {

    @Autowired
    private TemperatureRecordService temperatureRecordService;
    @Resource
    private TPointsService tpointsService;


    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ApiOperation(value = "查询 测温 信息列表", notes = "查询 测温 信息列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "vDatatime", value = "采集时间", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query"),
            @ApiImplicitParam(name = "vCfCode", value = "仓库名称", paramType = "query"),
            @ApiImplicitParam(name = "tType", value = "检测类型0测温，1粮油,2测水分", paramType = "query")
    })
    public ResponseEntity<PageInfo<TTestdata>> getList(Integer pageNum, Integer pageSize, String vCfCode, String vDatatime, String startTime, String endTime, String tType, @RequestParam @NotNull String orgId) {
        List<TTestdata> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = temperatureRecordService.getList(vCfCode, vDatatime, startTime, endTime, tType, orgId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed(e.getMessage());
        }
        PageInfo<TTestdata> pageInfo = new PageInfo<TTestdata>(list);
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * 获取一条数据
     *
     * @param vCfCode   库房编码
     * @param vDatatime 检测时间
     * @return
     */
    @RequestMapping(value = "/findByHouseAndTime", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    @ApiOperation(value = "根据库房编码和测温时间查询单条检测记录包括每层的最高温最低温平均温", notes = "根据库房编码和测温时间查询单条检测记录包括每层的最高温最低温平均温")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lqId", value = "粮情id", paramType = "query"),
            @ApiImplicitParam(name = "vCfCode", value = "库房编码", paramType = "query"),
            @ApiImplicitParam(name = "vDatatime", value = "测温时间", paramType = "query")
    })
    public ResponseEntity findByHouseAndTime(String lqId, String vCfCode, String vDatatime, @RequestParam @NotNull String orgId) {
        List<TTestdataLayer> tTestdataLayer = null;
        try {
            tTestdataLayer = temperatureRecordService.findByHouseAndTime(lqId, vCfCode, vDatatime, orgId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed("查询失败");
        }
        return ResponseEntity.ok(tTestdataLayer);
    }

    /**
     * @param lqId
     * @param vCfCode
     * @param vDatatime
     * @param viewFlag  true: 表格数据,false: 3D三温图数据
     * @return
     */
    @RequestMapping(value = "/findByHouseAndTimes", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    @ApiOperation(value = "根据库房编码和测温时间查询检测记录包括层，行，各个节点的值", notes = "根据库房编码和测温时间查询检测记录包括层，行，各个节点的值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lqId", value = "粮情id", paramType = "query"),
            @ApiImplicitParam(name = "vCfCode", value = "库房编码", paramType = "query"),
            @ApiImplicitParam(name = "vDatatime", value = "测温时间", paramType = "query"),
            @ApiImplicitParam(name = "viewFlag", value = "区分表格和3D图", paramType = "query")
    })
    public ResponseEntity findByHouseAndTimes(String lqId, String vCfCode, String vDatatime, boolean viewFlag) {
        ArrayList<Object> byHouseAndTimes = null;
        try {
            byHouseAndTimes = temperatureRecordService.findByHouseAndTimes(lqId, vCfCode, vDatatime, viewFlag);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed("查询失败");
        }
        return ResponseEntity.ok(byHouseAndTimes);
    }

    /**
     * 左图表对比接口
     *
     * @param pageNum
     * @param pageSize
     * @param vCfCodeLeft
     * @param startTimeLeft
     * @param endTimeLeft
     * @return
     */
    @RequestMapping(value = "/getComparisonLeft", method = RequestMethod.GET)
    @ApiOperation(value = "左图表对比信息列表", notes = "左图表对比列表,支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", defaultValue = "1", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", defaultValue = "7", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "vCfCodeLeft", value = "左图仓房编码", paramType = "query"),
            @ApiImplicitParam(name = "startTimeLeft", value = "左图开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTimeLeft", value = "左图结束时间", paramType = "query"),
            @ApiImplicitParam(name = "tType", value = "检测类型0测温，1粮油,2测水分", paramType = "query")
    })
    public ResponseEntity<PageInfo<TTestdata>> getComparisonLeft(Integer pageNum, Integer pageSize, String vCfCodeLeft,
                                                                 String startTimeLeft, String endTimeLeft, String tType,@RequestParam @NotNull String orgId) {
        List<TTestdata> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = temperatureRecordService.getComparison(vCfCodeLeft, startTimeLeft, endTimeLeft, null, null, null, tType, orgId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed(e.getMessage());
        }
        PageInfo<TTestdata> pageInfo = new PageInfo<TTestdata>(list);
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * 左图表对比接口
     *
     * @param pageNum
     * @param pageSize
     * @param vCfCodeRight
     * @param startTimeRight
     * @param endTimeRight
     * @return
     */
    @RequestMapping(value = "/getComparisonRight", method = RequestMethod.GET)
    @ApiOperation(value = "右图表对比信息列表", notes = "右图表对比列表,支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", defaultValue = "1", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", defaultValue = "7", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "vCfCodeRight", value = "右图仓房编码", paramType = "query"),
            @ApiImplicitParam(name = "startTimeRight", value = "右图开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTimeRight", value = "右图结束时间", paramType = "query"),
            @ApiImplicitParam(name = "tType", value = "检测类型0测温，1粮油,2测水分", paramType = "query")
    })
    public ResponseEntity<PageInfo<TTestdata>> getComparisonRight(Integer pageNum, Integer pageSize, String vCfCodeRight,
                                                                  String startTimeRight, String endTimeRight, String tType,@RequestParam @NotNull String orgId) {

        List<TTestdata> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = temperatureRecordService.getComparison(null, null, null, vCfCodeRight, startTimeRight, endTimeRight, tType, orgId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed(e.getMessage());
        }
        PageInfo<TTestdata> pageInfo = new PageInfo<TTestdata>(list);
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * 所有点粮情对比
     *
     * @param pageNum
     * @param pageSize
     * @param storehouse
     * @param time
     * @return
     */
    @RequestMapping(value = "/getGrainPointAll", method = RequestMethod.GET)
    @ApiOperation(value = "点粮情对比", notes = "点粮情对比，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "30", paramType = "query"),
            @ApiImplicitParam(name = "time", value = "采集时间", paramType = "query"),
            @ApiImplicitParam(name = "storehouse", value = "仓库编号", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "粮情,水分id", paramType = "query")
    })
    public ResponseEntity<PageInfo<Map<String, Object>>> getGrainPointAll(Integer pageNum, Integer pageSize, String storehouse, String time, String id,@RequestParam @NotNull String orgId) {
        List<Map<String, Object>> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = tpointsService.getGrainPointAll(storehouse, time, id, orgId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed(e.getMessage());
        }
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(list);
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * orgid下的所有仓房粮情检测
     *
     * @param storeCode
     * @return
     */
    @Autowired
    private SocketClient socketClient;
    @Autowired
    private ConstUtils constUtils;
    @Resource
    private RemoteControlUtil remoteControlUtil;

    @RequestMapping(value = "/sendTemperatureDetection", method = RequestMethod.POST)
    @ApiOperation(value = "粮情实时检测", notes = "粮情实时检测")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeCode", defaultValue = "001", value = "仓库编号", paramType = "query")
    })
    public ResponseEntity<Map<String, String>> sendTemperatureDetection(String storeCode,@RequestParam @NotNull String orgId) {
        // 1.获取库端url
        String url = temperatureRecordService.getRemoteUrl(orgId);
        if (StringUtils.isBlank(url))
            return ResponseEntity.failed("库端硬件地址未配置!");
        // 2.封装请求数据
        MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("storeCode", storeCode);
        return remoteControlUtil.remoteScan(
                String.format("%s%s", url, RemoteControlUtil.chooseCFCode),
                requestEntity
        );
    }

    @RequestMapping(value = "/sendTemperatureDetectionAll", method = RequestMethod.GET)
    @ApiOperation(value = "所有仓粮情实时检测", notes = "所有仓粮情实时检测")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tType", value = "检测类型0测温，1粮油,2测水分", paramType = "query")
    })
    public ResponseEntity sendTemperatureDetectionAll(String tType,@RequestParam @NotNull String orgId) {
        // 1.获取库端url
        String url = temperatureRecordService.getRemoteUrl(orgId);
        if (StringUtils.isBlank(url))
            return ResponseEntity.failed("库端硬件地址未配置!");
        // 2.封装请求数据
        Map<String, String> requestEntity = new HashMap<>();
        requestEntity.put("tType", tType);
        return remoteControlUtil.remoteScan(
                String.format("%s%s", url, RemoteControlUtil.foodAll),
                requestEntity
        );
    }

    /**
     * 单仓检测接口
     *
     * @param storeCode
     * @return
     */
    @RequestMapping(value = "/sendOnlyStoreDetection", method = RequestMethod.POST)
    @ApiOperation(value = "单仓粮情实时检测", notes = "单仓粮情实时检测")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeCode", defaultValue = "001", value = "仓库编号", paramType = "query"),
            @ApiImplicitParam(name = "tType", value = "检测类型0测温，1粮油,2测水分", paramType = "query")
    })
    public ResponseEntity sendOnlyStoreDetection(String storeCode, String tType,@RequestParam @NotNull String orgId) {
        // 1.获取库端url
        String url = temperatureRecordService.getRemoteUrl(orgId);
        if (StringUtils.isBlank(url))
            return ResponseEntity.failed("库端硬件地址未配置!");
        // 2.封装请求数据
        MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("storeCode", storeCode);
        requestEntity.add("tType", tType);
        return remoteControlUtil.remoteScan(
                String.format("%s%s", url, RemoteControlUtil.foodOnly),
                requestEntity
        );
    }

    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @ApiOperation(value = "根据粮情id查询粮情信息", notes = "根据粮情id查询粮情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "粮情id", paramType = "query")
    })
    public ResponseEntity findById(String id) {
        TTestdata byId = null;
        try {
            byId = temperatureRecordService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed("查询失败");
        }
        return ResponseEntity.ok(byId);
    }

}
