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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.jeecg.modules.apply.entity.ApplyDetails;
import org.jeecg.modules.apply.entity.ApplyRecord;
import org.jeecg.modules.apply.mapper.ApplyDetailsMapper;
import org.jeecg.modules.apply.mapper.ApplyRecordMapper;
import org.jeecg.modules.material.entity.BuMaterialInfo;
import org.jeecg.modules.material.mapper.BuMaterialInfoMapper;
import org.jeecg.modules.material.mapper.BuMaterialStockMapper;
import org.jeecg.modules.workOrder.entity.AvailableMaterial;
import org.jeecg.modules.workOrder.entity.ReplacementRecords;
import org.jeecg.modules.workOrder.entity.WorkOrder;
import org.jeecg.modules.workOrder.mapper.ReplacementRecordsMapper;
import org.jeecg.modules.workOrder.mapper.WorkOrderMapper;
import org.jeecg.modules.workOrder.service.IReplacementRecordsService;
import org.springframework.stereotype.Service;

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

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

/**
 * @Description: 材料更换记录
 * @Author: jeecg-boot
 * @Date:   2025-02-20
 * @Version: V1.0
 */
@RequiredArgsConstructor
@Service
public class ReplacementRecordsServiceImpl extends ServiceImpl<ReplacementRecordsMapper, ReplacementRecords> implements IReplacementRecordsService {
    private final BuMaterialStockMapper materialStockMapper;
    private final BuMaterialInfoMapper materialInfoMapper;

    private final WorkOrderMapper workOrderMapper;
    private final ApplyRecordMapper applyRecordMapper;
    private final ApplyDetailsMapper applyDetailsMapper;

    @Override
    public List<AvailableMaterial> getAvailableMaterials(String id) {
        WorkOrder workOrder = workOrderMapper.selectById(id);
        if(workOrder == null){
            return null;
        }
        ApplyRecord record = applyRecordMapper.selectOne(new LambdaQueryWrapper<ApplyRecord>().eq(ApplyRecord::getWorkOrder, id).eq(ApplyRecord::getApplyType, 0));
        if(record == null){
            return null;
        }
        List<ApplyDetails> details = applyDetailsMapper.selectList(new LambdaQueryWrapper<ApplyDetails>().eq(ApplyDetails::getApplyId, record.getId()));
        return details.stream().map(item -> {
            AvailableMaterial material = new AvailableMaterial();
            material.setId(item.getItemId());
            BuMaterialInfo materialInfo = materialInfoMapper.selectById(item.getItemId());
            if(materialInfo == null){
                return null;
            }
            material.setUnit(materialInfo.getUnit());
            material.setMaterialName(materialInfo.getName() + "(" + materialInfo.getSpecifaction() + ")");
            double quantity = baseMapper.selectList(new LambdaQueryWrapper<ReplacementRecords>().eq(ReplacementRecords::getMaterialCode, item.getItemId()))
                    .stream().mapToDouble(ReplacementRecords::getQuantity).sum();
            if(quantity >= item.getQuantity()){
                material.setQuantity(0.0);
            }else{
                material.setQuantity(item.getQuantity() - quantity);
            }
            return material;
        }).toList();
    }
}
