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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.engin.deposit.DepositManageRecordBean;
import com.zfire.mall.manager.bean.front.CustomerStockBean;
import com.zfire.mall.manager.bean.invoice.*;
import com.zfire.mall.manager.bean.k3.RequestApiBean;
import com.zfire.mall.manager.commonmapper.InvoiceMapper;
import com.zfire.mall.manager.commonmapper.InvoiceNewListMapper;
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.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.logic.customer.CustomerProductTypeLogic;
import com.zfire.mall.manager.logic.engin.deposit.DepositManageLogic;
import com.zfire.mall.manager.logic.k3.K3Logic;
import com.zfire.mall.manager.logic.k3.bill.GeJiangInStockLogic;
import com.zfire.mall.manager.logic.pick.PickLogic;
import com.zfire.mall.manager.logic.reserved.ReservedLogic;
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.service.*;
import com.zfire.mall.manager.utils.RedisUtil;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
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 javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 申请发货单
 *
 * @author change
 */
@Service
public class InvoiceNewListLogic {


    @Autowired
    CommonLogic commonLogic;

    @Autowired
    KingDeeDepartmentService kingDeeDepartmentService;

    @Autowired
    InvoiceNewListMapper invoiceNewListMapper;


    public IPage<InvoiceNewListBean> list(Page page, ZfireParamBean zfireParam) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        String customerId = adminUser.getIsCustomer() ? adminUser.getCustomerId() : "";

        Integer count= invoiceNewListMapper.listInvoiceHuiZongCount(zfireParam,customerId, adminUser.getServiceIds(), adminUser.getK3CategoryIds());

        IPage<InvoiceNewListBean> invoiceBeanIPage = invoiceNewListMapper.listInvoiceHuiZong(new Page<>(zfireParam.getPageNum(), zfireParam.getPageSize(), count, false),zfireParam,
                customerId, adminUser.getServiceIds(), adminUser.getK3CategoryIds());

        Map<String, String> kingDeeMap = kingDeeDepartmentService.lambdaQuery()
                .select(KingDeeDepartment::getNumber, KingDeeDepartment::getName).list()
                .stream().collect(Collectors.toMap(KingDeeDepartment::getNumber, KingDeeDepartment::getName));
        for (InvoiceNewListBean record : invoiceBeanIPage.getRecords()) {
            record.setK3OrgName(kingDeeMap.get(record.getK3OrgNumber()));
        }
        return invoiceBeanIPage;
    }
}
