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

import com.chinaitop.depot.intelligent.grainsituation.service.GasDetectionService;
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;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping(value = "/intelligents/gasDetection")
@Api(value = "GasDetectionController", description = "气体检测记录控制类")
@SuppressWarnings("all")
public class GasDetectionController {

    @Value("${gas-org-ids}")
    private String orgIds = "386";

    @Resource
    private GasDetectionService gasDetectionService;
    @Autowired
    private TemperatureRecordService temperatureRecordService;


    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ApiOperation(value = "查询 气体 信息列表", notes = "查询 气体 信息列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query"),
            @ApiImplicitParam(name = "vCfCode", value = "仓库名称", paramType = "query")
    })
    public ResponseEntity<PageInfo<Object>> getList(Integer pageNum, Integer pageSize, String vCfCode, String startTime, String endTime,Integer orgId) {
        List<Object> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = gasDetectionService.getList(vCfCode, startTime, endTime, "",orgId);//查询方法添加id查询
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed(e.getMessage());
        }
        PageInfo<Object> pageInfo = new PageInfo<Object>(list);
        return ResponseEntity.ok(pageInfo);
    }

    @RequestMapping(value = "/getListGroup", method = RequestMethod.GET)
    @ApiOperation(value = "查询 气体 检测列表", notes = "查询 气体 检测列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "vCfCode", value = "仓库名称", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "组织机构id", paramType = "query")
    })
    public ResponseEntity<PageInfo<Object>> getListGroup(Integer pageNum, Integer pageSize, String vCfCode,String orgId) {
        List<Object> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = gasDetectionService.getListGroup(vCfCode,orgId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed(e.getMessage());
        }
        PageInfo<Object> pageInfo = new PageInfo<Object>(list);
        return ResponseEntity.ok(pageInfo);
    }

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

    @Resource
    private RemoteControlUtil remoteControlUtil;

    @RequestMapping(value = "/sendNowGasDetection", method = RequestMethod.POST)
    @ApiOperation(value = "气体实时检测", notes = "气体实时检测")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeCode", defaultValue = "001", value = "仓库编号", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "组织机构id",  paramType = "query")
    })
    public ResponseEntity<Map<String, String>> sendNowInsectPest(String storeCode,Integer 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.nowGasDetection),
                requestEntity, "POST", null
        );
    }

    /**
     * 单仓检测接口
     *
     * @param vCfCode
     * @return
     */
    @RequestMapping(value = "/sendOnlyGasDetection", method = RequestMethod.POST)
    @ApiOperation(value = "单仓气体实时检测", notes = "单仓气体实时检测")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vCfCode", defaultValue = "001", value = "仓库编号", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "库点编号", paramType = "query")
    })
    public ResponseEntity sendOnlyInsectPestDetection(String vCfCode, String orgId) {


        String url = temperatureRecordService.getRemoteUrl(orgId);
        if (StringUtils.isBlank(url))
            return ResponseEntity.failed("库端硬件地址未配置!");
        // 2.封装请求数据
        MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("vCfCode", vCfCode);
        return remoteControlUtil.remoteScan(
                String.format("%s%s", url, remoteControlUtil.gasOnly),
                requestEntity, "POST", null);

    }

    /**
     * 所有实时检测
     *
     * @return
     */
    @RequestMapping(value = "/sendGasDetectionAll", method = RequestMethod.GET)
    @ApiOperation(value = "所有仓气体实时检测", notes = "所有仓气体实时检测")
    @ApiImplicitParam(name = "orgId", value = "库点编号", paramType = "query")
    public ResponseEntity sendTemperatureDetectionAll(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("orgId", orgId);
        return remoteControlUtil.remoteScan(
                String.format("%s%s", url, remoteControlUtil.gasAll),
                null, "GET", requestEntity
        );

    }

}
