package com.zfire.mall.manager.logic.k3;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.zfire.mall.manager.bean.k3.KingDeePurchaseStockOutAdd;
import com.zfire.mall.manager.bean.k3.KingDeePurchaseStockOutData;
import com.zfire.mall.manager.plus.entity.KingDeePurchaseStockOut;
import com.zfire.mall.manager.plus.entity.KingDeePurchaseStockOutItem;
import com.zfire.mall.manager.plus.service.KingDeePurchaseStockOutItemService;
import com.zfire.mall.manager.plus.service.KingDeePurchaseStockOutService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class K3PurchaseStockOutLogic {

    @Autowired
    KingDeePurchaseStockOutItemService kingDeePurchaseStockOutItemService;

    @Autowired
    KingDeePurchaseStockOutService kingDeePurchaseStockOutService;

    public void add(KingDeePurchaseStockOutAdd kingDeePurchaseStockInAdd) {
        kingDeePurchaseStockInAdd.setId(IdWorker.getIdStr());
        kingDeePurchaseStockInAdd.setBillNo(IdWorker.getIdStr());
        kingDeePurchaseStockInAdd.insert();

        for (KingDeePurchaseStockOutItem kingDeePurchaseStockOutItem : kingDeePurchaseStockInAdd.getKingDeePurchaseStockOutItems()) {

            kingDeePurchaseStockOutItem.setBillNo(kingDeePurchaseStockInAdd.getBillNo());
        }

        kingDeePurchaseStockOutItemService.saveBatch(kingDeePurchaseStockInAdd.getKingDeePurchaseStockOutItems());
    }

    public void update(KingDeePurchaseStockOutAdd kingDeePurchaseStockInAdd) {

        kingDeePurchaseStockInAdd.updateById();

        kingDeePurchaseStockOutItemService.lambdaUpdate()
                .eq(KingDeePurchaseStockOutItem::getBillNo,kingDeePurchaseStockInAdd.getBillNo())
                .remove();

        for (KingDeePurchaseStockOutItem kingDeePurchaseStockOutItem : kingDeePurchaseStockInAdd.getKingDeePurchaseStockOutItems()) {

            kingDeePurchaseStockOutItem.setBillNo(kingDeePurchaseStockInAdd.getBillNo());
        }

        kingDeePurchaseStockOutItemService.saveBatch(kingDeePurchaseStockInAdd.getKingDeePurchaseStockOutItems());
    }

    public void examine(String billNo, String cancelStatus) {
        KingDeePurchaseStockOut kingDeePurchaseStockOut = kingDeePurchaseStockOutService.getById(billNo);
        kingDeePurchaseStockOut.setCancelStatus(cancelStatus);
        kingDeePurchaseStockOut.updateById();
    }

    public KingDeePurchaseStockOutData detail(String id) {
        return null;
    }
}
