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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.PoySaleCountBean;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.front.CustomerOrderImport;
import com.zfire.mall.manager.bean.front.CustomerStockBean;
import com.zfire.mall.manager.bean.k3.K3CustomerBean;
import com.zfire.mall.manager.bean.k3.RequestApiBean;
import com.zfire.mall.manager.bean.k3.bill.*;
import com.zfire.mall.manager.bean.k3.bill.result.BillItem;
import com.zfire.mall.manager.bean.k3.bill.result.ResultEntity;
import com.zfire.mall.manager.bean.sale.*;
import com.zfire.mall.manager.commonmapper.CommonCountMapper;
import com.zfire.mall.manager.commonmapper.InvoiceMapper;
import com.zfire.mall.manager.commonmapper.SaleOrderMapper;
import com.zfire.mall.manager.constant.Constant;
import com.zfire.mall.manager.constant.KingDeeCons;
import com.zfire.mall.manager.enums.ExamineStatusEnum;
import com.zfire.mall.manager.enums.InvoiceCustomerEnum;
import com.zfire.mall.manager.enums.OrderTypeStatusEnum;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.logic.customer.CustomerProductTypeLogic;
import com.zfire.mall.manager.logic.invoice.InvoiceLogic;
import com.zfire.mall.manager.logic.k3.K3CustomerLogic;
import com.zfire.mall.manager.logic.k3.K3GeJiangLogic;
import com.zfire.mall.manager.logic.k3.K3Logic;
import com.zfire.mall.manager.logic.k3.K3MaterialLogic;
import com.zfire.mall.manager.logic.k3.bill.GeJiangInStockLogic;
import com.zfire.mall.manager.logic.k3.bill.SalesOrderLogic;
import com.zfire.mall.manager.logic.k3.bill.SalesOutStockLogic;
import com.zfire.mall.manager.logic.k3.bill.SalesReturnLogic;
import com.zfire.mall.manager.logic.retail.RetailOrderLogic;
import com.zfire.mall.manager.logic.stock.StockCommonLogic;
import com.zfire.mall.manager.logic.wallet.WalletLogic;
import com.zfire.mall.manager.plus.entity.*;
import com.zfire.mall.manager.plus.mapper.SalesOrderMapper;
import com.zfire.mall.manager.plus.service.*;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 销售出库单
 *
 * @author Administrator
 */
@Slf4j
@Service
public class SaleCodeLogic {


    @Autowired
    CodeManagerTempService codeManagerTempService;

    @Autowired
    CodeManagerService codeManagerService;

    @Autowired
    SaleOrderMapper saleOrderMapper;

    @Transactional(rollbackFor = Exception.class)
    public void importData(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();

        ExcelReader reader = ExcelUtil.getReader(inputStream);
        reader.addHeaderAlias("出货单号", "shipmentId");
        reader.addHeaderAlias("行号", "lineNumber");
        reader.addHeaderAlias("订单号", "orderId");
        reader.addHeaderAlias("货主编号", "ownerNo");
        reader.addHeaderAlias("收货客户", "receivingName");
        reader.addHeaderAlias("物料编号", "materialCode");
        reader.addHeaderAlias("物料型号", "specification");
        reader.addHeaderAlias("条码编号", "barcodeNo");
        reader.addHeaderAlias("扫描类", "scan");
        reader.addHeaderAlias("数量", "qty");
        reader.addHeaderAlias("单位", "unit");
        reader.addHeaderAlias("扫描人", "scannerName");
        reader.addHeaderAlias("扫描时间", "scanTime");
        reader.addHeaderAlias("扫描网点", "scanDot");

        List<CodeManagerTemp> codeManagerTemps = reader.readAll(CodeManagerTemp.class);

        String batch = IdWorker.getIdStr();

        for (CodeManagerTemp customerOrderImport : codeManagerTemps) {
            customerOrderImport.setBatch(batch);
        }

        codeManagerTempService.saveBatch(codeManagerTemps);

        saleOrderMapper.insertCode(batch);
    }

    /**
     * 出库条码列表
     * @param page
     * @param zfireParam
     * @return
     */
    public IPage<CodeManagerBean> list(Page page, ZfireParamBean zfireParam) {

        IPage<CodeManagerBean> codeManagerBeanIPage = saleOrderMapper.listCode(page, zfireParam);
        return codeManagerBeanIPage;
    }
}
