package com.fjhx.purchase.service;

import cn.hutool.core.bean.BeanUtil;
import com.fjhx.purchase.entity.purchase.po.Purchase;
import com.fjhx.purchase.entity.purchase.po.PurchaseDetail;
import com.fjhx.purchase.service.purchase.PurchaseDetailService;
import com.fjhx.purchase.service.purchase.PurchaseService;
import com.fjhx.wms.entity.purchase.po.PurchaseDetailPo;
import com.fjhx.wms.entity.purchase.po.PurchasePo;
import com.fjhx.wms.service.MyPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class MyPurchaseServiceImpl implements MyPurchaseService {
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Override
    public PurchasePo getPurchaseInfo(Long id) {
        Purchase purchase = purchaseService.getById(id);
        return BeanUtil.copyProperties(purchase, PurchasePo.class);
    }

    @Override
    public Map<Long, PurchaseDetailPo> grtPurchaseDetail(Long id) {
        List<PurchaseDetail> purchaseDetailList = purchaseDetailService.list(q -> q.eq(PurchaseDetail::getPurchaseId, id));
        List<PurchaseDetailPo> purchaseDetailPos = BeanUtil.copyToList(purchaseDetailList, PurchaseDetailPo.class);
        return purchaseDetailPos.stream().collect(Collectors.toMap(PurchaseDetailPo::getId, Function.identity()));
    }

}
