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

import cn.hutool.core.collection.CollUtil;
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.module.pms.controller.admin.iotdevicebom.vo.IotDeviceBomListReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicebom.vo.IotDeviceBomPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicebom.vo.IotDeviceBomSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicebom.IotDeviceBomDO;
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.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * PMS 设备BOM 关系 Mapper
 *
 * @author ruiqi
 */
@Mapper
public interface IotDeviceBomMapper extends BaseMapperX<IotDeviceBomDO> {

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

    default List<IotDeviceBomDO> selectList(IotDeviceBomListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<IotDeviceBomDO>()
                .likeIfPresent(IotDeviceBomDO::getName, reqVO.getName())
                .eqIfPresent(IotDeviceBomDO::getDeviceId, reqVO.getDeviceId())
                .eqIfPresent(IotDeviceBomDO::getStatus, reqVO.getStatus()));
    }

    default IotDeviceBomDO selectParentNode(IotDeviceBomSaveReqVO reqVO) {
        return selectOne(IotDeviceBomDO::getId, reqVO.getParentId());
    }

    default Map<Long, IotDeviceBomDO> selectNodeMapByIds(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) return Collections.emptyMap();
        // 一次性查询所有节点
        List<IotDeviceBomDO> nodes = selectList(new LambdaQueryWrapperX<IotDeviceBomDO>().in(IotDeviceBomDO::getId, ids));
        return nodes.stream().collect(Collectors.toMap(IotDeviceBomDO::getId, Function.identity()));
    }

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

}