package com.zt.plat.module.qms.business.config.dal.mapper;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import com.zt.plat.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.zt.plat.framework.security.core.util.SecurityFrameworkUtils;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleDispatchDetailDO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigWarehouseLocationPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigWarehouseLocationRespVO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigPermissionDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigWarehouseLocationDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigWarehouseLocationParDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 存放位置 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface ConfigWarehouseLocationMapper extends BaseMapperX<ConfigWarehouseLocationDO> {

    default PageResult<ConfigWarehouseLocationRespVO> selectPage(ConfigWarehouseLocationPageReqVO reqVO) {

        MPJLambdaWrapperX<ConfigWarehouseLocationDO> wrapper = new MPJLambdaWrapperX<>();
        Boolean adminFlag = reqVO.getAdminFlag();
        List<Long> authorizedWarehouseIds = reqVO.getAuthorizedWarehouseIds();
        // 非管理员并且没有权限的仓库
        if ((adminFlag == null || !adminFlag) && CollUtil.isEmpty(authorizedWarehouseIds))
            return new PageResult<>();
        //仓库
        wrapper.leftJoin(ConfigWarehouseLocationParDO.class, ConfigWarehouseLocationParDO::getId, ConfigWarehouseLocationDO::getParentId);

        wrapper.selectAll(ConfigWarehouseLocationDO.class)
                .selectAs(ConfigWarehouseLocationParDO::getName, ConfigWarehouseLocationRespVO::getWarehouseName)
                .selectAs(ConfigWarehouseLocationParDO::getCode, ConfigWarehouseLocationRespVO::getWarehouseCode)
                .eqIfPresent(ConfigWarehouseLocationDO::getWarehouseType, reqVO.getWarehouseType())
                .eqIfPresent(ConfigWarehouseLocationDO::getId, reqVO.getId())
                .eqIfPresent(ConfigWarehouseLocationDO::getParentId, reqVO.getParentId())
                .likeIfPresent(ConfigWarehouseLocationDO::getName, reqVO.getName())
                .eqIfPresent(ConfigWarehouseLocationDO::getCode, reqVO.getCode())
                .eqIfPresent(ConfigWarehouseLocationDO::getSortNo, reqVO.getSortNo())
                .eqIfPresent(ConfigWarehouseLocationDO::getCapacity, reqVO.getCapacity())
                .eqIfPresent(ConfigWarehouseLocationDO::getMainDepartmentId, reqVO.getMainDepartmentId())
                .likeIfPresent(ConfigWarehouseLocationDO::getMainDepartmentName, reqVO.getMainDepartmentName())
                .eqIfPresent(ConfigWarehouseLocationDO::getDataType, reqVO.getDataType())
                .eqIfPresent(ConfigWarehouseLocationDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .betweenIfPresent(ConfigWarehouseLocationDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(ConfigWarehouseLocationDO::getRemark, reqVO.getRemark())
                .and(adminFlag == null || !adminFlag && CollUtil.isNotEmpty(authorizedWarehouseIds),
                        wrapper1 ->
                                wrapper1.in(ConfigWarehouseLocationDO::getId, authorizedWarehouseIds)
                                        .or()
                                        .in(ConfigWarehouseLocationParDO::getId, authorizedWarehouseIds))
                .orderByDesc(ConfigWarehouseLocationDO::getSortNo);

        return selectJoinPage(reqVO, ConfigWarehouseLocationRespVO.class, wrapper);
    }

    List<ConfigWarehouseLocationDO> selectListWithPermission(@Param("param") ConfigWarehouseLocationPageReqVO param);
}