【调度系统】广东民航医疗快线调度系统源代码
wzp
2024-12-05 8a09f655eace34e9c90bbfc711eedb7a69e4e068
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*jshint eqeqeq:false */
/*global jQuery, define */
(function( factory ) {
    "use strict";
    if ( typeof define === "function" && define.amd ) {
        // AMD. Register as an anonymous module.
        define([
            "jquery",
            "./grid.base",
            "./grid.grouping"
        ], factory );
    } else {
        // Browser globals
        factory( jQuery );
    }
}(function( $ ) {
"use strict";
// To optimize the search we need custom array filter
// This code is taken from
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
//module begin
function _pivotfilter (fn, context) {
    /*jshint validthis: true */
    var i,
        value,
        result = [],
        length;
        
    if (!this || typeof fn !== 'function' || (fn instanceof RegExp)) {
        throw new TypeError();
    }
 
    length = this.length;
 
    for (i = 0; i < length; i++) {
        if (this.hasOwnProperty(i)) {
            value = this[i];
            if (fn.call(context, value, i, this)) {
                result.push(value);
                // We need break in order to cancel loop 
                // in case the row is found
                break;
            }
        }
    }
    return result;
}
$.assocArraySize = function(obj) {
    // http://stackoverflow.com/a/6700/11236
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) {
            size++;
        }
    }
    return size;
};
 
$.jgrid.extend({
    pivotSetup : function( data, options ){
        // data should come in json format
        // The function return the new colModel and the transformed data
        // again with group setup options which then will be passed to the grid
        var columns =[],
        pivotrows =[],
        summaries = [],
        member=[],
        labels=[],
        groupOptions = {
            grouping : true,
            groupingView :  {
                groupField : [],
                groupSummary: [],
                groupSummaryPos:[]
            }
        },
        headers = [],
        o = $.extend ( {
            rowTotals : false,
            rowTotalsText : 'Total',
            // summary columns
            colTotals : false,
            groupSummary : true,
            groupSummaryPos :  'header',
            frozenStaticCols : false
        }, options || {});
        this.each(function(){
 
            var 
                row,
                rowindex,
                i,
                
                rowlen = data.length,
                xlen, ylen, aggrlen,
                tmp,
                newObj,
                r=0;
            // utility funcs
            /* 
             * Filter the data to a given criteria. Return the firt occurance
             */
            function find(ar, fun, extra) {
                var res;
                res = _pivotfilter.call(ar, fun, extra);
                return res.length > 0 ? res[0] : null;
            }
            /*
             * Check if the grouped row column exist (See find)
             * If the row is not find in pivot rows retun null,
             * otherviese the column
             */
            function findGroup(item, index) {
                /*jshint validthis: true */
                var j = 0, ret = true, i;
                for(i in item) {
                    if( item.hasOwnProperty(i) ) {
                        if(item[i] != this[j]) {
                            ret =  false;
                            break;
                        }
                        j++;
                        if(j>=this.length) {
                            break;
                        }
                    }
                }
                if(ret) {
                    rowindex =  index;
                }
                return ret;
            }
            /*
             * Perform calculations of the pivot values.
             */
            function calculation(oper, v, field, rc, _cnt)  {
                var ret;
                switch (oper) {
                    case  "sum" : 
                        ret = parseFloat(v||0) + parseFloat((rc[field]||0));
                        break;
                    case "count" :
                        if(v==="" || v == null) {
                            v=0;
                        }
                        if(rc.hasOwnProperty(field)) {
                            ret = v+1;
                        } else {
                            ret = 0;
                        }
                        break;
                    case "min" : 
                        if(v==="" || v == null) {
                            ret = parseFloat(rc[field]||0);
                        } else {
                            ret =Math.min(parseFloat(v),parseFloat(rc[field]||0));
                        }
                        break;
                    case "max" : 
                        if(v==="" || v == null) {
                            ret = parseFloat(rc[field]||0);
                        } else {
                            ret = Math.max(parseFloat(v),parseFloat(rc[field]||0));
                        }
                        break;
                    case "avg" : //avg grouping
                         
                        ret = (parseFloat(v||0) * (_cnt -1) + parseFloat(rc[field]||0) ) /_cnt;
                         
                        break;    
                }
                return ret;
            }
            /*
             * The function agragates the values of the pivot grid.
             * Return the current row with pivot summary values
             */
            function agregateFunc ( row, aggr, value, curr) {
                // default is sum
                var arrln = aggr.length, i, label, j, jv, mainval="",swapvals=[];
                if($.isArray(value)) {
                    jv = value.length;
                    swapvals = value;
                } else {
                    jv = 1;
                    swapvals[0]=value;
                }
                member = [];
                labels = [];
                member.root = 0;
                if(! !!curr._count ){
                    curr._count = 1;
                }else{
                    curr._count ++;
                }
                for(j=0;j<jv;j++) {
                    var  tmpmember = [], vl;
                    for(i=0; i < arrln; i++) {
                        if(value == null) {
                            label = $.trim(aggr[i].member)+"_"+aggr[i].aggregator;
                            vl = label;
                            swapvals[0]= aggr[i].label || (aggr[i].aggregator+ " " +$.trim(aggr[i].member));
                        } else {
                            vl = value[j].replace(/\s+/g, '');
                            try {
                                label = (arrln === 1 ? mainval + vl : mainval + vl+"_"+aggr[i].aggregator+"_" + String(i));
                            } catch(e) {}
                            swapvals[j] = value[j];
                        }
                        //if(j<=1 && vl !==  '_r_Totals' && mainval === "") { // this does not fix full the problem
                            //mainval = vl;
                        //}
                        label = !isNaN(parseInt(label,10)) ? label + " " : label;
                        curr[label] =  tmpmember[label] = calculation( aggr[i].aggregator, curr[label], aggr[i].member, row, curr._count);
                    }
                    mainval += value[j].replace(/\s+/g, '');
                    //vl = !isNaN(parseInt(vl,10)) ? vl + " " : vl;
                    member[label] = tmpmember;
                    labels[label] = swapvals[j];
                }
                return curr;
            }
            // Making the row totals without to add in yDimension
            if(o.rowTotals && o.yDimension.length > 0) {
                var dn = o.yDimension[0].dataName;
                o.yDimension.splice(0,0,{dataName:dn});
                o.yDimension[0].converter =  function(){ return '_r_Totals'; };
            }
            // build initial columns (colModel) from xDimension
            xlen = $.isArray(o.xDimension) ? o.xDimension.length : 0;
            ylen = o.yDimension.length;
            aggrlen  = $.isArray(o.aggregates) ? o.aggregates.length : 0;
            if(xlen === 0 || aggrlen === 0) {
                throw("xDimension or aggregates optiona are not set!");
            }
            var colc;
            for(i = 0; i< xlen; i++) {
                colc = {name:o.xDimension[i].dataName, frozen: o.frozenStaticCols};
                if(o.xDimension[i].isGroupField == null) {
                    o.xDimension[i].isGroupField =  true;
                }
                colc = $.extend(true, colc, o.xDimension[i]);
                columns.push( colc );
            }
            var groupfields = xlen - 1, tree={};
            //tree = { text: 'root', leaf: false, children: [] };
            //loop over alll the source data
            while( r < rowlen ) {
                row = data[r];
                var xValue = [];
                var yValue = []; 
                tmp = {};
                i = 0;
                // build the data from xDimension
                do {
                    xValue[i]  = $.trim(row[o.xDimension[i].dataName]);
                    tmp[o.xDimension[i].dataName] = xValue[i];
                    i++;
                } while( i < xlen );
                
                var k = 0;
                rowindex = -1;
                // check to see if the row is in our new pivotrow set
                newObj = find(pivotrows, findGroup, xValue);
                if(!newObj) {
                    // if the row is not in our set
                    k = 0;
                    // if yDimension is set
                    if(ylen>=1) {
                        // build the cols set in yDimension
                        for(k=0;k<ylen;k++) {
                            yValue[k] = $.trim(row[o.yDimension[k].dataName]);
                            // Check to see if we have user defined conditions
                            if(o.yDimension[k].converter && $.isFunction(o.yDimension[k].converter)) {
                                yValue[k] = o.yDimension[k].converter.call(this, yValue[k], xValue, yValue);
                            }
                        }
                        // make the colums based on aggregates definition 
                        // and return the members for late calculation
                        tmp = agregateFunc( row, o.aggregates, yValue, tmp );
                    } else  if( ylen === 0 ) {
                        // if not set use direct the aggregates 
                        tmp = agregateFunc( row, o.aggregates, null, tmp );
                    }
                    // add the result in pivot rows
                    pivotrows.push( tmp );
                } else {
                    // the pivot exists
                    if( rowindex >= 0) {
                        k = 0;
                        // make the recalculations 
                        if(ylen>=1) {
                            for(k=0;k<ylen;k++) {
                                yValue[k] = $.trim(row[o.yDimension[k].dataName]);
                                if(o.yDimension[k].converter && $.isFunction(o.yDimension[k].converter)) {
                                    yValue[k] = o.yDimension[k].converter.call(this, yValue[k], xValue, yValue);
                                }
                            }
                            newObj = agregateFunc( row, o.aggregates, yValue, newObj );
                        } else  if( ylen === 0 ) {
                            newObj = agregateFunc( row, o.aggregates, null, newObj );
                        }
                        // update the row
                        pivotrows[rowindex] = newObj;
                    }
                }
                var kj=0, current = null,existing = null, kk;
                // Build a JSON tree from the member (see aggregateFunc) 
                // to make later the columns 
                // 
                for (kk in member) {
                    if(member.hasOwnProperty( kk )) {
                        if(kj === 0) {
                            if (!tree.children||tree.children === undefined){
                                tree = { text: kk, level : 0, children: [], label: kk  };
                            }
                            current = tree.children;
                        } else {
                            existing = null;
                            for (i=0; i < current.length; i++) {
                                if (current[i].text === kk) {
                                //current[i].fields=member[kk];
                                    existing = current[i];
                                    break;
                                }
                            }
                            if (existing) {
                                current = existing.children;
                            } else {
                                current.push({ children: [], text: kk, level: kj,  fields: member[kk], label: labels[kk] });
                                current = current[current.length - 1].children;
                            }
                        }
                        kj++;
                    }
                }
                r++;
            }
            var  lastval=[], initColLen = columns.length, swaplen = initColLen;
            if(ylen>0) {
                headers[ylen-1] = {    useColSpanStyle: false,    groupHeaders: []};
            }
            /*
             * Recursive function which uses the tree to build the 
             * columns from the pivot values and set the group Headers
             */
            function list(items) {
                var l, j, key, k, col;
                for (key in items) {     // iterate
                    if (items.hasOwnProperty(key)) {
                    // write amount of spaces according to level
                    // and write name and newline
                        if(typeof items[key] !== "object") {
                            // If not a object build the header of the appropriate level
                            if( key === 'level') {
                                if(lastval[items.level] === undefined) {
                                    lastval[items.level] ='';
                                    if(items.level>0 && items.text.indexOf('_r_Totals') === -1) {
                                        headers[items.level-1] = {
                                            useColSpanStyle: false,
                                            groupHeaders: []
                                        };
                                    }
                                }
                                if(lastval[items.level] !== items.text && items.children.length && items.text.indexOf('_r_Totals') === -1 ) {
                                    if(items.level>0) {
                                        headers[items.level-1].groupHeaders.push({
                                            titleText: items.label,
                                            numberOfColumns : 0
                                        });
                                        var collen = headers[items.level-1].groupHeaders.length-1,
                                        colpos = collen === 0 ? swaplen : initColLen;//+aggrlen;
                                        if(items.level-1=== (o.rowTotals ? 1 : 0)) {
                                            if(collen>0) {
                                                var l1=0;
                                                for(var kk=0; kk<collen; kk++) { 
                                                    l1 += headers[items.level-1].groupHeaders[kk].numberOfColumns;
                                                }
                                                if(l1) {
                                                    colpos = l1  + xlen;
                                                }
                                            }
                                        }
                                        if(columns[colpos]) {
                                            headers[items.level-1].groupHeaders[collen].startColumnName = columns[colpos].name;
                                            headers[items.level-1].groupHeaders[collen].numberOfColumns = columns.length - colpos;
                                        }
                                        initColLen = columns.length;
                                    }
                                }
                                lastval[items.level] = items.text;
                            }
                            // This is in case when the member contain more than one summary item
                            if(items.level === ylen  && key==='level' && ylen >0) {
                                if( aggrlen > 1){
                                    var ll=1;
                                    for( l in items.fields) {
                                        if(items.fields.hasOwnProperty(l)) {
                                            if(ll===1) {
                                                headers[ylen-1].groupHeaders.push({startColumnName: l, numberOfColumns: 1, titleText: items.label || items.text});
                                            }
                                            ll++;
                                        }
                                    }
                                    headers[ylen-1].groupHeaders[headers[ylen-1].groupHeaders.length-1].numberOfColumns = ll-1;
                                } else {
                                    headers.splice(ylen-1,1);
                                }
                            }
                        }
                        // if object, call recursively
                        if (items[key] != null && typeof items[key] === "object") {
                            list(items[key]);
                        }
                        // Finally build the columns
                        if( key === 'level') {
                            if( items.level > 0 &&  (items.level === (ylen===0?items.level:ylen) || lastval[items.level].indexOf('_r_Totals') !== -1 ) ){
                                j=0;
                                for(l in items.fields) {
                                    if(items.fields.hasOwnProperty( l ) ) {
                                        col = {};
                                        for(k in o.aggregates[j]) {
                                            if(o.aggregates[j].hasOwnProperty(k)) {
                                                switch( k ) {
                                                    case 'member':
                                                    case 'label':
                                                    case 'aggregator':
                                                        break;
                                                    default:
                                                        col[k] = o.aggregates[j][k];
                                                }
                                            }
                                        }    
                                        if(aggrlen > 1) {
                                            col.name = l;
                                            col.label = o.aggregates[j].label || items.label;
                                        } else {
                                            col.name = items.text;
                                            col.label = items.text==='_r_Totals' ? o.rowTotalsText : items.label;
                                        }
                                        columns.push (col);
                                        j++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
 
            list( tree );
            var nm;
            // loop again trougth the pivot rows in order to build grand total 
            if(o.colTotals) {
                var plen = pivotrows.length;
                while(plen--) {
                    for(i=xlen;i<columns.length;i++) {
                        nm = columns[i].name;
                        if(!summaries[nm]) {
                            summaries[nm] = parseFloat(pivotrows[plen][nm] || 0);
                        } else {
                            summaries[nm] += parseFloat(pivotrows[plen][nm] || 0);
                        }
                    }
                }
            }
            // based on xDimension  levels build grouping 
            if( groupfields > 0) {
                for(i=0;i<groupfields;i++) {
                    if(columns[i].isGroupField) {
                        groupOptions.groupingView.groupField.push(columns[i].name);
                        groupOptions.groupingView.groupSummary.push(o.groupSummary);
                        groupOptions.groupingView.groupSummaryPos.push(o.groupSummaryPos);
                    }
                }
            } else {
                // no grouping is needed
                groupOptions.grouping = false;
            }
            groupOptions.sortname = columns[groupfields].name;
            groupOptions.groupingView.hideFirstGroupCol = true;
        });
        // return the final result.
        return { "colModel" : columns, "rows": pivotrows, "groupOptions" : groupOptions, "groupHeaders" :  headers, summary : summaries };
    },
    jqPivot : function( data, pivotOpt, gridOpt, ajaxOpt) {
        return this.each(function(){
            var $t = this;
 
            function pivot( data) {
                var pivotGrid = jQuery($t).jqGrid('pivotSetup',data, pivotOpt),
                footerrow = $.assocArraySize(pivotGrid.summary) > 0 ? true : false,
                query= $.jgrid.from.call($t, pivotGrid.rows), i, so, st, len;
                if(pivotOpt.ignoreCase) {
                    query = query.ignoreCase();
                }
                for(i=0; i< pivotGrid.groupOptions.groupingView.groupField.length; i++) {
                    so = pivotOpt.xDimension[i].sortorder ? pivotOpt.xDimension[i].sortorder : 'asc';
                    st = pivotOpt.xDimension[i].sorttype ? pivotOpt.xDimension[i].sorttype : 'text';
                    query.orderBy(pivotGrid.groupOptions.groupingView.groupField[i], so, st, '', st);
                }
                len = pivotOpt.xDimension.length;
                if(pivotGrid.groupOptions.sortname && len) {
                    so = pivotOpt.xDimension[len-1].sortorder ? pivotOpt.xDimension[len-1].sortorder : 'asc';
                    st = pivotOpt.xDimension[len-1].sorttype ? pivotOpt.xDimension[len-1].sorttype : 'text';
                    query.orderBy(pivotGrid.groupOptions.sortname, so, st, '', st);                    
                }
                jQuery($t).jqGrid($.extend(true, {
                    datastr: $.extend(query.select(),footerrow ? {userdata:pivotGrid.summary} : {}),
                    datatype: "jsonstring",
                    footerrow : footerrow,
                    userDataOnFooter: footerrow,
                    colModel: pivotGrid.colModel,
                    viewrecords: true,
                    sortname: pivotOpt.xDimension[0].dataName // ?????
                }, pivotGrid.groupOptions, gridOpt || {}));
                var gHead = pivotGrid.groupHeaders;
                if(gHead.length) {
                    for( i = 0;i < gHead.length ; i++) {
                        if(gHead[i] && gHead[i].groupHeaders.length) {
                            jQuery($t).jqGrid('setGroupHeaders',gHead[i]);
                        }
                    }
                }
                if(pivotOpt.frozenStaticCols) {
                    jQuery($t).jqGrid("setFrozenColumns");
                }
            }
 
            if(typeof data === "string") {
                $.ajax($.extend({
                    url : data,
                    dataType: 'json',
                    success : function(response) {
                        pivot($.jgrid.getAccessor(response, ajaxOpt && ajaxOpt.reader ? ajaxOpt.reader: 'rows') );
                    }
                }, ajaxOpt || {}) );
            } else {
                pivot( data );
            }
        });
    }
});
//module end
}));