Index.js 13 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
//yoho币
'use strict';
var $ = require('jquery'),
    common=require('../../common/common');

var pricePercent = 0.2;  //yoho币审核比例线
var loadModal = null;
var importYohoCoinData = null;
var ENUM = {
    gender: {
        1: '男',
        2: '女',
        3: '通用'
    },
    status: {
        0: '未设置',
        1: '<b style="color:#428bca">待审核</b>',
        2: '<b style="color:#5cb85c">通过</b>',
        3: '<b style="color:#ff0000">驳回</b>'
    },
    auditor: {
        2965: 'Tracy',
        10235: 'David Wong'
    }
}

new common.dropDown({
    el: '#shop',
    ajax: 'shopsRest'
});
new common.dropDown({
    el: '#brand',
    ajax: 'brand'
});
new common.dropDown({
    el: '#supplier',
    ajax: 'querySupplier'
});

//多级菜单
var tabTree = new common.tabTree("#sortTree", {status: 1, sortLevel: 1});
tabTree.init();

//grid列表生成
var g = new common.grid({
    el: "#yohoCoin_table",
    parms: function() {
        var select = tabTree.getAddress();
        return {
            productSkn: common.util.__input("skn"),
            shopId: common.util.__input("shop"),
            brandId: common.util.__input("brand"),
            supplierId: common.util.__input("supplier"),
            gender: common.util.__input("gender"),
            auditStatus: common.util.__input("auditStatus"),
            maxSortId: select[0] ? select[0].id : "",
            middleSortId: select[1] ? select[1].id : "",
            smallSortId: select[2] ? select[2].id : "",
            sortId: select[3] ? select[3].id : "",
            returnMoney: common.util.__input("returnMoney")
        };
    },
    columns: [
        {display: '', type: 'checkbox'},
        {display: '', render: function(item){
            var html = "";
            if(item.__index == 0){
                $("#yohoCoin_table").find("thead tr th").eq(1).html($("#coinListHead").html());
            }
            item.genderName = ENUM.gender[item.gender];
            item.statusName = ENUM.status[item.productPrice.coinStatus];
            if(item.productPrice.auditorId){item.auditorName = ENUM.auditor[item.productPrice.auditorId];}
            if(item.productPrice.auditDt){item.updateTime = getTime(item.productPrice.auditDt);}

            html += common.util.__template2($("#coinList").html(),item);
            return html;
        }
    }]
});
g.init($("#gridurl").val());

//批量上传
ajaxUpload();

/**********************接口*************************/

//保存接口
function ajaxSave(productSkn, yohoCoin, auditorId, salesPrice){
    common.util.__ajax({
        url:'/good/yohoCoin/set',
        data: {
            "productSkn": productSkn,
            "returnCoinMoney": yohoCoin,
            "salesPrice": salesPrice,
            "auditorId": auditorId
        }
    },function(rs){
        freshYohoCoinRowData(rs.data, yohoCoin);
    });
}

//批量导入接口
function ajaxUpload(){
    common.edit.ajaxfileupload('#yohoCoinUpload',{
        params: {
            type: "yohoCoin",
            __type: "batch-import"
        },
        onStart: function() {
            loadModal = common.dialog.load();
            clearImportResult();
        },
        onComplete: function(response) {
            console.log("批量接口返回数据",response);
            if (response.code == 200 && response.data.successList) {
                loadingClose();
                showImportMessage("批量导入成功!", "success");
                common.util.__tip("批量导入成功!", 'success');
                //显示导入列表数据
                showImportTable(response.data);
                return true;
            }
            else if(response.code == 200 && response.data.failFileReason){
                loadingClose();
                showImportMessage(response.data.failFileReason.join('<br/>'), "error");
                common.util.__tip("上传出错!具体请看批量上传结果。", 'warning');
                return false;
            }
            else{
                loadingClose();
                showImportMessage(response.message, "error");
                common.util.__tip(response.message);
            }
        }
    });
}

//批量上传确认
function ajaxUploadSubmit(importAuditorId){
    common.util.__ajax({
        url:'/good/yohoCoin/insertBatch',
        data: {
            auditorId: importAuditorId,
            productPrices: JSON.stringify(importYohoCoinData)
        }
    },function(rs){
        if(rs.code == 200){
            common.util.__tip("批量导入保存成功!", "success");
            setTimeout(function() {
                location.href = '';
            }, 1000);
        }else{
            common.util.__tip(rs.message || rs.data.message);
        }
    },true);
}

/**********************业务方法*************************/

//显示批量导入结果
function showImportMessage(message, statue){
    $("#yohoCoinImportMessage").removeAttr("class").addClass(statue).show();
    $("#detailMassage").html(message);
}

//显示批量导入数据表
function showImportTable(data){
    importYohoCoinData = data.successList;

    $("#importResultTable").html(common.util.__template2($("#yohoCoinImportResultTable").html(),data));
    $("#yohoCoinImportResultSet").show();
    $("#yohoCoinImportResult").show();
}

//初始化批量导入结果及列表
function clearImportResult(){
    $("#detailMassage").empty();
    $("#importResultTable").empty();
    $("#importAuditor").val("");

    $("#yohoCoinImportMessage").hide();
    $("#yohoCoinImportResultSet").hide();
    $("#yohoCoinImportResult").hide();
}

//批量导入确认—校验审核人
function isNeedImportAuditor(){
    for(var i=0; i < importYohoCoinData.length; i++){
        if((importYohoCoinData[i].salesPrice * pricePercent) <= importYohoCoinData[i].needAuditCoin){
            return true;
        }
    }
    return false;
}

//吊牌价跟销售价校验
function checkPrice(retailPrice, salesPrice, yohoCoin){
    if(yohoCoin == 0){
        return true;
    }else{
        if(retailPrice == salesPrice){
            return true;
        }else{
            common.util.__tip("销售价与吊牌价不同,YOHO币只能为设置为0!");
            return false;
        }
    }
}

//yoho币金额校验
function checkYohoCoin(yohoCoin, auditor, price){
    if(yohoCoin.val()){
        if(!isNaN(yohoCoin.val())){
            if(yohoCoin.val() < 0){
                yohoCoin.focus().select().addClass("error");
                common.util.__tip("YOHO币金额不能小于0!");
                return false;
            }
            if(!/^\d+(\.\d{1,2})?$/g.test(yohoCoin.val())){
                common.util.__tip("YOHO币的小数点最多只能2位!");
                return false;
            }
            //yoho币大于等于销售额的20%需要校验审核人
            if(yohoCoin.val() >= (price * pricePercent)){
                return checkAuditor(auditor);
            }
        }else{
            yohoCoin.focus().select().addClass("error");
            common.util.__tip("YOHO币金额必须为数字!");
            return false;
        }
    }else{
        yohoCoin.focus().addClass("error");
        common.util.__tip("YOHO币金额不能为空!");
        return false;
    }
    return true;
}

//审核人校验
function checkAuditor(auditor){
    if(auditor.val() == ""){
        auditor.focus().addClass("error");
        common.util.__tip("审核人不能为空!");
        return false;
    }
    return true;
}

//保存单行数据刷新
function freshYohoCoinRowData(data, needAuditCoin){
    var productSkn = data.productSkn;
    var yohoCoin = data.returnCoinMoney;
    var status = data.coinStatus;
    var founderName = data.founderName;
    var updateTime = getTime(data.auditDt);
    var auditorId = data.auditorId;

    var SknDom = $('._' + productSkn);
    SknDom.find(".returnYohoCoin").text(yohoCoin);
    SknDom.find(".coinStatus").html(ENUM.status[status]);

    var OperatInfoHtml = "<p>操作人:"+ founderName +"</p>";
    if(status == 1 && auditorId) OperatInfoHtml += "<p>审核金额:"+ needAuditCoin +"</p><p>审核人:"+ ENUM.auditor[auditorId] +"</p>";
    OperatInfoHtml += "<div style='color: #999; font-size: 12px'>"+ updateTime + "</div>";
    SknDom.find(".OperatInfo").html(OperatInfoHtml);

    if(status == 1){
        SknDom.find(".operation").empty();
    }else{
        SknDom.find(".operation").html("<button type='button' name='修改' class='btn btn-info btn-sm btn-modify'>修改</button>");
    }
    SknDom.find(".dataForEditor td").hide();
}

//找到以选中的商品的SKN
function findCheckedSknList(){
    var sknList = new Array();
    var checkedItems = $("#yohoCoin_table").find(".wqt_checkbox:checked");
    $.each(checkedItems, function(index,value){
        var skn = $(value).parent().next().find(".dataForShow").data("skn");
        sknList.push(skn);
    });
    return sknList;
}

//关闭loading
function loadingClose(){
    if (loadModal) {
        loadModal.close();
    }
}

//时间转换
function getTime(time){
    var t = new Date(time);
    var formattedTime = common.util.__dateFormat(t, "yyyy-MM-dd hh:mm:ss");
    return formattedTime;
}

/**********************事件*************************/

//批量导入结果列表删除
$(document).on('click', '.btn-delete-yohoCoin', function() {
    var index = $(this).data("index");
    if(importYohoCoinData){
        [].splice.call(importYohoCoinData,index,1);
        $("#importResultTable").html(common.util.__template2($("#yohoCoinImportResultTable").html(),{"successList":importYohoCoinData}));
    }
});

//批量导入结果列表确定
$(document).on('click', '.btn-submit-yohoCoin', function() {
    var importAuditorIdInput = $("#importAuditor");
    var isNeedAuditor = isNeedImportAuditor();
    if(isNeedAuditor){
        if(!checkAuditor(importAuditorIdInput)){
            return false;
        }
    }
    ajaxUploadSubmit(importAuditorIdInput.val());
});

//保存操作
$(document).on('click', '.btn-save', function() {
    var productSkn = $(this).data('productskn');
    var sknDom = $('._' + productSkn);

    var retailPrice = sknDom.find('.retailPrice').text();  //吊牌价
    var salesPrice = sknDom.find('.salesPrice').text();  //销售价

    var yohoCoinInput = sknDom.find('.editor-yohoCoin');
    var auditorInput = sknDom.find('.editor-auditor');
    if(checkYohoCoin(yohoCoinInput, auditorInput, salesPrice)){
        if(checkPrice(retailPrice, salesPrice, yohoCoinInput.val())){
            ajaxSave(productSkn, yohoCoinInput.val(), auditorInput.val(), salesPrice);
        }
    }
});

//显示修改
$(document).on('click', '.btn-modify', function() {
    if($(this).hasClass("openEditor")){
        $(this).text($(this).attr("name")).removeClass("openEditor");
        var dataForShow = $(this).parents(".dataForShow");
        dataForShow.find(".dataForEditor td").hide();
        dataForShow.find(".editor-yohoCoin").val(dataForShow.find(".returnYohoCoin").text());
        dataForShow.find(".editor-auditor").val("");
    }else{
        $(this).text("收起").addClass("openEditor");
        $(this).parents(".dataForShow").find(".dataForEditor td").show();
    }
});

//查看驳回理由
$(document).on('click', '.noCheckReason', function() {
    var reason = $(this).data("text");
    common.dialog.confirm("查看驳回理由", reason, function(){}, function(){});
});

//显示批量导入
$(document).on('click', '#import-btn', function() {
    $('#yohoCoin_table').hide();
    $('#yohoCoinSearch').hide();
    $('#yohoCoin_import').show();
});

//导出
$(document).on('click', '#export-btn', function() {
    var exportSknList = findCheckedSknList();
    if(exportSknList.length > 0){
        var exportSknListShow = "";
        for(var i=0; i<exportSknList.length; i++){
            exportSknListShow += "<span style='margin-right: 10px; font-weight: bold; color: #5cb85c; display: inline-block'>"+ exportSknList[i] +"</span>"
        }
        common.dialog.confirm(
            "批量导出确认",
            "<div><p>请确认&nbsp;&nbsp;<b style='color:#428bca'>导出</b>&nbsp;&nbsp;SKN为:</p><p>"+ exportSknListShow +"</p></div>",
            function(){
                window.open("/ajax/down?queryConf=" + exportSknList.join("|") + "&type=yohoCoin");
            },
            function(){}
        );
    }else{
        common.util.__tip("没有选择任何商品!");
    }
});

//返回列表
$(document).on('click', '#return-btn', function() {
    $('#yohoCoin_import').hide();
    $('#yohoCoinSearch').show();
    $('#yohoCoin_table').show();
});


//校验反币金额2位小数
var isReturnMoneyOk = true;
$(document).on('change', '#returnMoney', function() {
    if(!/^-?\d+\.?\d{0,2}$/.test(this.value)){
        isReturnMoneyOk = false;
    }
});

//筛选
$(document).on('click', '#filter-btn', function() {
    if(isReturnMoneyOk){
        g.reload(1);
    }else{
        common.util.__tip("【反币金额】请输入最多2位小数的数字!");
    }
});

//全部
$(document).on('click', '#all-btn', function() {
    location.href = '';
});

//校验失败编辑
$(document).on('change', '.error', function() {
    $(this).removeClass('error');
});