package com.fjhx.purchase.service;

import cn.hutool.core.bean.BeanUtil;
import com.fjhx.common.utils.Assert;
import com.fjhx.purchase.entity.arrival.po.ArrivalDetail;
import com.fjhx.purchase.entity.purchase.po.Purchase;
import com.fjhx.purchase.entity.purchase.po.PurchaseDetail;
import com.fjhx.purchase.entity.sales.enums.SalesReturnDetailStatusEnum;
import com.fjhx.purchase.entity.sales.po.SalesReturnDetail;
import com.fjhx.purchase.service.arrival.ArrivalDetailService;
import com.fjhx.purchase.service.purchase.PurchaseDetailService;
import com.fjhx.purchase.service.purchase.PurchaseService;
import com.fjhx.purchase.service.sales.SalesReturnDetailService;
import com.fjhx.wms.entity.arrival.po.ArrivalDetailPo;
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 SalesReturnDetailService salesReturnDetailService;
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private PurchaseDetailService purchaseDetailService;
    @Autowired
    private ArrivalDetailService arrivalDetailService;

    /**
     * 修改采购退货状态
     */
    @Override
    public void updateSalesReturnStatus(Long id, Integer status) {
        switch (status) {
            case 0://待出库
                status = SalesReturnDetailStatusEnum.STATUS_20.getKey();//待退货
                break;
            case 1://部分出库
                status = SalesReturnDetailStatusEnum.STATUS_30.getKey();//部分退货
                break;
            case 2://已出库
                status = SalesReturnDetailStatusEnum.STATUS_40.getKey();//退货完成
                break;
        }
        SalesReturnDetail salesReturnDetail = salesReturnDetailService.getById(id);
        Assert.notEmpty(salesReturnDetail, "查询不到退货信息");
        salesReturnDetail.setStatus(status);
        salesReturnDetailService.updateById(salesReturnDetail);
    }

    @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()));
    }

    @Override
    public ArrivalDetailPo getArrivalDetailInfo(Long id) {
        ArrivalDetail arrivalDetail = arrivalDetailService.getById(id);
        ArrivalDetailPo arrivalDetailPo = BeanUtil.copyProperties(arrivalDetail, ArrivalDetailPo.class);
        return arrivalDetailPo;
    }

    @Override
    public List<ArrivalDetailPo> getArrivalDetailList(List<Long> ids) {
        List<ArrivalDetail> arrivalDetailList = arrivalDetailService.list(q -> q.in(ArrivalDetail::getId, ids));
        return BeanUtil.copyToList(arrivalDetailList, ArrivalDetailPo.class);
    }

    @Override
    public List<PurchaseDetailPo> getPurchaseDetailList(List<Long> ids) {
        List<PurchaseDetail> arrivalDetailList = purchaseDetailService.list(q -> q.in(PurchaseDetail::getId, ids));
        return BeanUtil.copyToList(arrivalDetailList, PurchaseDetailPo.class);
    }
}
