questionnaireManagerCtrl.js 13.8 KB
(function (angular) {
    function questionnaireManagerCtrl($scope,questionnaireService,SweetAlertX,DTColumnBuilder,DTOptionsBuilder,$uibModal) {

        $scope.questionnaire = {};

        $scope.statusList = [
            {value : null, text : '全部'},
            {value : 0, text : '待完善'},
            {value : 1, text : '待发送'},
            {value : 2, text : '已发送'},
            {value : 3, text : '已撤回'}];

        $scope.unanswerTypeList = [
            {value : null, text : '全部'},
            {value : 1, text : '大于0'},
            {value : 2, text : '等于0'}];

        $scope.typeList = [
            {value : null, text : '全部'},
            {value : 1, text : '打分型'},
            {value : 2, text : '多选型'}];

        $scope.operateList = [
            [{name : '查看与编辑'},{name : '查看商户明细'},{name : '复制'}],//待完善
            [{name : '发送'},{name : '查看与编辑'},{name : '查看商户明细'},{name : '复制'}],//待发送
            [{name : '撤回'},{name : '查看问卷'},{name : '查看结果'},{name : '查看商户明细'},{name : '复制'}],//已发送
            [{name : '发送'},{name : '查看问卷'},{name : '查看结果'},{name : '查看商户明细'},{name : '复制'}]//已撤回
        ];

        $scope.dtColumns = [
            DTColumnBuilder.newColumn('code').withOption('width',40).withTitle('问卷编号'),
            DTColumnBuilder.newColumn('name').withOption('width',80).withTitle('问卷名称'),
            DTColumnBuilder.newColumn('copyCode').withOption('width',40).withTitle('问卷来源'),
            DTColumnBuilder.newColumn('type').withOption('width',30).withTitle('类型').renderWith(function(data, type, obj, oSettings) {
                for(var i in $scope.typeList){
                    if($scope.typeList[i].value === data){
                        return $scope.typeList[i].text;
                    }
                }
            }),
            DTColumnBuilder.newColumn('status').withOption('width',30).withTitle('状态').renderWith(function(data, type, obj, oSettings) {
                for(var i in $scope.statusList){
                    if($scope.statusList[i].value === data){
                        return $scope.statusList[i].text;
                    }
                }
            }),
            DTColumnBuilder.newColumn('respondentsCount').withOption('width',40).withTitle('调查商户数').renderWith(function(data, type, obj, oSettings) {
                return data || '--';
            }),
            DTColumnBuilder.newColumn('unanswerCount').withOption('width',40).withTitle('待提交商户').renderWith(function(data, type, obj, oSettings) {
                if(obj.status === 0 || obj.status === 1){
                    return '--';
                }
                return data;
            }),
            DTColumnBuilder.newColumn('createdDate').withOption('width',60).withTitle('创建时间'),
            DTColumnBuilder.newColumn('updatedDate').withOption('width',60).withTitle('更新时间'),
            DTColumnBuilder.newColumn(null).withTitle('操作').withOption('width', 180).withClass('text-center').renderWith(function(data, type, full, oSettings) {
                var str = '<div class="form-inline">';
                angular.forEach($scope.operateList[data.status],function (item) {
                    str += '<div class="form-group"><a class="btn btn-link" ng-click="operate(\''+ item.name +'\','+ data.id +',\''+ data.code+'\',' +'\''+ data.name+'\','+ data.type +')">'+ item.name +'</a></div>'
                });
                str += '</div>';
                return str;
            })
        ];

        $scope.dtOptions = DTOptionsBuilder.newOptions()
            .withOption('id','id')
            .withFixedColumns({
                leftColumns: 1,
                rightColumns: 0
            })
            .withOption('lengthMenu', [20,40,60,100])
            .withOption('select', {
                type: 'checkbox'
            })
            .withOption('num', true)
            .withButtons([{
                text : '批量发送',
                action : function(e, dt, node, config) {
                    $scope.changeStatusList(2,'发送');
                }
            },{
                text : '批量撤回',
                action : function(e, dt, node, config) {
                    $scope.changeStatusList(3,'撤回');
                }
            },{
                text : '新建问卷',
                action : function(e, dt, node, config) {
                    $scope.add();
                }
            }]);

        $scope.dtInstance = {
            serverData : questionnaireService.list
        };

        $scope.operate = function (name,id,code,qnName,type) {
            switch (name){
                case '查看与编辑':
                    $scope.view(id);
                    break;
                case '查看问卷':
                    $scope.view(id);
                    break;
                case '查看商户明细':
                    $scope.viewMerchant(id,code,qnName);
                    break;
                case '查看结果':
                	$scope.answer(qnName,id, code, type);
                    break;
                case '复制':
                    $scope.copy(id);
                    break;
                case '发送':
                    $scope.changeStatus(id,2,'发送');
                    break;
                case '撤回':
                    $scope.changeStatus(id,3,'撤回');
                    break;
                default:
                    break;
            }
        };

        $scope.answer = function (name, id, code, type) {
        	 if (type == 2) {
        		 $uibModal.open({
                     templateUrl : 'biz/questionnaire/view/answerExamineChooesView.html',
                     controller : 'answerExamineChooesCtrl',
                     size : 'lg',
                     scope:$scope,
                     resolve : {
                    	 pageParam : function() {
    	                        return {
    	                        	 qnId: id,
    	                        	 qnCode: code,
    	                        	 qnName: name,
    	                        	 qnType: type
    	                        };
    	                    }                 }
                 }).result.then(function(){
                     //关闭弹框后执行方法
                 },function () {
                     $scope.select();
                 });
        	 } else if (type == 1) {
        		 $uibModal.open({
                     templateUrl : 'biz/questionnaire/view/answerExamineGradeView.html',
                     controller : 'answerExamineGradeCtrl',
                     size : 'lg',
                     scope:$scope,
                     resolve : {
                    	 pageParam : function() {
    	                        return {
    	                        	 qnId: id,
    	                        	 qnCode: code,
    	                        	 qnName: name,
    	                        	 qnType: type
    	                        };
    	                    }                 }
                 }).result.then(function(){
                     //关闭弹框后执行方法
                 },function () {
                     $scope.select();
                 });
        	 }
        };

        $scope.viewMerchant = function (id,code,qnName) {
            $uibModal.open({
                templateUrl : 'biz/questionnaire/view/viewMerchantDetail.html',
                controller : 'viewMerchantDetailCtrl',
                size : 'lg',
                scope:$scope,
                resolve : {
                    param: function () {
                        return {
                            id:id,
                            code:code,
                            qnName:qnName
                        };
                    }
                }
            }).result.then(function(){
                //关闭弹框后执行方法
            },function () {
                $scope.select();
            });
        };

        $scope.view = function (id) {
            $uibModal.open({
                templateUrl : 'biz/questionnaire/view/viewQuestionnaire.html',
                controller : 'viewQuestionnaireCtrl',
                size : 'lg',
                scope:$scope,
                resolve : {
                    param: function () {
                        return id;
                    }
                }
            }).result.then(function(){
                //关闭弹框后执行方法
            },function () {
                $scope.select();
            });
        };

        $scope.changeStatus = function (ids,status,name) {
            SweetAlertX.confirm({
                text: "是否确认"+ name +"问卷?",
                confirmButtonText: "确认",
                cancelButtonText: "取消"
            }, function () {
                questionnaireService.changeStatus({ids:ids,status:status},function (res) {
                   if(res && res.data){
                       $scope.select();
                       SweetAlertX.alert('',name + '问卷成功','success');
                   }else {
                       SweetAlertX.alert('',name + '问卷失败','error');
                   }
                });
            })
        };

        $scope.changeStatusList = function(status,name){
            var _selectedItems = $scope.dtInstance.getSelectItems();
            var ids = [];
            if (_selectedItems.length > 0) {
                for (var i = 0; i < _selectedItems.length; i++) {
                    var o = _selectedItems[i];
                    ids.push(o.id);
                }
                $scope.changeStatus(ids,status,name);
            } else {
                SweetAlertX.alert('','请选择需要'+ name +'的问卷','error');
            }
        };

        $scope.getParam = function () {
            var info = $scope.questionnaire;
            var param = {};
            var obj = {};
            if($scope.selectCondition && $scope.conditionText){
                param[$scope.selectCondition] = $scope.conditionText;
            }
            if(info.status != null){
                param.status = info.status.value;
            }
            if(info.unanswerType != null){
                param.unanswerType = info.unanswerType.value;
            }
            if(info.type != null){
                param.type = info.type.value;
            }
            if (info.createTime && info.createTime.startDate && info.createTime.endDate) {
                param.createdDateStart = info.createTime.startDate.format('YYYY-MM-DD HH:mm:ss');
                param.createdDateEnd = info.createTime.endDate.format('YYYY-MM-DD HH:mm:ss');
            }
            if (info.updateTime && info.updateTime.startDate && info.updateTime.endDate) {
                param.updatedDateStart = info.updateTime.startDate.format('YYYY-MM-DD HH:mm:ss');
                param.updatedDateEnd = info.updateTime.endDate.format('YYYY-MM-DD HH:mm:ss');
            }
            if($scope.copyCode){
                param.copyCode = $scope.copyCode;
            }
            obj.condition = param;
            return obj;
        };

        $scope.select = function () {
            var param = $scope.getParam();
            $scope.dtInstance.query(param);
        };

        $scope.add = function () {
            $uibModal.open({
                templateUrl : 'biz/questionnaire/view/addQuestionnaire.html',
                controller : 'addQuestionnaireCtrl',
                size : 'lg',
                scope:$scope,
                resolve : {}
            }).result.then(function(){
                //关闭弹框后执行方法
            },function () {
                $scope.select();
            });
        };

        $scope.copy = function (id) {
            questionnaireService.getById({id:id},function (res) {
                if(res && res.data){
                    $uibModal.open({
                        templateUrl : 'copy',
                        controller : 'copyCtrl',
                        size : 'md',
                        scope:$scope,
                        resolve : {
                            param: function () {
                                return res.data;
                            }
                        }
                    }).result.then(function(){
                        //关闭弹框后执行方法
                    },function () {
                        $scope.select();
                    });
                }
            });
        }
    }

    function copyCtrl($scope,param,questionnaireService,$uibModalInstance,SweetAlertX) {
        $scope.questionnaire = param;
        $scope.copyTypes = [
            {value:1,text:'仅复制问卷'},
            {value:2,text:'复制问卷+所有调查商户'},
            {value:3,text:'复制问卷+待提交商户'}
        ];

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

        $scope.confirm = function () {
            if ($scope.copyForm.$invalid) {
                // 验证没通过
                $scope.submitted = true;
                return;
            }
            if(!$scope.questionnaire || !$scope.questionnaire.code || !$scope.copyType){
                return;
            }
            var obj = {
                qnCode:$scope.questionnaire.code,
                type:$scope.copyType.value
            };
            questionnaireService.copy(obj,function (res) {
                if(res && res.data){
                    $scope.close();
                    SweetAlertX.alert('','复制问卷成功','success');
                }else{
                    SweetAlertX.alert('','复制问卷失败','error');
                }
            })
        }
    }

    angular.module('questionnaire')
        .controller('questionnaireManagerCtrl', questionnaireManagerCtrl)
        .controller('copyCtrl', copyCtrl);
})(angular);