package com.yc.ship.module.miniapplet.dal.mysql.catalogue;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.mybatis.core.mapper.BaseMapperX;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.module.miniapplet.controller.admin.catalogue.vo.CataloguePageReqVO;
import com.yc.ship.module.miniapplet.dal.dataobject.catalogue.CatalogueDO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 目录类型 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface CatalogueMapper extends BaseMapperX<CatalogueDO> {

    default PageResult<CatalogueDO> selectPage(CataloguePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CatalogueDO>()
                .likeIfPresent(CatalogueDO::getName, reqVO.getName())
                .eqIfPresent(CatalogueDO::getCode, reqVO.getCode())
                .eqIfPresent(CatalogueDO::getParentId, reqVO.getParentId())
                .eqIfPresent(CatalogueDO::getLogo, reqVO.getLogo())
                .eqIfPresent(CatalogueDO::getIsUse, reqVO.getIsUse())
                .eqIfPresent(CatalogueDO::getIsComment, reqVO.getIsComment())
                .eqIfPresent(CatalogueDO::getIsRelease, reqVO.getIsRelease())
                .eqIfPresent(CatalogueDO::getIsAudit, reqVO.getIsAudit())
                .eqIfPresent(CatalogueDO::getRemark, reqVO.getRemark())
                .eqIfPresent(CatalogueDO::getSortNum, reqVO.getSortNum())
                .betweenIfPresent(CatalogueDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CatalogueDO::getId));
    }

    default List<CatalogueDO> getCatalogueByParentId(Long parentId) {
        LambdaQueryWrapperX<CatalogueDO> wrapper = new LambdaQueryWrapperX<>();
        wrapper.eqIfPresent(CatalogueDO::getParentId, parentId);

        wrapper.eqIfPresent(CatalogueDO::getIsUse, 1);
        wrapper.orderByDesc(CatalogueDO::getId);
        return selectList(wrapper);
    }

    default List<CatalogueDO> getCatalogueList(Long id, Long prentId, String code,String name) {
        LambdaQueryWrapperX<CatalogueDO> wrapper = new LambdaQueryWrapperX<>();
        if (id != null) {
            wrapper.eqIfPresent(CatalogueDO::getId, id);
        }
        if (StringUtils.isNotBlank(code)) {
            wrapper.eqIfPresent(CatalogueDO::getCode, code);
        }
        if (StringUtils.isNotBlank(name)) {
            wrapper.likeIfPresent(CatalogueDO::getName, name);
        }
        if (prentId != null) {
            wrapper.eqIfPresent(CatalogueDO::getParentId, prentId);
        } else {
            wrapper.eqIfPresent(CatalogueDO::getParentId, 0);
        }
        wrapper.eqIfPresent(CatalogueDO::getIsUse, 1);
        wrapper.orderByAsc(CatalogueDO::getSortNum);
        return selectList(wrapper);
    }

    default CatalogueDO selectByCode(String code) {
        return selectOne(new LambdaQueryWrapperX<CatalogueDO>()
                .eq(CatalogueDO::getCode, code)
                .last("limit 1"));
    }
}
