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

import cn.hutool.core.collection.CollUtil;
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.iotdevicematerial.vo.IotDeviceMaterialPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicematerial.vo.IotDeviceMaterialSaveReqVO;
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.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * PMS 设备BOM物料关联 Mapper
 *
 * @author ruiqi
 */
@Mapper
public interface IotDeviceMaterialMapper extends BaseMapperX<IotDeviceMaterialDO> {

    default PageResult<IotDeviceMaterialDO> selectPage(IotDeviceMaterialPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotDeviceMaterialDO>()
                .eqIfPresent(IotDeviceMaterialDO::getDeviceCategoryId, reqVO.getDeviceCategoryId())
                .eqIfPresent(IotDeviceMaterialDO::getDeviceId, reqVO.getDeviceId())
                .likeIfPresent(IotDeviceMaterialDO::getName, reqVO.getName())
                .eqIfPresent(IotDeviceMaterialDO::getBomNodeId, reqVO.getBomNodeId())
                .eqIfPresent(IotDeviceMaterialDO::getCode, reqVO.getCode())
                .eqIfPresent(IotDeviceMaterialDO::getMaterialId, reqVO.getMaterialId())
                .eqIfPresent(IotDeviceMaterialDO::getQuantity, reqVO.getQuantity())
                .eqIfPresent(IotDeviceMaterialDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotDeviceMaterialDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(IotDeviceMaterialDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(IotDeviceMaterialDO::getId));
    }

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

    default List<IotDeviceMaterialDO> selectList(IotDeviceMaterialSaveReqVO reqVO) {
        MPJLambdaWrapperX<IotDeviceMaterialDO> query = new MPJLambdaWrapperX<IotDeviceMaterialDO>();
        query.innerJoin(IotMaterialDO.class, IotMaterialDO::getId, IotDeviceMaterialDO::getMaterialId)
                .eq(ObjectUtil.isNotEmpty(reqVO.getDeviceId()), IotDeviceMaterialDO::getDeviceId, reqVO.getDeviceId())
                .in(CollUtil.isNotEmpty(reqVO.getBomNodeIds()), IotDeviceMaterialDO::getBomNodeId, reqVO.getBomNodeIds());
        return selectJoinList(IotDeviceMaterialDO.class, query);
    }

    default List<IotDeviceMaterialDO> selectList(IotDeviceMaterialPageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<IotDeviceMaterialDO>()
                .eqIfPresent(IotDeviceMaterialDO::getDeviceCategoryId, reqVO.getDeviceCategoryId())
                .eqIfPresent(IotDeviceMaterialDO::getDeviceId, reqVO.getDeviceId())
                .inIfPresent(IotDeviceMaterialDO::getDeviceId, reqVO.getDeviceIds())
                .likeIfPresent(IotDeviceMaterialDO::getName, reqVO.getName())
                .eqIfPresent(IotDeviceMaterialDO::getBomNodeId, reqVO.getBomNodeId())
                .inIfPresent(IotDeviceMaterialDO::getBomNodeId, reqVO.getBomNodeIds())
                .eqIfPresent(IotDeviceMaterialDO::getCode, reqVO.getCode())
                .eqIfPresent(IotDeviceMaterialDO::getMaterialId, reqVO.getMaterialId())
                .eqIfPresent(IotDeviceMaterialDO::getQuantity, reqVO.getQuantity())
                .eqIfPresent(IotDeviceMaterialDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotDeviceMaterialDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(IotDeviceMaterialDO::getCreateTime, reqVO.getCreateTime()));
    }

    /**
     * 删除指定 deviceId 集合的设备BOM 挂载的物料
     *
     * @param deviceIds 设备id集合
     * @return 删除条数
     */
    @Delete("<script>" +
            "DELETE FROM rq_iot_device_material " +
            "WHERE device_id IN " +
            "<foreach collection=\"deviceIds\" item=\"id\" open=\"(\" separator=\",\" close=\")\">" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    Integer deleteByDeviceIds(@Param("deviceIds") List<Long> deviceIds);
}