package org.wcs.business.data.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.wcs.business.data.intf.IDataRecorder;
import org.wcs.constant.enums.database.EquipmentTypeEnum;
import org.wcs.mapper.intf.AppRecordApiRequestService;
import org.wcs.mapper.intf.AppRecordErrService;
import org.wcs.mapper.intf.AppRecordTaskMsgService;
import org.wcs.model.po.app.AppRecordApiRequest;
import org.wcs.model.po.app.AppRecordErr;
import org.wcs.model.po.app.AppRecordTaskMag;
import org.wcs.plugin.webHttpClient.model.HttpResponse;
import org.wcs.utils.AppStringUtils;
import org.wcs.utils.AppUUIDUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 数据记录器
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DataRecorder implements IDataRecorder {

    private final AppRecordApiRequestService recordApiRequestDao; // 请求外部系统的数据表操作
    private final AppRecordTaskMsgService recordStockMsgDao;
    private final AppRecordErrService recordErrService;

    /**
     * 记录接口请求外部数据
     * @param response API响应数据
     */
    @Override
    public void recordApiRequestData(HttpResponse response) {
        AppRecordApiRequest recordApiRequest = new AppRecordApiRequest();
        recordApiRequest.setRecordId(AppUUIDUtils.getNewUUID());
        recordApiRequest.setRequestUrl(AppStringUtils.isEmptyOr(response.getUrl(), "-"));
        recordApiRequest.setSuccess(response.isSuccess() ? 1 : 0);
        recordApiRequest.setMethod(AppStringUtils.isEmptyOr(response.getMethod(), "-"));
        recordApiRequest.setRequestMsg(AppStringUtils.isEmptyOr(response.getRequestText(), "-"));
        recordApiRequest.setResponseMsg(AppStringUtils.isEmptyOr(response.getResponseText(), "-"));
        recordApiRequest.setRequestTime(response.getRequestTime());
        recordApiRequest.setResponseTime(response.getResponseTime());
        recordApiRequest.setUseTime(BigDecimal.valueOf(response.getUseTime()));
        recordApiRequest.setErrMsg((response.getErrText() == null ? "" : response.getErrText()) + (response.getException() == null ? "" : response.getException().getMessage()));
        int insert = recordApiRequestDao.insert(recordApiRequest);
        if(insert <= 0) {
            log.error("记录接口请求外部数据失败：{}", AppStringUtils.objectToString(response));
        }
    }

    /**
     * 记录仓库任务数据
     * @param taskId 任务ID
     * @param vehicleNo 载具编号
     * @param msg 仓库信息
     */
    @Override
    public void recordStockMsg(String taskId, String vehicleNo, String msg) {
        AppRecordTaskMag recordTaskMsg = new AppRecordTaskMag();
        recordTaskMsg.setRecordId(AppUUIDUtils.getNewUUID());
        recordTaskMsg.setTaskId(AppStringUtils.isEmptyOr(taskId, "-"));
        recordTaskMsg.setVehicleNo(AppStringUtils.isEmptyOr(vehicleNo, ""));
        recordTaskMsg.setMessage(msg);
        recordTaskMsg.setCreateTime(LocalDateTime.now());
        boolean insert = recordStockMsgDao.insert(recordTaskMsg);
        if(!insert) {
            log.error("插入仓库任务信息异常，数据：{}", AppStringUtils.objectToString(recordTaskMsg));
        }
    }

    /**
     * 记录设备错误信息
     * @param equipmentName 设备名称
     * @param equipmentId 设备ID
     * @param errorCode 错误码
     */
    @Override
    public void recordStackerErr(String equipmentName, String equipmentId, Integer errorCode) {
        AppRecordErr recordErr = new AppRecordErr();
        recordErr.setId(AppUUIDUtils.getNewUUID());
        recordErr.setEquipmentName(AppStringUtils.isEmptyOr(equipmentName, "-"));
        recordErr.setEquipmentId(AppStringUtils.isEmptyOr(equipmentId, "-"));
        recordErr.setEquipmentType(EquipmentTypeEnum.STACKER.getCode());
        recordErr.setErrCode(AppStringUtils.isEmptyOr(String.valueOf(errorCode), "-"));
        recordErrService.insert(recordErr);
    }
}
