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.ConstantData;
import org.wcs.factory.AppServeResponseFactory;
import org.wcs.mapper.intf.AppRecordScanService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.recordScan.QueryRecordScanReq;
import org.wcs.model.po.app.AppRecordScan;
import org.wcs.model.vo.serve.AppServeDataResponse;
import org.wcs.model.vo.serve.common.PageDataResponse;
import org.wcs.model.vo.serve.recordScan.ExportAppRecordScanVo;
import org.wcs.model.vo.serve.recordScan.RecordScanVo;
import org.wcs.plugin.excel.ExcelPoi;
import org.wcs.serve.service.intf.IRecordScanService;
import org.wcs.utils.AppBeanUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@RequiredArgsConstructor
public class RecordScanService implements IRecordScanService {

    private final AppRecordScanService recordScanService;
    private final ExcelPoi excelPoi;

    /**
     * 查询记录扫描信息
     * @param request 查询参数
     * @param pageSize 每页数量
     * @param pageIndex 页码
     * @return 查询结果
     */
    @Override
    public AppServeDataResponse<PageDataResponse<RecordScanVo>> queryRecordScan(QueryRecordScanReq request, Integer pageSize, Integer pageIndex) {
        Tuple2<Long, List<AppRecordScan>> queryResult = recordScanService.query(request, pageSize, pageIndex);
        if(queryResult == null) {
            return AppServeResponseFactory.internalServerError("数据服务异常", null);
        }
        PageDataResponse<RecordScanVo> response = new PageDataResponse<>();
        response.setPageIndex(pageIndex);
        response.setPageSize(pageSize);
        response.setTotalCount(queryResult.getItem1());
        response.setData(AppBeanUtils.copyListProperties(queryResult.getItem2(), RecordScanVo::new));
        return AppServeResponseFactory.success("查询成功", response);
    }

    /**
     * 查询记录扫描率
     * @param request 查询参数
     * @return 查询结果
     */
    @Override
    public AppServeDataResponse<BigDecimal> queryRecordScanRatio(QueryRecordScanReq request) {
        List<AppRecordScan> recordScans = recordScanService.query(request);
        if(recordScans == null) {
            return AppServeResponseFactory.internalServerError("数据服务异常", null);
        }
        if(recordScans.isEmpty()) {
            return AppServeResponseFactory.success("无记录扫描信息", BigDecimal.valueOf(100.0));
        }
        int totalNumber = recordScans.size();
        int scanNoReadNumber = recordScans.stream().filter(recordScan -> Objects.equals(recordScan.getCode(), ConstantData.NO_READ)).toArray().length;
        BigDecimal multiplyValue = BigDecimal.valueOf(totalNumber - scanNoReadNumber).divide(BigDecimal.valueOf(totalNumber), 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
        return AppServeResponseFactory.success("查询成功", multiplyValue);
    }

    /**
     * 下载记录扫描信息
     * @param response 响应对象
     * @param request 搜索参数
     */
    @Override
    public void downLoadRecordScan(HttpServletResponse response, QueryRecordScanReq request) {
        List<AppRecordScan> recordScans = recordScanService.query(request);
        if(recordScans == null) {
            return;
        }
        List<ExportAppRecordScanVo> recordScanVos = AppBeanUtils.copyListProperties(recordScans, ExportAppRecordScanVo::new);
        try {
            excelPoi.doWriteExcel("扫码数据下载", response, recordScanVos, ExportAppRecordScanVo.class);
        } catch (Exception e) {
            log.error("下载扫码数据失败", e);
        }
    }
}
