package cn.iocoder.yudao.module.pms.dal.mysql.iotsapstock;

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.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.pms.controller.admin.iotsapstock.vo.IotSapStockPageReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotsapstock.IotSapStockDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * PMS SAP 库存(通用库存/项目部库存) Mapper
 *
 * @author ruiqi
 */
@Mapper
public interface IotSapStockMapper extends BaseMapperX<IotSapStockDO> {

    default PageResult<IotSapStockDO> selectPage(IotSapStockPageReqVO reqVO) {
        MPJLambdaWrapperX<IotSapStockDO> query = new MPJLambdaWrapperX<IotSapStockDO>();
        query.eqIfPresent(IotSapStockDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(IotSapStockDO::getFactoryId, reqVO.getFactoryId())
                .eqIfPresent(IotSapStockDO::getFactory, reqVO.getFactory())
                .eqIfPresent(IotSapStockDO::getStorageLocationId, reqVO.getStorageLocationId())
                .eqIfPresent(IotSapStockDO::getProjectDepartment, reqVO.getProjectDepartment())
                .likeIfPresent(IotSapStockDO::getMaterialCode, reqVO.getMaterialCode())
                .likeIfPresent(IotSapStockDO::getMaterialName, reqVO.getMaterialName())
                .likeIfPresent(IotSapStockDO::getMaterialGroupName, reqVO.getMaterialGroupName())
                .eqIfPresent(IotSapStockDO::getMaterialGroupId, reqVO.getMaterialGroupId())
                .eqIfPresent(IotSapStockDO::getQuantity, reqVO.getQuantity())
                .eqIfPresent(IotSapStockDO::getUnitPrice, reqVO.getUnitPrice())
                .eqIfPresent(IotSapStockDO::getUnit, reqVO.getUnit())
                .eqIfPresent(IotSapStockDO::getSafetyStock, reqVO.getSafetyStock())
                .eqIfPresent(IotSapStockDO::getStorageAreaId, reqVO.getStorageAreaId())
                .eqIfPresent(IotSapStockDO::getShelvesId, reqVO.getShelvesId())
                .eqIfPresent(IotSapStockDO::getCargoLocationId, reqVO.getCargoLocationId())
                .eqIfPresent(IotSapStockDO::getType, reqVO.getType())
                .eqIfPresent(IotSapStockDO::getSyncStatus, reqVO.getSyncStatus())
                .betweenIfPresent(IotSapStockDO::getSyncTime, reqVO.getSyncTime())
                .eqIfPresent(IotSapStockDO::getSyncError, reqVO.getSyncError())
                .eqIfPresent(IotSapStockDO::getSort, reqVO.getSort())
                .eqIfPresent(IotSapStockDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotSapStockDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(IotSapStockDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(IotSapStockDO::getFactoryId).orderByAsc(IotSapStockDO::getStorageLocationId);
        if ("Y".equals(reqVO.getConfigFlag())) {
            query.gt(IotSapStockDO::getSafetyStock, 0);
        } else if ("N".equals(reqVO.getConfigFlag())) {
            query.eq(IotSapStockDO::getSafetyStock, 0);
        }
        return selectJoinPage(reqVO, IotSapStockDO.class, query);
    }

    default List<IotSapStockDO> selectList(IotSapStockPageReqVO reqVO) {
        MPJLambdaWrapperX<IotSapStockDO> query = new MPJLambdaWrapperX<IotSapStockDO>();
        query.eqIfPresent(IotSapStockDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(IotSapStockDO::getFactoryCode, reqVO.getFactoryCode())
                .eqIfPresent(IotSapStockDO::getFactoryId, reqVO.getFactoryId())
                .inIfPresent(IotSapStockDO::getFactoryId, reqVO.getFactoryIds())
                .eqIfPresent(IotSapStockDO::getFactory, reqVO.getFactory())
                .eqIfPresent(IotSapStockDO::getStorageLocationId, reqVO.getStorageLocationId())
                .inIfPresent(IotSapStockDO::getStorageLocationId, reqVO.getStorageLocationIds())
                .eqIfPresent(IotSapStockDO::getProjectDepartment, reqVO.getProjectDepartment())
                .likeIfPresent(IotSapStockDO::getMaterialCode, reqVO.getMaterialCode())
                .likeIfPresent(IotSapStockDO::getMaterialName, reqVO.getMaterialName())
                .likeIfPresent(IotSapStockDO::getMaterialGroupName, reqVO.getMaterialGroupName())
                .eqIfPresent(IotSapStockDO::getMaterialGroupId, reqVO.getMaterialGroupId())
                .eqIfPresent(IotSapStockDO::getQuantity, reqVO.getQuantity())
                .eqIfPresent(IotSapStockDO::getUnitPrice, reqVO.getUnitPrice())
                .eqIfPresent(IotSapStockDO::getUnit, reqVO.getUnit())
                .eqIfPresent(IotSapStockDO::getSafetyStock, reqVO.getSafetyStock())
                .eqIfPresent(IotSapStockDO::getStorageAreaId, reqVO.getStorageAreaId())
                .eqIfPresent(IotSapStockDO::getShelvesId, reqVO.getShelvesId())
                .eqIfPresent(IotSapStockDO::getCargoLocationId, reqVO.getCargoLocationId())
                .eqIfPresent(IotSapStockDO::getType, reqVO.getType())
                .eqIfPresent(IotSapStockDO::getSyncStatus, reqVO.getSyncStatus())
                .betweenIfPresent(IotSapStockDO::getSyncTime, reqVO.getSyncTime())
                .eqIfPresent(IotSapStockDO::getSyncError, reqVO.getSyncError())
                .eqIfPresent(IotSapStockDO::getSort, reqVO.getSort())
                .eqIfPresent(IotSapStockDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotSapStockDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(IotSapStockDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(IotSapStockDO::getFactoryId).orderByAsc(IotSapStockDO::getStorageLocationId);
        if ("Y".equals(reqVO.getConfigFlag())) {
            query.gt(IotSapStockDO::getSafetyStock, 0);
        } else if ("N".equals(reqVO.getConfigFlag())) {
            query.eq(IotSapStockDO::getSafetyStock, 0);
        }
        return selectJoinList(IotSapStockDO.class, query);
    }

    default List<IotSapStockDO> getSafeList() {
        return selectList(new LambdaUpdateWrapper<IotSapStockDO>()
                .gt(IotSapStockDO::getSafetyStock, 0));
    }

    @Select("SELECT * FROM rq_iot_sap_stock WHERE deleted = 0 AND tenant_id = 1")
    @TenantIgnore
    List<IotSapStockDO> allSapStocks();

}
