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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.listV2.OrderPlanCustomerVO;
import com.zfire.mall.manager.bean.pick.PickManagerBean;
import com.zfire.mall.manager.bean.pick.PickManagerRequest;
import com.zfire.mall.manager.bean.ship.ShipDocumentDetail;
import com.zfire.mall.manager.commonmapper.CommonCountMapper;
import com.zfire.mall.manager.commonmapper.ShipMapper;
import com.zfire.mall.manager.constant.Constant;
import com.zfire.mall.manager.enums.SMSTypeEnum;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.common.SMSLogic;
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 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 org.springframework.util.CollectionUtils;

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

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

    @Autowired
    private OrderPlanService orderPlanService;

    @Autowired
    private OrderPlanCustomerService orderPlanCustomerService;

    @Autowired
    private CommonCountMapper commonCountMapper;

    public IPage<OrderPlan> list(String billNo, String materialName, String materialNumber, String specification, String materialType, Integer pageNum, Integer pageSize) {
        return orderPlanService.lambdaQuery()
                .eq(StringUtils.isNotBlank(billNo),OrderPlan::getBillNo,billNo)
                .eq(StringUtils.isNotBlank(materialName),OrderPlan::getMaterialName,materialName)
                .eq(StringUtils.isNotBlank(materialNumber),OrderPlan::getMaterialNumber,materialNumber)
                .eq(StringUtils.isNotBlank(specification),OrderPlan::getSpecification,specification)
                .eq(StringUtils.isNotBlank(materialType),OrderPlan::getMaterialType,materialType)
                .page(new Page<>(pageNum,pageSize));

    }

    public OrderPlan detail(String id) {
        return orderPlanService.getById(id);
    }


    public IPage<OrderPlanCustomer> listCustomer(String billNo, String materialName, String materialNumber, String specification, String materialType, String customerName, String customerNumber, String saleName, Integer pageNum, Integer pageSize) {
        return orderPlanCustomerService.lambdaQuery()
                .eq(StringUtils.isNotBlank(billNo),OrderPlanCustomer::getBillNo,billNo)
                .eq(StringUtils.isNotBlank(materialName),OrderPlanCustomer::getMaterialName,materialName)
                .eq(StringUtils.isNotBlank(materialNumber),OrderPlanCustomer::getMaterialNumber,materialNumber)
                .eq(StringUtils.isNotBlank(specification),OrderPlanCustomer::getSpecification,specification)
                .eq(StringUtils.isNotBlank(materialType),OrderPlanCustomer::getMaterialType,materialType)
                .eq(StringUtils.isNotBlank(customerName),OrderPlanCustomer::getCustomerName,customerName)
                .eq(StringUtils.isNotBlank(customerNumber),OrderPlanCustomer::getCustomerNumber,customerNumber)
                .eq(StringUtils.isNotBlank(saleName),OrderPlanCustomer::getSaleName,saleName)
                .page(new Page<>(pageNum,pageSize));
    }

    public OrderPlanCustomer detailCustomer(String id) {
        return orderPlanCustomerService.getById(id);
    }

    public void submitPlan(List<String> ids, HttpServletRequest request) {
        List<OrderPlanCustomer> list = orderPlanCustomerService.lambdaQuery().eq(OrderPlanCustomer::getId, ids).list();
        List<OrderPlan> orderPlanList = BeanUtil.copyToList(list, OrderPlan.class);
        orderPlanList.forEach(item->{
            item.setId(null);
        });
        orderPlanService.saveBatch(orderPlanList);
    }


    /**
     * 经销商采购计划单导出
     * @param orderPlanCustomers
     * @return
     */
    public ExcelData export(List<OrderPlanCustomer> orderPlanCustomers){
        String[] titles = new String[]{"单据号","订单类型","销售类型","厂产品代码","产品名称","规格型号",
                                        "计量单位","数量","单价","金额","经销商编码","经销商名称"};
        List<List<Object>> rows = new ArrayList<>();
        for (OrderPlanCustomer directTransferList : orderPlanCustomers) {
            List<Object> row = new ArrayList<>();
            row.add(directTransferList.getBillNo());
            row.add(directTransferList.getOrderType());
            row.add(directTransferList.getSaleName());
            row.add(directTransferList.getMaterialNumber());
            row.add(directTransferList.getMaterialName());
            row.add(directTransferList.getSpecification());
            row.add(directTransferList.getUnit());
            row.add("");
            row.add(directTransferList.getPrice());
            row.add(directTransferList.getAmount());
            row.add(directTransferList.getCustomerNumber());
            row.add(directTransferList.getCustomerName());
            rows.add(row);
        }
        ExcelData excelData = new ExcelData();
        excelData.setRows(rows);
        excelData.setTitles(Arrays.asList(titles));
        return excelData;
    }

    /**
     * 采购计划单导出
     */
    public ExcelData cExport(List<OrderPlan> orderPlans){
        String[] titles = new String[]{"单据号","销售类型","厂产品代码","产品名称","规格型号","计量单位",
                "数量","单价","金额","状态"};
        List<List<Object>> rows = new ArrayList<>();
        for (OrderPlan orderPlan : orderPlans) {
            List<Object> row = new ArrayList<>();
            row.add(orderPlan.getBillNo());
            row.add(orderPlan.getSaleName());
            row.add(orderPlan.getMaterialNumber());
            row.add(orderPlan.getMaterialName());
            row.add(orderPlan.getSpecification());
            row.add(orderPlan.getUnit());
            row.add("");
            row.add(orderPlan.getPrice());
            row.add(orderPlan.getAmount());
            row.add(orderPlan.getStatus());
            rows.add(row);
        }
        ExcelData excelData = new ExcelData();
        excelData.setRows(rows);
        excelData.setTitles(Arrays.asList(titles));
        return excelData;
    }

    public IPage<OrderPlanCustomerVO> listCustomerV2(ZfireParamBean zfireParamBean) {
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        return commonCountMapper.listCustomerV2(new Page(zfireParamBean.getPageNum(),zfireParamBean.getPageSize()),zfireParamBean);
    }
}
