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

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.direct.DirectTransferList;
import com.zfire.mall.manager.bean.direct.DirectTransferOrderBean;
import com.zfire.mall.manager.commonmapper.retail.RetailOrderCommonMapper;
import com.zfire.mall.manager.plus.entity.DirectTransferOrder;
import com.zfire.mall.manager.plus.entity.DirectTransferOrderItem;
import com.zfire.mall.manager.plus.entity.KingDeeStock;
import com.zfire.mall.manager.plus.entity.StockCorrespond;
import com.zfire.mall.manager.plus.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DirectTransferLogic {

    @Autowired
    DirectTransferOrderService directTransferOrderService;
    @Autowired
    DirectTransferOrderItemService directTransferOrderItemService;
    @Autowired
    RetailOrderCommonMapper retailOrderCommonMapper;
    @Autowired
    StockCorrespondService stockCorrespondService;
    @Autowired
    StockService stockService;
    @Autowired
    KingDeeStockService kingDeeStockService;

    /**
     * 直调单列表
     */
    public IPage<DirectTransferList> list(String deliverNo,String orderNo, String enginRecordNo, String orderType,String customerName,
                                           String startTime,String endTime,Integer pageNum,Integer pageSize){
        IPage<DirectTransferList> page = retailOrderCommonMapper.queryDirectTransfer(new Page(pageNum, pageSize), deliverNo, orderNo, enginRecordNo, orderType, customerName, startTime, endTime);
        return page;
    }


    /**
     * 直调单详情
     * @param id
     */
    public DirectTransferOrderBean detail(String id){

        DirectTransferOrder directTransferOrder = directTransferOrderService.getById(id);
        DirectTransferOrderBean bean = new DirectTransferOrderBean();
        BeanUtils.copyProperties(directTransferOrder,bean);
        List<DirectTransferOrderItem> list = directTransferOrderItemService.lambdaQuery()
                .eq(DirectTransferOrderItem::getDirectTransferOrderId, id).list();
        bean.setItems(list);
        //查询仓库名称
        StockCorrespond stockCorrespond = stockCorrespondService.getById(directTransferOrder.getCorrespondId());
        if(stockCorrespond != null){
            bean.setCorrespondName(stockCorrespond.getName());
        }
        KingDeeStock kingDeeStock = kingDeeStockService.getById(directTransferOrder.getStockId());
        if(kingDeeStock != null){
            bean.setStockName(kingDeeStock.getName());
        }
        return bean;
    }


    /**
     * 直调单
     * @param directTransferLists
     * @return
     */
    public ExcelData export(List<DirectTransferList> directTransferLists){
        String[] titles = new String[]{"直接调拨单号","审核状态","单据日期","经销商编码","经销商名称",
                "调拨方向","厂产品代码","产品名称","规格型号","单位","调拨数量",
                "调出仓库","调出仓位","调入仓库","调入仓位","备注信息","制单人","制单日期","审核人","审核日期"};
        List<List<Object>> rows = new ArrayList<>();
        for (DirectTransferList directTransferList : directTransferLists) {
            List<Object> row = new ArrayList<>();
            row.add(directTransferList.getDeliverNo());
            //row.add(directTransferList.getExamineStatus());
           // row.add(directTransferList.getOrderTime());
           // row.add(directTransferList.getCustomerNumber());
            row.add(directTransferList.getCustomerName());
          //  row.add(directTransferList.getDirection());
           // row.add(directTransferList.getMaterialCode());
            row.add(directTransferList.getMaterialName());
            row.add(directTransferList.getSpecification());
            row.add(directTransferList.getUnit());
            row.add(directTransferList.getQty());
            row.add(directTransferList.getCorrespondName());
           // row.add(directTransferList.getStockName());
           // row.add(directTransferList.getInCorrespondName());
           // row.add(directTransferList.getInStockName());
            row.add(directTransferList.getRemark());
            row.add(directTransferList.getCreateBy());
            row.add(directTransferList.getCreateTime());
           // row.add(directTransferList.getApprovalName());
          //  row.add(directTransferList.getApprovalTime());

            rows.add(row);
        }
        ExcelData excelData = new ExcelData();
        excelData.setRows(rows);
        excelData.setTitles(Arrays.asList(titles));
        return excelData;
    }

}
