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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import digital.laboratory.platform.reagent.dto.PurchaseListDTO;
import digital.laboratory.platform.reagent.entity.*;
import digital.laboratory.platform.reagent.mapper.PurchaseListMapper;
import digital.laboratory.platform.reagent.service.*;
import digital.laboratory.platform.reagent.vo.PurchaseListDetailsVO;
import digital.laboratory.platform.reagent.vo.PurchaseListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * (采购清单)服务实现类
 *
 * @author Zhang Xiaolong created at  2023-03-10
 * @describe (采购清单) 服务实现类
 */
@Service
public class PurchaseListServiceImpl extends ServiceImpl<PurchaseListMapper, PurchaseList> implements PurchaseListService {

    @Autowired
    private PurchaseListService purchaseListService;
    @Autowired
    private PurchaseListDetailsService purchaseListDetailsService;

    @Autowired
    private WarehousingRecordFormService warehousingRecordFormService;

    @Autowired
    private CatalogueDetailsService catalogueDetailsService;

    @Autowired
    private WarehousingContentService warehousingContentService;

    @Autowired
    private SupplierInformationService supplierInformationService;

    @Override//通过ID查找清单
    public PurchaseListVO getPurchaseList(String purchaseListId) {

        PurchaseListVO purchaseListVO = new PurchaseListVO();

        PurchaseList byId = purchaseListService.getById(purchaseListId);

        List<PurchaseListDetailsVO> purchaseListDetailsVOList = purchaseListDetailsService.getPurchaseListDetailsVOList(purchaseListId);

        for (PurchaseListDetailsVO purchaseListDetailsVO : purchaseListDetailsVOList) {

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

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

            SupplierInformation one = supplierInformationService.getOne(supplierInformationLambdaQueryWrapper);

            purchaseListDetailsVO.setSupplierName(one.getSupplierName());

        }

        BeanUtils.copyProperties(byId, purchaseListVO);

        purchaseListVO.setPurchaseListDetailsVOS(purchaseListDetailsVOList);

        return purchaseListVO;
    }

    @Transactional
    @Override//新增采购清单
    public PurchaseList addListById(List<PurchaseListDTO> purchaseListDTOList, PurchaseList purchaseList){

        purchaseList.setPurchaseListId(IdWorker.get32UUID().toUpperCase());

        purchaseList.setName(LocalDate.now()+"采购清单");

        List<PurchaseListDetails> purchaseListDetailsList= new ArrayList<>();
        //创建入库内容集合
        List<WarehousingContent> warehousingContentList = new ArrayList<>();
        //创建入库记录表
        WarehousingRecordForm warehousingRecordForm = new WarehousingRecordForm();
        //录入入库记录表信息
        warehousingRecordForm.setWarehousingRecordFormId(IdWorker.get32UUID().toUpperCase());

        warehousingRecordForm.setPurchaseListId(purchaseList.getPurchaseListId() );

        warehousingRecordForm.setPurchaseNumber(LocalDate.now()+"入库任务");

        for (PurchaseListDTO purchaseListDTO : purchaseListDTOList) {

            PurchaseListDetails purchaseListDetails = new PurchaseListDetails();

            BeanUtils.copyProperties(purchaseListDTO,purchaseListDetails);

            purchaseListDetails.setPurchaseListId(purchaseList.getPurchaseListId());

            purchaseListDetails.setPurchaselistDetailsId(IdWorker.get32UUID().toUpperCase());

            purchaseListDetailsList.add(purchaseListDetails);

            WarehousingContent warehousingContent = new WarehousingContent();
            //完善入库内容信息
            warehousingContent.setWarehousingContentId(IdWorker.get32UUID().toUpperCase());

            warehousingContent.setTotalQuantity(purchaseListDetails.getPurchaseQuantity());

            warehousingContent.setReagentConsumableId(purchaseListDetails.getReagentConsumableId());

            warehousingContent.setWarehousingRecordFormId(warehousingRecordForm.getWarehousingRecordFormId());

            LambdaQueryWrapper<CatalogueDetails> catalogueDetailsLambdaQueryWrapper = new LambdaQueryWrapper<>();

            catalogueDetailsLambdaQueryWrapper.eq(CatalogueDetails::getReagentConsumableId,warehousingContent.getReagentConsumableId());

            List<CatalogueDetails> list = catalogueDetailsService.list(catalogueDetailsLambdaQueryWrapper);

            warehousingContentList.add(warehousingContent);

        }
        if (purchaseListService.save(purchaseList)&purchaseListDetailsService.saveBatch(purchaseListDetailsList)& warehousingRecordFormService.save(warehousingRecordForm)
                & warehousingContentService.saveBatch(warehousingContentList)){
            return purchaseList;
        }else {
            return null;
        }
    }
}
