Source: emapImport/emapImportview.js

(function($) {
    /**
     * @module WIS_IMPORT_VIEW
     * @alias 导入view模块
     * @description 导入组件view层
     * @param  {object} option 
     * @example
    var option = {
        title: '导入数据',
        btns: {},
        params: {
            height: 450,
            close: function() {
                if ($.isFunction(opt.closeCallback)) {
                    opt.closeCallback();
                }
            }
        },
        callback: function(){}
    };
    var view = new WIS_IMPORT_VIEW(option);
    view.show();
     */
    var importView = function(option, queryTimes,isMapping) {
        this.option = option;
        this.option.queryTimes = queryTimes;
        this.isMapping = isMapping;
    };
    var proto = importView.prototype;
    var content ='';
    /**
     * @method show
     * @description 显示导入窗口
     */
    proto.show = function() {
        createdViewHtml(this.isMapping);
        var title = this.option.title;
        var btns = this.option.btns || {};
        if (this.isMapping) {
            var params = $.extend({
                height: 450,
                width:600
            }, this.option.params);
        } else {
            var params = $.extend({
                height: 450
            }, this.option.params);
        }
        var callback = this.option.callback;
        if (this.isMapping) {
            //debugger
            btns = [{
                text:$.i18n('bh-ipm-startImporting'),
                className:'bh-btn-primary',
                callback:emapMapBeginImport
            },{
                text:$.i18n('bh-ipm-close'),
                className:'bh-btn-default',
                callback:function(){}
            }];
        }
        //debugger
        this.$window = BH_UTILS.bhWindow(content, title, btns, params, callback);
    };
    /**
     * @method hide
     * @description 隐藏导入窗口
     */
    proto.hide = function() {
        this.$window.jqxWindow('close');
    };
    /**
     * @method getUploadBtn
     * @description 获取上传按钮
     */
    proto.getUploadBtn = function() {
        return this.$window.find('[role=fileInput]');
    };
    /**
     * @method getDownloadImportTplBtn
     * @description 获取下载导入模板按钮
     */
    proto.getDownloadImportTplBtn = function() {
        return this.$window.find('[role="downTplBtn"]');
    };
    /**
     * @method getImportConfirmBtn
     * @description 获取导入确认按钮
     */
    proto.getImportConfirmBtn = function() {
        return this.$window.find('[role=importConfirmBtn]');
    };
    /**
     * @method getReImportBtn
     * @description 获取重新导入按钮
     */
    proto.getReImportBtn = function() {
        return this.$window.find('[role=reImportBtn]');
    };
    /**
     * @method getDownloadResultBtn
     * @description 获取下载导入结果按钮
     */
    proto.getDownloadResultBtn = function() {
        return this.$window.find('a.emap-import-export');
    };
    /**
     * @method getMapDownloadResultBtn
     * @description 获取Map下载导入结果按钮
     */
    proto.getMapDownloadResultBtn = function() {
        return this.$window.find('a.emap-import-map-export');
    };
    /**
     * @method getCloseBtn
     * @description 获取关闭导入窗口按钮
     */
    proto.getCloseBtn = function() {
        return this.$window.find('[role=closeConfirmBtn]');
    };
    /**
     * @method getSavePlanBtn
     * @description 获取保存方案按钮
     */
    proto.getSavePlanBtn = function() {
        return this.$window.find('[role=savePlanBtn]');
    };
    /**
     * @method getDeletePlanBtn
     * @description 获取删除方案按钮
     */
    proto.getDeletePlanBtn = function() {
        return this.$window.find('[role=deletePlanBtn]');
    };
    /**
     * @method renderImportTempFileResult
     * @description 渲染导入临时文件结果
     * @param {object} result - 导入结果
     */
    proto.renderImportTempFileResult = function(result) {
        var step1Contents = this.$window.find(".emap-import-step-content");
        // if (e.target.files) {
        step1Contents.eq(0).hide();
        step1Contents.eq(1).show();
        if (result.isSuccess) {
            step1Contents.eq(1).find('span.emap-import-file-name').text(result.file[0].name).attr('title', result.file[0].name);
            if (result.fileReader) {
                step1Contents.eq(1).find('span.emap-import-file-size').text('(' + parseInt(result.file[0].size / 1024) + 'k)');
            } else {
                step1Contents.eq(1).find('span.emap-import-file-size').hide();
            }
            this.getImportConfirmBtn().attr('disabled', false);
        } else {
            step1Contents.eq(1).find('span.emap-import-file-name').html('<span class="bh-color-danger">' + $.i18n('bh-ip-uploadRightExcel') + '</span>').attr("title", result.file[0].name);
            this.getImportConfirmBtn().attr('disabled', true);
        }
    };
    /**
     * @method renderImportingFileProgress
     * @description 渲染导入文件进度条
     */
    proto.renderImportingFileProgress = function() {
        var stepContent = this.$window.find('.emap-import-step1-content');
        stepContent.children('a').hide();
        if (this.isMapping) {
                stepContent.prepend('<div class="emap-import-step1-loading-block "><div class="emap-sk-spinner emap-sk-spinner-fading-circle " style="height: 28px; width: 28px;">' +
                '<div class="emap-sk-circle1 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle2 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle3 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle4 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle5 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle6 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle7 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle8 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle9 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle10 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle11 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle12 emap-sk-circle"></div>' +
                '</div>' +
                '</div>');
        } else {
                stepContent.prepend('<div class="emap-import-step1-loading-block bh-pull-right"><div class="emap-sk-spinner emap-sk-spinner-fading-circle bh-pull-right" style="height: 28px; width: 28px;">' +
                '<div class="emap-sk-circle1 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle2 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle3 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle4 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle5 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle6 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle7 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle8 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle9 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle10 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle11 emap-sk-circle"></div>' +
                '<div class="emap-sk-circle12 emap-sk-circle"></div>' +
                '</div>' +
                '<p class="bh-pull-right" style="margin-right: 12px;line-height:28px;">' + $.i18n('bh-ip-uploading') + '……</p></div>');
        }
    };
    /**
     * @method renderProgress
     * @description 根据后台数据渲染导入文件进度条
     */
    proto.changeProgressBar = function(resp) {
        var width = resp.YWCS / resp.ZS * 100 + '%';
        $('.emap-import-loading-bar div', this.$window).stop().animate({
            width: width
        }, this.option.queryTimes + 10);
    };
    /**
     * @method renderUploadTempFileResult
     * @description 渲染导入文件进度条
     */
    proto.renderUploadTempFileResult = function() {
    };
    /**
     * @method renderSaveAttachmentResult
     * @description 渲染保存临时文件结果
     */
    proto.renderSaveAttachmentResult = function() {
    };
    /**
     * @method renderImportRownumResult
     * @description 渲染导入数据条数结果
     */
    proto.renderImportRownumResult = function(result) {
        if (result.code == '500') {
            $('div.emap-import-step-content:eq(2)', this.$window).html('<p></p>');
            $('.emap-import-step1-content', this.$window).find('div.emap-import-step1-loading-block').remove();
            $('div.emap-import-step:eq(2)', this.$window).addClass('active').find('.emap-import-result-detail').html('<span style="color: red">' + (result.msg || result.message || $.i18n('bh-ip-importFailed')) + '</span>');
            return;
        }
        $('.emap-import-step2-count', this.$window).html($.i18n('bh-ip-numOfImportDatas') + result.rowNumber + $.i18n('bh-ip-piece'));
        $('.emap-import-step1-content', this.$window).find('div.emap-import-step1-loading-block').remove();
        $('div.emap-import-step:eq(1)', this.$window).addClass('active');
        if (!result.useNewImport) {
            $('.emap-import-loading-bar div', this.$window).animate({
                width: '87%'
            }, 3000);
        }
    };
    /**
     * @method renderImportCompleteResult
     * @description 渲染导入完成后的结果视图
     */
    proto.renderImportCompleteResult = function(result, opt) {
        var self = this;
        if (self.isMapping) {
            $('.emap-import-mapping-title').bhStepWizard("changeToFinished",'mapstep1');
            $('.emap-import-mapping-title').bhStepWizard("activeNextItem");
            if (result.attachment) {
                window.attachment = result.attachment;
                reRenderPlanMapping(result, opt);
            } else {
                $('.emap-map-step2-error').show();
                $('.emap-map-step2-success').hide();
                $('.bh-btn-primary','#buttons').hide();
            }  
        } else {
            if (result.status == 1) {
                $('.emap-import-loading-bar div', self.$window).stop().animate({
                    'width': '100%'
                }, 500, function() {
                    var callback = function() {};
                    if ($.isFunction(opt.importCallback)) {
                        callback = opt.importCallback(result.total, result.success);
                    }
                    $("div.emap-import-step-content:eq(2)", self.$window).html('<p class="bh-color-success">' + $.i18n('bh-ip-successInImport') + '</p>');
                    $("div.emap-import-step:eq(2)", self.$window).addClass("active").find(".emap-import-result-detail").html($.i18n('bh-ip-importFinished') + ", " + $.i18n('bh-ip-numOfSuccess') + result.success + $.i18n('bh-ip-piece') + "," + $.i18n('bh-ip-numOfFail') + (result.total - result.success) + $.i18n('bh-ip-piece'));
                    if (callback) {
                        callback($("div.emap-import-step:eq(2)", self.$window).find(".emap-import-export"));
                    }
                    if (result.downLoadUrl) {
                        self.getDownloadResultBtn().attr('href', result.downLoadUrl).parent().show();
                    } else {
                        self.getDownloadResultBtn().parent().show();
                    }
                    if (opt.autoClose) {
                        self.hide();
                    }
                });
            } else {
                $('div.emap-import-step-content:eq(2)', self.$window).html('<p></p>');
                $('div.emap-import-step:eq(2)', self.$window).addClass('active').find('.emap-import-result-detail').html('<span style="color: red">' + $.i18n('bh-ip-numOfFail') + (result.msg?(': ' + result.msg) : (result.message?(': ' + result.message) : '')) + '</span>');
                if (!result.attachment) {
                    $('div.emap-import-step:eq(2)', self.$window).find('.emap-import-export').parent().hide();
                }
            }
        } 
    };
    /**
     * @method renderReUploadBehaviour
     * @description 渲染重新上传按钮
     */
    proto.renderReUploadBehaviour = function() {
        var contents = this.$window.find('.emap-import-step1-content');
        contents.eq(0).show();
        contents.eq(1).hide();
    };
    proto.savePlan = function(opt,data) {
        if (!$('.emap-import-step2-planTitle').val()) {
            $.bhTip && $.bhTip({
                content: $.i18n('bh-ipm-pleaseInputPlanName'),
                state: 'danger',
                iconClass: 'icon-close'
            });
            return false;
        }
        // //获取当前所有保存方案数据
        // var tmpShemas = $('.step2-dropdown-content').jqxDropDownList('getItems');
        // //debugger
        // var  isTitleRepeat = false;
        // tmpShemas.forEach(function(item){
        //     if (item.label == $('.emap-import-step2-planTitle').val()) {
        //         isTitleRepeat = true;
        //     }
        // });
        // if(isTitleRepeat){
        //     $.bhTip && $.bhTip({
        //         content: '当前方案名称重复,请重新输入!',
        //         state: 'danger',
        //         iconClass: 'icon-close'
        //     });
        //     return false;
        // }
        // 保存方案参数
        var savePlanData = {
            app: opt.app, // *
            module: opt.module, // *
            page: opt.page, // *
            action: opt.action,
            schemaName:$('.emap-import-step2-planTitle').val(),
            mapping:getMapping()
        };
        if (opt.params) {
            $.extend(savePlanData, opt.params);
        }
        if (savePlanData.mapping) {
            $.ajax({
                type: 'post',
                url: opt.contextPath + '/sys/emapcomponent/import/schema/save.do',
                data: savePlanData,
                dataType: 'json',
                success: function(json) {
                    if (json.code == '0') {
                        $.bhTip && $.bhTip({
                            content: $.i18n('bh-ipm-saveSuccess'),
                            state: 'success',
                            iconClass: 'icon-close'
                        });
                        $('.form-tbody-select-yellow').removeClass('form-tbody-select-yellow');
                        //清空popover的值并关掉
                        $('.emap-import-step2-planTitle').val('');
                        $("#emap-import-map-pop").jqxPopover('close');
                        //$("#emap-import-map-pop").jqxPopover('destroy');
                        //重新渲染方案
                        reRenderPlanMapping({},opt);
                    } else {
                        $.bhTip && $.bhTip({
                            content: $.i18n('bh-ipm-saveFail'),
                            state: 'danger',
                            iconClass: 'icon-close'
                        });
                    }
                },
                error: function(e) {
                    $.bhTip && $.bhTip({
                        content: $.i18n('bh-ipm-saveFail'),
                        state: 'danger',
                        iconClass: 'icon-close'
                    });
                    console && console.log(e);
                }
            });
        } else {
           console.log('mapping 是空的!!')
        }
    }
    proto.deletePlan = function (opt,data) {
      var tmpPlanObj = $('.step2-dropdown-content').jqxDropDownList('getSelectedItem');
      if (tmpPlanObj && tmpPlanObj.label) {
        BH_UTILS.bhDialogWarning({
            title:'',
            content:$.i18n('bh-imp-sureToDeletePlan'),
            callback:function (params) {
              // 删除方案参数
              var deletePlanData = {
                  app: opt.app, // *
                  module: opt.module, // *
                  page: opt.page, // *
                  action: opt.action,
                  schemaName:tmpPlanObj.label
              };
              if (opt.params) {
                $.extend(deletePlanData, opt.params);
              }
              $.ajax({
                  type: 'post',
                  url: opt.contextPath + '/sys/emapcomponent/import/schema/delete.do',
                  data: deletePlanData,
                  dataType: 'json',
                  success: function(json) {
                      if (json.code == '0') {
                          $.bhTip && $.bhTip({
                              content: $.i18n('bh-imp-deletePlanSuccess'),
                              state: 'success',
                              iconClass: 'icon-close'
                          });
                          reRenderPlanMapping({},opt);
                      } else {
                          $.bhTip && $.bhTip({
                              content: $.i18n('bh-imp-deletePlanFail'),
                              state: 'danger',
                              iconClass: 'icon-close'
                          });
                      }
                  },
                  error: function(e) {
                      $.bhTip && $.bhTip({
                          content: $.i18n('bh-imp-deletePlanFail'),
                          state: 'danger',
                          iconClass: 'icon-close'
                      });
                      console && console.log(e);  
                  }
              });
            }
        });  
      } else {
        $.bhTip && $.bhTip({
            content: $.i18n('bh-imp-chooseAPlan'),
            state: 'warning',
            iconClass: 'icon-close'
        }); 
      }
    };
    window.WIS_IMPORT_VIEW = importView;
    
    function createdViewHtml(isMapping){
        if(isMapping){
            content = '<div class="emap-import-mapping">'+
            '<div class="emap-import-mapping-title bh-clearfix"></div>'+
            '<div class="mapping-container bh-clearfix">'+
            '<div id="mapstep1" class="mapstep1">'+
            '<div class="emap-import-step-content mapstep1-content">'+
            '<div class="mapstep1-content-text bh-color-grey-2">'+$.i18n('bh-ip-firstTimeTips')+'<a role="downTplBtn" href="javascript:void(0)">' + $.i18n('bh-ip-downloadModules') + 
            '</a></div>'+
            '<a href="javascript:void(0)" class="bh-btn bh-btn-primary bh-btn-small emap-import-input-a">' +
            $.i18n('bh-ip-uploadFile') +
            '<input type="file" role="fileInput"/></a>' +
            '</div>'+
            '</div>'+
            '<div id="mapstep2" class="mapstep2" >'+
            '<div class="emap-map-step2">'+
            '<div class="emap-map-step2-error" >'+
            '<i class="iconfont icon-highlightremove bh-color-danger" ></i>'+
            '<div class="bh-pt-16">'+$.i18n('bh-imp-pleaseReUpload')+'</div>'+
            '</div>'+
            '<div class="emap-map-step2-success" style="position:relative;">'+
            '<div class="step2-success-header">'+
            '<div class="step2-dropdown" >'+
            '<label class="step2-dropdown-title">'+$.i18n('bh-imp-mapPlan')+'</label>:'+
            '<div class="bh-mh-4 step2-dropdown-content bh-control-form"></div>'+
            '</div>'+
            '<button class="bh-btn bh-btn-default" role="savePlanBtn">'+$.i18n('bh-imp-savePlan')+'</button>'+
            '<button role="deletePlanBtn" class="bh-btn bh-btn-default">'+$.i18n('bh-imp-deletePlan')+'</button>'+
            '</div>'+
            '<div class="step2-form" style="overflow:auto;height:220px;padding-top:0;margin-top:16px;">'+
            '<table class="bh-table">'+
            '<thead><tr class="bh-bg-primary-5"><td>'+$.i18n('bh-imp-originalColumn')+'</td><td>'+$.i18n('bh-imp-targetColumn')+'</td></tr></thead>'+
            '<tbody class="step2-form-tbody"></tbody>'+
            '</table>'+
            '</div>'+
            '</div>'+
            '</div>'+
            '</div>' +
            '<div id="mapstep3" class="mapstep3 bh-text-center bh-color-grey-3">'+
            '<p class="emap-import-map-result-detail"></p>' +
            '<p class="bh-pt-8">' + $.i18n('bh-ip-viewDetails') + 
            '<a class="emap-import-map-export" href="javascript:void(0)">' + $.i18n('bh-ip-downloadResult') + '</a>' + 
            $.i18n('bh-ip-viewing') +
            '</p>' +
            '</div>'+
            '</div></div>';
            
            setTimeout(function(){
                //debugger
                $('.emap-import-mapping').parents('.content').css("height","100%");
                $('.emap-import-mapping-title').bhStepWizard({
                    items: [
                        { stepId: "mapstep1", title: $.i18n('bh-ip-uploadFile') },
                        { stepId: "mapstep2", title: $.i18n('bh-imp-setMapping') },
                        { stepId: "mapstep3", title: $.i18n('bh-ip-finish') }
                    ],
                    active: "mapstep1",//可选, 当前激活项的stepId
                    finished: [], //可选, 当前已完成项的stepId数组,默认值为[]
                    isAddClickEvent: false,
                    change: function (param) {
                        if (param.stepId == 'mapstep1') {
                            $('#buttons').hide();
                        } else if(param.stepId == 'mapstep2') {
                            $('.bh-btn-primary','#buttons').show();
                            $('.bh-btn-default','#buttons').hide();
                            $('#buttons').show();
                            //设置popover内容与注册popover
                            if($('#emap-import-map-pop').length>0){
                                $('#emap-import-map-pop').remove()
                            }
                            $('body').append('<div id="emap-import-map-pop" class="bh-ph-8 bh-pv-8" style="display:none;"><div  class=""><label style="display: inline-block; padding-right: 4px;">'+$.i18n('bh-imp-planName')+':</label><input class="bh-control-form emap-import-step2-planTitle" placeholder="'+$.i18n('bh-imp-fiftyWords')+'" type="text" /></div><div class="bh-mt-16 bh-text-right" ><button class="bh-btn bh-btn-primary" role="map-step2-pop-ok">'+$.i18n('bh-imp-sure')+'</button></div> </div>');
                            $("#emap-import-map-pop").jqxPopover({
                                offset: {left: -50, top:0},
                                arrowOffsetValue: 50,
                                title: $.i18n('bh-imp-savePlan'),
                                showCloseButton: true,
                                selector: $("[role='savePlanBtn']")
                            });
                            //debugger
                            $('button[role="map-step2-pop-ok"]','body').off('click').on('click',function(){
                                proto.savePlan(opt,{});
                            });
                        }else {
                            $('.bh-btn-default','#buttons').show();
                            $('.bh-btn-primary','#buttons').hide();
                            $('#buttons').show();
                        }
                    } //可选, 焦点项变化的回调,默认值为null
                });
                $('#buttons').hide();
            },1); 
        }else {
            content = '<div class="emap-import-content">' +
            '<div class="emap-import-step active">' +
            '<h5 class="emap-import-step-title">' +
    
            '<span>1</span>' +
            $.i18n('bh-ip-uploadFile') +
            '<div class="bh-color-caption emap-import-p emap-import-step1-intro bh-mb-8" style="line-height:20px;white-space:initial">' + $.i18n('bh-ip-firstTimeTips') + '<a role="downTplBtn" href="javascript:void(0)">' + $.i18n('bh-ip-downloadModules') + '</a>' + $.i18n('bh-ip-viewing') + '</div>' +
            '</h5>' +
    
            '<div class="emap-import-step-content emap-import-step1-content">' +
            '<a href="javascript:void(0)" class="bh-btn bh-btn-primary bh-btn-small emap-import-input-a">' +
            $.i18n('bh-ip-startUploading') +
            '<input type="file" role="fileInput"/>' +
            '</a>' +
    
            '</p>' +
            '</div>' +
            '<div class="emap-import-step-content emap-import-step1-content" style="display: none;">' +
            '<p class="bh-color-caption emap-import-p  emap-import-step1-file">' +
            '<span class="emap-import-file-name"></span>' +
            '<span class="emap-import-file-size"></span>' +
            '</p>' +
            '<a href="javascript:void(0)" class="emap-import-reload-a bh-mh-8" role="reImportBtn">' + $.i18n('bh-ip-reupload') + '</a>' +
            '<a href="javascript:void(0)" role="importConfirmBtn" class="bh-btn bh-btn-primary bh-btn-small">' +
            $.i18n('bh-ip-sureUpload') +
            '</a>' +
    
    
            '</div>' +
            '</div>' +
            '<div class="emap-import-step ">' +
            '<h5 class="emap-import-step-title">' +
            '<span>2</span>' +
            $.i18n('bh-ip-importDatas') +
            '</h5>' +
    
            '<div class="emap-import-step-content">' +
            '<p class="emap-import-step2-intro">' + $.i18n('bh-ip-waitUploadedAutoImporting') + '</p>' +
    
            '<div class="emap-import-step2-content">' +
            '<div class="emap-import-loading-bar">' +
            '<div></div>' +
            '</div>' +
            '<p class="emap-import-step2-count"></p>' +
            '</div>' +
            '</div>' +
            '</div>' +
    
            '<div class="emap-import-step ">' +
            '<h5 class="emap-import-step-title">' +
            '<span>3</span>' +
            $.i18n('bh-ip-finish') +
            '</h5>' +
    
            '<div class="emap-import-step-content emap-import-step3-content">' +
    
            '<p class="emap-import-result-detail">' + $.i18n('bh-ip-importDetails') + '</p>' +
            '<p>' + $.i18n('bh-ip-viewDetails') + '<a class="emap-import-export" href="javascript:void(0)">' + $.i18n('bh-ip-downloadResult') + '</a>' + $.i18n('bh-ip-viewing') + '</p>' +
            '<button role="closeConfirmBtn" class="bh-btn bh-btn-default bh-btn-small bh-pull-right bh-mh-8 bh-mv-8">' + $.i18n('bh-ip-sureClose') + '</button>' +
            '</div>' +
            '</div>' +
            '</div>';
        }
    }
    function emapMapBeginImport() {
        var importParams = window.downTplData;
        importParams.attachment = window.attachment;
        importParams.mapping = getMapping();
        if (window.opt.params) {
            $.extend(importParams, window.opt.params);
        }
        if (importParams.mapping) {
            $('.emap-map-step2-success').append('<div id="emap-map-loader"></div>');
            $('#emap-map-loader').jqxLoader({autoOpen:true});
            $.ajax({
                type: 'post',
                url: window.opt.contextPath + '/sys/emapcomponent/imexport/import.do',
                data: importParams,
                dataType: 'json',
                success: function(json) {
                    $('#emap-map-loader').jqxLoader({autoOpen:false});
                    $('.emap-import-mapping-title').bhStepWizard("changeToFinished",'mapstep2');
                    $('.emap-import-mapping-title').bhStepWizard("activeNextItem");
                    if (json.attachment) {
                        $('.emap-import-map-export').attr('href',window.opt.contextPath+ '/sys/emapcomponent/file/getAttachmentFile/'+json.attachment+'.do');
                    }
                    $('.emap-import-map-result-detail').text($.i18n('bh-ip-successInImport')+','+$.i18n('bh-ip-numOfSuccess')+json.success+$.i18n('bh-ip-piece')+','+$.i18n('bh-ip-numOfFail')+(Number(json.total) - Number(json.success))+$.i18n('bh-ip-piece'));
                },
                error: function(e) {
                    $.bhTip && $.bhTip({
                        content: $.i18n('bh-ip-importFailed'),
                        state: 'danger',
                        iconClass: 'icon-close'
                    });
                    console && console.log(e);  
                }
            });
            return false;
        } else {
            console.log("mapping 是空的!!")
            return false;
        }
    }
    function getMapping(){
        var mappingString = '';
        var databaseColumns = []
        $('tr','.step2-form-tbody').each(function() {
            //debugger
            if ($(this).find(".form-tbody-select").val()) {
                mappingString += $(this).children('td')[0].textContent + ':' + $(this).find(".form-tbody-select").val() + ',';
                databaseColumns.push($(this).find(".form-tbody-select").val())
            } else {
                viewUnselectedTd();
                return '';
            }
        });
        if(_){
            var databaseColumnsTmp = _.uniq(databaseColumns);
            if(databaseColumnsTmp.length === databaseColumns.length){
                //console.log(mappingString.slice(0,mappingString.length - 1));
                 return mappingString.slice(0,mappingString.length - 1);
            }else {
                $.bhTip && $.bhTip({
                    content: $.i18n('bh-imp-noRepeatKey'),
                    state: 'warning',
                    iconClass: 'icon-close'
                });
                return '';
            }
        }else {
            console.log("未能检测到lodash,导入映射此处依赖lodash")
        } 
    }
    function viewUnselectedTd(){
        $(".form-tbody-select").each(function(){
            if (!$(this).val()) {
                $(this).addClass('form-tbody-select-red'); 
            }
        });
        $.bhTip && $.bhTip({
            content: $.i18n('bh-imp-unselectColumn'),
            state: 'danger',
            iconClass: 'icon-close'
        });
    }
    function reRenderPlanMapping(result,opt) {
        var getListData = {
            app: opt.app, // *
            module: opt.module, // *
            page: opt.page, // *
            action: opt.action,
            attachment:result.attachment?result.attachment:window.attachment
        };
        if (opt.params) {
            $.extend(getListData, opt.params);
        }
        $.ajax({
            type: 'post',
            url: opt.contextPath + '/sys/emapcomponent/import/schema/list.do',
            data: getListData,
            dataType: 'json',
            success: function(json) {
                if (json.importTitles && json.modelTitles) {
                    // var json = {
                    //     importTitles:'wid,姓名,职工号,性别,年龄,职业,籍贯',
                    //     modelTitles:'wid,职工1,姓名,性别,年龄,职业,籍贯',
                    //     rowNumber:'4',
                    //     schemas:[{
                    //         MAPPING:'职工号:职工1,姓名:姓名1',
                    //         WID:'2456321',
                    //         SCHEMA_NAME:'第一方案'
                    //     },{
                    //         MAPPING:'职工号:职工1,姓名:姓名1',
                    //         WID:'24563221',
                    //         SCHEMA_NAME:'第二方案'
                    //     }]
                    // };
                    var source = [];
                    if(json.schemas && json.schemas.length>0){
                        json.schemas.forEach(function (item) {
                            source.push({
                                label:item.SCHEMA_NAME,
                                value:item.WID+'&'+item.MAPPING
                            });
                        })
                    }
                    //是否方案下拉已经初始化过了
                    if ($('.step2-dropdown-content').children().length>0) {
                        $(".step2-dropdown-content").jqxDropDownList('clear');
                        source.forEach(function (item) {
                            $(".step2-dropdown-content").jqxDropDownList('addItem',item);
                        });
                        $(".step2-dropdown-content").jqxDropDownList('selectIndex', 0 );
                        var tmpDropDownItem = $(".step2-dropdown-content").jqxDropDownList('getSelectedItem');
                        reRenderMappingTable({mapItem:tmpDropDownItem.value.split('&')[1]});
                    } else {
                        $('.step2-dropdown-content').jqxDropDownList({ 
                            source: source,
                            width: '200',
                            height: '25',
                            placeHolder:$.i18n('bh-ddt-pleaseChoose'),
                            dropDownHeight: 150
                            //autoDropDownHeight: true
                        });
                        $('.step2-dropdown-content').on('open', function (event) {
                            if(source.length>0){
                                var tmpPlanObj = $('.step2-dropdown-content').jqxDropDownList('getSelectedItem');
                                //debugger
                                if (tmpPlanObj && tmpPlanObj.label) {
                                    if ($('.form-tbody-select-yellow').length>0) {
                                        //$(this).jqxDropDownList("close" ); 
                                        // BH_UTILS.bhDialogWarning({
                                        //     content:'当前方案已修改,请先保存。若切换方案,当前更改会丢失!'
                                        // });
                                        $.bhTip && $.bhTip({
                                            content: $.i18n('bh-imp-planHaveChanged'),
                                            state: 'warning',
                                            iconClass: 'icon-close'
                                        });
                                    }
                                } else {
                                    //$(this).jqxDropDownList("close" );
                                    // BH_UTILS.bhDialogWarning({
                                    //     content:'请先保存当前方案,若切换方案,当前方案会丢失!'
                                    // });
                                    $.bhTip && $.bhTip({
                                        content: $.i18n('bh-imp-savePlanNow'),
                                        state: 'warning',
                                        iconClass: 'icon-close'
                                    });
                                }
                            }
                        });  
                        reRenderMappingTable({resItem:json});
                        //$('.step2-form').niceScroll();
                        $('.emap-map-step2-success').show();
                        $('.emap-map-step2-error').hide();
                    }
                    //监听方案名称下拉变化
                    $('.step2-dropdown-content').on('change', function (event){     
                        var args = event.args;
                        if (args) {
                            // // index represents the item's index.                      
                            // var index = args.index;
                            var subitem = args.item;
                            // // get item's label and value.
                            // var label = item.label;
                            // var value = item.value;
                            reRenderMappingTable({mapItem:subitem.value.split('&')[1]});   
                        } 
                    });  
                } else {
                    $.bhTip && $.bhTip({
                        content: $.i18n('bh-imp-getListFail'),
                        state: 'danger',
                        iconClass: 'icon-close'
                    });
                }
            },
            error: function(e) {
                $.bhTip && $.bhTip({
                    content: $.i18n('bh-imp-getPlanFail'),
                    state: 'danger',
                    iconClass: 'icon-close'
                });
                console && console.log(e);   
            }
        });
    }
    function reRenderMappingTable(params) {
        var mappingTag = '';
        if (params) {
            var importArray = [];
            var modelArray = [];
            if (params.mapItem) {
                mappingTag = 'saved';
                var mappingArray = params.mapItem.split(',');
                mappingArray.forEach(function(item){
                    if (item.indexOf(':') != -1) {
                        importArray.push(item.split(':')[0]);
                        modelArray.push(item.split(':')[1]);
                    } else {
                        console.log('传递的mapping item参数格式错误,请检查~');
                    }
                }); 
            } else if(params.resItem) {
                mappingTag = 'saving';
                importArray = params.resItem.importTitles.split(',');
                modelArray = params.resItem.modelTitles.split(',');
            }
            $('.step2-form-tbody').html('');
            importArray.forEach(function(item,index){
                var optionHtml = '';
                var selectOptionFlag = false;
                modelArray.forEach(function(subitem,subIndex){
                    if(mappingTag === 'saving'){
                        if (item == subitem) {
                            selectOptionFlag = true;
                            optionHtml +='<option value="'+subitem+'" selected>'+subitem+'</option>';
                        } else {
                            optionHtml +='<option value="'+subitem+'">'+subitem+'</option>';
                        } 
                    }else if(mappingTag === 'saved'){
                        if (index == subIndex) {
                            selectOptionFlag = true;
                            optionHtml +='<option value="'+subitem+'" selected>'+subitem+'</option>';
                        } else {
                            optionHtml +='<option value="'+subitem+'">'+subitem+'</option>';
                        } 
                    }
                });
                var trHtml = '<tr><td>'+item+'</td><td><select class="form-tbody-select" style="width: 100%;height:24px;">'+optionHtml+'</select></td></tr>';
                $('.step2-form-tbody').append(trHtml);
                //如果未匹配到默认的option
                if(!selectOptionFlag){
                    $('.form-tbody-select:last','.step2-form-tbody').selectedIndex = -1;
                }
                //监听select值的变化
                //debugger
                $('.form-tbody-select','.step2-form-tbody').on('change','',function(e){
                    //debugger
                    $(this).addClass('form-tbody-select-yellow');
                });
            });
        } else {
            console.log('传递的mapping参数为空,请检查~');
        }
    }
})(jQuery);