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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import digital.laboratory.platform.reagent.dto.StandardMaterialApplicationDTO;
import digital.laboratory.platform.reagent.entity.ReagentConsumableInventory;
import digital.laboratory.platform.reagent.entity.StandardMaterialApplication;
import digital.laboratory.platform.reagent.mapper.StandardMaterialApplicationMapper;
import digital.laboratory.platform.reagent.service.ReagentConsumableInventoryService;
import digital.laboratory.platform.reagent.service.ReagentConsumablesService;
import digital.laboratory.platform.reagent.service.StandardMaterialApplicationService;
import digital.laboratory.platform.reagent.vo.StandardMaterialApplicationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * (标准物质领用/归还登记表)服务实现类
 *
 * @author Zhang Xiaolong created at  2023-03-10
 * @describe (标准物质领用/归还登记表) 服务实现类
 */
@Service
public class StandardMaterialApplicationServiceImpl extends ServiceImpl<StandardMaterialApplicationMapper, StandardMaterialApplication> implements StandardMaterialApplicationService {

    @Autowired
    private StandardMaterialApplicationService standardMaterialApplicationService;

    @Autowired
    private ReagentConsumablesService reagentConsumablesService;

    @Autowired
    private ReagentConsumableInventoryService reagentConsumableInventoryService;

    @Override//归还标准物质
    public StandardMaterialApplication returnById(StandardMaterialApplicationDTO standardMaterialApplicationDTO){

        StandardMaterialApplication byId = standardMaterialApplicationService.getById(standardMaterialApplicationDTO.getStandardMaterialApplicationId());

        byId.setPurposeAndQuantity(standardMaterialApplicationDTO.getUseQuantity());
        byId.setDateOfReturn(LocalDateTime.now());
        byId.setStatus(1);

        reagentConsumableInventoryService.addById(byId.getReferenceSubstanceId(),byId.getRequisitionedQuantity());

        if (standardMaterialApplicationService.updateById(byId)){
            return byId;
        }else throw new RuntimeException(String.format("归还失败"));
    }

    @Override//领用记录
    public IPage <StandardMaterialApplicationVO> getPage(IPage<StandardMaterialApplication> page, QueryWrapper<StandardMaterialApplication> qw){

        IPage<StandardMaterialApplicationVO> standardMaterialApplicationVOPage = baseMapper.getStandardMaterialApplicationVOPage(page, qw);

        return standardMaterialApplicationVOPage;

    }
    //归还记录
    @Override//归还记录
    public IPage <StandardMaterialApplicationVO> getList(IPage<StandardMaterialApplication> page, QueryWrapper<StandardMaterialApplication> qw){

        IPage<StandardMaterialApplicationVO> standardMaterialApplicationVOList = baseMapper.getStandardMaterialApplicationVOList(page, qw);

        return standardMaterialApplicationVOList;

    }


    }

