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

import cn.hutool.core.date.DateUtil;
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.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.listV2.LogisticsConfigurationVO;
import com.zfire.mall.manager.bean.stock.StockVO;
import com.zfire.mall.manager.commonmapper.CommonCountMapper;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.common.CommonLogic;

import com.zfire.mall.manager.plus.entity.*;
import com.zfire.mall.manager.plus.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author change
 */
@Service
@Slf4j
public class LogisticsLogic {


    @Autowired
    CommonLogic commonLogic;

    @Autowired
    LogisticsConfigurationService logisticsConfigurationService;


    @Autowired
    CommonCountMapper commonCountMapper;


    public void add(LogisticsConfiguration logisticsConfiguration, HttpServletRequest request) {
        if (logisticsConfigurationService.lambdaQuery().eq(LogisticsConfiguration::getLogisticsCompany,logisticsConfiguration.getLogisticsCompany()).count() > 0){
            throw new RemoteServiceException("该物流公司已存在");
        }
        logisticsConfiguration.insert();
    }

    public void update(LogisticsConfiguration logisticsConfiguration, HttpServletRequest request) {
        if (logisticsConfigurationService.lambdaQuery()
                .eq(LogisticsConfiguration::getLogisticsCompany,logisticsConfiguration.getLogisticsCompany())
                .ne(LogisticsConfiguration::getId,logisticsConfiguration.getId())
                .count() > 0){
            throw new RemoteServiceException("该物流公司已存在");
        }
        logisticsConfiguration.updateById();
        commonLogic.saveLog(logisticsConfiguration.getId());
    }


    public LogisticsConfiguration detail(String id) {
        return logisticsConfigurationService.getById(id);
    }

    public IPage<LogisticsConfiguration> list(String logisticsCompany, String customerTel, Integer pageNum, Integer pageSize) {
        return logisticsConfigurationService.lambdaQuery()
                .eq(StringUtils.isNotEmpty(logisticsCompany),LogisticsConfiguration::getLogisticsCompany, logisticsCompany)
                .eq(StringUtils.isNotEmpty(customerTel),LogisticsConfiguration::getCustomerTel, customerTel)
                .orderByDesc(LogisticsConfiguration::getCreateTime)
                .page(new Page<>(pageNum, pageSize));

    }

    public void delete(List<String> id) {
        logisticsConfigurationService.removeByIds(id);
    }

    public ExcelData export(List<LogisticsConfiguration> records) {
        String[] titles = new String[]{"物流公司名称", "物流公司代码", "提货人", "提货人电话", "客服电话","创建人", "创建时间","更新人","更新时间"};
        List<List<Object>> rows = new ArrayList<>();
        for (LogisticsConfiguration adminUser : records) {
            List<Object> row = new ArrayList<>();
            row.add(adminUser.getLogisticsCompany());
            row.add(adminUser.getLogisticsNumber());
            row.add(adminUser.getPickName());
            row.add(adminUser.getPickTel());
            row.add(adminUser.getCustomerTel());
            row.add(adminUser.getCreateBy());
            row.add(DateUtil.formatDate(adminUser.getCreateTime()));
            row.add(adminUser.getUpdateBy());
            row.add(DateUtil.formatDate(adminUser.getUpdateTime()));
            rows.add(row);
        }
        ExcelData excelData = new ExcelData();
        excelData.setRows(rows);
        excelData.setTitles(Arrays.asList(titles));
        excelData.setName("sheet1");
        return excelData;
    }


    public IPage<LogisticsConfigurationVO> listV2(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        IPage<LogisticsConfigurationVO> list = commonCountMapper.listLogisticsConfigurationV2(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()),zfireParamBean);
        return list;

    }
}
