package com.mvc.controller.aiiden;

import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.model.TAireadModel;
import com.mvc.model.aiiden.AiMeterParmModel;
import com.mvc.service.aiiden.AiIdenService;
import com.mvc.service.aiiden.TaskReviewService;
import com.mvc.service.copy.TAireadService;
import com.mvc.service.tcpsocket.TcpClient;
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 javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

@Api(tags = "AI识别接口访问控制")
@RequestMapping("/AiIdenAction")
@Controller
public class AiIdenAction {
    @Autowired
    private AiIdenService aiIdenService;

    @Autowired
    private TAireadService tAireadService;

    //@Autowired
    //private TAireadMapper tAireadMapper;

    @ApiOperation("获取服务器侧AI识别结果")
    @RequestMapping(value = "/getWaterResult/", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getWaterResult(@RequestBody AiMeterParmModel model) throws IOException, IllegalAccessException {
        try {
            var res = aiIdenService.getWaterResult(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("获取服务器侧AI识别结果")
    @RequestMapping(value = "/getWaterResultByAiread/", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getWaterResultByAiread(@RequestBody TAireadModel aireadModel) throws IOException, IllegalAccessException {
        try {
            var res = aiIdenService.getWaterResultByAiread(aireadModel);
            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("获取服务器侧识别结果的标注图URL")
    @RequestMapping(value = "/getAiResultImage/{imageName}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getAiResultImage(@PathVariable("imageName") String imageName) throws IOException {
        try {
            String res = aiIdenService.getAiResultImage(imageName,false);
            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;
        }
    }

    @Autowired
    private TaskReviewService taskReviewService;

    @Resource
    private TcpClient tcpClient;

    @ApiOperation("测试使用-获取服务器侧AI识别结果")
    @RequestMapping(value = "/getWaterResultTest", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getWaterResultTest() throws IOException, IllegalAccessException {
        try {
            //String message ="7e7e16000001002920203202df020002231014160000F1F1000001002948F0F0231014160038120371FF01450000058998500000FF10111100FF110899FF1218001930FF1318028077FF14180079FF15280098000000FF1630221116153459FF175089860439102190023884FF180841FF19500a0b0c0d0e0f10111213FF07450000058998500000FF0830011220400022FF09100504FF03100028FF04100112FF05100581FF0602ad26976631887193fde3fe349f60d33d87d59bfc6a2ec6878d2ecb9c43b87a863fe34c6b1b05c7fa3e49e80336695d8f41c9a7e9eea08b7ebc7df6ff001a93fb334e1d6dcf0327e76ff1a6db41640961a4bfdd8738eb8918ff005a90697a61e7ecd81ebe637f8d1cc161ff00d95a4e3fd48cff00d746ff001a69d274ded07fe3edfe34ee2b00d1f4e23fd47fe3edfe348747b0ed07fe3edfe345c2c30e9161ff003c7ff1f6ff001a8aef4bb38ed66748b0cb1b11f31eb8fad1702ecca4e00a861f360897cc8536671c75fad4ecc458e0ee3daab8593ed23c9c6e0bf36ee945b51f90fb7c9ceefbc58e47a53a68cb060ab9e071eb4340969a0cdc8d3205899383925715636fc800e94dec3be9720e7c9791a52ac09c00c40fcaa54524eecf51405f51aee410889bdce4e338c7e349bb77f09460d82339a56d02e13894a62160afd8b554bbf3e28658e670c1a26e547438a22aec2fa97653b483863fee8e950ed95d447232ec073920e6993ba2c71b491d28f295806590a1f55c517bb298ab1ac7855e7be49a5f28ca1b692083c1a0421864277ca54ed1801453c0ced02860473c21d4ee8079a7ba8e94e55db907b7e34dec0884a33ccbb5fcb603ef01cd10a90a7736e6df82deb4ba0751d3608284f07db3515d5b95b4b879242e442c172318e0d25dc3ccb6075a42055085c714d28be8295862aa81d0518e78247d0d00077631bdb1f5a52c53054e08a43245b8918364f6f4a84316049eb4c2c0cab2a00e33fa52b285c2a8c0142d844170cc1700e3240a82e97c8b5b88d09da6263c9cf6a51dc96f50ffd9033fa9";
            //tcpClient.SendtoServer(NumberUtil.hexStringToBytes(message),"b800c7dabc834d7e85229be685539533");
            //tcpClient.SendtoServer2(NumberUtil.hexStringToBytes(message),"b800c7dabc834d7e85229be685539533");
            //tcpClient.test();
            /*TaskReviewModel queryModel=new TaskReviewModel();
            queryModel.setPage(1);
            queryModel.setRows(100);
            queryModel.setTaskDate("20230128");
            //queryModel.setStationCode("0000010028");

            Map<String, Object> map = taskReviewService.findAllTaskReview(queryModel);
            List<TaskReviewModel> list = (List<TaskReviewModel>)map.get("list");
            for(TaskReviewModel model:list){
                if(model.getStationCode().equals("0000010028")) continue;
                if(StringUtils.isNotEmpty(model.getAireadId())){
                    TAireadModel aireadModel = tAireadService.getTaireadByAireadId(model.getAireadId());
                    StoreBlockingQueue.tAireadQueue.put(aireadModel);
                }
            }*/

/*            TAiread tAiread = tAireadMapper.selectByPrimaryKey(27888);
            TAireadModel model=new TAireadModel();
            BeanUtils.copyProperties(tAiread,model);
            //AiIdenTask.addTAireadModel(model);
            model.setSmallJpgUrl("E:/Postman/files/" + "11220600048_20220816080044_B.jpg");
            StoreBlockingQueue.tAireadQueue.put(model);*/
/*            AiMeterParmModel model = new AiMeterParmModel();
            model.setLast_value_time("2022-07-25 09:52:59.217");
            model.setUnit_number(new BigDecimal(1));
            model.setNum_numbers(6);
            model.setMeter_type("DN50");
            model.setDevice_id("TEST");
            model.setIs_app(false);
            //model.setImage_file("E:/Postman/files/" + "image_20220426_20_34_12_00026.jpg");
            //model.setImage_file("E:/Postman/files/" + "112206000360_20220706163021_M.jpg");         //识别不了的图
            model.setImage_file("E:/Postman/files/" + "112112001130_20220724000046695_M.jpg");
            model.setLast_value(BigDecimal.valueOf(347));
            var res = aiIdenService.getWaterResult(model);
            if(res!=null&&res.getResult()!=null){
                AiMeterResultModel resultModel=(AiMeterResultModel)res.getResult();
                aiIdenService.getAiResultImage(resultModel.getAi_image_url(),false);
            }*/

            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;
        }
    }
}
