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


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.zfire.mall.manager.bean.k3.KingDeeOtherStockInAdd;
import com.zfire.mall.manager.plus.entity.KingDeeOtherStockIn;
import com.zfire.mall.manager.plus.entity.KingDeeOtherStockInItem;
import com.zfire.mall.manager.plus.service.KingDeeOtherStockInItemService;
import com.zfire.mall.manager.plus.service.KingDeeOtherStockInService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class K3OtherStockInLogic {

    @Autowired
    KingDeeOtherStockInService kingDeeOtherStockInService;

    @Autowired
    KingDeeOtherStockInItemService kingDeeOtherStockInItemService;

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

        for (KingDeeOtherStockInItem kingDeeOtherStockIn : kingDeePurchaseStockInAdd.getKingDeeOtherStockInItems()) {
            kingDeeOtherStockIn.setBillNo(kingDeeOtherStockIn.getId());
        }

        kingDeeOtherStockInItemService.saveBatch(kingDeePurchaseStockInAdd.getKingDeeOtherStockInItems());

    }

    public void update(KingDeeOtherStockInAdd kingDeePurchaseStockInAdd) {

        kingDeePurchaseStockInAdd.updateById();

        kingDeeOtherStockInItemService.lambdaUpdate().eq(KingDeeOtherStockInItem::getBillNo,kingDeePurchaseStockInAdd.getBillNo())
                .remove();

        for (KingDeeOtherStockInItem kingDeeOtherStockIn : kingDeePurchaseStockInAdd.getKingDeeOtherStockInItems()) {
            kingDeeOtherStockIn.setBillNo(kingDeeOtherStockIn.getId());
        }

        kingDeeOtherStockInItemService.saveBatch(kingDeePurchaseStockInAdd.getKingDeeOtherStockInItems());
    }

    public void examine(String billNo, String cancelStatus) {
        KingDeeOtherStockIn kingDeeOtherStockIn = kingDeeOtherStockInService.getById(billNo);

        kingDeeOtherStockIn.setCancelStatus(cancelStatus);
        kingDeeOtherStockIn.updateById();
    }
}
