package com.bonus.tcp.smartIdentify.controller;

import com.bonus.common.core.utils.StringHelper;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.entity.tcp.BraceletImageVo;
import com.bonus.common.entity.tcp.TcpMessage;
import com.bonus.tcp.smartIdentify.service.BraceletImageService;
import com.bonus.tcp.websocket.handle.AqdSocketHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 手环数据绑定
 */
@Slf4j
@RestController
@RequestMapping("/bracelet")
public class BraceletImageController {


    @Autowired
    private BraceletImageService service;
    @Autowired
    AqdSocketHandler aqdSocketHandler;

    /**
     * 手环绑定人员识别
     * @param vo
     * @return
     */
    @PostMapping("/uploadImage")
    public AjaxResult uploadImage(@RequestBody BraceletImageVo vo) {
        try{
            return  service.uploadImage(vo);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
            return  AjaxResult.success("识别成功");
    }


    /**
     * 人脸校验
     * @param vo
     * @return
     */
    @PostMapping("/uploadFile")
    public AjaxResult userVerification(@RequestBody BraceletImageVo vo) {
        try{
            return  service.userVerification(vo);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return  AjaxResult.error("校验失败");
    }
    /**
     * 数据推送
     * @param vo
     * @return
     */
    @PostMapping("/setMessage")
    public AjaxResult setMessage(@RequestBody BraceletImageVo vo) {
        try{
            return  service.setMessage(vo);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return  AjaxResult.success("识别成功");
    }

    /**
     * 手环绑定人员识别
     * @param
     * @return
     */
    @PostMapping("/openCamera")
    public AjaxResult openCamera(@RequestBody BraceletImageVo vo) {
        try{
            if(StringHelper.isNotEmpty(vo.getDevCode())){
                List<String> list= Arrays.asList(vo.getDevCode().split("@"));
                List<String> shList=service.getBdShCode(list);
                if(StringUtils.isNotEmpty(shList)){
                    shList.forEach(devCode->{
                        service.updateVerdateStatus(devCode);
                        aqdSocketHandler.sendMessageToUser(devCode,"open");
                    });
                }
            }

        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return  AjaxResult.success("发送成功");
    }

    /**
     * 手环绑定人员识别
     * @param
     * @return
     */
    @PostMapping("/openCameraByGt")
    public AjaxResult openCameraByGt(@RequestBody BraceletImageVo vo) {
        try{
            List<String> list= service.getShCodeByGt(vo);
            if(StringUtils.isNotEmpty(list)){
                list.forEach(devCode->{
                    service.updateVerdateStatus(devCode);
                    aqdSocketHandler.sendMessageToUser(devCode,"open");
                });
            }
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return  AjaxResult.success("发送成功");
    }

}
