package org.jeecg.modules.manual.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.manual.entity.Manual;
import org.jeecg.modules.manual.mapper.ManualMapper;
import org.jeecg.modules.manual.service.IManualService;
import org.jeecg.modules.regular.entity.RegularDetail;
import org.jeecg.modules.regular.mapper.RegularDetailMapper;
import org.jeecg.modules.workOrder.entity.WorkOrder;
import org.jeecg.modules.workOrder.mapper.WorkOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description: 作业指导书
 * @Author: jeecg-boot
 * @Date:   2025-01-17
 * @Version: V1.0
 */
@Service
public class ManualServiceImpl extends ServiceImpl<ManualMapper, Manual> implements IManualService {

    @Autowired
    private WorkOrderMapper workOrderMapper;
    @Autowired
    private RegularDetailMapper regularDetailMapper;

    @Override
    public List<Manual> queryByWorkOrder(String workOrder) {
        WorkOrder workOrderEntity = workOrderMapper.selectById(workOrder);
        if (workOrderEntity == null) {
            return Collections.emptyList(); // 提前返回空结果
        }
        Set<String> regularIds = regularDetailMapper.selectList(new LambdaQueryWrapper<RegularDetail>()
                .eq(RegularDetail::getRegularId,workOrderEntity.getRegularId()))
                .stream().map(RegularDetail::getId).collect(Collectors.toSet());
        regularIds.add(workOrderEntity.getRegularId());

        return baseMapper.selectList(new LambdaQueryWrapper<Manual>().in(Manual::getRegularDetailId,regularIds));
    }

}
