import.js 8.45 KB
/**
 * Created by DEV003 on 2015/1/14.
 */
angular.module("xn/template/importButton.html", []).run(["$templateCache", function ($templateCache) {
    "use strict";
    $templateCache.put("xn/template/importButton.html",
        "<div class=\"mb_10 pr_10 clearfix\" >"+
            "<a class=\"btn btn-primary  btn-sm  f-right\" ng-click=\"openImport()\">{{importLine.title}}</a>"+
        "</div>");
}]);

angular.module("xn/template/importBox.html", []).run(["$templateCache", function ($templateCache) {
    "use strict";
    $templateCache.put("xn/template/importBox.html",
        "<div class=\"modal-content   xn-import\" >"+
            "<div class=\"modal-header\">"+
            "   <h3 class=\"modal-title ng-binding\">导入</h3>"+
            "</div>"+
            "<div class=\"xn-input-line  mt_20 clearfix\">"+
                "<label for=\"inputFile\" class=\"col-sm-offset-2 col-sm-2 f-left l-h-34 mr_10 \">选择导入文件:</label>"+
                "<div class=\"xn-input\">"+
                    "<div class=\"input-group\">"+
                        "<input type=\"text\" class=\"form-control\" id=\"{{textId}}\"  placeholder=\"导入文件\" name=\"textFile\"  />"+
                        "<input type=\"file\" class=\"none\" id=\"{{selectItemId}}\"  ng-click=\"upFileClick()\" contentEditable=\"false\" accept=\"textml\" />"+
                        "<label class=\"input-group-addon xn-cursor\" for=\"{{selectItemId}}\">选择</label>"+
                    "</div>"+
                "</div>"+

            "</div>"+
            "<div class=\"xn-input-line clearfix\">"+
                "<a class=\"col-sm-2 col-sm-offset-2  l-h-34\" href=\"javascript:;\" ng-click=\"downloadTemplate()\">下载模板</a>"+
                "<a ng-if=\"isImportError\" class=\"col-sm-2  l-h-34 \" href=\"javascript:;\" ng-click=\"downloadError()\">下载错误信息</a>"+
                "<a ng-if=\"lineData\" class=\"col-sm-2  l-h-34 \" href=\"javascript:;\" ng-click=\"importData()\">导入正确数据</a>"+
            "</div>"+
            "<div class=\"form-group  clearfix\">"+
                "<div class=\"form-group clearfix\">"+
                    "<div  class=\"hint col-md-6 col-sm-offset-2\">注意!导入前请仔细检查导入数据的准确性!</div>"+
                "</div>"+
            "</div>"+
            "<div class=\"modal-footer\">"+
                "<button class=\"btn btn-primary\"  ng-click=\"upload()\">上传</button>"+
                "<button class=\"btn btn-warning\" ng-click=\"cancel()\">取消</button>"+
            "</div>"+
        "<div>");
}]);


var xnImportLine = function ($http,$modal,FoundationService,dialogService) {
    return {
        restrict: "A",
        templateUrl: "xn/template/importButton.html",
        scope: {importLine: '=ngModel'},
        replace: true,
        require: "?ngModel",
        link: function (scope,ngModel) {
            if (!ngModel) return;
            var importLine = scope.importLine;

            scope.openImport = function(){
                importLine.flag = true;
                var modalInstance = $modal.open({
                    templateUrl: "xn/template/importBox.html",
                    controller: ImportLineController,
                    size:"lg",
                    resolve: {
                        items: function () {
                            return {
                                uploadMethod:importLine.uploadMethod,
                                downLoadUrl:importLine.downLoadUrl,
                                downLoadMethod:importLine.downLoadMethod,
                                params:importLine.params
                            };
                        }
                    }
                });
                modalInstance.result.then(function (data) {
                    importLine.data = data;
                }, function () {
                });
            }
        }
    };
};

var ImportLineController  =["$http","$scope","$modalInstance","items","dialogService",
    function ($http,$scope, $modalInstance,items,dialogService) {

        var uploadMethod = items.uploadMethod;
        var downLoadUrl = items.downLoadUrl;
        var downLoadMethod = items.downLoadMethod;
        var json = items.params;

        $scope.selectItemId = "AA"+new Date().getTime();
        var selectItem = document.getElementById($scope.selectItemId);
        var i = 0;
        while(selectItem != null){
            $scope.selectItemId = "AA"+new Date().getTime()+i;
            selectItem = document.getElementById($scope.selectItemId);
            i++;
        }

        $scope.textId = "BB"+new Date().getTime();
        var textItem = document.getElementById($scope.textId);
        while(textItem != null){
            $scope.textId = "BB"+new Date().getTime()+i;
            textItem = document.getElementById($scope.textId);
            i++;
        }

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };

        $scope.upFileClick=function(){
            var upImportFileId=document.getElementById($scope.selectItemId);
            upImportFileId.onchange=function(){
                var upImportFile;
                var upImportFileList =upImportFileId.files;
                var upImportFilename;
                if(upImportFileList){
                    upImportFile = upImportFileList[0];
                    upImportFilename=upImportFile.name;
                }else{
                    upImportFilename =upImportFileId.value;
                }
                var str =upImportFilename.substr(upImportFilename.indexOf(".")+1);
                if(!(str=="xls" || str=="xlsx" || str=="et")){
                    $("#"+$scope.textId).val("");
                    dialogService.tip([{"message":"请上传Excel支持的文件(xls,xlsx,et等)!" }]);
                }else{
                    $("#"+$scope.textId).val(upImportFilename);
                }
            };
        };

        $scope.upload = function(){
            $scope.importList(json).success(function(data){
                $scope.lineData = null;
                if(data.errors === null || data.errors.length > 0){
                    dialogService.tip([{"message":"存在错误数据!" }]);
                    if(data.url != null){
                        $scope.isImportError = true;
                        $scope.errotUrl = data.url;
                    }
                    if(data.result != null){
                        if(data.result.length>0){
                            $scope.lineData = data;
                        }
                    }
                }else{
                    $scope.isImportError = false;
                    $scope.errotUrl = null;
                    dialogService.tip([{"message": "导入成功!"}]);
                    $modalInstance.close(data);
                }
            });
        };

        $scope.downloadTemplate = function(){
            $scope.outputTemplate().success(function(data){
                if(data.errors === null || data.errors.length > 0){
                    dialogService.tip(data.errors);
                } else {
                    window.open(data.url,"_self");
                }
            });
        };


        $scope.importData = function(){
            dialogService.tip([{"message": "导入成功!"}]);
            $modalInstance.close($scope.lineData);
        };

        //导入列表
        $scope.importList = function(json){
            var file = document.getElementById($scope.selectItemId).files[0];
            var url="/api/fileImport.do";
            var fd = new FormData();
            fd.append("file",file);
            fd.append("method", uploadMethod);
            fd.append("json",JSON.stringify(json));
            return $http({
                method : 'POST',
                url : url,
                headers: {'Content-Type': undefined },
                transformRequest: angular.identity,
                data: fd
            });
        };

        //导出列表
        $scope.outputTemplate = function(){
            return $http({
                method: 'POST',
                url: downLoadUrl,
                params: {"method": downLoadMethod}
            });
        };

        //下载错误
        $scope.downloadError = function(){
            window.open($scope.errotUrl,"_self");
        }
    }];


angular.module("xn.directive.import", ["ng","xn.service.foundation","xn/template/importButton.html","xn/template/importBox.html"])
    .directive('xnImportLine', ["$http","$modal","FoundationService","dialogService", xnImportLine]);