| 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 namedParams = [:] |
|---|
| 27 | namedParams.startDate = params.startDate ?: dateUtilService.today |
|---|
| 28 | namedParams.endDate = params.endDate ?: dateUtilService.today |
|---|
| 29 | namedParams.endDate++ // Start of next day required. |
|---|
| 30 | namedParams.immediateCallout = TaskType.read(1) |
|---|
| 31 | namedParams.unscheduledBreakin = TaskType.read(2) |
|---|
| 32 | namedParams.preventativeMaintenance = TaskType.read(4) |
|---|
| 33 | namedParams.notStarted = TaskStatus.read(1) |
|---|
| 34 | |
|---|
| 35 | result.taskQuery = "from Task as task \ |
|---|
| 36 | where (task.trash = false \ |
|---|
| 37 | and task.taskStatus != :notStarted \ |
|---|
| 38 | and task.targetStartDate < :endDate \ |
|---|
| 39 | and task.targetStartDate >= :startDate \ |
|---|
| 40 | and ( \ |
|---|
| 41 | task.taskType = :immediateCallout \ |
|---|
| 42 | or task.taskType = :unscheduledBreakin \ |
|---|
| 43 | or task.taskType = :preventativeMaintenance \ |
|---|
| 44 | ) \ |
|---|
| 45 | )" |
|---|
| 46 | |
|---|
| 47 | result.taskQuery = "select distinct task " + result.taskQuery |
|---|
| 48 | result.taskList = Task.executeQuery(result.taskQuery, namedParams) |
|---|
| 49 | result.taskCount = result.taskList.size() |
|---|
| 50 | |
|---|
| 51 | // Counts |
|---|
| 52 | result.totalAssetsOnTasksCount = 0 |
|---|
| 53 | result.immediateCalloutCount = 0 |
|---|
| 54 | result.unscheduledBreakinCount = 0 |
|---|
| 55 | result.preventativeMaintenanceCount = 0 |
|---|
| 56 | |
|---|
| 57 | result.summaryOfCalculationMethod = 'HQL query: \n\n' |
|---|
| 58 | def tempStringArray = result.taskQuery.split(' ') |
|---|
| 59 | tempStringArray.each() { |
|---|
| 60 | if(it != '') result.summaryOfCalculationMethod += it +'\n' |
|---|
| 61 | } |
|---|
| 62 | result.summaryOfCalculationMethod += '\n'+'Calculations: '+'\n\n' |
|---|
| 63 | |
|---|
| 64 | result.summaryOfCalculationMethod += 'totalAssetsOnTasksCount = A count of unique assets on each task. \n' |
|---|
| 65 | result.taskList.each() { task -> |
|---|
| 66 | if(task.primaryAsset) { |
|---|
| 67 | result.totalAssetsOnTasksCount++ |
|---|
| 68 | if(task.taskType == namedParams.immediateCallout) result.immediateCalloutCount++ |
|---|
| 69 | if(task.taskType == namedParams.unscheduledBreakin) result.unscheduledBreakinCount++ |
|---|
| 70 | if(task.taskType == namedParams.preventativeMaintenance) result.preventativeMaintenanceCount++ |
|---|
| 71 | } |
|---|
| 72 | task.associatedAssets.each() { associatedAsset -> |
|---|
| 73 | if(associatedAsset.id != task.primaryAsset?.id) { |
|---|
| 74 | result.totalAssetsOnTasksCount++ |
|---|
| 75 | if(task.taskType == namedParams.immediateCallout) result.immediateCalloutCount++ |
|---|
| 76 | if(task.taskType == namedParams.unscheduledBreakin) result.unscheduledBreakinCount++ |
|---|
| 77 | if(task.taskType == namedParams.preventativeMaintenance) result.preventativeMaintenanceCount++ |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | } // each() task |
|---|
| 81 | |
|---|
| 82 | // Percentages |
|---|
| 83 | result.immediateCalloutPercentage = 0 |
|---|
| 84 | result.totalPreventativePercentage = 0 |
|---|
| 85 | |
|---|
| 86 | result.summaryOfCalculationMethod += 'totalPreventativeCount = unscheduledBreakinCount + preventativeMaintenanceCount\n' |
|---|
| 87 | result.totalPreventativeCount = result.unscheduledBreakinCount + result.preventativeMaintenanceCount |
|---|
| 88 | try { |
|---|
| 89 | result.summaryOfCalculationMethod += 'immediateCalloutPercentage = (immediateCalloutCount / totalAssetsOnTasksCount)*100 \n' |
|---|
| 90 | result.summaryOfCalculationMethod += 'totalPreventativePercentage = (totalPreventativeCount / totalAssetsOnTasksCount)*100 \n' |
|---|
| 91 | result.immediateCalloutPercentage = (result.immediateCalloutCount / result.totalAssetsOnTasksCount)*100 |
|---|
| 92 | result.totalPreventativePercentage = (result.totalPreventativeCount / result.totalAssetsOnTasksCount)*100 |
|---|
| 93 | } |
|---|
| 94 | catch(ArithmeticException e) { |
|---|
| 95 | log.error "Could not calculate percentages: "+e |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | // Success. |
|---|
| 99 | return result |
|---|
| 100 | |
|---|
| 101 | } // getQuickSearch |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | } // end class |
|---|