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

import cn.hutool.core.bean.BeanUtil;
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.bean.k3.KingDeeOtherStockOutDetail;
import com.zfire.mall.manager.enums.ExamineStatusEnum;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.stock.StockCommonLogic;
import com.zfire.mall.manager.plus.entity.*;
import com.zfire.mall.manager.plus.service.KingDeeOtherStockOutItemService;
import com.zfire.mall.manager.plus.service.KingDeeOtherStockOutService;
import com.zfire.mall.manager.plus.service.StockCorrespondService;
import com.zfire.mall.manager.plus.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class K3OtherStockOutLogic {


    @Autowired
    KingDeeOtherStockOutService kingDeeOtherStockOutService;

    @Autowired
    KingDeeOtherStockOutItemService kingDeeOtherStockOutItemService;

    @Autowired
    StockService stockService;

    @Autowired
    StockCommonLogic stockCommonLogic;

    @Autowired
    StockCorrespondService stockCorrespondService;

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

        List<StockCorrespond> list = stockCorrespondService.lambdaQuery().list();

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

            if (StringUtils.isEmpty(kingDeeOtherStockOutItem.getStockId()))
                throw new RemoteServiceException("仓库空了");


            List<StockCorrespond> collect = list.stream().filter(item -> item.getId().equals(kingDeeOtherStockOutItem.getStockId())).collect(Collectors.toList());

            if (CollectionUtils.isEmpty(collect))
                throw new RemoteServiceException("未找到对应仓库");


            StockCorrespond stockCorrespond = collect.get(0);
            kingDeeOtherStockOutItem.setStockName(stockCorrespond.getName());
        }

        kingDeeOtherStockOutItemService.saveBatch(kingDeeOtherStockOutAdd.getKingDeeOtherStockOutItems());

    }

    public void update(KingDeeOtherStockOutAdd kingDeeOtherStockOutAdd) {

        kingDeeOtherStockOutAdd.updateById();

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

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

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

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

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

        List<KingDeeOtherStockOutItem> kingDeeOtherStockOutItems = kingDeeOtherStockOutItemService.lambdaQuery()
                .eq(KingDeeOtherStockOutItem::getBillNo, billNo).list();
        for (KingDeeOtherStockOutItem kingDeeOtherStockOutItem : kingDeeOtherStockOutItems) {
            this.stockSub(kingDeeOtherStockOut,kingDeeOtherStockOutItem);
        }
    }


    @Transactional(rollbackFor = Exception.class)
    public void stockSub(KingDeeOtherStockOut kingDeeOtherStockOut, KingDeeOtherStockOutItem kingDeeOtherStockOutItem) {

        Stock stock = stockService.lambdaQuery().eq(Stock::getStockId, kingDeeOtherStockOutItem.getStockId())
                .eq(Stock::getMaterialId, kingDeeOtherStockOutItem.getMaterialId()).last("limit 1").one();

        if (stock == null)
            throw new RemoteServiceException(kingDeeOtherStockOutItem.getMaterialName()+"该物料没有库存，不能退库");


        stockCommonLogic.stockAccLog("其他出库减少库存",stock,kingDeeOtherStockOutItem.getQty(),kingDeeOtherStockOutItem.getBillNo(),false,"其他出库单");

        stock.setStockQty(stock.getStockQty().subtract(kingDeeOtherStockOutItem.getQty()));

        if (stock.getStockQty().doubleValue() < 1)
            throw new RemoteServiceException(kingDeeOtherStockOutItem.getMaterialName()+"该物料没有库存，不能退库");

        stock.updateById();

    }

    public KingDeeOtherStockOutDetail detail(String id) {
        KingDeeOtherStockOut kingDeeOtherStockOut = kingDeeOtherStockOutService.getById(id);
        KingDeeOtherStockOutDetail kingDeeOtherStockOutDetail = BeanUtil.toBean(kingDeeOtherStockOut, KingDeeOtherStockOutDetail.class);

        List<KingDeeOtherStockOutItem> kingDeeOtherStockOutItems = kingDeeOtherStockOutItemService.lambdaQuery().eq(KingDeeOtherStockOutItem::getBillNo, id).list();

        kingDeeOtherStockOutDetail.setKingDeeOtherStockOutItems(kingDeeOtherStockOutItems);
        return kingDeeOtherStockOutDetail;
    }
}
