//#import Util.js
//#import DateUtil.js
//#import excel.js
//#import $owl_mall_u:services/modelService.jsx
//#import $owl_user_batch_update_state:services/modelService.jsx

;(function () {

    var jBatchImport = owl_user_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 actionType = jBatchImport.actionType;

        var sheet = jData.sheets[0];
        var rows = sheet.rows;

        for (var j = 1; j < rows.length; j++) {
            try {
                total++;

                var row = rows[j];
                var cells = row.cells;
                var uid = getCellValue(cells, 0);
                var mobile = getCellValue(cells, 1);
                var loginId = getCellValue(cells, 2);

                if (uid == "" && mobile == "" && loginId == "") {
                    logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：关键的【会员内部ID】，【会员手机号】，【会员登录ID】不能同时为空");
                    totalFailed++;
                    continue;
                }

                var jUser;
                if (uid) {
                    jUser = owl_mall_uService.get(uid);
                    if (!jUser) {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：ID为【" + uid + "】的用户不存在");
                        totalFailed++;
                        continue;
                    }
                } else if (mobile) {
                    jUser = owl_mall_uService.getUniqueObj("mobile", mobile);
                    if (!jUser) {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：手机号为【" + mobile + "】的用户不存在");
                        totalFailed++;
                        continue;
                    }
                } else {
                    jUser = owl_mall_uService.getUniqueObj("loginId", loginId);
                    if (!jUser) {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：登录ID为【" + loginId + "】的用户不存在");
                        totalFailed++;
                        continue;
                    }
                }

                if (jUser.isEnable == actionType) {
                    logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：用户已经是【" + getStateName(jUser.isEnable) + "】状态");
                    totalFailed++;
                    continue;
                }

                jUser.isEnable = actionType;
                owl_mall_uService.update(jUser);
                totalSuccess++;

                logMsg = appendLog(logMsg, "第[" + j + "]行 " + "操作成功");
            } catch (e) {
                totalFailed++;
                logMsg = appendLog(logMsg, "第[" + j + "]行 操作失败，失败原因：" + e);
            }
        }

    } catch (e) {
        $.log("\nuser_batch_update_state....doUpdateUserTask.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_user_batch_update_stateService.update(jBatchImport);
    }

})();

function appendLog(logInfo, s) {
    if (logInfo) {
        return logInfo += "\n" + s;
    }
    return s;
}

function getCellValue(cells, index) {
    for (var i = 0; i < cells.length; i++) {
        var jCell = cells[i];
        if (jCell.columnIndex == index) {
            if (jCell.value) {
                return jCell.value;
            }
        }
    }
    return "";
}

function getStateName(state) {
    if (state == "1") {
        return "正常";
    }
    return "冻结";
}