package com.cim.idm.service.Impl;

import com.cim.idm.dao.PurchaseReturnServiceDao;
import com.cim.idm.dao.ToSapDao;
import com.cim.idm.model.PurchaseReturnDto;
import com.cim.idm.service.PurchaseReturnService;
import com.cim.idm.service.impl.ToSAPServiceImpl;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class PurchaseReturnServiceImpl implements PurchaseReturnService {

    @Autowired
    private PurchaseReturnServiceDao purchaseReturnServiceDao;

    private ToSapDao toSapDao;

    @Autowired
    private ToSAPServiceImpl toSAPServiceImpl;
    /**
     * @param siteName
     * @param user
     * @param receiverequestname
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public List<PurchaseReturnDto> getPurchaseReturnList(String siteName, String user, String receiverequestname,String erpFactory) throws Exception {
        return purchaseReturnServiceDao.getPurchaseReturnList(siteName, user, receiverequestname,erpFactory);
    }

    /**
     * @param siteName
     * @param user
     * @param receiverequestname
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public List<PurchaseReturnDto> getPurchaseReturnInfo(String siteName, String user, String receiverequestname) throws Exception {
        return purchaseReturnServiceDao.getPurchaseReturnInfo(siteName, user, receiverequestname);
    }

    /**
     * @param siteName
     * @param user
     * @param receiverequestname
     * @param receiverequestdetailname
     * @param receiveactno
     * @param acttype
     * @param receivequantity
     * @param submaterialspecname
     * @return
     * @throws Exception
     */
    @Override
    public String purDessignOrAssign(String siteName, String user, String receiverequestname, String receiverequestdetailname, String receiveactno, String acttype, String receivequantity, String submaterialspecname) throws Exception {
        return "";
    }

    /**
     * @param siteName
     * @param user
     * @param receiverequestname
     * @return
     * @throws Exception
     */
    @Override
    public String getPurPackingInfo(String siteName, String user, String receiverequestname) throws Exception {
        return "";
    }

    /**
     * @param siteName
     * @param user
     * @param receiverequestname
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public String completeReceiveRequest(String siteName, String user, String receiverequestname) throws Exception {
        //查找需要过账的采购退库信息
        List<MaterialPacking> purchaseReturnBoxList = purchaseReturnServiceDao.getPurchaseReturnBoxList(siteName, receiverequestname);
        //将 MaterialPacking的MaterialPackingName放入一个List<String>中
        List<String> purchaseReturnBoxListName = new ArrayList<>();
        for (MaterialPacking materialPacking : purchaseReturnBoxList) {
            purchaseReturnBoxListName.add(materialPacking.getMaterialPackingName());
        }
        //过账SAP
//        String undoId = toSAPServiceImpl.orderStockOut(receiverequestname, siteName, user, purchaseReturnBoxListName);
        String undoId = toSAPServiceImpl.OrderStockBackOut(receiverequestname, siteName, user);
//        String undoId = "111";
                //更新库存状态
        toSapDao.updateStockState(purchaseReturnBoxList, "StockOut", undoId);
        //更新出入库流水
        toSapDao.saveUnDoInfo(purchaseReturnBoxList, undoId, "");
        return "";
    }
}
