variety.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package supply_analysis
  2. import (
  3. "eta_gn/eta_api/models/data_manage/supply_analysis"
  4. "eta_gn/eta_api/models/system"
  5. "eta_gn/eta_api/utils"
  6. "time"
  7. )
  8. // IsVarietySuperAdmin 判断是否供应分析的品种超管
  9. func IsVarietySuperAdmin(sysUser *system.Admin) (isSuperAdmin bool) {
  10. //ficc管理员和超管有权限创建和管理品种
  11. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  12. isSuperAdmin = true
  13. }
  14. return
  15. }
  16. // HasVarietyPlantPermission 判断是否供应分析的品种超管
  17. func HasVarietyPlantPermission(sysUser *system.Admin, varietyId int) (hasPermission bool, err error) {
  18. hasPermission = IsVarietySuperAdmin(sysUser)
  19. if hasPermission {
  20. return
  21. }
  22. tmp, err := supply_analysis.GetVarietyPermissionByVarietyIdAndUserId(varietyId, sysUser.AdminId)
  23. if err != nil && !utils.IsErrNoRow(err) {
  24. return
  25. }
  26. if tmp != nil { //找到记录那就代表有操作权限
  27. hasPermission = true
  28. }
  29. return
  30. }
  31. // CalculateByDay 计算日度减产量
  32. func CalculateByDay(varietyInfo *supply_analysis.Variety) (dataMap map[time.Time]float64, startDate, lastDate time.Time, err error) {
  33. // 获取所有的装置
  34. plantList, err := supply_analysis.GetAllVarietyPlantByVarietyId(varietyInfo.VarietyId)
  35. if err != nil {
  36. return
  37. }
  38. // 产生的数据的结束日期
  39. {
  40. currDate := time.Now()
  41. currHour := currDate.Hour()
  42. if currHour < 22 {
  43. lastDate, _ = time.ParseInLocation(utils.FormatDate, currDate.Format(utils.FormatDate), time.Local)
  44. } else {
  45. lastDate, _ = time.ParseInLocation(utils.FormatDate, currDate.AddDate(0, 0, 1).Format(utils.FormatDate), time.Local)
  46. }
  47. lastDate = lastDate.AddDate(0, 0, 365) //业务需要计算的数据日期往当前日期后面加上配置的日期
  48. }
  49. // 维修数据的开始日期和结束日期
  50. //所有设备在该日期的减产数
  51. maintenanceDataMap := make(map[time.Time][]float64)
  52. for _, plantInfo := range plantList {
  53. if plantInfo.IsStop == 1 { // 如果是停产了,那么结束日期就是到 产生的数据的结束日期
  54. plantInfo.ResumptionDate = lastDate.Format(utils.FormatDate)
  55. }
  56. // 数据不全
  57. if plantInfo.MaintenanceDate == `` || plantInfo.ResumptionDate == `` {
  58. continue
  59. }
  60. // 日均产量减量为0,说明数据不全,不参与计算
  61. if plantInfo.AverageDailyCapacityReduction == 0 {
  62. continue
  63. }
  64. // 开始检修日期
  65. maintenanceDate, _ := time.ParseInLocation(utils.FormatDate, plantInfo.MaintenanceDate, time.Local)
  66. if startDate.IsZero() || maintenanceDate.Before(startDate) {
  67. startDate = maintenanceDate
  68. }
  69. // 结束检修日期
  70. resumptionDate, _ := time.ParseInLocation(utils.FormatDate, plantInfo.ResumptionDate, time.Local)
  71. for tmpDate := maintenanceDate; !tmpDate.After(resumptionDate); tmpDate = tmpDate.AddDate(0, 0, 1) {
  72. dataList, ok := maintenanceDataMap[tmpDate]
  73. if !ok {
  74. dataList = make([]float64, 0)
  75. }
  76. dataList = append(dataList, plantInfo.AverageDailyCapacityReduction)
  77. maintenanceDataMap[tmpDate] = dataList
  78. }
  79. }
  80. // 一个有用的装置都没有,那么就不需要计算了
  81. if len(maintenanceDataMap) <= 0 {
  82. return
  83. }
  84. // 计算出数据
  85. dataMap = make(map[time.Time]float64)
  86. for tmpDate := startDate; !tmpDate.After(lastDate); tmpDate = tmpDate.AddDate(0, 0, 1) {
  87. var calculateVal float64
  88. tmpDataList, ok := maintenanceDataMap[tmpDate]
  89. // 如果没有维修数据,那么退出当前循环,进入下一个循环
  90. if !ok {
  91. calculateVal = 0
  92. } else {
  93. for _, tmpVal := range tmpDataList {
  94. calculateVal += tmpVal
  95. }
  96. }
  97. dataMap[tmpDate] = calculateVal
  98. }
  99. return
  100. }