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

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.invoice.StockStatusBean;
import com.zfire.mall.manager.bean.k3.RequestApiBean;
import com.zfire.mall.manager.bean.k3.StockPositionStock;
import com.zfire.mall.manager.bean.sale.SaleOrderBean;
import com.zfire.mall.manager.bean.stock.*;
import com.zfire.mall.manager.commonmapper.stock.StockCommonMapper;
import com.zfire.mall.manager.constant.KingDeeCons;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.logic.k3.K3Logic;
import com.zfire.mall.manager.logic.k3.bill.SalesReturnLogic;
import com.zfire.mall.manager.plus.entity.*;
import com.zfire.mall.manager.plus.service.*;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
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 java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Slf4j
@Service
public class StockOutInLogic {

    @Autowired
    StockCommonMapper stockCommonMapper;

    @Autowired
    KingDeePurchaseStockOutService kingDeePurchaseStockOutService;


    @Autowired
    KingDeePurchaseStockInService kingDeePurchaseStockInService;

    @Autowired
    KingDeePurchaseStockInItemService kingDeePurchaseStockInItemService;

    @Autowired
    KingDeePurchaseStockOutItemService kingDeePurchaseStockOutItemService;

    @Autowired
    KingDeeOtherStockInService kingDeeOtherStockInService;

    @Autowired
    KingDeeOtherStockInItemService kingDeeOtherStockInItemService;

    @Autowired
    KingDeeOtherStockOutService kingDeeOtherStockOutService;

    @Autowired
    KingDeeOtherStockOutItemService kingDeeOtherStockOutItemService;

    @Autowired
    SalesReturnLogic salesReturnLogic;

    @Autowired
    StockService stockService;


    public IPage<KingDeePurchaseStockOutBean> listPurchaseOrderOut(String supplierName,List<String> correspondId, String startTime, String endTime, String materialOldNumber,
                                                                   String materialName, String materialNumber, String specification, String billNo, Integer pageNum, Integer pageSize) {
        IPage<KingDeePurchaseStockOutBean> list = stockCommonMapper.listPurchaseOrderOut(new Page<>(pageNum, pageSize), supplierName,materialOldNumber, billNo,
                 materialNumber, correspondId, specification, materialName,
                startTime, endTime);
        return list;
    }

    public IPage<KingDeeOtherStockOutBean> listOtherStockOut(List<String> correspondId, String startTime, String endTime, String materialOldNumber, String materialName, String materialNumber, String specification, String billNo, Integer pageNum, Integer pageSize) {
        IPage<KingDeeOtherStockOutBean> list = stockCommonMapper.listOtherStockOut(new Page<>(pageNum, pageSize), materialOldNumber, billNo,
                materialNumber, correspondId, specification, materialName,
                startTime, endTime);
        return list;
    }

    public IPage<KingDeeOtherStockInBean> listOtherStockIn(List<String> correspondId, String startTime, String endTime, String materialOldNumber, String materialName, String materialNumber, String specification, String billNo, Integer pageNum, Integer pageSize) {
        IPage<KingDeeOtherStockInBean> list = stockCommonMapper.listOtherStockIn(new Page<>(pageNum, pageSize), materialOldNumber, billNo,
                materialNumber, correspondId, specification, materialName,
                startTime, endTime);
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    public void trialApprovalPurchaseOrderOut(String billNo,String id) {
        List<KingDeePurchaseStockOutItem> list = kingDeePurchaseStockOutItemService.lambdaQuery().eq(KingDeePurchaseStockOutItem::getBillNo, billNo).list();
        for (KingDeePurchaseStockOutItem kingDeePurchaseStockOutItem : list) {
            Stock one = stockService.lambdaQuery().eq(Stock::getStockId, kingDeePurchaseStockOutItem.getStockId())
                    .eq(Stock::getMaterialId, kingDeePurchaseStockOutItem.getMaterialId()).last("limit 1").one();
            one.setStockQty(one.getStockQty().add(kingDeePurchaseStockOutItem.getRmRealQty()));
            one.updateById();
        }
        kingDeePurchaseStockOutService.removeById(id);
        kingDeePurchaseStockOutItemService.lambdaUpdate().eq(KingDeePurchaseStockOutItem::getBillNo, billNo).remove();
        salesReturnLogic.unAuditByPurchaseStockOutBillNo(billNo);
    }

    @Transactional(rollbackFor = Exception.class)
    public void trialApprovalPurchaseOrderIn(String id, String billNo) {
        List<KingDeePurchaseStockInItem> list = kingDeePurchaseStockInItemService.lambdaQuery().eq(KingDeePurchaseStockInItem::getBillNo, billNo).list();
        for (KingDeePurchaseStockInItem kingDeePurchaseStockOutItem : list) {
            Stock one = stockService.lambdaQuery().eq(Stock::getStockId, kingDeePurchaseStockOutItem.getStockId())
                    .eq(Stock::getMaterialId, kingDeePurchaseStockOutItem.getMaterialId()).last("limit 1").one();
            one.setStockQty(one.getStockQty().subtract(kingDeePurchaseStockOutItem.getRealQty()));
            one.updateById();
        }
        kingDeePurchaseStockInService.removeById(id);
        kingDeePurchaseStockInItemService.lambdaUpdate().eq(KingDeePurchaseStockInItem::getBillNo, billNo).remove();
        salesReturnLogic.unAuditByPurchaseStockInBillNo(billNo);
    }

    @Transactional(rollbackFor = Exception.class)
    public void trialApprovalOtherStockIn(String id, String billNo) {
        List<KingDeeOtherStockInItem> list = kingDeeOtherStockInItemService.lambdaQuery().eq(KingDeeOtherStockInItem::getBillNo, billNo).list();
        for (KingDeeOtherStockInItem kingDeePurchaseStockOutItem : list) {
            Stock one = stockService.lambdaQuery().eq(Stock::getStockId, kingDeePurchaseStockOutItem.getStockId())
                    .eq(Stock::getMaterialId, kingDeePurchaseStockOutItem.getMaterialId()).last("limit 1").one();
            one.setStockQty(one.getStockQty().subtract(kingDeePurchaseStockOutItem.getQty()));
            one.updateById();
        }
        kingDeeOtherStockInService.removeById(id);
        kingDeeOtherStockInItemService.lambdaUpdate().eq(KingDeeOtherStockInItem::getBillNo, billNo).remove();
        salesReturnLogic.unAuditByOtherStockInBillNo(billNo);
    }

    public void trialApprovalOtherStockOut(String id, String billNo) {
        List<KingDeeOtherStockOutItem> list = kingDeeOtherStockOutItemService.lambdaQuery().eq(KingDeeOtherStockOutItem::getBillNo, billNo).list();
        for (KingDeeOtherStockOutItem kingDeePurchaseStockOutItem : list) {
            Stock one = stockService.lambdaQuery().eq(Stock::getStockId, kingDeePurchaseStockOutItem.getStockId())
                    .eq(Stock::getMaterialId, kingDeePurchaseStockOutItem.getMaterialId()).last("limit 1").one();
            one.setStockQty(one.getStockQty().add(kingDeePurchaseStockOutItem.getQty()));
            one.updateById();
        }
        kingDeeOtherStockOutService.removeById(id);
        kingDeeOtherStockOutItemService.lambdaUpdate().eq(KingDeeOtherStockOutItem::getBillNo, billNo).remove();
        salesReturnLogic.unAuditByOtherStockOutBillNo(billNo);
    }

    public IPage<KingDeePurchaseStockOutVO> listPurchaseOrderOutV2(ZfireParamBean zfireParamBean) {
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        IPage<KingDeePurchaseStockOutVO> listStockToDayCustomerV2 = stockCommonMapper.listPurchaseOrderOutV2(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
        return listStockToDayCustomerV2;
    }

    public IPage<KingDeeOtherStockOutVO> listOtherStockOutV2(ZfireParamBean zfireParamBean) {
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        IPage<KingDeeOtherStockOutVO> listStockToDayCustomerV2 = stockCommonMapper.listOtherStockOutV2(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
        return listStockToDayCustomerV2;
    }

    public IPage<KingDeeOtherStockInVO> listOtherStockInV2(ZfireParamBean zfireParamBean) {
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        IPage<KingDeeOtherStockInVO> listStockToDayCustomerV2 = stockCommonMapper.listOtherStockInV2(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
        return listStockToDayCustomerV2;
    }
}
