package com.mvc.controller.taizhang;

import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.model.TAicnnModel;
import com.mvc.service.PackLogMana;
import com.mvc.service.taizhang.TAicnnService;
import com.mvc.utils.NumberUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "CNN网络输出数据接口")
@RequestMapping("/TAicnnAction")
@Controller
public class TAicnnAction {

    @Autowired
    private TAicnnService TAicnnService;

    @ApiOperation("网络输出数据分页查询")
    @RequestMapping(value = "/findAllTAicnn",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> findAllTAicnn(@RequestBody TAicnnModel model){
        try {
            Map<String,Object> res= TAicnnService.findAllTAicnn(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 = "/saveTAicnn",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> saveTAicnn(@RequestBody TAicnnModel model){

        try {
            TAicnnModel resModel= TAicnnService.saveTAicnn(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 = "/updateTAicnn",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateTAicnn(@RequestBody TAicnnModel model){
        try {
            TAicnnModel resModel= TAicnnService.updateTAicnn(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("根据读数表中的信息获取对应的CNN数据")
    @RequestMapping(value = "/findTAicnnDataByTAireadInfo/{randomCode}/{deviceSn}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> findTAicnnDataByTAireadInfo(@PathVariable("randomCode") String randomCode,@PathVariable("deviceSn") String deviceSn){
        try {
            randomCode = randomCode.replace("Z","/");
            TAicnnModel res= TAicnnService.findTAicnnDataByTAireadInfo(randomCode,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("根据读数表中的信息获取对应的CNN数据")
    @RequestMapping(value = "/findTAicnnDataByTAireadInfo/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> findTAicnnDataByTAireadInfo(@PathVariable("id") Integer id){
        try {
            TAicnnModel res= TAicnnService.findAicnnDataById(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 = "/deleteTAicnn/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> deleteTAicnn(@PathVariable("id") int id){
        try {
            int  res= TAicnnService.deleteTAicnn(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("调试接口-读取CNN原始数据")
    @RequestMapping(value = "/getTAicnnRawData/{stationCode}/{fileName}/{flag}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getTAicnnRawData(@PathVariable("stationCode") String stationCode,
                                               @PathVariable("fileName") String fileName,
                                               @PathVariable("flag") Integer flag){
        try {
            byte[] buffer= PackLogMana.getCnnRawData(stationCode,fileName);
            //测试用来对比数据
            String res= NumberUtil.bytesToHexStr(buffer);
            System.out.println("------------------读取取CNN数据报文------------------");
            StringBuilder sb=new StringBuilder();
            String temp="";
            int num=0;
            while (res.length()>2){
                temp=res.substring(0,4);
                if(flag==2){
                    temp=NumberUtil.hex2DecString(temp);
                }
                sb.append(StringUtils.leftPad(temp,3,' '));
                sb.append(",");
                res=res.substring(4);
                num++;
                if(num==6){
                    sb.append("\r\n");
                    num=0;
                }
            }
            System.out.println(sb);
            System.out.println("-----------------CNN数据报文打印完毕------------------");
            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("调试接口-读取CNN原始数据2")
    @RequestMapping(value = "/getTAicnnRawData2/{stationCode}/{fileName}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getTAicnnRawData2(@PathVariable("stationCode") String stationCode, @PathVariable("fileName") String fileName){
        try {
            System.out.println("------------------读取取CNN数据报文------------------");
            int[][] data= PackLogMana.getCnnRawData2(stationCode,fileName);
            if(data!=null){
                for (int[] ints : data) {  //a.for
                    for (int anInt : ints) {  //ints.for
                        System.out.print(anInt+ "\t");
                    }
                    System.out.println();
                }
            }
            System.out.println("-----------------CNN数据报文打印完毕------------------");
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",data);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }
}
