package com.zcloud.zcGbsServicer.command.query;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import com.zcloud.zcGbsServicer.command.convertor.WaterSourceCoConvertor;
import com.zcloud.zcGbsServicer.dto.WaterSourcePageQry;
import com.zcloud.zcGbsServicer.dto.WaterSourceScreenQry;
import com.zcloud.zcGbsServicer.dto.clientobject.WaterSourceCO;
import com.zcloud.zcGbsServicer.dto.clientobject.WaterSourceScreenCO;
import com.zcloud.zcGbsServicer.persistence.dataobject.WaterSourceDO;
import com.zcloud.zcGbsServicer.persistence.repository.WaterSourceRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
* web-app
* @Author makejava
* @Date 2026-01-21 17:23:50
*/
@Component
@AllArgsConstructor
public class WaterSourceQueryExe {
    private final WaterSourceRepository waterSourceRepository;
    private final WaterSourceCoConvertor waterSourceCoConvertor;

    /**
    * 鍒嗛〉
    *
    * @param waterSourcePageQry
    * @return
    */
    public PageResponse<WaterSourceCO> execute(WaterSourcePageQry waterSourcePageQry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(waterSourcePageQry);
        params.put("tenant_id", waterSourcePageQry.getEqCorpId());
        PageResponse<WaterSourceDO> pageResponse = waterSourceRepository.listPage(params);
        List<WaterSourceCO> examCenterCOS = waterSourceCoConvertor.converDOsToCOs(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    /**
     * 璇︽儏
     */
    public WaterSourceCO getInfoById(Long id) {
        WaterSourceDO waterSourceDO = waterSourceRepository.getById(id);
        return waterSourceCoConvertor.converDOToCO(waterSourceDO);
    }

    public PageResponse<WaterSourceScreenCO> listScreen(WaterSourceScreenQry qry) {

        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        IPage<WaterSourceDO> page = new Query<WaterSourceDO>().getPage(params);
        PageResponse<WaterSourceDO> pageResponse = waterSourceRepository.listScreen(page, params);
        List<WaterSourceScreenCO> data = waterSourceCoConvertor.converDOsToScreenCOs(pageResponse.getData());
        return PageResponse.of(data, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }
}
