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.repo.impl.BaseRepositoryImpl;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.primeport.domain.enums.MenuEnum;
import com.zcloud.primeport.persistence.dataobject.MkmjGateDO;
import com.zcloud.primeport.persistence.dataobject.MkmjPassageDO;
import com.zcloud.primeport.persistence.mapper.MkmjGateMapper;
import com.zcloud.primeport.persistence.repository.MkmjGateRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.zcloud.gbscommon.utils.Query;
import com.jjb.saas.framework.repository.common.PageHelper;

@Service
@RequiredArgsConstructor
public class MkmjGateRepositoryImpl extends BaseRepositoryImpl<MkmjGateMapper, MkmjGateDO> implements MkmjGateRepository {
    private final MkmjGateMapper mkmjGateMapper;

    @Override
    public PageResponse<MkmjGateDO> 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<MkmjGateDO> result = mkmjGateMapper.listPage(page, params, menuPerms);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

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

    @Override
    public Integer countByPassageId(String passageId) {
        QueryWrapper<MkmjGateDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("passage_id", passageId);
        queryWrapper.eq("delete_enum", "false");
        return Math.toIntExact(mkmjGateMapper.selectCount(queryWrapper));
    }

    @Override
    public Boolean checkName(String gateName, Long passageId, Long id) {
        QueryWrapper<MkmjGateDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gate_name", gateName);
        queryWrapper.eq("passage_id", passageId);
        queryWrapper.eq("delete_enum", "false");
        if (id != null) {
            queryWrapper.ne("id", id);
        }
        return mkmjGateMapper.selectCount(queryWrapper) == 0;
    }
}

