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


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.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.front.*;
import com.zfire.mall.manager.bean.pick.PickManagerBeanVO;
import com.zfire.mall.manager.commonmapper.front.CustomerFrontMapper;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.plus.entity.CustomerCode;
import com.zfire.mall.manager.plus.entity.CustomerCodeCheck;
import com.zfire.mall.manager.plus.entity.KingDeeCustomer;
import com.zfire.mall.manager.plus.service.CustomerCodeCheckService;
import com.zfire.mall.manager.plus.service.CustomerCodeService;
import com.zfire.mall.manager.plus.service.KingDeeCustomerService;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CustomerCodeLogic {

    @Autowired
    CustomerFrontMapper customerFrontMapper;

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    CustomerCodeService customerCodeService;

    @Autowired
    CustomerCodeCheckService customerCodeCheckService;

    @Autowired
    KingDeeCustomerService kingDeeCustomerService;

    public IPage<CustomerCodeVO> listV2(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> customers=new ArrayList<>();
        if (adminUser.getIsCustomer()) {
            KingDeeCustomer kingDeeCustomer = kingDeeCustomerService.getById(adminUser.getCustomerId());
            customers.add(kingDeeCustomer.getName());
            if (kingDeeCustomer.getJiaxianType().equals("KING")){
                List<KingDeeCustomer> list = kingDeeCustomerService.lambdaQuery().eq(KingDeeCustomer::getHigherPrantNumber, adminUser.getCustomerNumber()).list();
                if (CollectionUtils.isNotEmpty(list)){
                    list.stream().forEach(item -> customers.add(item.getName()));
                }
            }
        }
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        IPage<CustomerCodeVO> customerCodeVOIPage = customerFrontMapper.listV2(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()),
                zfireParamBean,customers);
        return customerCodeVOIPage;
    }

    public IPage<CustomerCodeCustomerVO> listCustomer(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> customers=new ArrayList<>();
        if (adminUser.getIsCustomer()) {
            KingDeeCustomer kingDeeCustomer = kingDeeCustomerService.getById(adminUser.getCustomerId());
            customers.add(kingDeeCustomer.getName());
            if (kingDeeCustomer.getJiaxianType().equals("KING")){
                List<KingDeeCustomer> list = kingDeeCustomerService.lambdaQuery().eq(KingDeeCustomer::getHigherPrantNumber, adminUser.getCustomerNumber()).list();
                if (CollectionUtils.isNotEmpty(list)){
                    list.stream().forEach(item -> customers.add(item.getName()));
                }
            }
        }
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        IPage<CustomerCodeCustomerVO> customerCodeVOIPage = customerFrontMapper.listCustomer(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()),
                zfireParamBean,customers);
        return customerCodeVOIPage;
    }

    public void importData(InputStream inputStream, HttpServletRequest request) {
        //读取excel数据
        Long start, end;
        start = System.currentTimeMillis();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        reader.addHeaderAlias("条形码", "barCode");
        reader.addHeaderAlias("产品名称", "specification");
        reader.addHeaderAlias("出库采集时间(2023-04-10 14:00:00)", "consignerTime");
        reader.addHeaderAlias("销售公司", "salesCompany");
        reader.addHeaderAlias("收货地址", "address");
        reader.addHeaderAlias("发货日期(2023-04-10 14:00:00)", "deliveryTime");
        reader.addHeaderAlias("订单号", "orderNumber");

        List<CustomerCode> customerCodes = reader.readAll(CustomerCode.class);

        end = System.currentTimeMillis();
        log.info("Excel导入时长" + customerCodes.size() + "条数量，" + "时间" + (end - start) + "ms");
        //保存excel数据
        String batch = IdWorker.getIdStr();
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Date date = new Date();

        customerCodes.stream().forEach(item -> {
            item.setType("CUSTOMER");
            item.setSalesCompany(adminUser.getCustomerName());
            item.setSalesCompanyCode(adminUser.getCustomerNumber());

        });

        customerCodeService.saveBatch(customerCodes);

    }

    public IPage<CustomerCodeCustomerCheckVO> listCustomerCheck(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> customers=new ArrayList<>();
        if (adminUser.getIsCustomer()) {
            KingDeeCustomer kingDeeCustomer = kingDeeCustomerService.getById(adminUser.getCustomerId());
            customers.add(kingDeeCustomer.getName());
            if (kingDeeCustomer.getJiaxianType().equals("KING")){
                List<KingDeeCustomer> list = kingDeeCustomerService.lambdaQuery().eq(KingDeeCustomer::getHigherPrantNumber, adminUser.getCustomerNumber()).list();
                if (CollectionUtils.isNotEmpty(list)){
                    list.stream().forEach(item -> customers.add(item.getName()));
                }
            }
        }
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        IPage<CustomerCodeCustomerCheckVO> customerCodeVOIPage = customerFrontMapper.listCustomerCheck(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()),
                zfireParamBean,customers);
        return customerCodeVOIPage;
    }

    public void importCheck(InputStream inputStream, HttpServletRequest request) {
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        reader.addHeaderAlias("条码", "barCode");
        String batch = IdWorker.getIdStr();

        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<CustomerCodeCheck> customerCodes = reader.readAll(CustomerCodeCheck.class);
        for (CustomerCodeCheck customerCode : customerCodes) {
            customerCode.setBatch(batch);
            customerCode.setCustomerName(adminUser.getNickName());
            customerCode.setCustomerNumber(adminUser.getUserName());
            customerCode.setCustomerPhone(adminUser.getLinkPhone());
            customerCode.setSalesCompany(adminUser.getCustomerName());
            customerCode.setVerificationStatus(2);
        }
        customerCodeCheckService.saveBatch(customerCodes);
        customerFrontMapper.updateBatch(batch);
    }

    public IPage<CustomerCodeVO> listDaCang(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> customers=new ArrayList<>();
        if (adminUser.getIsCustomer()) {
            KingDeeCustomer kingDeeCustomer = kingDeeCustomerService.getById(adminUser.getCustomerId());
            customers.add(kingDeeCustomer.getName());
            if (kingDeeCustomer.getJiaxianType().equals("KING")){
                List<KingDeeCustomer> list = kingDeeCustomerService.lambdaQuery().eq(KingDeeCustomer::getHigherPrantNumber, adminUser.getCustomerNumber()).list();
                if (CollectionUtils.isNotEmpty(list)){
                    list.stream().forEach(item -> customers.add(item.getName()));
                }
            }
        }
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        IPage<CustomerCodeVO> customerCodeVOIPage = customerFrontMapper.listDaCang(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()),
                zfireParamBean,customers);
        return customerCodeVOIPage;
    }
}
