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

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.lang3.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.service.TemperatureRecordService;
import com.chinaitop.depot.intelligent.utils.RemoteControlUtil;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@SuppressWarnings("all")
@RestController
@RequestMapping(value = "/intelligents/wertherinfo")
@Api(value = "WertherinfoController", description = "气象站设备控制类")
public class WertherinfoController {
    @Autowired
    private TemperatureRecordService temperatureRecordService;
    @Resource
    private WertherinfoService wertherinfoService;
    @Resource
    private RemoteControlUtil remoteControlUtil;

    @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"),
            @ApiImplicitParam(name = "orgId", value = "库id", paramType = "query")
    })
    public ResponseEntity<PageInfo<TWertherinfo>> getList(Integer pageNum, Integer pageSize, String vqxzname, String searchStartDate, String searchEndDate, @RequestParam @NotNull String orgId) {
        List<TWertherinfo> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = wertherinfoService.getList(vqxzname, searchStartDate, searchEndDate, orgId);
        } 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"),
            @ApiImplicitParam(name = "orgId", value = "单位ID", paramType = "form")
    })
    public ResponseEntity save(String grainJson, @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("grainJson", grainJson);
        return remoteControlUtil.remoteScan(
                String.format("%s%s", url, RemoteControlUtil.wertherinfoSave),
                requestEntity
        );
//        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"),
            @ApiImplicitParam(name = "orgId", value = "单位ID", paramType = "form")
    })
    public ResponseEntity deleteById(String id, String wertherinfojson, @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("wertherinfojson", wertherinfojson);
        requestEntity.add("id", id);
        return remoteControlUtil.remoteScan(
                String.format("%s%s", url, RemoteControlUtil.wertherinfoRemove),
                requestEntity
        );
//        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"),
            @ApiImplicitParam(name = "orgId", value = "库id", paramType = "query")
    })
    public ResponseEntity getBycfCode(String vdevcode, String vdevname, String orgId) {
        boolean bycfCode = wertherinfoService.getBycfCode(vdevcode, vdevname, orgId);
        return ResponseEntity.ok(bycfCode);
    }

    @RequestMapping(value = "/sendNowWeatherDetection", method = RequestMethod.POST)
    @ApiOperation(value = "气象实时检测", notes = "气象实时检测")
    public ResponseEntity sendNowWeatherDetection(@RequestParam @NotNull String orgId) {
        // 1.获取库端url
        String url = temperatureRecordService.getRemoteUrl(orgId);
        if (StringUtils.isBlank(url))
            return ResponseEntity.failed("库端硬件地址未配置!");
        return remoteControlUtil.remoteScan(
                String.format("%s%s", url, RemoteControlUtil.wertherinfo),
                new LinkedMultiValueMap<>()
        );
    }

    @RequestMapping(value = "/synchronizationAll", method = RequestMethod.GET)
    @ApiOperation(value = "与智远同步气象站信息基础数据", notes = "与智远同步气象站信息基础数据")
    @ApiImplicitParam(name = "orgId", value = "库房编码", paramType = "query")
    public ResponseEntity synchronizationAll(@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("orgId", orgId);
        return remoteControlUtil.remoteScan(
                String.format("%s%s", url, RemoteControlUtil.wertherinfoSYN),
                requestEntity
        );
    }

}
