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.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import digital.laboratory.platform.common.mybatis.security.service.DLPUser;
import digital.laboratory.platform.reagent.dto.StorageRegistrationFormDTO;
import digital.laboratory.platform.reagent.entity.*;
import digital.laboratory.platform.reagent.mapper.StorageRegistrationFormMapper;
import digital.laboratory.platform.reagent.service.*;
import digital.laboratory.platform.reagent.vo.StorageRegistrationFormVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * (试剂耗材入库登记表)服务实现类
 *
 * @author Zhang Xiaolong created at  2023-03-10
 * @describe (试剂耗材入库登记表) 服务实现类
 */
@Service
public class StorageRegistrationFormServiceImpl extends ServiceImpl<StorageRegistrationFormMapper, StorageRegistrationForm> implements StorageRegistrationFormService {

//    @Autowired
//    private StorageRegistrationFormService storageRegistrationFormService;
//
//    @Autowired
//    private LatticeFormService latticeFormService;
//
//    @Autowired
//    private CabinetFormService cabinetFormService;
//
//    @Autowired
//    private StorageRoomFormService storageRoomFormService;
//
//    @Autowired
//    private ReagentConsumableInventoryService reagentConsumableInventoryService;
//
//    @Autowired
//    private ReagentConsumablesService reagentConsumablesService;
//
//    @Autowired
//    private WarehousingBatchListService warehousingBatchListService;

//    @Override//分页查看
//    public List<StorageRegistrationFormVO> getStorageRegistrationFormVOList(QueryWrapper<StorageRegistrationForm> qw) {
//
//        List<StorageRegistrationFormVO> storageRegistrationFormVOList = baseMapper.getStorageRegistrationFormVOList(qw);
//
//        for (StorageRegistrationFormVO storageRegistrationFormVO : storageRegistrationFormVOList) {
//
//            LatticeForm byId = latticeFormService.getById(storageRegistrationFormVO.getLatticeId());
//
//            CabinetForm byId1 = cabinetFormService.getById(byId.getCabinetFormId());
//
//            StorageRoomForm byId2 = storageRoomFormService.getById(byId1.getStorageRoomFormId());
//
//            storageRegistrationFormVO.setLocation(byId2.getName() + byId1.getName() + byId.getNumber());
//        }
//        return storageRegistrationFormVOList;
    }


//    @Override//入库任务
//    public List<StorageRegistrationForm> editFormById(List<StorageRegistrationFormDTO> storageRegistrationFormDTOList, DLPUser dlpUser) {
//
//        List<StorageRegistrationForm> storageRegistrationForms = new ArrayList<>();
//
//        for (StorageRegistrationFormDTO storageRegistrationFormDTO : storageRegistrationFormDTOList) {
//
//            StorageRegistrationForm byId = storageRegistrationFormService.getById(storageRegistrationFormDTO.getStorageRegistrationFormId());
//
//            BeanUtils.copyProperties(storageRegistrationFormDTO, byId);
//
//            byId.setDepositorId(dlpUser.getId());
//
//            byId.setDateOfArrival(LocalDateTime.now());
//
//            storageRegistrationForms.add(byId);
//            //创建库存批次表
//            LambdaQueryWrapper<ReagentConsumableInventory> reagentConsumableInventoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
//
//            reagentConsumableInventoryLambdaQueryWrapper.eq(ReagentConsumableInventory::getReagentConsumablesId, byId.getReagentConsumableId());
//
//            ReagentConsumableInventory byId1 = reagentConsumableInventoryService.getById(reagentConsumableInventoryLambdaQueryWrapper);
//            //判断仓库是否存在该试剂耗材
//            if (byId1 == null) {
//                //建立库存表与库存批次表
//                ReagentConsumableInventory reagentConsumableInventory = new ReagentConsumableInventory();
//
//                reagentConsumableInventory.setReagentConsumablesId(byId.getReagentConsumableId());
//
//                ReagentConsumables reagentConsumables = reagentConsumablesService.getById(reagentConsumableInventory.getReagentConsumablesId());
//
//                BeanUtils.copyProperties(reagentConsumables, reagentConsumableInventory);
//
//                reagentConsumableInventory.setTotalQuantity(reagentConsumableInventory.getTotalQuantity() + byId.getQuantity());
//
//                BatchDetails batchDetails = new BatchDetails();
//
//                batchDetails.setBatchDetailsId(IdWorker.get32UUID().toUpperCase());
//
//                LambdaQueryWrapper<SignedBatchList> signedBatchListLambdaQueryWrapper = new LambdaQueryWrapper<>();
//
//                signedBatchListLambdaQueryWrapper.eq(SignedBatchList::getSignedBatchListId, byId.getSignedBatchListId());
//
//                SignedBatchList signedBatchList = warehousingBatchListService.getById(signedBatchListLambdaQueryWrapper);
//
//                batchDetails.setBatch(signedBatchList.getBatch());
//
//                batchDetails.setBatchNumber(signedBatchList.getBatchNumber());
//
//                batchDetails.setQuantity(signedBatchList.getQuantity());
//
//                batchDetails.setDateOfProduction(signedBatchList.getDateOfProduction());
//
//                batchDetails.setExpirationDate(signedBatchList.getExpirationDate());
//
//                batchDetails.setPurchaseTime(signedBatchList.getDateOfReceipt());
//
//                batchDetails.setReagentConsumableInventoryId(reagentConsumableInventory.getReagentConsumableInventoryId());
//
//                batchDetails.setSupplierId(signedBatchList.getSupplierId());
//
//                batchDetails.setWarningValue(signedBatchList.getWarningValue());
//
//                if (reagentConsumableInventory.getCategory().equals("标准物质")) {
//
//                    Calendar calendar = null;
//
//                    int year = calendar.get(Calendar.YEAR);
//
//                    batchDetails.setServiceStatus(1);
//
//                    LatticeForm latticeForm = latticeFormService.getById(byId.getLatticeId());
//
//                    CabinetForm cabinetForm = cabinetFormService.getById(latticeForm.getCabinetFormId());
//
//                    StorageRoomForm storageRoomForm = storageRoomFormService.getById(cabinetForm.getStorageRoomFormId());
//
//                    for (int i = 0; i < batchDetails.getQuantity(); i++) {
//
//                        ReagentConsumable reagentConsumable = new ReagentConsumable();
//
//                        reagentConsumable.setId(IdWorker.get32UUID().toUpperCase());
//
//                        reagentConsumable.setBatchDetailsId(batchDetails.getBatchDetailsId());
//
//                        reagentConsumable.setNumber(reagentConsumables.getEnglishName() + "-" + year + "-" + storageRoomForm.getFloor() + "-" +
//                                i + 1);
//                    }
//                }
//                reagentConsumableInventoryService.save(reagentConsumableInventory);
//            } else {
//            }
//        }
//        if (storageRegistrationFormService.updateBatchById(storageRegistrationForms)) {
//            return storageRegistrationForms;
//        } else {
//            return null;
//        }
//    }

