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

import com.chinaitop.depot.intelligent.socket.SocketClient;
import com.chinaitop.depot.intelligent.utils.HexConvertUtils;
import com.chinaitop.depot.intelligent.weather.model.TWertherinfo;
import com.chinaitop.depot.intelligent.weather.service.WertherinfoService;
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.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/intelligents/wertherinfo")
@Api(value = "WertherinfoController", description = "气象站设备控制类")
public class WertherinfoController {

    @Resource
    private WertherinfoService wertherinfoService;

    @Autowired
    private SocketClient socketClient;

    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ApiOperation(value = "查询 气象站设备 信息列表", notes = "查询 气象站设备 信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "vqxzname", value = "气象站名称", paramType = "query"),
            @ApiImplicitParam(name = "searchStartDate", value = "开始时间", paramType = "query"),
            @ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query")
    })
    public ResponseEntity<PageInfo<TWertherinfo>> getList(Integer pageNum, Integer pageSize, String vqxzname, String searchStartDate, String searchEndDate) {
        List<TWertherinfo> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = wertherinfoService.getList(vqxzname,searchStartDate,searchEndDate);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed("查询气象站列表失败");
        }
        PageInfo<TWertherinfo> pageInfo = new PageInfo<TWertherinfo>(list);
        return ResponseEntity.ok(pageInfo);
    }

    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    @ApiOperation(value = "根据气象站id查询 信息", notes = "根据气象站id查询 信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "气体id", paramType = "query")
    })
    public ResponseEntity<TWertherinfo> getById(String id) {
        TWertherinfo tWertherinfo = null;
        if (StringUtils.isNotBlank(id)) {
            tWertherinfo = wertherinfoService.getById(id);
        } else {
            return ResponseEntity.failed("ID数据有误!");
        }
        return ResponseEntity.ok(tWertherinfo);
    }

    @RequestMapping(value = "/save", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value = "数据更新与添加", notes = "数据更新与添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "grainJson", value = "表数据json串", paramType = "form")
    })
    public ResponseEntity save(String grainJson) {
        try {
            wertherinfoService.save(grainJson);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed("保存失败");
        }
        return ResponseEntity.ok();
    }

    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ApiOperation(value = "根据气象站id删除", notes = "根据气象站id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "气体象站数据id", paramType = "query"),
            @ApiImplicitParam(name = "wertherinfojson", value = "气体象站数据json", paramType = "form")
    })
    public ResponseEntity deleteById(String id,String wertherinfojson) {
        if (StringUtils.isNotBlank(id)) {
            int count = wertherinfoService.deleteById(id,wertherinfojson);
            if (count > 0) {
                ResponseEntity.ok();
                return ResponseEntity.ok();
            } else {
                return ResponseEntity.failed("删除失败!");
            }
        }
        return ResponseEntity.failed("ID数据有误!");
    }
    @RequestMapping(value = "/getBycfCode", method = RequestMethod.GET)
    @ApiOperation(value = "校验是否重复", notes = "校验是否重复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vdevcode", value = "设备编码", paramType = "query"),
            @ApiImplicitParam(name = "vdevname", value = "设备名称", paramType = "query")
    })
    public ResponseEntity getBycfCode(String vdevcode,String vdevname) {
        boolean bycfCode = wertherinfoService.getBycfCode(vdevcode, vdevname);
        return ResponseEntity.ok(bycfCode);
    }

    @RequestMapping(value = "/sendNowWeatherDetection", method = RequestMethod.POST)
    @ApiOperation(value = "气象实时检测", notes = "气象实时检测")
    public ResponseEntity sendNowWeatherDetection() {
       //7E 0005 06 0101 0000000000
        byte[] b = new byte[11];
        b[0] = 0x7E;
        b[1] = 0x00;
        b[2] = 0x05;
        b[3] = 0X06;
        b[4] = 0x01;
        b[5] = 0x01;
        b[6] = 0x00;
        b[7] = 0x00;
        b[8] = 0x00;
        b[9] = 0x00;
        b[10] = 0x00;
        String result = "";
        // 发送指令
        try {
            String s = socketClient.startClient(b);
            // 0：失败；1：成功；2：设备通讯异常；3：设备故障；4：未知原因; 5: 本地模式(有的设备有本地模式，即无法远程控制)
            result = TypeEnum.resultDispose(s);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok("设备连接异常!");
        }
        return ResponseEntity.ok(result);
    }

    @RequestMapping(value = "/synchronizationAll", method = RequestMethod.GET)
    @ApiOperation(value = "与智远同步气象站信息基础数据", notes = "与智远同步气象站信息基础数据")
    @ApiImplicitParam(name = "orgId", value = "库房编码", paramType = "query")
    public ResponseEntity synchronizationAll(String orgId) {
        if (StringUtils.isBlank(orgId)){
            return ResponseEntity.ok("库房编码不能为空！");
        }

        String msg = null;
        try {
            msg = wertherinfoService.synchronizationAll(orgId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok("同步失败！");
        }
        return ResponseEntity.ok(msg);
    }
}
