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.MkmjDO;
import com.zcloud.primeport.persistence.mapper.MkmjMapper;
import com.zcloud.primeport.persistence.repository.MkmjRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@RequiredArgsConstructor
public class MkmjRepositoryImpl extends BaseRepositoryImpl<MkmjMapper, MkmjDO> implements MkmjRepository {
    private final MkmjMapper mkmjMapper;

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

    @Override
    public Boolean checkName(String mkmjName, Long excludeId) {
        QueryWrapper<MkmjDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mkmj_name", mkmjName);
        queryWrapper.eq("delete_enum", "false");
        if (excludeId != null) {
            queryWrapper.ne("id", excludeId);
        }
        return mkmjMapper.selectCount(queryWrapper) == 0;
    }
}


