package cn.iocoder.yudao.module.system.dal.mysql.saporg;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.saporg.vo.SapOrgPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.saporg.SapOrgDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;

/**
 * SAP中的组织主数据(工厂 成本中心) Mapper
 *
 * @author ruiqi
 */
@Mapper
public interface SapOrgMapper extends BaseMapperX<SapOrgDO> {

    default PageResult<SapOrgDO> selectPage(SapOrgPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SapOrgDO>()
                .eqIfPresent(SapOrgDO::getFactoryCode, reqVO.getFactoryCode())
                .likeIfPresent(SapOrgDO::getFactoryName, reqVO.getFactoryName())
                .eqIfPresent(SapOrgDO::getCostCenterCode, reqVO.getCostCenterCode())
                .likeIfPresent(SapOrgDO::getCostCenterName, reqVO.getCostCenterName())
                .eqIfPresent(SapOrgDO::getStorageLocationCode, reqVO.getStorageLocationCode())
                .likeIfPresent(SapOrgDO::getStorageLocationName, reqVO.getStorageLocationName())
                .eqIfPresent(SapOrgDO::getType, reqVO.getType())
                .eqIfPresent(SapOrgDO::getResponsiblePerson, reqVO.getResponsiblePerson())
                .eqIfPresent(SapOrgDO::getSort, reqVO.getSort())
                .eqIfPresent(SapOrgDO::getStatus, reqVO.getStatus())
                .eqIfPresent(SapOrgDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(SapOrgDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(SapOrgDO::getId));
    }

    default List<SapOrgDO> selectList(Collection<Long> ids, Integer type, Collection<Integer> statuses) {
        return selectList(new LambdaQueryWrapperX<SapOrgDO>()
                .inIfPresent(SapOrgDO::getId, ids)
                .eqIfPresent(SapOrgDO::getType, type)
                .inIfPresent(SapOrgDO::getStatus, statuses)
                .orderByAsc(SapOrgDO::getSort));
    }

    /**
     * 根据已经选定的 SAP工厂 筛选 成本中心列表 or 库存地点列表
     * @param type
     * @param factoryCodes 工厂 code 列表
     * @return
     */
    default List<SapOrgDO> selectedList(Integer type, List<String> factoryCodes) {
        return selectList(new LambdaQueryWrapperX<SapOrgDO>()
                .eqIfPresent(SapOrgDO::getType, type)
                .inIfPresent(SapOrgDO::getFactoryCode, factoryCodes));
    }

}