luck_sheet_table.go 6.1 KB

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