//#import Util.js
//#import DateUtil.js
//#import excel.js
//#import $owl_orgs:services/modelService.jsx
//#import $owl_org_batch_update_state:services/modelService.jsx

(function () {

    var jBatchImport = owl_org_batch_update_stateService.get(batchId);

    var logMsg = "";
    var total = 0;
    var totalSuccess = 0;
    var totalFailed = 0;
    var beginTime = (new Date()).getTime();
    try {

        var jFile = jBatchImport.file;
        if (!jFile || jFile.length == 0 || !jFile[0].url) {
            logMsg = appendLog(logMsg, "缺少Excel文件");
            return;
        }
        var filePath = jFile[0].url;

        var jData = Excel.parse(filePath);
        if (!jData) {
            logMsg = appendLog(logMsg, "读取Excel文件失败");
            return;
        }
        if (!jData.sheets || jData.sheets.length == 0) {
            logMsg = appendLog(logMsg, "Excel中的sheet为空");
            return;
        }

        var sheet = jData.sheets[0];
        var rows = sheet.rows;

        var firstRow = rows[0];
        var firstRowCells = firstRow.cells;
        var maxCell = firstRowCells.length;
        if (maxCell > 100) {
            maxCell = 100;
        }

        var jTitleIndex = {};
        for (var i = 0; i < maxCell; i++) {
            var jCell = firstRowCells[i];
            if (jCell.value) {
                jTitleIndex[jCell.value] = jCell.columnIndex;
            }
        }

        for (var j = 1; j < rows.length; j++) {
            try {
                total++;

                var row = rows[j];
                var cells = row.cells;
                var merchantId = getCellValue(cells, jTitleIndex, "商家ID");
                var orgTypeName = getCellValue(cells, jTitleIndex, "组织类型");
                var merTypeName = getCellValue(cells, jTitleIndex, "商家类型");
                var deliveryModeName = getCellValue(cells, jTitleIndex, "配送方式");
                var certifyStateName = getCellValue(cells, jTitleIndex, "审核状态");
                var publishStateName = getCellValue(cells, jTitleIndex, "发布状态");
                var enableStateName = getCellValue(cells, jTitleIndex, "冻结状态");
                var productCanBuyStateName = getCellValue(cells, jTitleIndex, "商品可否购买");
                var canGiveIntegralName = getCellValue(cells, jTitleIndex, "可否赠送积分");

                if (!merchantId) {
                    logMsg = appendLog(logMsg, "第[" + j + "]行 " + "操作终止，原因是：关键的【商家ID】不能为空");
                    totalFailed++;
                    continue;
                }

                var jOrg = owl_orgsService.get(merchantId);
                if (!jOrg) {
                    logMsg = appendLog(logMsg, "第[" + j + "]行 " + "操作终止，原因是：ID为【" + merchantId + "】的商家或组织机构不存在");
                    totalFailed++;
                    continue;
                }

                var orgType = null;
                if (orgTypeName) {
                    if (orgTypeName == "组织机构") {
                        orgType = "org";
                    } else if (orgTypeName == "商家") {
                        orgType = "merchant";
                    } else {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "操作终止，原因是：组织类型只能是【组织机构】或者【商家】");
                        totalFailed++;
                        continue;
                    }
                }
                var merType = null;
                if (merTypeName) {
                    merType = getMerTypeName(merTypeName);
                    if (!merType) {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "操作终止，原因是：商家类型只能是【普通商家】或者【集采商家】或者【跨境直邮】或者【跨境保税】");
                        totalFailed++;
                        continue;
                    }
                }
                var publishState = null;
                if (publishStateName) {
                    if (publishStateName == "发布") {
                        publishState = "1";
                    } else if (publishStateName == "不发布") {
                        publishState = "0";
                    } else {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "操作终止，原因是：发布状态只能是【发布】或者【不发布】");
                        totalFailed++;
                        continue;
                    }
                }
                var certifyState = null;
                if (certifyStateName) {
                    if (certifyStateName == "审核通过") {
                        certifyState = "3";
                    } else if (certifyStateName == "审核不通过") {
                        certifyState = "2";
                    } else {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "操作终止，原因是：审核状态只能是【审核通过】或者【审核不通过】");
                        totalFailed++;
                        continue;
                    }
                }
                var enableState = null;
                if (enableStateName) {
                    if (enableStateName == "冻结") {
                        enableState = "N";
                    } else if (enableStateName == "激活") {
                        enableState = "Y";
                    } else {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "操作终止，原因是：冻结状态只能是【冻结】或者【激活】");
                        totalFailed++;
                        continue;
                    }
                }
                var deliveryMode = null;
                if (deliveryModeName) {
                    if (deliveryModeName == "快递") {
                        deliveryMode = "0";
                    } else if (deliveryModeName == "自提") {
                        deliveryMode = "1";
                    } else if (deliveryModeName == "快递和自提") {
                        deliveryMode = "2";
                    } else if (deliveryModeName == "同上级") {
                        deliveryMode = "3";
                    } else {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "操作终止，原因是：配送方式只能是【快递】或者【自提】或者【快递和自提】或者【同上级】");
                        totalFailed++;
                        continue;
                    }
                }
                var productCanBuyState = null;
                if (productCanBuyStateName) {
                    if (productCanBuyStateName == "可以") {
                        productCanBuyState = "1";
                    } else if (productCanBuyStateName == "不可以") {
                        productCanBuyState = "0";
                    } else {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "操作终止，原因是：商品可否购买只能是【可以】或者【不可以】");
                        totalFailed++;
                        continue;
                    }
                }
                var canGiveIntegral = null;
                if (canGiveIntegralName) {
                    if (canGiveIntegralName == "可以") {
                        canGiveIntegral = "1";
                    } else if (canGiveIntegralName == "不可以") {
                        canGiveIntegral = "0";
                    } else {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "操作终止，原因是：可否赠送积分只能是【可以】或者【不可以】");
                        totalFailed++;
                        continue;
                    }
                }

                var isUpdated = false;
                if (orgType) {
                    if (orgType != jOrg.orgType) {
                        jOrg.orgType = orgType;
                        isUpdated = true;
                    }
                }
                if (merType) {
                    if (merType != jOrg.merType) {
                        jOrg.merType = merType;
                        isUpdated = true;
                    }
                }
                if (certifyState) {
                    if (certifyState != jOrg.publishState) {
                        jOrg.certifyState = certifyState;
                        isUpdated = true;
                    }
                }
                if (publishState) {
                    if (publishState != jOrg.publishState) {
                        jOrg.publishState = publishState;
                        isUpdated = true;
                    }
                }
                if (enableState) {
                    if (enableState != jOrg.enableState) {
                        jOrg.enableState = enableState;
                        isUpdated = true;
                    }
                }
                if (deliveryMode) {
                    if (deliveryMode != jOrg.deliveryMode) {
                        jOrg.deliveryMode = deliveryMode;
                        isUpdated = true;
                    }
                }
                if (productCanBuyState) {
                    if (productCanBuyState != jOrg.productCanBuyState) {
                        jOrg.productCanBuyState = productCanBuyState;
                        isUpdated = true;
                    }
                }
                if (canGiveIntegral) {
                    if (canGiveIntegral != jOrg.canGiveIntegral) {
                        jOrg.canGiveIntegral = canGiveIntegral;
                        isUpdated = true;
                    }
                }

                if (!isUpdated) {
                    logMsg = appendLog(logMsg, "第[" + j + "]行 " + "操作终止，原因是：没有做任何修改");
                    totalFailed++;
                    continue;
                }

                owl_orgsService.update(jOrg);
                totalSuccess++;

                logMsg = appendLog(logMsg, "第[" + j + "]行 " + "操作成功");
            } catch (e) {
                totalFailed++;
                logMsg = appendLog(logMsg, "第[" + j + "]行 操作失败，失败原因：" + e);
            }
        }

    } catch (e) {
        $.log("\norg_batch_update_state....doUpdateTask.jsx....error=" + e);
        logMsg = appendLog(logMsg, "操作出现异常，异常信息为：" + e);
    } finally {
        var endTime = (new Date()).getTime();
        var logInfo = "总耗时=" + (endTime - beginTime) + "毫秒";
        logInfo += "。总处理数量=" + total;
        logInfo += "。成功处理数量=" + totalSuccess;
        logInfo += "; 未处理总数量=" + totalFailed;

        logMsg = appendLog(logMsg, "");
        logMsg = appendLog(logMsg, logInfo);

        jBatchImport.msg = logMsg;
        owl_org_batch_update_stateService.update(jBatchImport);
    }

})();

function appendLog(logInfo, s) {
    if (logInfo) {
        return logInfo + "\n" + s;
    }
    return s;
}

function getCellValue(cells, jTitleIndex, name, extName) {
    var index = jTitleIndex[name];
    if (!index && index != 0) {
        return "";
    }
    for (var i = 0; i < cells.length; i++) {
        var jCell = cells[i];
        if (jCell.columnIndex == index) {
            if (jCell.value) {
                return jCell.value;
            }
        }
    }
    return "";
}

function getMerTypeName(merTypeName) {
    if (merTypeName == "普通商家") {
        return "com";
    } else if (merTypeName == "集采商家") {
        return "gp";
    } else if (merTypeName == "跨境直邮") {
        return "crossDirectMail";
    } else if (merTypeName == "跨境保税") {
        return "crossBonded";
    }
    return "";
}