luck_sheet_table.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package excel
  2. import (
  3. "eta_gn/eta_chart_lib/models"
  4. "eta_gn/eta_chart_lib/utils"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. func HandleRuleToTableCell(excelInfoId int, oldTableData TableData) (newTableData TableData, err error) {
  10. newTableData = oldTableData
  11. excelRuleMappingList, err := models.GetExcelRuleMappingByExcelInfoId(excelInfoId)
  12. if err != nil {
  13. return
  14. }
  15. if len(excelRuleMappingList) == 0 {
  16. return
  17. }
  18. tableDataList := oldTableData.TableDataList
  19. excelRuleMap := make(map[int]*models.ExcelInfoRuleMappingView)
  20. for _, v := range excelRuleMappingList {
  21. excelRuleMap[v.ExcelInfoRuleMappingId] = v
  22. }
  23. ruleScopeMap := generateRuleScopeIndexMap(excelRuleMappingList)
  24. for row, scopeValues := range ruleScopeMap {
  25. for col, ruleId := range scopeValues {
  26. if v, ok := excelRuleMap[ruleId]; ok {
  27. if len(tableDataList) > row && len(tableDataList[row]) > col {
  28. if checkCellRule(v, tableDataList[row][col].Monitor, tableDataList) {
  29. tableDataList[row][col].Background = v.BackgroundColor
  30. tableDataList[row][col].FontColor = v.FontColor
  31. }
  32. } else {
  33. continue
  34. }
  35. } else {
  36. continue
  37. }
  38. }
  39. }
  40. return
  41. }
  42. func getCellValueByType(value string, valueType int, tableDataList [][]LuckySheetDataValue) (float64, bool) {
  43. if valueType == 2 {
  44. coords := strings.Split(value, ",")
  45. var coordIntArr []int
  46. for _, v := range coords {
  47. t, _ := strconv.Atoi(v)
  48. coordIntArr = append(coordIntArr, t)
  49. }
  50. if len(coordIntArr) == 2 {
  51. x, y := coordIntArr[0]-1, coordIntArr[1]-1
  52. conditionValue, err := strconv.ParseFloat(tableDataList[y][x].Monitor, 64)
  53. if err != nil {
  54. return 0, false
  55. }
  56. return conditionValue, true
  57. }
  58. } else {
  59. conditionValue, err := strconv.ParseFloat(value, 64)
  60. if err != nil {
  61. return 0, false
  62. }
  63. return conditionValue, true
  64. }
  65. return 0, false
  66. }
  67. func checkCellRule(ruleInfo *models.ExcelInfoRuleMappingView, value string, tableDataList [][]LuckySheetDataValue) bool {
  68. var tableValue float64
  69. var tableTime time.Time
  70. var err error
  71. if ruleInfo.RuleType == 5 {
  72. tableTime, err = time.ParseInLocation(utils.FormatDate, value, time.Local)
  73. if err != nil {
  74. return false
  75. }
  76. } else {
  77. tableValue, err = strconv.ParseFloat(value, 64)
  78. if err != nil {
  79. return false
  80. }
  81. }
  82. switch ruleInfo.RuleType {
  83. case 1:
  84. conditionValue, ok := getCellValueByType(ruleInfo.LeftValueBack, ruleInfo.LeftValueType, tableDataList)
  85. if !ok {
  86. return false
  87. }
  88. if tableValue > conditionValue {
  89. return true
  90. }
  91. case 2:
  92. conditionValue, ok := getCellValueByType(ruleInfo.LeftValueBack, ruleInfo.LeftValueType, tableDataList)
  93. if !ok {
  94. return false
  95. }
  96. if tableValue < conditionValue {
  97. return true
  98. }
  99. case 3:
  100. leftcondValue, ok := getCellValueByType(ruleInfo.LeftValueBack, ruleInfo.LeftValueType, tableDataList)
  101. if !ok {
  102. return false
  103. }
  104. rightcondValue, ok := getCellValueByType(ruleInfo.RightValue, ruleInfo.RightValueType, tableDataList)
  105. if !ok {
  106. return false
  107. }
  108. if leftcondValue <= tableValue && tableValue <= rightcondValue {
  109. return true
  110. }
  111. case 4:
  112. conditionValue, ok := getCellValueByType(ruleInfo.LeftValueBack, ruleInfo.LeftValueType, tableDataList)
  113. if !ok {
  114. return false
  115. }
  116. if tableValue == conditionValue {
  117. return true
  118. }
  119. case 5:
  120. var dateStart, dataEnd time.Time
  121. switch ruleInfo.LeftValueBack {
  122. case "today":
  123. dateStart = utils.Today()
  124. if tableTime == dateStart {
  125. return true
  126. }
  127. case "tomorrow":
  128. dateStart = utils.Tomorrow()
  129. if tableTime == dateStart {
  130. return true
  131. }
  132. case "last7days":
  133. dateStart, dataEnd = utils.Last7Days()
  134. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  135. return true
  136. }
  137. case "lastweek":
  138. dateStart, dataEnd = utils.LastWeek()
  139. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  140. return true
  141. }
  142. case "thisweek":
  143. dateStart, dataEnd = utils.ThisWeek()
  144. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  145. return true
  146. }
  147. case "nextweek":
  148. dateStart, dataEnd = utils.NextWeek()
  149. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  150. return true
  151. }
  152. case "lastmonth":
  153. dateStart, dataEnd = utils.LastMonth()
  154. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  155. return true
  156. }
  157. case "thismonth":
  158. dateStart, dataEnd = utils.ThisMonth()
  159. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  160. return true
  161. }
  162. case "nextmonth":
  163. dateStart, dataEnd = utils.NextMonth()
  164. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  165. return true
  166. }
  167. default:
  168. return false
  169. }
  170. }
  171. return false
  172. }
  173. func generateRuleScopeIndexMap(items []*models.ExcelInfoRuleMappingView) (ruleScopeMap map[int]map[int]int) {
  174. ruleScopeMap = make(map[int]map[int]int)
  175. for _, item := range items {
  176. coords := strings.Split(item.ScopeCoord, ",")
  177. var coordIntArr []int
  178. for _, v := range coords {
  179. t, _ := strconv.Atoi(v)
  180. coordIntArr = append(coordIntArr, t)
  181. }
  182. if len(coords) == 4 {
  183. xmin, ymin, xmax, ymax := coordIntArr[0]-1, coordIntArr[1]-1, coordIntArr[2]-1, coordIntArr[3]-1
  184. for i := ymin; i <= ymax; i++ {
  185. for j := xmin; j <= xmax; j++ {
  186. if _, ok := ruleScopeMap[i]; !ok {
  187. ruleScopeMap[i] = make(map[int]int)
  188. }
  189. ruleScopeMap[i][j] = item.ExcelInfoRuleMappingId
  190. }
  191. }
  192. }
  193. if len(coords) == 2 {
  194. x, y := coordIntArr[0]-1, coordIntArr[1]-1
  195. if _, ok := ruleScopeMap[y]; !ok {
  196. ruleScopeMap[y] = make(map[int]int)
  197. }
  198. ruleScopeMap[y][x] = item.ExcelInfoRuleMappingId
  199. }
  200. }
  201. return
  202. }