package com.mvc.controller.app;

import com.mvc.model.TAppComlogModel;
import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.service.PackLogMana;
import com.mvc.service.app.TAppComlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "APP串口日志类")
@RequestMapping("/TAppComlogAction")
@Controller
public class TAppComlogAction {

    @Autowired
    private TAppComlogService tAppComlogService;

    @ApiOperation("分页查询APP串口日志")
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> findAllTAppComlog(@RequestBody TAppComlogModel model){
        try {
            Map<String,Object> res= tAppComlogService.findAllTAppComlog(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("新增APP串口日志记录")
    @RequestMapping(value = "/saveTAppComlog",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> saveTAppComlog(@RequestBody TAppComlogModel model){
        try {
            TAppComlogModel resModel= tAppComlogService.saveTAppComlog(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("更新APP串口日志")
    @RequestMapping(value = "/updateTAppComlog",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateDeviceAcc(@RequestBody TAppComlogModel model){
        try {
            TAppComlogModel resModel= tAppComlogService.updateTAppComlog(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("删除APP串口日志")
    @RequestMapping(value = "/deleteTAppComlog/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> deleteTAppComlog(@PathVariable("id") int id){
        try {
            int  res= tAppComlogService.deleteTAppComlog(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("根据ID查询报文(带报文内容)")
    @RequestMapping(value = "/findTAppComlogById/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> findTAppComlogById(@PathVariable("id") Integer id) {
        try {
            TAppComlogModel res= tAppComlogService.findTAppComlogById(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 = "/testFile/",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> testFile(){
        try {
            String abc = PackLogMana.getFileContent("20221008100624.txt","1000000001",0);
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",1);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据设备SN和采样时间获取记录")
    @RequestMapping(value = "/findTAppComlogBySnAndTime/{deviceSn}/{sampleTime}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> findTAppComlogBySnAndTime(@PathVariable("deviceSn") String deviceSn,@PathVariable("sampleTime") String sampleTime){
        try {
            TAppComlogModel res= tAppComlogService.findTAppComlogBySnAndTime(deviceSn,sampleTime);
            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;
        }
    }
}
