//#import Util.js
//#import DigestUtil.js
//#import excel.js
//#import $owl_mall_u:services/modelService.jsx
//#import $owl_user_batch_import:services/modelService.jsx
//#import $owlService:services/common/CommonUtil.jsx
//#import $commonService:services/UserChoiseUtil.jsx

;(function () {

    var jBatchImport = owl_user_batch_importService.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 isCheckLoginId = jBatchImport.isCheckNullLoginId == "n";
        var isCheckEmail = jBatchImport.isCheckNullEmail == "n";
        var isCheckMobile = jBatchImport.isCheckNullMobile == "n";

        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 loginId = getCellValue(cells, 0);
                var email = getCellValue(cells, 1);
                var mobile = getCellValue(cells, 2);
                var password = getCellValue(cells, 3);
                var realName = getCellValue(cells, 4);
                var nickName = getCellValue(cells, 5);
                var source = getCellValue(cells, 6);
                var parentId = getCellValue(cells, 7);

                if (loginId == "" && email == "" && mobile == "" && password == "" && realName == "" && nickName == ""
                    && source == "") {
                    logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：当前行为空行");
                    totalFailed++;
                    continue;
                }

                if (loginId == "" && email == "" && mobile == "") {
                    logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：登陆ID、邮箱、手机号码不能同时为空");
                    totalFailed++;
                    continue;
                }

                if (!source) {
                    logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：会员来源不能为空");
                    totalFailed++;
                    continue;
                }


                var jSource = UserChoiseUtil.getSourceByName(source);
                if (!jSource) {
                    logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：来源【" + source + "】尚未定义");
                    continue;
                }

                var jUser = null;
                if (loginId) {
                    jUser = owl_mall_uService.getUniqueObj("loginId", loginId);
                    if (jUser) {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：登录ID为【" + loginId + "】的用户已经存在");
                        totalFailed++;
                        continue;
                    }
                } else {
                    if (isCheckLoginId) {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：在不允许登录ID为空的情况下，登录ID为空");
                        totalFailed++;
                        continue;
                    }
                }

                if (mobile) {
                    if (!CommonUtil.checkMobile(mobile)) {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：手机号码【" + mobile + "】格式不正确");
                        totalFailed++;
                        continue;
                    }

                    jUser = owl_mall_uService.getUniqueObj("mobile", mobile);
                    if (jUser) {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：手机号码为【" + mobile + "】的用户已经存在");
                        totalFailed++;
                        continue;
                    }
                } else {
                    if (isCheckMobile) {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：在不允许手机号码为空的情况下，手机号码为空");
                        totalFailed++;
                        continue;
                    }
                }

                if (email) {
                    jUser = owl_mall_uService.getUniqueObj("email", email);
                    if (jUser) {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：邮箱为【" + email + "】的用户已经存在");
                        totalFailed++;
                        continue;
                    }
                } else {
                    if (isCheckEmail) {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：在不允许电子邮箱为空的情况下，电子邮箱为空");
                        totalFailed++;
                        continue;
                    }
                }


                var jParentUser = null;
                if (parentId) {
                    if (parentId.indexOf("owl_mall_u_") > -1) {
                        jParentUser = owl_mall_uService.get(parentId);
                    } else {
                        jParentUser = owl_mall_uService.getUniqueObj("mobile", parentId);
                        if (!jParentUser) {
                            jParentUser = owl_mall_uService.getUniqueObj("loginId", parentId);
                        }
                    }
                    if (!jParentUser) {
                        logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入终止，原因是：推荐人【" + parentId + "】不存在");
                        totalFailed++;
                        continue;
                    }
                }

                jUser = {};
                jUser.loginId = loginId;
                jUser.email = email;
                jUser.mobile = mobile;
                jUser.password = password;
                jUser.realName = realName;
                jUser.nickName = nickName;
                jUser.source_isOnline = jSource.is_online;
                jUser.source_id = jSource.id;
                jUser.source_name = jSource.name;
                if (jParentUser) {
                    jUser.recommendedUserId = jParentUser.id;
                }
                if(password) {
                    var random = Math.random();
                    jUser.passwordhash = DigestUtil.sha1(password + random);
                    jUser.random = random;
                }

                jUser = owl_mall_uService.add(jUser);

                logMsg = appendLog(logMsg, "第[" + j + "]行 " + "导入成功");
                totalSuccess++;

            } catch (e) {
                totalFailed++;
                logMsg = appendLog(logMsg, "第[" + j + "]行 导入失败，失败原因：" + e);
            }
        }

    } catch (e) {
        $.log("\nuser_batch_import....doAddUserTask.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_importService.update(jBatchImport);
    }

})();

function getGenderValue(gender) {
    if (gender && gender != "") {
        if (gender == "男") {
            return "0"
        } else if (gender == "女") {
            return "1"
        } else if (gender == "保密") {
            return "2"
        } else {
            throw "性别为【" + gender + "】格式不正确，必须为：男、女、保密";
        }
    }
    return "";
}

function getMarriageValue(marriage) {
    if (marriage && marriage != "") {
        if (marriage == "已婚") {
            return "1"
        } else if (marriage == "未婚") {
            return "0"
        } else {
            throw "婚姻状态为【" + marriage + "】格式不正确，必须为：已婚、未婚";
        }
    }
    return "";
}


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 "";
}
