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

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.iotcommonbommaterial.vo.IotCommonBomMaterialPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotcommonbommaterial.vo.IotCommonBomMaterialSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotcommonbommaterial.IotCommonBomMaterialDO;
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 IotCommonBomMaterialMapper extends BaseMapperX<IotCommonBomMaterialDO> {

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

    default List<IotCommonBomMaterialDO> selectList(IotCommonBomMaterialSaveReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<IotCommonBomMaterialDO>()
                .eqIfPresent(IotCommonBomMaterialDO::getDeviceCategoryId, reqVO.getDeviceCategoryId())
                .eqIfPresent(IotCommonBomMaterialDO::getBomNodeId, reqVO.getBomNodeId())
                .eqIfPresent(IotCommonBomMaterialDO::getCode, reqVO.getCode())
                .inIfPresent(IotCommonBomMaterialDO::getBomNodeId, reqVO.getBomNodeIds())
                .eqIfPresent(IotCommonBomMaterialDO::getMaterialId, reqVO.getMaterialId()));
    }

    default List<IotCommonBomMaterialDO> selectList1(IotCommonBomMaterialSaveReqVO reqVO) {
        MPJLambdaWrapperX<IotCommonBomMaterialDO> query = new MPJLambdaWrapperX<IotCommonBomMaterialDO>();
        query.innerJoin(IotMaterialDO.class, IotMaterialDO::getId, IotCommonBomMaterialDO::getMaterialId)
                .eq(ObjectUtil.isNotEmpty(reqVO.getDeviceCategoryId()), IotCommonBomMaterialDO::getDeviceCategoryId, reqVO.getDeviceCategoryId())
                .in(CollUtil.isNotEmpty(reqVO.getBomNodeIds()), IotCommonBomMaterialDO::getBomNodeId, reqVO.getBomNodeIds());
        return selectJoinList(IotCommonBomMaterialDO.class, query);
    }

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