package digital.laboratory.platform.reagent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import digital.laboratory.platform.reagent.entity.BatchDetails;
import digital.laboratory.platform.reagent.entity.ReagentConsumableInventory;
import digital.laboratory.platform.reagent.entity.ReferenceMaterial;
import digital.laboratory.platform.reagent.entity.SupplierInformation;
import digital.laboratory.platform.reagent.mapper.BatchDetailsMapper;
import digital.laboratory.platform.reagent.service.BatchDetailsService;
import digital.laboratory.platform.reagent.service.ReagentConsumableInventoryService;
import digital.laboratory.platform.reagent.service.ReferenceMaterialService;
import digital.laboratory.platform.reagent.service.SupplierInformationService;
import digital.laboratory.platform.reagent.vo.BatchDetailsVO;
import digital.laboratory.platform.reagent.vo.ReferenceMaterialVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 批次明细服务实现类
 *
 * @author Zhang Xiaolong created at  2023-03-10
 * @describe 批次明细 服务实现类
 */
@Service
public class BatchDetailsServiceImpl extends ServiceImpl<BatchDetailsMapper, BatchDetails> implements BatchDetailsService {

    @Autowired
    private BatchDetailsService batchDetailsService;

    @Autowired
    private ReagentConsumableInventoryService reagentConsumableInventoryService;

    @Autowired
    private SupplierInformationService supplierInformationService;

    @Autowired
    private ReferenceMaterialService referenceMaterialService;
    @Override
    public List<BatchDetailsVO> getBatchDetailsList(String reagentConsumableInventoryId) {

        LambdaQueryWrapper<BatchDetails> batchDetailsLambdaQueryWrapper = new LambdaQueryWrapper<>();

        batchDetailsLambdaQueryWrapper.eq(BatchDetails::getReagentConsumableInventoryId,reagentConsumableInventoryId);

        List<BatchDetails> list = batchDetailsService.list(batchDetailsLambdaQueryWrapper);

        ReagentConsumableInventory byId = reagentConsumableInventoryService.getById(reagentConsumableInventoryId);

        List<BatchDetailsVO> batchDetailsVOS = new ArrayList<>();

        for (BatchDetails batchDetails : list) {

            LambdaQueryWrapper<SupplierInformation> supplierInformationLambdaQueryWrapper = new LambdaQueryWrapper<>();

            supplierInformationLambdaQueryWrapper.eq(SupplierInformation::getSupplierInformationId,batchDetails.getSupplierId());

            SupplierInformation byId1 = supplierInformationService.getById(supplierInformationLambdaQueryWrapper);

            BatchDetailsVO batchDetailsVO = new BatchDetailsVO();

            BeanUtils.copyProperties(batchDetails,batchDetailsVO);

            List<ReferenceMaterialVO> referenceMaterialVOList = referenceMaterialService.getReferenceMaterialVOList(batchDetailsVO.getBatchDetailsId());

            batchDetailsVO.setSupplierName(byId1.getSupplierName());

            batchDetailsVO.setReferenceMaterialVOS(referenceMaterialVOList);

            batchDetailsVOS.add(batchDetailsVO);

        }
        return batchDetailsVOS;
    }

}
