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


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.k3.*;
import com.zfire.mall.manager.commonmapper.manage.TransferMapper;
import com.zfire.mall.manager.constant.KingDeeCons;
import com.zfire.mall.manager.plus.entity.*;
import com.zfire.mall.manager.plus.service.KingDeePurchaseOrderItemService;
import com.zfire.mall.manager.plus.service.KingDeePurchaseOrderService;
import com.zfire.mall.manager.plus.service.KingDeeTransferItemService;
import com.zfire.mall.manager.plus.service.KingDeeTransferService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@Service
@Slf4j
public class K3PurchaseOrderLogic {

    @Autowired
    K3Logic k3Logic;

    @Autowired
    KingDeePurchaseOrderService kingDeePurchaseOrderService;

    @Autowired
    KingDeePurchaseOrderItemService kingDeePurchaseOrderItemService;

    @Autowired
    TransferMapper transferMapper;


//    public void allSync(String orgId, String orgNumber, String startTime, String endTime) throws Exception {
//        // 调用采购订单接口
//        RequestApiBean request = k3Logic.getInstance(orgId, orgNumber, KingDeeCons.PURCHASE_ORDER, startTime, endTime);
//        String filterStr = "FDocumentStatus = 'C' "+ k3Logic.supplySql("FApproveDate", request.getStartTime(), request.getEndTime());
//        request.setFieldKeys("FID,FBillNo,FBusinessType,FDate,FBillTypeID,FAssignSupplierId.FName,FPurchaseDeptId.FName,FPurchaserId.FName,FPayConditionId.FName,FApproverId.FName,FCreatorId.FName," +
//                "FChangerId.FName,FBillAllAmount,FBillAmount,FBillTaxAmount,FSettleCurrId.FName");
//        request.setFilterString(String.format(filterStr, orgNumber));
//        request.setOrderString("FID ASC");
//
//        String[] fields = new String[]{"id","billNo","businessType","forDate","billType","assignSupplierId","purchaseDeptId","purchaserId","payConditionId","approverName","creatorName",
//                "changerName","billAllAmount","billAmount","billTaxAmount","settleCurr"};
//        List<KingDeePurchaseOrder> objects = k3Logic.allSync(request, fields, KingDeePurchaseOrder.class);
//        if (!CollectionUtils.isEmpty(objects)) {
//            kingDeePurchaseOrderService.saveOrUpdateBatch(objects);
//        }
//        allSyncOrder(orgId,orgNumber,startTime,endTime);
//    }


//    public void allSyncOrder(String orgId, String orgNumber, String startTime, String endTime) throws Exception {
//        // 调用采购订单接口
//        RequestApiBean request = k3Logic.getInstance(orgId, orgNumber, KingDeeCons.PURCHASE_ORDER, startTime, endTime);
//        String filterStr = "FDocumentStatus = 'C' "+ k3Logic.supplySql("FApproveDate", request.getStartTime(), request.getEndTime());
//        request.setFieldKeys("FPOOrderEntry_FEntryID,FID,FMaterialId,FMaterialId.FName,FMaterialId.FNumber,FUnitId.FName,FQty,FPrice,FEntryAmount,FEntryNote,FAllAmount ," +
//                "FInvoiceQty,FInvoiceAmount,FModel,FEntryTaxAmount,FTaxPrice");
//        request.setFilterString(String.format(filterStr, orgNumber));
//        request.setOrderString("FID ASC");
//
//        String[] fields = new String[]{"id","fId","materialId","materialName","materialNumber","unit","qty","price","entryAmount","entryNote","allAmount","invoiceQty",
//                "invoiceAmount","model","entryTaxAmount","taxPrice"};
//        List<KingDeePurchaseOrderItem> objects = k3Logic.allSync(request, fields, KingDeePurchaseOrderItem.class);
//        if (Objects.nonNull(objects)) {
//            kingDeePurchaseOrderItemService.saveOrUpdateBatch(objects);
//        }
//    }
//
    public IPage<KingDeePurchaseOrderBean> list(String businessType, String assignSupplierId, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        return transferMapper.listPurchase(new Page<>(pageNum,pageSize),businessType,assignSupplierId,startTime,endTime);
    }

    public KingDeePurchaseOrderData detail(String id) {
        return transferMapper.detailPurchase(id);
    }

    /**
     * 采购单数据导出
     */
    public ExcelData export(List<KingDeePurchaseOrderBean> kingDeePurchaseOrderBeanList){
        String[] titles = new String[]{"业务类型","采购日期","订单编号","供应商","部门","业务员",
                "付款条件","备注","存货编码","存货名称","规格型号","主计量","数量","原币含税单价",
                "原币单价","原币税额","原币价税合计","采购单价","制单人","审核人","变更人"};
        List<List<Object>> rows = new ArrayList<>();
        for (KingDeePurchaseOrderBean kingDeePurchaseOrderBean : kingDeePurchaseOrderBeanList) {
            List<Object> row = new ArrayList<>();
            row.add(kingDeePurchaseOrderBean.getBusinessType());
            row.add(kingDeePurchaseOrderBean.getFDate());
            row.add(kingDeePurchaseOrderBean.getBillNo());
            row.add(kingDeePurchaseOrderBean.getAssignSupplierId());
            row.add(kingDeePurchaseOrderBean.getPurchaseDeptId());
            row.add(kingDeePurchaseOrderBean.getPurchaserId());
            row.add(kingDeePurchaseOrderBean.getPayConditionId());
            row.add(kingDeePurchaseOrderBean.getSettleCurr());
            row.add(kingDeePurchaseOrderBean.getEntryNote());
            row.add(kingDeePurchaseOrderBean.getMaterialNumber());
            row.add(kingDeePurchaseOrderBean.getMaterialName());
            row.add(kingDeePurchaseOrderBean.getModel());
            row.add(kingDeePurchaseOrderBean.getUnit());
            row.add(kingDeePurchaseOrderBean.getQty());
            row.add(kingDeePurchaseOrderBean.getTaxPrice());
            row.add(kingDeePurchaseOrderBean.getPrice());
            row.add(kingDeePurchaseOrderBean.getEntryTaxAmount());
            row.add(kingDeePurchaseOrderBean.getBillAllAmount());
            row.add(kingDeePurchaseOrderBean.getPrice());
            row.add(kingDeePurchaseOrderBean.getCreateBy());
            row.add(kingDeePurchaseOrderBean.getApproverName());
            row.add(kingDeePurchaseOrderBean.getChangerName());
            rows.add(row);
        }
        ExcelData excelData = new ExcelData();
        excelData.setRows(rows);
        excelData.setTitles(Arrays.asList(titles));
        return excelData;
    }

}
