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.admin.AdminUserCom;
import com.zfire.mall.manager.bean.k3.KingDeeOtherStockInAdd;
import com.zfire.mall.manager.bean.k3.KingDeeOtherStockInDetail;
import com.zfire.mall.manager.enums.ExamineStatusEnum;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.logic.stock.StockCommonLogic;
import com.zfire.mall.manager.plus.entity.*;
import com.zfire.mall.manager.plus.service.KingDeeOtherStockInItemService;
import com.zfire.mall.manager.plus.service.KingDeeOtherStockInService;
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.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class K3OtherStockInLogic {

    @Autowired
    KingDeeOtherStockInService kingDeeOtherStockInService;

    @Autowired
    KingDeeOtherStockInItemService kingDeeOtherStockInItemService;

    @Autowired
    StockService stockService;

    @Autowired
    StockCommonLogic stockCommonLogic;

    @Autowired
    StockCorrespondService stockCorrespondService;

    @Autowired
    CommonLogic commonLogic;


    public void add(KingDeeOtherStockInAdd kingDeePurchaseStockInAdd) {
        kingDeePurchaseStockInAdd.setId(IdWorker.getIdStr());
        kingDeePurchaseStockInAdd.setBillNo(kingDeePurchaseStockInAdd.getId());
        kingDeePurchaseStockInAdd.setCancelStatus(ExamineStatusEnum.WAIT.getKey());
        kingDeePurchaseStockInAdd.setForDate(new Date());
        kingDeePurchaseStockInAdd.insert();

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

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

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


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

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


            StockCorrespond stockCorrespond = collect.get(0);

            kingDeeOtherStockIn.setStockName(stockCorrespond.getName());
        }

        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(kingDeePurchaseStockInAdd.getId());
        }

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

    @Transactional(rollbackFor = Exception.class)
    public void examine(String billNo, String cancelStatus) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        KingDeeOtherStockIn kingDeeOtherStockIn = kingDeeOtherStockInService.getById(billNo);

        kingDeeOtherStockIn.setApproveDate(new Date());
        kingDeeOtherStockIn.setApproverId(adminUser.getNickName());

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

        List<KingDeeOtherStockInItem> kingDeeOtherStockInItems = kingDeeOtherStockInItemService.lambdaQuery().eq(KingDeeOtherStockInItem::getBillNo, billNo).list();

        for (KingDeeOtherStockInItem kingDeeOtherStockInItem : kingDeeOtherStockInItems) {
            this.stockAdd(kingDeeOtherStockIn,kingDeeOtherStockInItem);

        }
    }


    @Transactional(rollbackFor = Exception.class)
    public void stockAdd(KingDeeOtherStockIn kingDeeOtherStockIn, KingDeeOtherStockInItem kingDeeOtherStockInItem) {

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

        if (stock == null){
            stock = new Stock();

            stock.setStockQty(BigDecimal.ZERO);
            stock.setSyncTime(new Date());
            stock.setStockId(kingDeeOtherStockInItem.getStockId());
            stock.setMaterialId(kingDeeOtherStockInItem.getMaterialId());
            stock.setSupplyOrgId(kingDeeOtherStockInItem.getSupplyOrgId());



            stockCommonLogic.stockAccLog("其他入库单增加库存",stock,kingDeeOtherStockInItem.getQty(),kingDeeOtherStockInItem.getBillNo(),true,"其他入库单");

            stock.setStockQty(kingDeeOtherStockInItem.getQty());
            stock.insert();

            return;
        }

        stockCommonLogic.stockAccLog("其他入库单增加库存",stock,kingDeeOtherStockInItem.getQty(),kingDeeOtherStockInItem.getBillNo(),true,"其他入库单");

        stock.setStockQty(kingDeeOtherStockInItem.getQty().add(stock.getStockQty()));
        stock.updateById();

    }

    public KingDeeOtherStockInDetail detail(String id) {
        KingDeeOtherStockIn kingDeeOtherStockIn = kingDeeOtherStockInService.getById(id);

        KingDeeOtherStockInDetail kingDeeOtherStockInDetail = BeanUtil.toBean(kingDeeOtherStockIn, KingDeeOtherStockInDetail.class);

        List<KingDeeOtherStockInItem> kingDeeOtherStockInItems = kingDeeOtherStockInItemService.lambdaQuery().eq(KingDeeOtherStockInItem::getBillNo, id).list();

        kingDeeOtherStockInDetail.setKingDeeOtherStockInItems(kingDeeOtherStockInItems);


        return kingDeeOtherStockInDetail;
    }
}
