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


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.k3.KingDeePurchaseStockInBean;
import com.zfire.mall.manager.bean.k3.KingTransfer;
import com.zfire.mall.manager.bean.k3.KingTransferBean;
import com.zfire.mall.manager.bean.k3.RequestApiBean;
import com.zfire.mall.manager.bean.pick.PickManagerBean;
import com.zfire.mall.manager.commonmapper.manage.TransferMapper;
import com.zfire.mall.manager.constant.KingDeeCons;
import com.zfire.mall.manager.plus.entity.KingDeeSupplier;
import com.zfire.mall.manager.plus.entity.KingDeeTransfer;
import com.zfire.mall.manager.plus.entity.KingDeeTransferItem;
import com.zfire.mall.manager.plus.service.KingDeeSupplierService;
import com.zfire.mall.manager.plus.service.KingDeeTransferItemService;
import com.zfire.mall.manager.plus.service.KingDeeTransferService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class K3TransferLogic {


    @Autowired
    K3Logic k3Logic;

    @Autowired
    KingDeeTransferService kingDeeTransferService;

    @Autowired
    KingDeeTransferItemService kingDeeTransferItemService;

    @Autowired
    TransferMapper transferMapper;


//    public void allSync(String orgId, String orgNumber, String startTime, String endTime) throws Exception {
//        // 调用调拨单接口
//        RequestApiBean request = k3Logic.getInstance(orgId, orgNumber, KingDeeCons.STOCK_TRANSFER, startTime, endTime);
//        String filterStr = "FDocumentStatus = 'C' "+ k3Logic.supplySql("FApproveDate", request.getStartTime(), request.getEndTime());
//        request.setFieldKeys("FID,FBillNo,FStockOrgId.FName,FStockOutOrgId.FName,FTransferBizType,FNote,FApproverId.FName,FCancellerId.FName,FBillTypeID.FName,FDocumentStatus,FDate,FApproveDate,FCancelDate");
//        request.setFilterString(String.format(filterStr, orgNumber));
//        request.setOrderString("FID ASC");
//
//        String[] fields = new String[]{"id","billNo","stockOrgId","stockOutOrgId","transferBizType","note","approverId","cancellerId","billType","documentStatus","fDate","approveDate","cancelDate"};
//        List<KingDeeTransfer> objects = k3Logic.allSync(request, fields, KingDeeTransfer.class);
//        syncTransferItem(orgId,orgNumber,startTime,endTime);
//        if (CollectionUtils.isNotEmpty(objects)) {
//            kingDeeTransferService.saveOrUpdateBatch(objects);
//        }
//
//    }

//    public void syncTransferItem(String orgId, String orgNumber, String startTime, String endTime) throws Exception {
//        // 调用调拨单物料接口
//        RequestApiBean request = k3Logic.getInstance(orgId, orgNumber, KingDeeCons.STOCK_TRANSFER, startTime, endTime);
//        String filterStr = "FDocumentStatus = 'C' "+ k3Logic.supplySql("FApproveDate", request.getStartTime(), request.getEndTime());
//        request.setFieldKeys("FBillEntry_FEntryID,FID,FBillNo,FMaterialId,FMaterialId.FNumber,FMaterialName,FSrcStockId.FName,FDestStockId.FName,FQty,FOwnerId,FOwnerOutId,FModel,FBaseUnitId.FName,FConsignAmount,FConsignPrice,FSrcStockLocId,FDestStockLocId,FAuxPropId");
//        request.setFilterString(String.format(filterStr, orgNumber));
//        request.setOrderString("FID ASC");
//
//        String[] fields = new String[]{"id","fId","billNo","materialId","materialNumber","materialName","srcStockId","destStockId","qty","ownerId","ownerOutId","model","baseUnitId","consignAmount","consignPrice","srcStockLocId","destStockLocId","auxPropId"};
//        List<KingDeeTransferItem> objects = k3Logic.allSync(request, fields, KingDeeTransferItem.class);
//        if (Objects.nonNull(objects)) {
//            kingDeeTransferItemService.saveOrUpdateBatch(objects);
//        }
//    }

    /**
     * 调拨单列表
     * @param billNo
     * @param materialName
     * @param startTime
     * @param endTime
     * @param pageNum
     * @param pageSize
     * @return
     */
    public IPage<KingTransfer> list(String billNo, String materialName, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        return transferMapper.list(new Page<>(pageNum,pageSize),billNo,materialName,startTime,endTime);
    }

    /**
     * 调拨单详情
     * @param id
     * @return
     */
    public KingTransferBean detail(String id) {
        return transferMapper.detail(id);
    }

    /**
     * 调拨单导出
     * @param kingTransferList
     * @return
     */
    public ExcelData export(List<KingTransfer> kingTransferList){
        String[] titles = new String[]{"单据编号","单据类型","调拨类型","申请日期","单据状态",
                "关闭状态","物料编码","物料名称","规格型号","辅助属性","单位",
                "申请数量","调出仓库","调入仓库"};
        List<List<Object>> rows = new ArrayList<>();
        for (KingTransfer kingTransfer: kingTransferList) {
            List<Object> row = new ArrayList<>();
            row.add(kingTransfer.getBillNo());
            row.add(kingTransfer.getBillType());
            row.add(kingTransfer.getTransferBizType());
            row.add(kingTransfer.getFDate());
            row.add(kingTransfer.getDocumentStatus());
            row.add(kingTransfer.getCancelStatus());
            row.add(kingTransfer.getMaterialNumber());
            row.add(kingTransfer.getMaterialName());
            row.add(kingTransfer.getModel());
            //row.add(kingTransfer.getAuxPropId());
            row.add(kingTransfer.getBaseUnitId());
            row.add(kingTransfer.getSrcStockName());
            row.add(kingTransfer.getDestStockName());
            rows.add(row);
        }
        ExcelData excelData = new ExcelData();
        excelData.setRows(rows);
        excelData.setTitles(Arrays.asList(titles));
        return excelData;
    }

}
