variety.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. func IsVarietySuperAdmin(sysUser *system.Admin) (isSuperAdmin bool) {
  9. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  10. isSuperAdmin = true
  11. }
  12. return
  13. }
  14. func HasVarietyPlantPermission(sysUser *system.Admin, varietyId int) (hasPermission bool, err error) {
  15. hasPermission = IsVarietySuperAdmin(sysUser)
  16. if hasPermission {
  17. return
  18. }
  19. tmp, err := supply_analysis.GetVarietyPermissionByVarietyIdAndUserId(varietyId, sysUser.AdminId)
  20. if err != nil && !utils.IsErrNoRow(err) {
  21. return
  22. }
  23. if tmp != nil { //找到记录那就代表有操作权限
  24. hasPermission = true
  25. }
  26. return
  27. }
  28. func CalculateByDay(varietyInfo *supply_analysis.Variety) (dataMap map[time.Time]float64, startDate, lastDate time.Time, err error) {
  29. plantList, err := supply_analysis.GetAllVarietyPlantByVarietyId(varietyInfo.VarietyId)
  30. if err != nil {
  31. return
  32. }
  33. {
  34. currDate := time.Now()
  35. currHour := currDate.Hour()
  36. if currHour < 22 {
  37. lastDate, _ = time.ParseInLocation(utils.FormatDate, currDate.Format(utils.FormatDate), time.Local)
  38. } else {
  39. lastDate, _ = time.ParseInLocation(utils.FormatDate, currDate.AddDate(0, 0, 1).Format(utils.FormatDate), time.Local)
  40. }
  41. lastDate = lastDate.AddDate(0, 0, 365) //业务需要计算的数据日期往当前日期后面加上配置的日期
  42. }
  43. maintenanceDataMap := make(map[time.Time][]float64)
  44. for _, plantInfo := range plantList {
  45. if plantInfo.IsStop == 1 { // 如果是停产了,那么结束日期就是到 产生的数据的结束日期
  46. plantInfo.ResumptionDate = lastDate.Format(utils.FormatDate)
  47. }
  48. if plantInfo.MaintenanceDate == `` || plantInfo.ResumptionDate == `` {
  49. continue
  50. }
  51. if plantInfo.AverageDailyCapacityReduction == 0 {
  52. continue
  53. }
  54. maintenanceDate, _ := time.ParseInLocation(utils.FormatDate, plantInfo.MaintenanceDate, time.Local)
  55. if startDate.IsZero() || maintenanceDate.Before(startDate) {
  56. startDate = maintenanceDate
  57. }
  58. resumptionDate, _ := time.ParseInLocation(utils.FormatDate, plantInfo.ResumptionDate, time.Local)
  59. for tmpDate := maintenanceDate; !tmpDate.After(resumptionDate); tmpDate = tmpDate.AddDate(0, 0, 1) {
  60. dataList, ok := maintenanceDataMap[tmpDate]
  61. if !ok {
  62. dataList = make([]float64, 0)
  63. }
  64. dataList = append(dataList, plantInfo.AverageDailyCapacityReduction)
  65. maintenanceDataMap[tmpDate] = dataList
  66. }
  67. }
  68. if len(maintenanceDataMap) <= 0 {
  69. return
  70. }
  71. dataMap = make(map[time.Time]float64)
  72. for tmpDate := startDate; !tmpDate.After(lastDate); tmpDate = tmpDate.AddDate(0, 0, 1) {
  73. var calculateVal float64
  74. tmpDataList, ok := maintenanceDataMap[tmpDate]
  75. if !ok {
  76. calculateVal = 0
  77. } else {
  78. for _, tmpVal := range tmpDataList {
  79. calculateVal += tmpVal
  80. }
  81. }
  82. dataMap[tmpDate] = calculateVal
  83. }
  84. return
  85. }