package org.wcs.serve.service.impl;

import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.wcs.constant.enums.database.BaseErroorEquipmentTypeEnum;
import org.wcs.constant.enums.database.BaseErrorLevelEnum;
import org.wcs.constant.enums.database.BaseErrorTypeEnum;
import org.wcs.constant.enums.database.EquipmentTypeEnum;
import org.wcs.factory.AppServeResponseFactory;
import org.wcs.mapper.intf.AppRecordErrService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.recordError.QueryEquipmentErrorReq;
import org.wcs.model.po.app.JoinRecordError;
import org.wcs.model.vo.serve.AppServeDataResponse;
import org.wcs.model.vo.serve.common.PageDataResponse;
import org.wcs.model.vo.serve.recordError.ExportEquipmentErrorVo;
import org.wcs.model.vo.serve.recordError.QueryEquipmentErrorVo;
import org.wcs.plugin.excel.ExcelPoi;
import org.wcs.serve.service.intf.IRecordErrorService;
import org.wcs.utils.AppBeanUtils;
import org.wcs.utils.AppStringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class RecordErrorService implements IRecordErrorService {

    private final AppRecordErrService recordErrorService;
    private final ExcelPoi excelPoi;

    /**
     * 查询设备错误
     * @param request 查询参数
     * @param pageSize 分页大小
     * @param pageIndex 页面序号
     * @return 查询结果
     */
    @Override
    public AppServeDataResponse<PageDataResponse<QueryEquipmentErrorVo>> queryEquipmentError(QueryEquipmentErrorReq request, Integer pageSize, Integer pageIndex) {
        if(request == null || pageSize == null || pageIndex == null || pageSize <= 0 || pageIndex <= 0) {
            return AppServeResponseFactory.fail("参数错误", null);
        }
        Tuple2<Long, List<JoinRecordError>> queryRecordErrorDetail = recordErrorService.queryRecordErrorDetail(request, pageSize, pageIndex);
        if(queryRecordErrorDetail == null) {
            return AppServeResponseFactory.fail("查询失败，后台异常", null);
        }
        PageDataResponse<QueryEquipmentErrorVo> pageDataResponse = new PageDataResponse<>();
        pageDataResponse.setPageIndex(pageIndex);
        pageDataResponse.setPageSize(pageSize);
        pageDataResponse.setTotalCount(queryRecordErrorDetail.item1);
        pageDataResponse.setData(AppBeanUtils.copyListProperties(queryRecordErrorDetail.item2, QueryEquipmentErrorVo::new));
        return AppServeResponseFactory.success("查询成功", pageDataResponse);
    }

    /**
     * 导出设备错误
     * @param response 响应
     * @param request 错误查询参数
     */
    @Override
    public void exportEquipmentError(HttpServletResponse response, QueryEquipmentErrorReq request) {
        List<JoinRecordError> queryRecordErrorList = recordErrorService.queryRecordErrorDetail(request);
        if(queryRecordErrorList == null) {
            log.error("查询设备错误失败，参数：{}", AppStringUtils.objectToString(request));
            return;
        }
        List<ExportEquipmentErrorVo> exportEquipmentErrorVoList = new ArrayList<>();
        for (JoinRecordError recordError : queryRecordErrorList) {
            ExportEquipmentErrorVo exportEquipmentErrorVo = new ExportEquipmentErrorVo();
            exportEquipmentErrorVo.setId(recordError.getId());
            exportEquipmentErrorVo.setEquipmentType(BaseErroorEquipmentTypeEnum.getName(recordError.getEquipmentType()));
            exportEquipmentErrorVo.setEquipmentName(recordError.getEquipmentName());
            exportEquipmentErrorVo.setEquipmentId(recordError.getEquipmentId());
            exportEquipmentErrorVo.setErrCode(recordError.getErrCode());
            exportEquipmentErrorVo.setErrLevel(BaseErrorLevelEnum.getName(recordError.getErrLevel()));
            exportEquipmentErrorVo.setErrType(BaseErrorTypeEnum.getName(recordError.getErrType()));
            exportEquipmentErrorVo.setErrMsg(recordError.getErrMsg());
            exportEquipmentErrorVo.setCreateTime(recordError.getCreateTime());
            exportEquipmentErrorVo.setRecoverTime(recordError.getRecoverTime());
            exportEquipmentErrorVoList.add(exportEquipmentErrorVo);
        }
        try {
            excelPoi.doWriteExcel("设备错误.xlsx", response, exportEquipmentErrorVoList, ExportEquipmentErrorVo.class);
        } catch (IOException e) {
            log.error("导出设备错误失败", e);
        }
    }
}
