1 | import grails.orm.PagedResultList |
---|
2 | |
---|
3 | /** |
---|
4 | * Service class that encapsulates the business logic for Task searches. |
---|
5 | */ |
---|
6 | class TaskReportService { |
---|
7 | |
---|
8 | boolean transactional = false |
---|
9 | |
---|
10 | def authService |
---|
11 | def dateUtilService |
---|
12 | // def messageSource |
---|
13 | |
---|
14 | // def g = new org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib() |
---|
15 | |
---|
16 | def paramsMax = 100000 |
---|
17 | |
---|
18 | /** |
---|
19 | * Selects and returns the reactive ratio. |
---|
20 | * @param params The request params, may contain param to specify the search. |
---|
21 | * @param locale The locale to use when generating result.message. |
---|
22 | */ |
---|
23 | def getReactiveRatio(params, locale) { |
---|
24 | def result = [:] |
---|
25 | |
---|
26 | def currentUser = authService.currentUser |
---|
27 | def startOfToday = dateUtilService.today |
---|
28 | def startOfYesterday = dateUtilService.yesterday |
---|
29 | def startOfTomorrow = dateUtilService.tomorrow |
---|
30 | def oneWeekAgo = dateUtilService.oneWeekAgo |
---|
31 | |
---|
32 | def paginateParams = [:] |
---|
33 | paginateParams.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
34 | paginateParams.offset = params?.offset?.toInteger() ?: 0 |
---|
35 | |
---|
36 | def sort = "task." + (params?.sort ?: "targetStartDate") |
---|
37 | def order = params?.order == "desc" ? "desc" : "asc" |
---|
38 | def orderBy = " order by " + sort + ' ' + order |
---|
39 | |
---|
40 | def namedParams = [:] |
---|
41 | namedParams.startDate = params.startDate ?: dateUtilService.today |
---|
42 | namedParams.endDate = params.endDate ?: dateUtilService.tomorrow |
---|
43 | namedParams.immediateCallout = TaskType.read(1) |
---|
44 | namedParams.unscheduledBreakin = TaskType.read(2) |
---|
45 | namedParams.preventativeMaintenance = TaskType.read(4) |
---|
46 | |
---|
47 | result.taskQuery = "from Task as task \ |
---|
48 | where (task.trash = false \ |
---|
49 | and task.targetStartDate < :endDate \ |
---|
50 | and task.targetStartDate >= :startDate \ |
---|
51 | and ( \ |
---|
52 | task.taskType = :immediateCallout \ |
---|
53 | or task.taskType = :unscheduledBreakin \ |
---|
54 | or task.taskType = :preventativeMaintenance \ |
---|
55 | ) \ |
---|
56 | )" |
---|
57 | |
---|
58 | result.taskQuery = "select distinct task " + result.taskQuery + orderBy |
---|
59 | result.taskList = Task.executeQuery(result.taskQuery, namedParams, paginateParams) |
---|
60 | result.taskCount = result.taskList.size() |
---|
61 | |
---|
62 | // Counts |
---|
63 | result.totalTaskOnAssetCount = 0 |
---|
64 | result.immediateCalloutCount = 0 |
---|
65 | result.unscheduledBreakinCount = 0 |
---|
66 | result.preventativeMaintenanceCount = 0 |
---|
67 | |
---|
68 | // Count the tasks performed against assets. |
---|
69 | result.taskList.each() { task -> |
---|
70 | if(task.primaryAsset) { |
---|
71 | result.totalTaskOnAssetCount++ |
---|
72 | if(task.taskType == namedParams.immediateCallout) result.immediateCalloutCount++ |
---|
73 | if(task.taskType == namedParams.unscheduledBreakin) result.unscheduledBreakinCount++ |
---|
74 | if(task.taskType == namedParams.preventativeMaintenance) result.preventativeMaintenanceCount++ |
---|
75 | } |
---|
76 | task.associatedAssets.each() { associatedAsset -> |
---|
77 | if(associatedAsset.id != task.primaryAsset?.id) { |
---|
78 | result.totalTaskOnAssetCount++ |
---|
79 | if(task.taskType == namedParams.immediateCallout) result.immediateCalloutCount++ |
---|
80 | if(task.taskType == namedParams.unscheduledBreakin) result.unscheduledBreakinCount++ |
---|
81 | if(task.taskType == namedParams.preventativeMaintenance) result.preventativeMaintenanceCount++ |
---|
82 | } |
---|
83 | } |
---|
84 | } // each() task |
---|
85 | |
---|
86 | // Percentages |
---|
87 | result.immediateCalloutPercentage = 0 |
---|
88 | result.totalPreventativePercentage = 0 |
---|
89 | |
---|
90 | result.totalPreventativeCount = result.unscheduledBreakinCount + result.preventativeMaintenanceCount |
---|
91 | try { |
---|
92 | result.immediateCalloutPercentage = (result.immediateCalloutCount / result.totalTaskOnAssetCount)*100 |
---|
93 | result.immediateCalloutPercentage = result.immediateCalloutPercentage.toInteger() |
---|
94 | result.totalPreventativePercentage = (result.totalPreventativeCount / result.totalTaskOnAssetCount)*100 |
---|
95 | result.totalPreventativePercentage = result.totalPreventativePercentage.toInteger() |
---|
96 | } |
---|
97 | catch(ArithmeticException e) { |
---|
98 | } |
---|
99 | |
---|
100 | // Success. |
---|
101 | return result |
---|
102 | |
---|
103 | } // getQuickSearch |
---|
104 | |
---|
105 | |
---|
106 | } // end class |
---|