package com.mvc.controller.taizhang;


import com.mvc.model.CorrectMap;
import com.mvc.model.CustomeGroupQueryModel;
import com.mvc.model.TAireadModel;
import com.mvc.model.ErrorMap;
import com.mvc.service.copy.TAireadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = "设备读数表接口")
@RequestMapping("/TAireadAction")
@Controller
public class TAireadAction {

    @Resource
    private TAireadService tAireadService;

    @ApiOperation("设备读数分页查询-根据设备sn查询")
    @RequestMapping(value = "/list/{deviceSn}/{startTime}/{endTime}/{page}/{rows}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findAllTAiread(@PathVariable("deviceSn") String deviceSn, @PathVariable("startTime") String startTime, @PathVariable("endTime") String endTime,
                                              @PathVariable("page") Integer page, @PathVariable("rows") Integer rows) {
        try {
            Map<String, Object> res = tAireadService.findAllTAiread(deviceSn, startTime, endTime, page, rows);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("设备读数分页查询-根据遥测站代码查询")
    @RequestMapping(value = "/listByStationcode/{stationCode}/{startTime}/{endTime}/{page}/{rows}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findAllTAireadBystationCode(@PathVariable("stationCode") String stationCode, @PathVariable("startTime") String startTime, @PathVariable("endTime") String endTime,
                                                           @PathVariable("page") Integer page, @PathVariable("rows") Integer rows) {
        try {
            Map<String, Object> res = tAireadService.findAllTAireadByStationCode(stationCode, startTime, endTime, page, rows);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据读数对象查询列表")
    @RequestMapping(value = "/findAllTAireadByCondition", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findAllTAireadByCondition(@RequestBody TAireadModel model) {
        try {
            Map<String, Object> res = tAireadService.findAllTAireadByCondition(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("新增设备读数")
    @RequestMapping(value = "/saveTAiread", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveTAiread(@RequestBody TAireadModel model) {

        try {
            TAireadModel resModel = tAireadService.saveTAiread(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", resModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("更新设备读数")
    @RequestMapping(value = "/updateTAiread", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateTAiread(@RequestBody TAireadModel model) {
        try {
            TAireadModel resModel = tAireadService.updateTAiread(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", resModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("删除设备读数")
    @RequestMapping(value = "/deleteTAiread/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteTAiread(@PathVariable("id") int id) {
        try {
            int res = tAireadService.deleteTAiread(id);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据树结构获取最新的数据")
    @RequestMapping(value = "/findLatestDataByStationCodes/{type}/{code}/{page}/{rows}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findLatestDataByStationCodes(@PathVariable("type") int type, @PathVariable("code") String code,
                                                            @PathVariable("page") Integer page, @PathVariable("rows") Integer rows) {
        try {
            Map<String, Object> res = tAireadService.findLatestDataByStationCodes(type, code,false, page, rows);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据遥测站串获取每个设备最新的一条数据")
    @RequestMapping(value = "/findLatestTAireadByStationCodes/{stationCodes}/{page}/{rows}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findLatestTAireadByStationCodes(@PathVariable("stationCodes") String stationCodes,
                                                               @PathVariable("page") Integer page, @PathVariable("rows") Integer rows) {
        try {
            Map<String, Object> res = tAireadService.findLatestTAireadByStationCodes(stationCodes,false,false, page, rows);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据树结构获取最新的数据(客户)")
    @RequestMapping(value = "/findLatestDataByStationCodesForCustomer/{type}/{code}/{page}/{rows}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findLatestDataByStationCodesForCustomer(@PathVariable("type") int type, @PathVariable("code") String code,
                                                            @PathVariable("page") Integer page, @PathVariable("rows") Integer rows) {
        try {
            Map<String, Object> res = tAireadService.findLatestDataByStationCodes(type, code,true, page, rows);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据遥测站串获取每个设备最新的一条数据(客户)")
    @RequestMapping(value = "/findLatestTAireadByStationCodesForCustomer/{stationCodes}/{page}/{rows}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findLatestTAireadByStationCodesForCustomer(@PathVariable("stationCodes") String stationCodes,
                                                               @PathVariable("page") Integer page, @PathVariable("rows") Integer rows) {
        try {
            Map<String, Object> res = tAireadService.findLatestTAireadByStationCodes(stationCodes,false,true, page, rows);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据遥测站串获取每个设备最新的一条数据")
    @RequestMapping(value = "/findLatestTAireadSortStationCodes/{stationCodes}/{page}/{rows}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findLatestTAireadSortStationCodes(@PathVariable("stationCodes") String stationCodes,
                                                               @PathVariable("page") Integer page, @PathVariable("rows") Integer rows) {
        try {
            //加此接口有特殊原因,在重点关注页面查询组下站点与右侧数据时,由于数据有分页,但数据又没有站点名称,排序要做关联表,TAiRead表数据量大,因此按编号排序
            Map<String, Object> res = tAireadService.findLatestTAireadByStationCodes(stationCodes,true,false, page, rows);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("查询所有站点最新数据，根据各版本号进行过滤(前台重点关注模块使用)")
    @RequestMapping(value = "/findTAireadListByVersion",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> findTAireadListByVersion(@RequestBody CustomeGroupQueryModel model){
        try {
            var res= tAireadService.findTAireadListByVersion(model);
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据deviceSn获取最新的一条读数记录")
    @RequestMapping(value = "/findLatestTAireadByDeviceSn/{deviceSn}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findLatestTAireadByDeviceSn(@PathVariable("deviceSn") String deviceSn) {
        try {
            TAireadModel res = tAireadService.findLatestTAireadByDeviceSn(deviceSn);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据设备SN串获取每个设备最新的一条数据")
    @RequestMapping(value = "/findLatestTAireadByDeviceSns/{deviceSns}/{page}/{rows}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findLatestTAireadByDeviceSns(@PathVariable("deviceSns") String deviceSns,
                                                                 @PathVariable("page") Integer page, @PathVariable("rows") Integer rows) {
        try {
            //加此接口有特殊原因,在重点关注页面查询组下站点与右侧数据时,由于数据有分页,但数据又没有站点名称,排序要做关联表,TAiRead表数据量大,因此按编号排序
            Map<String, Object> res = tAireadService.findLatestTAireadByDeviceSns(deviceSns,true, page, rows);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据时间范围分页获取所有站点读数")
    @RequestMapping(value = "/findAllTaireadByTime/{startTime}/{endTime}/{page}/{rows}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findAllTaireadByTime(@PathVariable("startTime") String startTime, @PathVariable("endTime") String endTime,
                                              @PathVariable("page") Integer page, @PathVariable("rows") Integer rows) {
        try {
            Map<String, Object> res = tAireadService.findAllTaireadByTime(startTime, endTime, page, rows);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }
}
