package com.zcloud.primeport.command.query;

import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Tools;
import com.zcloud.primeport.command.convertor.MkmjCoConvertor;
import com.zcloud.primeport.dto.MkmjPageQry;
import com.zcloud.primeport.dto.clientobject.MkmjCO;
import com.zcloud.primeport.persistence.dataobject.MkmjDO;
import com.zcloud.primeport.persistence.repository.MkmjRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

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


/**
 * web-app
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:15
 */
@Component
@AllArgsConstructor
public class MkmjQueryExe {
    private final MkmjRepository mkmjRepository;
    private final MkmjCoConvertor mkmjCoConvertor;

    /**
     * 分页
     *
     * @param mkmjPageQry
     * @return
     */
    public PageResponse<MkmjCO> execute(MkmjPageQry mkmjPageQry) {
        Map<String, Object> parmas = PageQueryHelper.toHashMap(mkmjPageQry);
        PageResponse<MkmjDO> pageResponse = mkmjRepository.listPage(parmas);
        List<MkmjCO> examCenterCOS = mkmjCoConvertor.converDOsToCOs(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public MkmjCO getInfoById(Long id) {
        return mkmjCoConvertor.converDOToCO(mkmjRepository.getById(id));
    }

    public List<MkmjCO> listAll(MkmjPageQry qry) {
        QueryWrapper<MkmjDO> mkmjDOQueryWrapper = new QueryWrapper<>();
        mkmjDOQueryWrapper.eq("mkmj_status", "2");
        if (Tools.notEmpty(qry.getHgAuthArea())) {
            mkmjDOQueryWrapper.eq("hg_auth_area", qry.getHgAuthArea());
        }
        if (qry.getMkmjLevel() != null) {
            mkmjDOQueryWrapper.eq("mkmj_level", qry.getMkmjLevel());
        }
        if (qry.getClosedAreaId() != null) {
            mkmjDOQueryWrapper.eq("closed_area_id", qry.getClosedAreaId());
        }
        if (Tools.notEmpty(qry.getMkmjType())) {
            mkmjDOQueryWrapper.eq("mkmj_type", qry.getMkmjType());
        }
        List<MkmjDO> list = mkmjRepository.list(mkmjDOQueryWrapper);
        List<MkmjCO> examCenterCOS = mkmjCoConvertor.converDOsToCOs(list);
        return examCenterCOS;
    }
}

