app-index-analysis.js 12.8 KB
Newer Older
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
// indexAnalysis
frameworkModule.directive('appIndexAnalysis', ['$log', '$translate', '$timeout', 'vatSessionService', 'dashboardConfigSummaryService', 'vatDashboardService',
    function ($log, $translate, $timeout, vatSessionService, dashboardConfigSummaryService, vatDashboardService) {
        'use strict';
        $log.debug('appIndexAnalysis.ctor()...');

        return {
            restrict: 'A',
            templateUrl: '/app/dashboard/app-index-analysis/app-index-analysis.html' + '?_=' + Math.random(),
            replace: true,
            scope: true,
            link: function (scope, element) {
                var thisConstant = {
                    filterMonth: 'filterMonth',
                    dateFormate: 'yyyy-MM',
                    dashSignSeparator: '-',
                    rowCountPerPage: 9,
                    defaultHeight: 400,
                    orgColumn: 'v1',
                    prefixField: 'v'
                };

                var thisDateService = {

                    // 获取当前月份的上一个月时间
                    getLastMonth: function () {
                        var date = new Date();
                        date.setMonth(date.getMonth() - 1);
                        return date;
                    },

                    // 获取查询条件
                    getQueryModel: function () {
                        var arr = scope.filterMonth.split(thisConstant.dashSignSeparator);
                        var year = parseInt(arr[0]);
                        var month = parseInt(arr[1]);

                        var model = {
                            year: year,
                            period: month,
                            organizationIDList: scope.selectedOrganizationList
                        };

                        return model;
                    },

                    // 验证有效月份
                    validDateMonth: function () {
                        var date = scope.filterMonth + '-01';
                        return window.PWC.checkDate(date);
                    },

                    // 转换为页面展示数据
                    parseShowData: function (data) {
                        if (!_.isEmpty(data)) {
                            var values = _.values(data.header);
                            var keys = _.keys(data.header);
                            var length = keys ? keys.length : 0;
                            data.keys = keys;
                            data.headerObj = JSON.parse(data.headerObj);
                        }

                        return data;
                    },

                    // 查询汇总数据
                    querySummaryProcess: function () {
                        if (!thisDateService.validDateMonth) {
                            return;
                        }

                        var model = thisDateService.getQueryModel();
                        vatSessionService.querySummayDashboardModel = model;
                        dashboardConfigSummaryService.querySingleSummaryData(model, scope.widget.dataModel.chartConfig).success(function (data) {
                            // 进行数据转换
                            scope.querySummaryResult = thisDateService.parseShowData(data);
                            if (!_.isEmpty(scope.querySummaryResult)) {
                                scope.hasData = true;
                            } else {
                                scope.hasData = false;
                            }

                            $timeout(function () {
                                thisDateService.drawTableList(scope.querySummaryResult);
                            }, 100);
                        });
                    },

                    drawTableList: function (data) {
                        if (!_.isEmpty(data)) {
                            thisDateService.drawTable(data);
                        }
                    },

                    drawTable: function (node) {
                        if (_.isEmpty(node.data)) {
                            node.isEmpty = true;
                        }
                        else {
                            node.isEmpty = false;
                            var vatGallery = thisDateService.createGalleryData(node);

                            var itemTemplate = function (item, index) {
                                var columns = thisDateService.createColumns(node);
                                if (index === 0) {
                                    var blankAreaHeight = (thisConstant.rowCountPerPage - item.dataValue.length) * 40;
                                    var newHeight = thisConstant.defaultHeight - blankAreaHeight;
                                    $timeout(function () {
                                        element.find('.table-content').height(newHeight);
                                    }, 100);
                                }

                                return $('<div class="gallery-table"/>').dxDataGrid({
                                    dataSource: item.dataValue,
                                    width: '100%',
                                    showRowLines: true,
                                    showColumnLines: false,
                                    noDataText: '',
                                    cellHintEnabled: true,
                                    showBorders: false,
                                    format: { type: 'fixedPoint', precision: 2 },
                                    columns: columns
                                });

                            };
                            var galleryWidget = element.find('.table-content').dxGallery({
                                dataSource: vatGallery,
                                loop: false,
                                width: '100%',
                                showIndicator: true,
                                noDataText: '',
                                showNavButtons: true,
                                itemTemplate: itemTemplate,

                            }).dxGallery("instance");
                        }
                    },
                    // 构造GalleryData
                    createGalleryData: function (node) {

                        var index = Math.ceil(node.data.length / thisConstant.rowCountPerPage);
                        // var x = node.data.length % thisConstant.rowCountPerPage;
                        // if (x !== 0) {
                        //     index = index + 1;
                        // }

                        var result = [];
                        for (var i = 0; i < index; i++) {
                            var item = {};
                            item.id = i;
                            item.dataValue = [];
                            var start = i * thisConstant.rowCountPerPage;
                            for (var j = start; j < start + thisConstant.rowCountPerPage; j++) {
                                var rowData = angular.copy(node.data[j]);
                                if (!rowData) {
                                    continue;
                                }

                                for (var k = 1; k < node.headerObj.length; k++) {
                                    var dataModel = node.headerObj[k];
                                    var field = thisConstant.prefixField + (k + 1);
                                    if (dataModel.dataType === 1) {
                                        rowData[field] = Number(rowData[field]).formatAmount();

                                    } else if (dataModel.dataType === 3) {
                                        rowData[field] = rowData[field] == 1 ? '是' : '否';
                                    }
                                    else {
                                        var percentData = Number(rowData[field]) * 100;
                                        rowData[field] = Number(percentData).formatAmount() + '%';
                                    }
                                }
                                item.dataValue.push(rowData);
                            }

                            result.push(item);
                        }

                        return result;
                    },

                    // 构造列表
                    createColumns: function (node) {
                        var result = [];
                        node.keys.forEach(function (key) {
                            var item = {};
                            item.dataField = key,
                                item.caption = $translate.instant(node.header[key]);
                            if (key !== thisConstant.orgColumn) {
                                item.format = { type: 'fixedPoint' };
                                item.cssClass = 'orange-color';
                                item.alignment = 'right';
                            }

                            result.push(item);
                        });

                        return result;
                    },

                    isChangedArray: function (newValue, oldValue) {
                        if (!newValue && !oldValue) {
                            return false;
                        }

                        if (!newValue) {
                            return true;
                        }

                        if (!oldValue) {
                            return true;
                        }

                        if (newValue.length !== oldValue.length) {
                            return true;
                        }

                        // 排序
                        newValue = newValue.sort();
                        oldValue = oldValue.sort();

                        for (var i = 0; i < newValue.length; i++) {
                            if (newValue[i] !== oldValue[i]) {
                                return true;
                            }
                        }

                        return false;
                    },
                    querySummary: function (queryModel) {
                        if (thisDateService.isChangedArray(queryModel, scope.selectedOrganizationList)) {
                            scope.selectedOrganizationList = queryModel;
                            thisDateService.querySummaryProcess();
                        }
                    }
                };

                var mainModule = {

                    // 初始化参数
                    initParams: function () {
                        scope.selectedOrganizationList = [];
                        scope.hasData = true;
                        mainModule.initFilterMonth();

                        mainModule.watchFilter();
                    },

                    // 初始化查询月份
                    initFilterMonth: function () {

                        //if (vatSessionService.lastDashboardSummaryQueryMonth) {
                        //    scope.filterMonth = vatSessionService.lastDashboardSummaryQueryMonth;
                        //} else {
                        //    var lastmonth = thisDateService.getLastMonth();
                        //    scope.filterMonth = lastmonth.formatDateTime(thisConstant.dateFormate);
                        //}

                        // TODO: Dashboard Demo for Edmond
                        var lastmonth = new Date(2017, 0, 1);
                        scope.filterMonth = lastmonth.formatDateTime(thisConstant.dateFormate);

                        var elem1 = $("#" + thisConstant.filterMonth);
                        elem1.datepicker({
                            minViewMode: 1,
                            autoclose: true,
                            language: "zh-CN",
                            format: "yyyy-mm",
                        });

                        elem1.datepicker('setDate', lastmonth);
                    },

                    // 监视查询条件变更
                    watchFilter: function () {

                        // 日期选择改变时
                        scope.$watch('filterMonth', function (newValue, oldValue) {
                            if (newValue !== oldValue) {
                                vatSessionService.lastDashboardSummaryQueryMonth = newValue;
                                thisDateService.querySummaryProcess();
                            }
                        });
                    },

                    // 入口函数
                    main: function () {
                        mainModule.initParams();
                    }
                };

                //开始
                (function initialize() {
                    $log.debug('appIndexAnalysis.link.initialize()...');
                    scope.querySummary = thisDateService.querySummary;
                    scope.showFilter = false;
                    mainModule.main();
                })();
            }
        };
    }
]);