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.bean.k3.KingDeeOtherStockOutAdd;
import com.zfire.mall.manager.plus.entity.KingDeeOtherStockIn;
import com.zfire.mall.manager.plus.entity.KingDeeOtherStockInItem;
import com.zfire.mall.manager.plus.entity.KingDeeOtherStockOut;
import com.zfire.mall.manager.plus.entity.KingDeeOtherStockOutItem;
import com.zfire.mall.manager.plus.service.KingDeeOtherStockOutItemService;
import com.zfire.mall.manager.plus.service.KingDeeOtherStockOutService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class K3OtherStockOutLogic {


    @Autowired
    KingDeeOtherStockOutService kingDeeOtherStockInService;

    @Autowired
    KingDeeOtherStockOutItemService kingDeeOtherStockInItemService;

    public void add(KingDeeOtherStockOutAdd kingDeeOtherStockOutAdd) {
        kingDeeOtherStockOutAdd.setId(IdWorker.getIdStr());
        kingDeeOtherStockOutAdd.setBillNo(kingDeeOtherStockOutAdd.getId());
        kingDeeOtherStockOutAdd.insert();

        for (KingDeeOtherStockOutItem kingDeeOtherStockOutItem : kingDeeOtherStockOutAdd.getKingDeeOtherStockOutItems()) {
            kingDeeOtherStockOutItem.setBillNo(kingDeeOtherStockOutAdd.getId());
        }

        kingDeeOtherStockInItemService.saveBatch(kingDeeOtherStockOutAdd.getKingDeeOtherStockOutItems());

    }

    public void update(KingDeeOtherStockOutAdd kingDeeOtherStockOutAdd) {

        kingDeeOtherStockOutAdd.updateById();

        kingDeeOtherStockInItemService.lambdaUpdate().eq(KingDeeOtherStockOutItem::getBillNo,kingDeeOtherStockOutAdd.getBillNo())
                .remove();

        for (KingDeeOtherStockOutItem kingDeeOtherStockOutItem : kingDeeOtherStockOutAdd.getKingDeeOtherStockOutItems()) {
            kingDeeOtherStockOutItem.setBillNo(kingDeeOtherStockOutAdd.getId());
        }

        kingDeeOtherStockInItemService.saveBatch(kingDeeOtherStockOutAdd.getKingDeeOtherStockOutItems());
    }

    public void examine(String billNo, String cancelStatus) {
        KingDeeOtherStockOut kingDeeOtherStockOut = kingDeeOtherStockInService.getById(billNo);

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