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

import cn.hutool.core.util.ObjectUtil;
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.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterial.vo.IotMaterialPageReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotcommonbommaterial.IotCommonBomMaterialDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicematerial.IotDeviceMaterialDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmaterial.IotMaterialDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;

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

/**
 * PMS 功能优化 物料 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface IotMaterialMapper extends BaseMapperX<IotMaterialDO> {

    default PageResult<IotMaterialDO> selectPage(IotMaterialPageReqVO reqVO, Collection<Long> ids) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotMaterialDO>()
                //.eqIfPresent(IotMaterialDO::getMaterialGroupId, reqVO.getMaterialGroupId())
                .inIfPresent(IotMaterialDO::getMaterialGroupId, ids)
                .eqIfPresent(IotMaterialDO::getBomId, reqVO.getBomId())
                .eqIfPresent(IotMaterialDO::getType, reqVO.getType())
                .likeIfPresent(IotMaterialDO::getCode, reqVO.getCode())
                .likeIfPresent(IotMaterialDO::getName, reqVO.getName())
                .eqIfPresent(IotMaterialDO::getModel, reqVO.getModel())
                .eqIfPresent(IotMaterialDO::getUnit, reqVO.getUnit())
                .eqIfPresent(IotMaterialDO::getSapCode, reqVO.getSapCode())
                .eqIfPresent(IotMaterialDO::getSyncStatus, reqVO.getSyncStatus())
                .betweenIfPresent(IotMaterialDO::getSyncTime, reqVO.getSyncTime())
                .eqIfPresent(IotMaterialDO::getSyncError, reqVO.getSyncError())
                .eqIfPresent(IotMaterialDO::getSort, reqVO.getSort())
                .eqIfPresent(IotMaterialDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotMaterialDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(IotMaterialDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(IotMaterialDO::getId));
    }

    default List<IotMaterialDO> selectList(IotMaterialPageReqVO reqVO, Collection<Long> ids) {
        return selectList(new LambdaQueryWrapperX<IotMaterialDO>()
                .inIfPresent(IotMaterialDO::getMaterialGroupId, ids)
                .eqIfPresent(IotMaterialDO::getBomId, reqVO.getBomId())
                .eqIfPresent(IotMaterialDO::getType, reqVO.getType())
                .likeIfPresent(IotMaterialDO::getCode, reqVO.getCode())
                .inIfPresent(IotMaterialDO::getCode, reqVO.getCodes())
                .likeIfPresent(IotMaterialDO::getName, reqVO.getName())
                .eqIfPresent(IotMaterialDO::getModel, reqVO.getModel())
                .eqIfPresent(IotMaterialDO::getUnit, reqVO.getUnit())
                .eqIfPresent(IotMaterialDO::getSapCode, reqVO.getSapCode())
                .eqIfPresent(IotMaterialDO::getSyncStatus, reqVO.getSyncStatus())
                .betweenIfPresent(IotMaterialDO::getSyncTime, reqVO.getSyncTime())
                .eqIfPresent(IotMaterialDO::getSyncError, reqVO.getSyncError())
                .eqIfPresent(IotMaterialDO::getSort, reqVO.getSort())
                .eqIfPresent(IotMaterialDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotMaterialDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(IotMaterialDO::getCreateTime, reqVO.getCreateTime()));
    }

    /**
     * 查询 未同步的物料 列表
     * @return
     */
    default List<IotMaterialDO> selectSyncList() {
        return selectList(new LambdaQueryWrapperX<IotMaterialDO>()
                .isNull(IotMaterialDO::getSyncTime)
                .last("LIMIT 1000"));
    }

    /**
     * 将所有物料记录的 sync_time 设置为 NULL
     * @param
     */
    @Update("UPDATE rq_iot_material SET sync_time = NULL")
    void resetMaterialSyncTime();

    /**
     * 查找出表中最大的 ID
     * @return
     */
    default List<IotMaterialDO> theMaxOne(){
        return selectList(new LambdaQueryWrapperX<IotMaterialDO>().orderByDesc(IotMaterialDO::getId));
    }

    default List<IotMaterialDO> selectListByStatus(Integer status) {
        return selectList(IotMaterialDO::getStatus, status);
    }

    default PageResult<IotMaterialDO> getMaterialsByBomId(IotMaterialPageReqVO reqVO) {
        MPJLambdaWrapperX<IotMaterialDO> query = new MPJLambdaWrapperX<IotMaterialDO>();
        query.leftJoin(IotCommonBomMaterialDO.class, IotCommonBomMaterialDO::getMaterialId, IotMaterialDO::getId)
                .eq(ObjectUtil.isNotNull(reqVO.getBomId()), IotCommonBomMaterialDO::getBomNodeId, reqVO.getBomId())
                .like(IotDeviceMaterialDO::getName, reqVO.getName());
        return selectJoinPage(reqVO, IotMaterialDO.class, query);
    }

    default PageResult<IotMaterialDO> getMaterialsByDeviceBomId(IotMaterialPageReqVO reqVO) {
        MPJLambdaWrapperX<IotMaterialDO> query = new MPJLambdaWrapperX<IotMaterialDO>();
        query.leftJoin(IotDeviceMaterialDO.class, IotDeviceMaterialDO::getMaterialId, IotMaterialDO::getId)
                .eq(ObjectUtil.isNotNull(reqVO.getBomId()), IotDeviceMaterialDO::getBomNodeId, reqVO.getBomId())
                .like(IotDeviceMaterialDO::getName, reqVO.getName());
        return selectJoinPage(reqVO, IotMaterialDO.class, query);
    }

}
