//#import Util.js
//#import DateUtil.js
//#import excel.js
//#import $owl_mall_u:services/modelService.jsx
//#import $owl_mall_o:services/modelService.jsx
//#import $owl_user_batch_export_bigmember_info:services/modelService.jsx
//#import $owlService:services/org/OrgUtil.jsx
//#import $wsBigMemberInterface:services/TotappsCRMService.jsx
//#import $wsBigMemberInterface:services/BigMemberUtil.jsx

;(function () {

    var jBatchImport = owl_user_batch_export_bigmember_infoService.get(batchId);

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

        var jFile = jBatchImport.file_userIds;
        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;
        }

        logMsg = appendLog(logMsg, "数据导出正在进行");
        jBatchImport.msg = logMsg;
        owl_user_batch_export_bigmember_infoService.update(jBatchImport);

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

        var jBigMemberArgs = TotappsCRMService.getArgs();
        var branchCode = "124";//默认是江豚网

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

                var row = rows[j];
                var cells = row.cells;
                var userId = getCellValue(cells, 0);

                if (!userId || userId == "") {
                    totalFailed++;
                    recordList.push({userId: userId});
                    continue;
                }

                var jUser;
                if (userId.indexOf("owl_mall_u_") >= 0) {
                    jUser = owl_mall_uService.get(userId);
                } else {
                    jUser = owl_mall_uService.getUniqueObj("mobile", userId);
                }
                if (!jUser) {
                    $.log("user_batch_export_bigmember_info...batchExportTask.jsx...user is null...userId=" + userId);
                    totalNullUser++;
                    continue;
                }
                if (!jUser.mobile) {
                    $.log("user_batch_export_bigmember_info...batchExportTask.jsx...mobile is null...userId=" + userId);
                    totalNullMobile++;
                    continue;
                }

                userId = jUser.id;
                var createTime = DateUtil.getLongDate(new Date(jUser.owl_createTime).getTime());

                var jCheckResult = BigMemberUtil.getMemberInfoByMobile(jBigMemberArgs, jUser.mobile, branchCode);
                if (Number(jCheckResult.rescode) != 0) {
                    $.log("user_batch_export_bigmember_info...batchExportTask.jsx...getMemberInfoByMobile error...userId=" + userId + "...error=" + jCheckResult.errtext);
                    totalException++;

                    var jRecord = {};
                    jRecord.userId = userId;
                    jRecord.mobile = jUser.mobile || "";
                    jRecord.createTime = createTime;
                    recordList.push(jRecord);
                    continue;
                }
                var bigInfo = jCheckResult.data;

                var jRecord = {};
                jRecord.userId = userId;
                jRecord.memberid = bigInfo.memberid || "";
                jRecord.mobile = jUser.mobile || "";
                jRecord.createTime = createTime;
                jRecord.membername = bigInfo.membername || "";
                jRecord.birthday = bigInfo.birthday || "";
                jRecord.sex = bigInfo.sex || "";
                jRecord.cardno = bigInfo.cardno || "";
                jRecord.point = bigInfo.point || "";
                jRecord.companypoint = bigInfo.companypoint || "";
                jRecord.branchpoint = bigInfo.branchpoint || "";
                jRecord.grade = bigInfo.grade || "";
                jRecord.gradename = bigInfo.gradename || "";
                jRecord.branchgrade = bigInfo.branchgrade || "";
                jRecord.discount = bigInfo.discount || "";
                jRecord.groupamt = bigInfo.groupamt || "";
                jRecord.companyamt = bigInfo.companyamt || "";
                jRecord.carno = bigInfo.carno || "";
                jRecord.regbranch = bigInfo.regbranch || "";
                jRecord.consumeenddate = bigInfo.consumeenddate || "";

                recordList.push(jRecord);

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

        var titles = getTitles();

        var export_fileName = DateUtil.getLongDate(new Date().getTime());

        var fileUrl = Excel.createExcelList(export_fileName, titles, recordList);
        $.log("user_batch_export_bigmember_info....batchExportTask.jsx....fileUrl=" + fileUrl);

        jBatchImport = owl_user_batch_export_bigmember_infoService.get(jBatchImport.id);
        jBatchImport.file = {url: fileUrl, name: export_fileName};

    } catch (e) {
        $.log("user_batch_export_bigmember_info....batchExportTask.jsx....error=" + e);
        logMsg = appendLog(logMsg, "操作出现异常，异常信息为：" + e);
    } finally {
        var endTime = (new Date()).getTime();
        var logInfo = "总耗时=" + (endTime - beginTime) + "毫秒";
        logInfo += "。总处理数量=" + total;
        logInfo += "。成功处理数量=" + totalSuccess;
        logInfo += "; 未处理总数量=" + totalFailed;
        logInfo += "; 用户不存在数量=" + totalNullUser;
        logInfo += "; 手机号码为空数量=" + totalNullMobile;
        logInfo += "; 获取大会员信息异常数量=" + totalException;

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

        jBatchImport.msg = logMsg;
        owl_user_batch_export_bigmember_infoService.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 getTitles() {
    var index = 0;

    var titles = [];
    titles.push({"index": index++, "columnWidth": "30", "field": "userId", "title": "会员ID"});
    titles.push({"index": index++, "columnWidth": "12", "field": "memberid", "title": "大会员ID"});
    titles.push({"index": index++, "columnWidth": "12", "field": "mobile", "title": "手机号码"});
    titles.push({"index": index++, "columnWidth": "20", "field": "createTime", "title": "注册时间"});
    titles.push({"index": index++, "columnWidth": "12", "field": "birthday", "title": "会员生日"});
    titles.push({"index": index++, "columnWidth": "10", "field": "sex", "title": "会员性别"});
    titles.push({"index": index++, "columnWidth": "16", "field": "cardno", "title": "会员卡号"});
    titles.push({"index": index++, "columnWidth": "12", "field": "point", "title": "集团积分"});
    titles.push({"index": index++, "columnWidth": "12", "field": "companypoint", "title": "实体积分"});
    titles.push({"index": index++, "columnWidth": "12", "field": "branchpoint", "title": "机构积分"});
    titles.push({"index": index++, "columnWidth": "10", "field": "grade", "title": "集团等级"});
    titles.push({"index": index++, "columnWidth": "12", "field": "gradename", "title": "集团等级名称"});
    titles.push({"index": index++, "columnWidth": "12", "field": "branchgrade", "title": "实体等级"});
    titles.push({"index": index++, "columnWidth": "12", "field": "discount", "title": "折扣率"});
    titles.push({"index": index++, "columnWidth": "12", "field": "groupamt", "title": "集团会籍消费"});
    titles.push({"index": index++, "columnWidth": "12", "field": "companyamt", "title": "实体会籍消费"});
    titles.push({"index": index++, "columnWidth": "12", "field": "carno", "title": "车牌号"});
    titles.push({"index": index++, "columnWidth": "12", "field": "regbranch", "title": "归属机构"});
    titles.push({"index": index++, "columnWidth": "12", "field": "consumeenddate", "title": "会籍到期"});
    return titles;
}