luck_sheet_table.go 5.8 KB

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