package com.yx.face.service.impl;

import com.yx.face.dao.FacePassDao;
import com.yx.face.model.entity.FacePass;
import com.yx.face.service.feign.TBDeviceFaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

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

/**
 * @description:
 * @ClassName AsyncTriggerRetry
 * @Author WXG
 * @Date 2021/11/30 13:16
 */
@Service
@Slf4j
public class AsyncTriggerRetryImpl {

    @Autowired
    private TBDeviceFaceService tbDeviceFaceService;
    @Resource
    private FacePassDao facePassDao;
    @Async
    public void send(Map<String, Object> params, FacePass facePass) {
        try {
            Map<String, Object> result = tbDeviceFaceService.downUser(params);
            log.info("下发人脸downFace = {}", result);
            if (result.get("msg") != null) {
                String msg = result.get("msg").toString();
                if (msg.contains("不在线")) {
                    //设备不在线 重试 3次
                    Boolean retry = triggerRetry(params);
                    if (!retry) {
                        facePass.setStatus(3);
                        facePassDao.updateByPrimaryKeySelective(facePass);
                    }

                }
            }
        } catch (Exception e) {
            log.info("下发失败 原因 {}", e.getMessage());
            //设备不在线 重试 3次
            Boolean retry = triggerRetry(params);
            if (!retry) {
                facePass.setStatus(3);
                facePassDao.updateByPrimaryKeySelective(facePass);
            }
        }
    }

    public Boolean triggerRetry(Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        int times = 1;
        while (times <= 3) {
            try {
                Thread.sleep(1000 * 20);   // 休眠20秒
                result = tbDeviceFaceService.downUser(params);
                log.info("下发人脸重试downFace = {} 重试次数 {}", params, times);
                if (result.get("msg") != null) {
                    String msg = result.get("msg").toString();
                    if (msg.contains("不在线")) {
                        times++;
                    } else {
                        //代表成功
                        times = 6;
                    }
                } else {
                    times++;
                }
            } catch (Exception e) {
                times++;
                log.info("下发失败 原因 {}", e.getMessage());
            }
        }
        //成功
        if (times == 6) {
            return true;
        }
        return false;
    }


    //发送失败重试三次
    @Async
    public void delUserRetry(Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        int times = 1;
        while (times <= 3) {
            try {
                log.info("删除人脸重试delUser = {} 重试次数 {}", params, times);
                Thread.sleep(1000 * 20);   // 休眠20秒
                result = tbDeviceFaceService.delUser(params);
                if (result.get("msg") != null) {
                    String msg = result.get("msg").toString();
                    if (msg.contains("不在线")) {
                        times++;
                    } else {
                        //代表成功
                        times = 6;
                    }
                } else {
                    times++;
                }
            } catch (Exception e) {
                times++;
                log.info("下发失败 原因 {}", e.getMessage());
            }
        }
    }
}
