package com.cf.imes.module.executor.service.orderitem;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cf.imes.module.executor.dal.dataobject.orderItem.OrderItemDO;
import com.cf.imes.module.executor.dal.mysql.orderItem.OrderItemMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单明细服务实现类 ServiceImpl
 *
 * @author Gqr
 * @since 2026/3/18 11:29
 */
@Service
public class OrderItemServiceImpl implements OrderItemService {
    @Resource
    private OrderItemMapper orderItemMapper;

    @Override
    public List<OrderItemDO> getPlateItemCompId(Long orderId, Long id) {
        List<OrderItemDO> plateItemDOS = orderItemMapper.selectList(new LambdaQueryWrapper<OrderItemDO>()
                .eq(OrderItemDO::getPlateId, id)
                .eq(OrderItemDO::getOrderId, orderId)
                .select(OrderItemDO::getCompId, OrderItemDO::getGroupId, OrderItemDO::getBodyId));
        if (CollUtil.isNotEmpty(plateItemDOS)) {
            return plateItemDOS;
        }
        return null;
    }

    @Override
    public List<OrderItemDO> getPartItemCompId(Long partId) {
        List<OrderItemDO> partItemDOS = orderItemMapper.selectList(new LambdaQueryWrapper<OrderItemDO>()
                .eq(OrderItemDO::getPartsId, partId)
                .select(OrderItemDO::getCompId, OrderItemDO::getGroupId, OrderItemDO::getBodyId));
        if (CollUtil.isNotEmpty(partItemDOS)) {
            return partItemDOS;
        }
        return null;
    }

    @Override
    public List<String> getComponentIdsCorrelationItemIds(List<Long> orderIds, List<Long> componentIds, List<Long> groupIds) {
        List<String> resultList = new ArrayList<>();
        if (CollUtil.isEmpty(componentIds) && CollUtil.isEmpty(groupIds)) {
            return resultList;
        }
        // 查询关联的板件和配件id
        resultList.addAll(orderItemMapper.selectComponentCorrelationPlateIds(orderIds, componentIds, groupIds));
        resultList.addAll(orderItemMapper.selectComponentCorrelationPartsIds(orderIds, componentIds, groupIds));
        return resultList;
    }
}
