package com.zcloud.primeport.persistence.repository.impl;

import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jjb.saas.framework.repository.common.PageHelper;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import com.zcloud.primeport.domain.enums.MenuEnum;
import com.zcloud.primeport.persistence.dataobject.MkmjPassageDO;
import com.zcloud.primeport.persistence.mapper.MkmjPassageMapper;
import com.zcloud.primeport.persistence.repository.MkmjPassageRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@RequiredArgsConstructor
public class MkmjPassageRepositoryImpl extends BaseRepositoryImpl<MkmjPassageMapper, MkmjPassageDO> implements MkmjPassageRepository {
    private final MkmjPassageMapper mkmjPassageMapper;

    @Override
    public PageResponse<MkmjPassageDO> listPage(Map<String, Object> params) {
        Page<Map<String, Object>> page = new Page<>(Integer.parseInt(params.get("pageIndex").toString()), Integer.parseInt(params.get("pageSize").toString()));
        String menuPerms = "";
        if (!org.springframework.util.ObjectUtils.isEmpty(params.get("menuPath"))) {
            menuPerms = MenuEnum.getMenuKeyByPath(params.get("menuPath").toString());
        }
        page.setOptimizeJoinOfCountSql(false);

        IPage<MkmjPassageDO> iPage = mkmjPassageMapper.listPage(page, params, menuPerms);
        return PageHelper.pageToResponse(iPage, iPage.getRecords());
    }

    @Override
    public Integer countByMkmjId(Long mkmjId) {
        QueryWrapper<MkmjPassageDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mkmj_id", mkmjId);
        queryWrapper.eq("delete_enum", "false");
        return Math.toIntExact(mkmjPassageMapper.selectCount(queryWrapper));
    }
}


