package com.zhongshu.card.server.core.service.gateDoor;

import cn.hutool.json.JSONUtil;
import com.github.microservice.auth.client.service.UserFaceService;
import com.github.microservice.models.gateDoor.OnLineAckInfoModel;
import com.github.microservice.models.gateDoor.OnLineInfoParam;
import com.github.microservice.models.gateDoor.OnLineModel;
import com.github.microservice.models.gateDoor.OnLineParam;
import com.github.microservice.models.gateDoor.gateDoor.GateDoorIotParam;
import com.github.microservice.models.gateDoor.use.*;
import com.github.microservice.net.ResultContent;
import com.github.microservice.types.deviceUse.OperateState;
import com.github.microservice.types.deviceUse.OperateType;
import com.zhongshu.card.client.type.payment.RequestType;
import com.zhongshu.card.server.core.domain.devices.DeviceUseRecords;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.service.devices.DeviceUseRecordService;
import com.zhongshu.card.server.core.service.devices.permiss.DevicePermissVerifyService;
import com.zhongshu.card.server.core.service.payment.RequestInfoService;
import com.zhongshu.card.server.core.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author TRX
 * @date 2024/9/12
 */
@Slf4j
@Service
public class GateDoorService extends SuperService {

    @Autowired
    RequestInfoService requestInfoService;

    @Autowired
    private DeviceUseRecordService deviceUseRecordService;

    @Autowired
    private DevicePermissVerifyService devicePermissVerifyService;

    @Autowired
    private UserFaceService userFaceService;

    /**
     * 闸机方法 onLine
     *
     * @return
     */
    public ResultContent onLine(OnLineParam param) {
        OnLineModel resultModel = new OnLineModel();
        resultModel.setMessageId(param.getMqttDataId());

        OnLineInfoParam paramFaceInfo = param.getInfo();
        String facesluiceId = "";
        if (ObjectUtils.isNotEmpty(paramFaceInfo)) {
            facesluiceId = paramFaceInfo.getFacesluiceId();
        }
        OnLineAckInfoModel info = new OnLineAckInfoModel();
        info.setFacesluiceId(facesluiceId);
        resultModel.setInfo(info);
        resultModel.setSuccess();

        requestInfoService.addRequestInfo(param, resultModel, RequestType.GateDoor);
        return ResultContent.buildSuccess(JSONUtil.toJsonStr(resultModel));
    }

    public ResultContent offLine(OnLineParam param) {
        OnLineModel resultModel = new OnLineModel();
        resultModel.setMessageId(param.getMqttDataId());

        OnLineAckInfoModel info = new OnLineAckInfoModel();
        info.setFacesluiceId(CommonUtil.UUID());
        resultModel.setInfo(info);
        resultModel.setSuccess();

        requestInfoService.addRequestInfo(param, resultModel, RequestType.GateDoor);
        return ResultContent.buildSuccess(JSONUtil.toJsonStr(resultModel));
    }

    /**
     * 门闸的开门使用的 参数
     *
     * @param param
     * @return
     */
    public ResultContent gateDoorUseRecord(GateDoorIotParam param) {
        GateDoorUseResult result = new GateDoorUseResult();
        GateDoorInfoParam info = param.getInfo();
        log.info("闸机使用记录 gateDoorUseRecord...");
        if (ObjectUtils.isNotEmpty(info)) {
            // 封装记录日志参数
            GateDoorUseParam useParam = new GateDoorUseParam();
            useParam.setMqttDataId(param.getMqttDataId());
            useParam.setDeviceId(param.getDeviceId());
            useParam.setGateWayId(param.getGateWayId());

            String VerifyStatus = info.getVerifyStatus();
            if (StringUtils.isNotEmpty(VerifyStatus)) {
                if ("1".equals(VerifyStatus)) {
                    // 成功
                    useParam.setOperateState(OperateState.Success);
                } else if ("2".equals(VerifyStatus)) {
                    // 失败
                    useParam.setOperateState(OperateState.Failed);
                } else {
                    // 未知
                    useParam.setOperateState(OperateState.Unknown);
                }
            } else {
                // 未知
                useParam.setOperateState(OperateState.Failed);
            }
            // 使用操作方式
            if (StringUtils.isNotEmpty(info.getPic())) {
                // 人脸形式
                useParam.setOperateType(OperateType.Face);
            } else {
                String cardNum2 = info.getCardNum2();
                if (StringUtils.isNotEmpty(cardNum2)) {
                    useParam.setOperateType(OperateType.Card);
                    useParam.setCardNo(cardNum2);
                }
            }
            if (useParam.getOperateType() == null) {
                useParam.setOperateType(OperateType.Unknown);
            }
            GateDoorFailCardInfo cardInfo = info.getCardInfo();
            if (StringUtils.isEmpty(useParam.getCardNo()) && ObjectUtils.isNotEmpty(cardInfo)) {
                useParam.setCardNo(cardInfo.getCardNum());
                useParam.setOperateType(OperateType.Card);
            }
            useParam.setTime(info.getTime());
            useParam.setUserId(info.getCustomId());
            useParam.setIsOffLine(param.getIsOffLine());
            useParam.setData(param);
            useParam.setOperator(param.getOperator());
            useParam.setPic(info.getPic());
            info.setPic("");
            // 保存使用记录
            ResultContent<DeviceUseRecords> resultContent = deviceUseRecordService.saveDeviceLogs(useParam);
            if (resultContent.isSuccess()) {
                DeviceUseRecords records = resultContent.getContent();
                if (records.getOperateState() == OperateState.Success) {
                    result.setSuccess("成功");
                } else {
                    result.setFailed(records.getMsg());
                }
            } else {
                result.setFailed(resultContent.getMsg());
            }
        } else {
            result.setFailed("数据为空");
        }
        return ResultContent.buildSuccess(JSONUtil.toJsonStr(result));
    }

    /**
     * 门闸人脸在线验证
     *
     * @param param
     * @return
     */
    public ResultContent gateDoorOnLineVerify(GateDoorIotParam param) {
        GateDoorOnLineUseResult result = new GateDoorOnLineUseResult();
        GateDoorInfoParam info = param.getInfo();
        if (ObjectUtils.isEmpty(info)) {
            info = new GateDoorInfoParam();
        }

        log.info("闸机使用记录 gateDoorOnLineVerify...");
        if (ObjectUtils.isNotEmpty(info)) {
            String deviceId = param.getDeviceId();
            String gateWayId = param.getGateWayId();
            String mqttDataId = param.getMqttDataId();
            result.setMqttDataId(mqttDataId);
            String userId = info.getCustomId();

            GateDoorUseParam deviceUserParam = new GateDoorUseParam();
            ResultContent<String> resultContent = devicePermissVerifyService.verifyDevice(userId, deviceId, deviceUserParam);
            deviceUserParam.setMqttDataId(mqttDataId);
            deviceUserParam.setOperateType(OperateType.Face);
            deviceUserParam.setGateWayId(gateWayId);
            deviceUserParam.setData(info);
            deviceUserParam.setIsOffLine(Boolean.FALSE);
            deviceUserParam.setPic(info.getPic());
            if (resultContent.isSuccess()) {
                result.setSuccess();
                result.setMsg(resultContent.getContent());
                deviceUserParam.setOperateState(OperateState.Success);
                deviceUserParam.setIsPassed(Boolean.TRUE);
                deviceUserParam.setVerifyMsg(resultContent.getContent());
            } else {
                result.setFailed(resultContent.getMsg());
                deviceUserParam.setOperateState(OperateState.Failed);
                deviceUserParam.setIsPassed(Boolean.FALSE);
                deviceUserParam.setVerifyMsg(resultContent.getMsg());
            }
            // 添加设备使用记录
            deviceUseRecordService.asyncSaveRecord(deviceUserParam);
        } else {
            result.setFailed("参数错误");
        }
        return ResultContent.buildSuccess(JSONUtil.toJsonStr(result));
    }
}
