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


import cn.hutool.core.date.DateUtil;
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.k3.*;
import com.zfire.mall.manager.commonmapper.manage.TransferMapper;
import com.zfire.mall.manager.constant.KingDeeCons;
import com.zfire.mall.manager.plus.entity.KingDeePurchaseOrder;
import com.zfire.mall.manager.plus.entity.KingDeePurchaseOrderItem;
import com.zfire.mall.manager.plus.entity.KingDeePurchaseStockIn;
import com.zfire.mall.manager.plus.entity.KingDeePurchaseStockInItem;
import com.zfire.mall.manager.plus.service.KingDeePurchaseOrderItemService;
import com.zfire.mall.manager.plus.service.KingDeePurchaseOrderService;
import com.zfire.mall.manager.plus.service.KingDeePurchaseStockInItemService;
import com.zfire.mall.manager.plus.service.KingDeePurchaseStockInService;
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 K3PurchaseStockInLogic {

    @Autowired
    K3Logic k3Logic;

    @Autowired
    KingDeePurchaseStockInService kingDeePurchaseStockInService;

    @Autowired
    KingDeePurchaseStockInItemService kingDeePurchaseStockInItemService;

    @Autowired
    TransferMapper transferMapper;


//    public void allSync(String orgId, String orgNumber, String startTime, String endTime) throws Exception {
//        // 调用入库单接口
//        RequestApiBean request = k3Logic.getInstance(orgId, orgNumber, KingDeeCons.PURCHASE_IN_STOCK, startTime, endTime);
//        String filterStr = "FDocumentStatus = 'C' "+ k3Logic.supplySql("FApproveDate", request.getStartTime(), request.getEndTime());
//        request.setFieldKeys("FID,FBillNo,FDate,FStockOrgId,FDemandOrgId,FPurchaseOrgId,FSupplierId,FStockDeptId,FApproverId.FName,FApproveDate,FCancellerId,FCancelDate," +
//                "FSupplyId.FName");
//        request.setFilterString(String.format(filterStr, orgNumber));
//        request.setOrderString("FID ASC");
//
//        String[] fields = new String[]{"id","billNo","fDate","stockOrgId","demandOrgId","purchaseOrgId","supplierId","stockDeptId","approverId","approveDate","cancellerId","cancelDate",
//                "supplyName"};
//        List<KingDeePurchaseStockIn> objects = k3Logic.allSync(request, fields, KingDeePurchaseStockIn.class);
//        if (!CollectionUtils.isEmpty(objects)) {
//            kingDeePurchaseStockInService.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_IN_STOCK, startTime, endTime);
//        String filterStr = "FDocumentStatus = 'C' "+ k3Logic.supplySql("FApproveDate", request.getStartTime(), request.getEndTime());
//        request.setFieldKeys("FInStockEntry_FEntryID,FID,FBillNo,FMaterialId,FMaterialId.FNumber,FMaterialId.FName,FStockId.FName,FNote,FMustQty,FRealQty,FUnitId.FName,FAuxUnitQty,FPrice,FEntryTaxRate" +
//                ",FAmount,FTaxPrice,FEntryTaxAmount,FAllAmount,FUOM");
//        request.setFilterString(String.format(filterStr, orgNumber));
//        request.setOrderString("FID ASC");
//
//        String[] fields = new String[]{"id","fId","billNo","materialId","materialNumber","materialName","stockId","note","mustQty","realQty","unit","auxUnitQty","price","entryTaxRate",
//                "amount","taxPrice","entryTaxAmount","allAmount","uom"};
//        List<KingDeePurchaseStockInItem> objects = k3Logic.allSync(request, fields, KingDeePurchaseStockInItem.class);
//        if (Objects.nonNull(objects)) {
//            kingDeePurchaseStockInItemService.saveOrUpdateBatch(objects);
//        }
//    }

    public IPage<KingDeePurchaseStockInBean> list(String materialCode,String billNo, String materialOldNumber,String materialName, String materialNumber, String supplyName, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        return transferMapper.listStockIn(new Page<>(pageNum,pageSize),materialCode,billNo,materialName,materialNumber,supplyName,startTime,endTime,materialOldNumber);
    }

    public KingDeePurchaseStockInData detail(String id) {
        return transferMapper.detailStockIn(id);
    }

    /**
     * 采购入库单
     * @param kingDeePurchaseStockInBeanList
     * @return
     */
    public ExcelData export(List<KingDeePurchaseStockInBean> kingDeePurchaseStockInBeanList){
        String[] titles = new String[]{"入库单号","入库日期","仓库","供货单位","审核日期",
                "备注","存货编码","存货名称","规格型号","主计量","数量",
                "原币含税单价","原币金额","原币税额","原币价税合计","税率","制单人","审核人"};
        List<List<Object>> rows = new ArrayList<>();
        for (KingDeePurchaseStockInBean kingDeePurchaseStockInBean : kingDeePurchaseStockInBeanList) {
            List<Object> row = new ArrayList<>();
            row.add(kingDeePurchaseStockInBean.getBillNo());
            row.add(DateUtil.formatDate(kingDeePurchaseStockInBean.getFDate()));
            row.add(kingDeePurchaseStockInBean.getStockId());
            row.add(kingDeePurchaseStockInBean.getSupplyName());
            row.add(DateUtil.formatDate(kingDeePurchaseStockInBean.getApproveDate()));
            //备注
            row.add(kingDeePurchaseStockInBean.getNote());
            row.add(kingDeePurchaseStockInBean.getMaterialNumber());
            row.add(kingDeePurchaseStockInBean.getMaterialName());
            row.add(kingDeePurchaseStockInBean.getUom());
            row.add(kingDeePurchaseStockInBean.getUnit());
            row.add(kingDeePurchaseStockInBean.getAuxUnitQty());
            row.add(kingDeePurchaseStockInBean.getTaxPrice());
            row.add(kingDeePurchaseStockInBean.getAmount());
            row.add(kingDeePurchaseStockInBean.getEntryTaxAmount());
            row.add(kingDeePurchaseStockInBean.getAllAmount());
            row.add(kingDeePurchaseStockInBean.getEntryTaxRate());
            row.add(kingDeePurchaseStockInBean.getCreateBy());
            row.add(kingDeePurchaseStockInBean.getApproverId());
            rows.add(row);
        }
        ExcelData excelData = new ExcelData();
        excelData.setRows(rows);
        excelData.setTitles(Arrays.asList(titles));
        return excelData;
    }


}
