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

import com.chinaitop.depot.intelligent.coldStorage.model.TKtinfoHistory;
import com.chinaitop.depot.intelligent.coldStorage.service.ColdStorageService;
import com.chinaitop.depot.intelligent.grainsituation.service.TemperatureRecordService;
import com.chinaitop.depot.intelligent.insideCirculation.service.InsideCirculationService;
import com.chinaitop.depot.intelligent.socket.SocketClient;
import com.chinaitop.depot.intelligent.utils.ConstUtils;
import com.chinaitop.depot.intelligent.utils.HexConvertUtils;
import com.chinaitop.depot.intelligent.utils.RemoteControlUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unissoft.model.ResponseEntity;
import com.unissoft.model.TypeEnum;
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.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.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qingsong.han
 * @description: 低温储粮模块
 * @create 2019-01-24 15:25
 */

@RestController
@RequestMapping(value = "/intelligents/insideCirculation")
@Api(value = "InsideCirculationController", description = "内环流控制类")
@SuppressWarnings("all")
public class InsideCirculationController {
    @Autowired
    private SocketClient socketClient;
    @Autowired
    private TemperatureRecordService temperatureRecordService;
    @Autowired
    private ConstUtils constUtils;
    @Autowired
    private InsideCirculationService insideCirculationService;
    @Resource
    private RemoteControlUtil remoteControlUtil;

    /**
     * 内环流控制
     * @param vcfcode
     * @param ktinfoJson json数据socket发送成功保存空调作业记录
     * @param uperson socket发送成功保存空调作业记录操作人
     * @param vdevcode
     * @param type
     *        关闭:close,打开:open,反转:reverse,停止:stop,设置当前温度:temp,设置当前风速:wind,设置当前模式:mode
     * @return
     */
    @RequestMapping(value = "/sendAirConditionDir", method = RequestMethod.POST)
    @ApiOperation(value = "空调控制指令", notes = "空调控制指令")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vcfcode",defaultValue = "001",value = "仓库编号", paramType = "query"),
            @ApiImplicitParam(name = "vdevcode",defaultValue = "00001",value = "设备编号", paramType = "query"),
            @ApiImplicitParam(name = "type",defaultValue = "temp",value = "操作类型", paramType = "query"),
            @ApiImplicitParam(name = "ktinfoJson",value = "空调控制数据json", paramType = "query"),
            @ApiImplicitParam(name = "uperson",value = "操作人", paramType = "query")
    })
    public ResponseEntity<Map<String,String>> sendAirConditionDir(String vcfcode,String vdevcode,String type,String ktinfoJson,String uperson,Integer orgId) {
        Map<String,String> map = new LinkedHashMap<>();
        // 1.获取库端url
        String url = temperatureRecordService.getRemoteUrl(orgId+"");
        if (StringUtils.isBlank(url))
            return ResponseEntity.failed("库端硬件地址未配置!");
        // 2.封装请求数据
        MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("vcfcode", vcfcode);
        requestEntity.add("tType", type);
        requestEntity.add("ktinfoJson", ktinfoJson);
        requestEntity.add("uperson", uperson);
        requestEntity.add("orgId", orgId+"");
        return remoteControlUtil.remoteScan(
                String.format("%s%s", url, remoteControlUtil.airConditionDir),
                requestEntity, "POST", null
        );
    }

    /**
     * 内环流作业记录条件列表
     * @param pageNum
     * @param pageSize
     * @param orgId
     * @param vcfcode
     * @return
     */
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ApiOperation(value = "内环流记录列表", notes = "空调作业记录列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "组织机构id", paramType = "query"),
            @ApiImplicitParam(name = "vcfcode", value = "仓房编号", paramType = "query")
    })
    public ResponseEntity<PageInfo<TKtinfoHistory>> getList(Integer pageNum, Integer pageSize, String orgId, String vcfcode) {
        List<TKtinfoHistory> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = insideCirculationService.getList(orgId,vcfcode);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed(e.getMessage());
        }
        PageInfo<TKtinfoHistory> pageInfo = new PageInfo<TKtinfoHistory>(list);
        return ResponseEntity.ok(pageInfo);
    	
    }

}
