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

import cn.hutool.core.util.StrUtil;
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.iotbom.vo.IotBomListReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotbom.vo.IotBomPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotbom.vo.IotBomSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotbom.IotBomDO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

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

/**
 * PMS BOM 关系 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface IotBomMapper extends BaseMapperX<IotBomDO> {

    default PageResult<IotBomDO> selectPage(IotBomPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotBomDO>()
                .likeIfPresent(IotBomDO::getName, reqVO.getName())
                .eqIfPresent(IotBomDO::getCode, reqVO.getCode())
                .eqIfPresent(IotBomDO::getParentId, reqVO.getParentId())
                .eqIfPresent(IotBomDO::getChildIds, reqVO.getChildIds())
                .eqIfPresent(IotBomDO::getLevel, reqVO.getLevel())
                .eqIfPresent(IotBomDO::getDeviceCategoryId, reqVO.getDeviceCategoryId())
                .eqIfPresent(IotBomDO::getSort, reqVO.getSort())
                .eqIfPresent(IotBomDO::getType, reqVO.getType())
                .eqIfPresent(IotBomDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotBomDO::getLeafFlag, reqVO.getLeafFlag())
                .eqIfPresent(IotBomDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotBomDO::getVersion, reqVO.getVersion())
                .betweenIfPresent(IotBomDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(IotBomDO::getId));
    }

    default List<IotBomDO> selectList(IotBomListReqVO reqVO, Set<Long> ids) {
        return selectList(new LambdaQueryWrapperX<IotBomDO>()
                .likeIfPresent(IotBomDO::getName, reqVO.getName())
                .inIfPresent(IotBomDO::getDeviceCategoryId, ids)
                .eqIfPresent(IotBomDO::getStatus, reqVO.getStatus())
                .orderByAsc(IotBomDO::getDeviceCategoryId).orderByAsc(IotBomDO::getSort));
    }

    default IotBomDO selectParentNode(IotBomSaveReqVO reqVO) {
        return selectOne(IotBomDO::getId, reqVO.getParentId());
    }

    default PageResult<IotBomDO> deviceAssociateBomPage(IotBomPageReqVO reqVO) {
        MPJLambdaWrapperX<IotBomDO> query = new MPJLambdaWrapperX<IotBomDO>();
        query.innerJoin(IotDeviceDO.class, IotDeviceDO::getAssetClass, IotBomDO::getDeviceCategoryId)
                .eq(IotBomDO::getLeafFlag, 1)
                .like(IotBomDO::getType, "2")
                .like(StrUtil.isNotBlank(reqVO.getDeviceName()), IotDeviceDO::getDeviceName, reqVO.getDeviceName())
                .like(StrUtil.isNotBlank(reqVO.getName()), IotBomDO::getName, reqVO.getName());

        return selectJoinPage(reqVO, IotBomDO.class, query);
    }

    default List<IotBomDO> getListByDeviceCategoryIds(Collection<Long> deviceCategoryIds) {
        return selectList(new LambdaQueryWrapperX<IotBomDO>()
                .inIfPresent(IotBomDO::getDeviceCategoryId, deviceCategoryIds)
                .eq(IotBomDO::getLeafFlag, 1)
                .like(IotBomDO::getType, "2"));
    }

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