package com.foreverwin.mesnac.dataimport.handler;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mesnac.dataimport.client.InventoryWSClient;
import com.foreverwin.mesnac.dataimport.handler.base.BaseHandler;
import com.foreverwin.mesnac.dataimport.reader.FileReader;
import com.foreverwin.mesnac.dataimport.reader.FileReaderBuilder;
import com.foreverwin.mesnac.dataimport.reader.RowVisitor;
import com.foreverwin.mesnac.dataimport.service.MasterImportLogService;
import com.foreverwin.mesnac.dataimport.service.MasterObjectDefine;
import com.foreverwin.mesnac.dataimport.util.AppUtil;
import com.foreverwin.mesnac.dataimport.util.StringUtils;
import com.foreverwin.modular.core.util.I18nUtil;
import com.sap.me.inventory.Inventory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;
import java.util.Map;

public class InventoryHandler extends BaseHandler {
    @Autowired
    MasterImportLogService logService;

    @Override
    public String importFile(String site, InputStream inputStream, String fileType, String mode) {
        String transId = StringUtils.getTimeString() + "_" + RandomUtil.randomNumbers(8);
        try {
            FileReader fileReader = new FileReaderBuilder().build(fileType);
            RowVisitor<JSONObject> visitor = new RowVisitor<JSONObject>() {
                @Override
                public int visit(long index, JSONObject jsonObject) {
                    try {
                        if (jsonObject.containsKey("id") && jsonObject.getString("id").trim().length() > 0) {
                            Object[] params = new Object[10];
                            params[0] = index;
                            params[1] = jsonObject.getString("id");
                            jsonObject.put("site" , site);
                            Inventory result = InventoryWSClient.find(jsonObject);
                            boolean insert = false;
                            String resultMessage = null;
                            //存在值，做更新，
                            if (result != null && result.getInventoryId() != null && result.getInventoryId().length() > 0) {
                                //return result;
                                if (AppUtil.canUpdate(mode)) {
                                    resultMessage = InventoryWSClient.validateAndUpdate(jsonObject, result);
                                    if (resultMessage == null) {
                                        logService.logMessage(transId, index, I18nUtil.getI18nText("MaterData.import.updateinfo.inventory" , params));
                                        return 1;
                                    }
                                }
                            } else {//做插入
                                if (AppUtil.canInsert(mode)) {
                                    resultMessage = InventoryWSClient.validateAndAdd(jsonObject);
                                    if (resultMessage == null) {
                                        logService.logMessage(transId, index, I18nUtil.getI18nText("MaterData.import.createinfo.inventory" , params));
                                        return 1;
                                    }
                                    insert = true;
                                } else if (AppUtil.canUpdate(mode)) {
                                    resultMessage = I18nUtil.getI18nText("MaterData.import.upDateObjectNotExist");
                                }
                            }
                            if (resultMessage != null) {
                                params[2] = resultMessage;
                                if(insert){
                                    logService.logMessage(transId, index, I18nUtil.getI18nText("MaterData.import.createfail.inventory" , params));
                                }else{
                                    logService.logMessage(transId, index, I18nUtil.getI18nText("MaterData.import.updatefail.inventory" , params));
                                }
                            }
                        }
                    } catch (Exception e) {
                        logService.logMessage(transId, index, "Import Error  at WebService->" + e.getMessage());
                    }
                    //System.out.println(jsonObject);
                    return 0;
                }

                @Override
                public String[] getHeader() {
                    return null;
                    //return new String[] { "partQual", "partQualDesc", "dataType", "shortCode", "ShortCodeDesc" };
                }
            };
            int row = fileReader.visitor(visitor).read(inputStream, getHeaders());
            logService.logMessage(transId, 99999, I18nUtil.getI18nText("MaterData.import.sumary" , new Object[]{row}));
        } catch (Exception e) {
            logService.logMessage(transId, -1, "Import Error  at WebService->" + e.getMessage());
        }
        //get transinfo by transID
        String message = logService.queryMessage(transId);
        return message;
    }

    @Override
    public Map<String, String> getHeaders() {
        return MasterObjectDefine.getHeadsMapping("INVENTORY");
    }
}
