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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import digital.laboratory.platform.reagent.entity.ReagentConsumableInventory;
import digital.laboratory.platform.reagent.entity.ReagentConsumables;
import digital.laboratory.platform.reagent.entity.ReferenceMaterial;
import digital.laboratory.platform.reagent.mapper.ReagentConsumableInventoryMapper;
import digital.laboratory.platform.reagent.service.BatchDetailsService;
import digital.laboratory.platform.reagent.service.ReagentConsumableInventoryService;
import digital.laboratory.platform.reagent.service.ReagentConsumablesService;
import digital.laboratory.platform.reagent.service.ReferenceMaterialService;
import digital.laboratory.platform.reagent.vo.BatchDetailsVO;
import digital.laboratory.platform.reagent.vo.ReagentConsumableInventoryVO;
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 ReagentConsumableInventoryServiceImpl extends ServiceImpl<ReagentConsumableInventoryMapper, ReagentConsumableInventory> implements ReagentConsumableInventoryService {

    @Autowired
    private ReagentConsumableInventoryService reagentConsumableInventoryService;
    @Autowired
    private BatchDetailsService batchDetailsService;
    @Autowired
    private ReagentConsumablesService reagentConsumablesService;

    @Autowired
    private ReferenceMaterialService referenceMaterialService;

    @Override
    public List<ReagentConsumableInventoryVO >getReagentConsumableInventoryVOList(){

        LambdaQueryWrapper<ReagentConsumableInventory> reagentConsumableInventoryLambdaQueryWrapper = new LambdaQueryWrapper<>();

        QueryWrapper<ReagentConsumableInventory> reagentConsumableInventoryQueryWrapper = new QueryWrapper<>();

        List<ReagentConsumableInventory> list = reagentConsumableInventoryService.list(reagentConsumableInventoryLambdaQueryWrapper);

        ArrayList<ReagentConsumableInventoryVO> reagentConsumableInventoryVOS = new ArrayList<>();

        for (ReagentConsumableInventory reagentConsumableInventory : list) {

            List<BatchDetailsVO> batchDetailsVOList = batchDetailsService.getBatchDetailsList(reagentConsumableInventory.getReagentConsumableInventoryId());

            ReagentConsumableInventoryVO reagentConsumableInventoryVO = new ReagentConsumableInventoryVO();

            BeanUtils.copyProperties(reagentConsumableInventory,reagentConsumableInventoryVO);
            //赋值试剂耗材批次明细
            reagentConsumableInventoryVO.setBatchDetailsVOS(batchDetailsVOList);

            LambdaQueryWrapper<ReagentConsumables> reagentConsumablesLambdaQueryWrapper = new LambdaQueryWrapper<>();

            reagentConsumablesLambdaQueryWrapper.eq(ReagentConsumables::getReagentConsumableId,reagentConsumableInventoryVO.getReagentConsumableInventoryId());
            //赋值试剂耗材名称
            reagentConsumableInventoryVO.setReagentConsumablesName(reagentConsumablesService.getById(reagentConsumablesLambdaQueryWrapper).getReagentConsumableName());

            reagentConsumableInventoryVOS.add(reagentConsumableInventoryVO);
        }
        return reagentConsumableInventoryVOS;
    }

    @Override//领用减少库存
    public ReagentConsumableInventory addById (String reagentConsumableId, Integer quantity){

        LambdaQueryWrapper<ReagentConsumableInventory> reagentConsumableInventoryLambdaQueryWrapper = new LambdaQueryWrapper<>();

        reagentConsumableInventoryLambdaQueryWrapper.eq(ReagentConsumableInventory::getReagentConsumableId,reagentConsumableId);

        ReagentConsumableInventory one = reagentConsumableInventoryService.getOne(reagentConsumableInventoryLambdaQueryWrapper);

        one.setTotalQuantity(one.getTotalQuantity()+quantity);

        if (reagentConsumableInventoryService.updateById(one)){

            return one;
        }else throw  new RuntimeException(String.format("库存添加失败"));
    }

    @Override//归还/入库增加库存
    public ReagentConsumableInventory reduceById(String reagentConsumableId, Integer quantity){

        LambdaQueryWrapper<ReagentConsumableInventory> reagentConsumableInventoryLambdaQueryWrapper = new LambdaQueryWrapper<>();

        reagentConsumableInventoryLambdaQueryWrapper.eq(ReagentConsumableInventory::getReagentConsumableId,reagentConsumableId);

        ReagentConsumableInventory one = reagentConsumableInventoryService.getOne(reagentConsumableInventoryLambdaQueryWrapper);

        one.setTotalQuantity(one.getTotalQuantity()-quantity);

        if (reagentConsumableInventoryService.updateById(one)){

            return one;
        }else throw  new RuntimeException(String.format("库存减少失败"));
    }

    @Override
    public List<ReagentConsumableInventoryVO >getReferenceMaterialVOList(){

        LambdaQueryWrapper<ReagentConsumableInventory> reagentConsumableInventoryLambdaQueryWrapper = new LambdaQueryWrapper<>();

        QueryWrapper<ReagentConsumableInventory> reagentConsumableInventoryQueryWrapper = new QueryWrapper<>();

        reagentConsumableInventoryQueryWrapper.eq("category","标准物质");

        reagentConsumableInventoryQueryWrapper.eq("category","标准溶液");

        List<ReagentConsumableInventory> list = reagentConsumableInventoryService.list(reagentConsumableInventoryLambdaQueryWrapper);

        ArrayList<ReagentConsumableInventoryVO> reagentConsumableInventoryVOS = new ArrayList<>();

        for (ReagentConsumableInventory reagentConsumableInventory : list) {

            List<BatchDetailsVO> batchDetailsVOList = batchDetailsService.getBatchDetailsList(reagentConsumableInventory.getReagentConsumableInventoryId());

            ReagentConsumableInventoryVO reagentConsumableInventoryVO = new ReagentConsumableInventoryVO();

            BeanUtils.copyProperties(reagentConsumableInventory,reagentConsumableInventoryVO);
            //赋值试剂耗材批次明细
            reagentConsumableInventoryVO.setBatchDetailsVOS(batchDetailsVOList);

            LambdaQueryWrapper<ReagentConsumables> reagentConsumablesLambdaQueryWrapper = new LambdaQueryWrapper<>();

            reagentConsumablesLambdaQueryWrapper.eq(ReagentConsumables::getReagentConsumableId,reagentConsumableInventoryVO.getReagentConsumableInventoryId());
            //赋值试剂耗材名称
            reagentConsumableInventoryVO.setReagentConsumablesName(reagentConsumablesService.getById(reagentConsumablesLambdaQueryWrapper).getReagentConsumableName());

            reagentConsumableInventoryVOS.add(reagentConsumableInventoryVO);
        }
        return reagentConsumableInventoryVOS;
    }

    @Override//标准物质/溶液停用
    public ReferenceMaterial deactivateById(String referenceMaterialId){

        ReferenceMaterial byId = referenceMaterialService.getById(referenceMaterialId);

        byId.setStatus(-1);

        return byId;
    }


}
