package org.wcs.serve.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.wcs.factory.AppServeResponseFactory;
import org.wcs.mapper.intf.AppRecordApiRequestService;
import org.wcs.mapper.intf.AppRecordApiResponseService;
import org.wcs.model.dto.serve.recordApi.QueryRecordApiRequestReq;
import org.wcs.model.dto.serve.recordApi.QueryRecordApiResponseReq;
import org.wcs.model.po.app.AppRecordApiRequest;
import org.wcs.model.po.app.AppRecordApiResponse;
import org.wcs.model.vo.serve.AppServeDataResponse;
import org.wcs.model.vo.serve.common.PageDataResponse;
import org.wcs.model.vo.serve.recordApi.RecordApiRequestVo;
import org.wcs.model.vo.serve.recordApi.RecordApiResponseVo;
import org.wcs.serve.service.intf.IRecordApiService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.utils.AppBeanUtils;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class RecordApiService implements IRecordApiService {

    private final AppRecordApiRequestService recordApiRequestService;
    private final AppRecordApiResponseService recordApiResponseService;

    /**
     * 查询记录API请求列表
     * @param request 查询参数
     * @param pageSize 每页数量
     * @param pageIndex 页码
     * @return AppServeDataResponse<PageDataResponse<RecordApiRequestVo>>
     */
    @Override
    public AppServeDataResponse<PageDataResponse<RecordApiRequestVo>> queryRecordApiRequest(QueryRecordApiRequestReq request, Integer pageSize, Integer pageIndex) {
        if(request == null || pageSize == null || pageIndex == null || pageSize <= 0 || pageIndex <= 0) {
            return AppServeResponseFactory.fail("参数错误", null);
        }
        Tuple2<Long, List<AppRecordApiRequest>> pageResult = recordApiRequestService.queryByPage(request, pageSize, pageIndex);
        if(pageResult == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        PageDataResponse<RecordApiRequestVo> response = new PageDataResponse<>();
        response.setPageIndex(pageIndex);
        response.setPageSize(pageSize);
        response.setTotalCount(pageResult.getItem1());
        response.setData(AppBeanUtils.copyListProperties(pageResult.getItem2(), RecordApiRequestVo::new));
        return AppServeResponseFactory.success("查询成功", response);
    }

    /**
     * 获取记录API响应列表
     * @param request 查询参数
     * @param pageSize 每页数量
     * @param pageIndex 页码
     * @return AppServeDataResponse<PageDataResponse<RecordApiResponseVo>>
     */
    @Override
    public AppServeDataResponse<PageDataResponse<RecordApiResponseVo>> queryRecordApiResponse(QueryRecordApiResponseReq request, Integer pageSize, Integer pageIndex) {
        if(request == null || pageSize == null || pageIndex == null || pageSize <= 0 || pageIndex <= 0) {
            return AppServeResponseFactory.fail("参数错误", null);
        }
        Tuple2<Long, List<AppRecordApiResponse>> pageResult = recordApiResponseService.queryByPage(request, pageSize, pageIndex);
        if(pageResult == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        PageDataResponse<RecordApiResponseVo> response = new PageDataResponse<>();
        response.setPageIndex(pageIndex);
        response.setPageSize(pageSize);
        response.setTotalCount(pageResult.getItem1());
        response.setData(AppBeanUtils.copyListProperties(pageResult.getItem2(), RecordApiResponseVo::new));
        return AppServeResponseFactory.success("查询成功", response);
    }


}
