lucky_sheet_table.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package excel
  2. import (
  3. "eta_gn/eta_api/models/data_manage/excel"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. func HandleTableCell(oldTableData TableData) (newTableData TableData) {
  12. newTableData = oldTableData
  13. mergeList := oldTableData.MergeList
  14. lenMergeList := len(mergeList)
  15. if lenMergeList <= 0 {
  16. return
  17. }
  18. deleteRowMap := make(map[int]map[int]int)
  19. deleteRowList := make([]int, 0)
  20. deleteColumnMap := make(map[int][]int, 0)
  21. for i := lenMergeList - 1; i >= 0; i-- {
  22. tmpMerge := mergeList[i]
  23. for rowIndex := tmpMerge.MergeRowNum; rowIndex >= 0; rowIndex-- {
  24. tmpColumnMap := make(map[int]int)
  25. if columnMap, ok := deleteRowMap[tmpMerge.StartRowIndex+rowIndex]; ok {
  26. tmpColumnMap = columnMap
  27. } else {
  28. deleteRowList = append(deleteRowList, tmpMerge.StartRowIndex+rowIndex)
  29. }
  30. deleteColumnList := make([]int, 0)
  31. if columnList, ok := deleteColumnMap[tmpMerge.StartRowIndex+rowIndex]; ok {
  32. deleteColumnList = columnList
  33. }
  34. for columnIndex := tmpMerge.MergeColumnNum; columnIndex >= 0; columnIndex-- {
  35. if rowIndex == 0 && columnIndex == 0 {
  36. continue
  37. }
  38. tmpColumnMap[tmpMerge.StartColumnIndex+columnIndex] = tmpMerge.StartColumnIndex + columnIndex
  39. deleteColumnList = append(deleteColumnList, tmpMerge.StartColumnIndex+columnIndex)
  40. }
  41. deleteRowMap[tmpMerge.StartRowIndex+rowIndex] = tmpColumnMap
  42. deleteColumnMap[tmpMerge.StartRowIndex+rowIndex] = deleteColumnList
  43. }
  44. }
  45. sort.Ints(deleteRowList)
  46. for i := len(deleteRowList) - 1; i >= 0; i-- {
  47. rowIndex := deleteRowList[i]
  48. deleteColumnList := deleteColumnMap[rowIndex]
  49. sort.Ints(deleteColumnList)
  50. for i := len(deleteColumnList) - 1; i >= 0; i-- {
  51. columnIndex := deleteColumnList[i]
  52. if rowIndex >= len(newTableData.TableDataList) {
  53. continue
  54. }
  55. tmpColumnDataList := newTableData.TableDataList[rowIndex]
  56. if columnIndex >= len(tmpColumnDataList) {
  57. continue
  58. }
  59. newTableData.TableDataList[rowIndex] = append(tmpColumnDataList[:columnIndex], tmpColumnDataList[columnIndex+1:]...) // 删除开头N个元素
  60. }
  61. }
  62. for rowIndex, colList := range newTableData.TableDataList {
  63. for colIndex, v := range colList {
  64. v.Monitor = handleCellVal(v)
  65. colList[colIndex] = v
  66. }
  67. newTableData.TableDataList[rowIndex] = colList
  68. }
  69. return
  70. }
  71. func handleCellVal(tmpTableColData LuckySheetDataValue) (valueStr string) {
  72. valueStr = tmpTableColData.Monitor
  73. if valueStr == `` {
  74. if valueStr == `` && tmpTableColData.CellType.S != nil {
  75. for _, cellS := range tmpTableColData.CellType.S {
  76. valueStr += fmt.Sprint(cellS.Value)
  77. }
  78. }
  79. }
  80. return
  81. }
  82. func HandleRuleToTableCell(excelInfoId int, oldTableData TableData) (newTableData TableData, err error) {
  83. newTableData = oldTableData
  84. excelRuleMappingList, err := excel.GetExcelRuleMappingByExcelInfoId(excelInfoId)
  85. if err != nil {
  86. return
  87. }
  88. if len(excelRuleMappingList) == 0 {
  89. return
  90. }
  91. tableDataList := oldTableData.TableDataList
  92. excelRuleMap := make(map[int]*excel.ExcelInfoRuleMappingView)
  93. for _, v := range excelRuleMappingList {
  94. excelRuleMap[v.ExcelInfoRuleMappingId] = v
  95. }
  96. ruleScopeMap := generateRuleScopeIndexMap(excelRuleMappingList)
  97. for row, scopeValues := range ruleScopeMap {
  98. for col, ruleId := range scopeValues {
  99. if v, ok := excelRuleMap[ruleId]; ok {
  100. if len(tableDataList) > row && len(tableDataList[row]) > col {
  101. if checkCellRule(v, tableDataList[row][col].Monitor, tableDataList) {
  102. tableDataList[row][col].Background = v.BackgroundColor
  103. tableDataList[row][col].FontColor = v.FontColor
  104. }
  105. } else {
  106. continue
  107. }
  108. } else {
  109. continue
  110. }
  111. }
  112. }
  113. return
  114. }
  115. func getCellValueByType(value string, valueType int, tableDataList [][]LuckySheetDataValue) (float64, bool) {
  116. if valueType == 2 {
  117. coords := strings.Split(value, ",")
  118. var coordIntArr []int
  119. for _, v := range coords {
  120. t, _ := strconv.Atoi(v)
  121. coordIntArr = append(coordIntArr, t)
  122. }
  123. if len(coordIntArr) == 2 {
  124. x, y := coordIntArr[0]-1, coordIntArr[1]-1
  125. conditionValue, err := strconv.ParseFloat(tableDataList[y][x].Monitor, 64)
  126. if err != nil {
  127. return 0, false
  128. }
  129. return conditionValue, true
  130. }
  131. } else {
  132. conditionValue, err := strconv.ParseFloat(value, 64)
  133. if err != nil {
  134. return 0, false
  135. }
  136. return conditionValue, true
  137. }
  138. return 0, false
  139. }
  140. func checkCellRule(ruleInfo *excel.ExcelInfoRuleMappingView, value string, tableDataList [][]LuckySheetDataValue) bool {
  141. var tableValue float64
  142. var tableTime time.Time
  143. var err error
  144. if ruleInfo.RuleType == 5 {
  145. tableTime, err = time.ParseInLocation(utils.FormatDate, value, time.Local)
  146. if err != nil {
  147. return false
  148. }
  149. } else {
  150. tableValue, err = strconv.ParseFloat(value, 64)
  151. if err != nil {
  152. return false
  153. }
  154. }
  155. switch ruleInfo.RuleType {
  156. case 1:
  157. conditionValue, ok := getCellValueByType(ruleInfo.LeftValueBack, ruleInfo.LeftValueType, tableDataList)
  158. if !ok {
  159. return false
  160. }
  161. if tableValue > conditionValue {
  162. return true
  163. }
  164. case 2:
  165. conditionValue, ok := getCellValueByType(ruleInfo.LeftValueBack, ruleInfo.LeftValueType, tableDataList)
  166. if !ok {
  167. return false
  168. }
  169. if tableValue < conditionValue {
  170. return true
  171. }
  172. case 3:
  173. leftcondValue, ok := getCellValueByType(ruleInfo.LeftValueBack, ruleInfo.LeftValueType, tableDataList)
  174. if !ok {
  175. return false
  176. }
  177. rightcondValue, ok := getCellValueByType(ruleInfo.RightValueBack, ruleInfo.RightValueType, tableDataList)
  178. if !ok {
  179. return false
  180. }
  181. if leftcondValue <= tableValue && tableValue <= rightcondValue {
  182. return true
  183. }
  184. case 4:
  185. conditionValue, ok := getCellValueByType(ruleInfo.LeftValueBack, ruleInfo.LeftValueType, tableDataList)
  186. if !ok {
  187. return false
  188. }
  189. if tableValue == conditionValue {
  190. return true
  191. }
  192. case 5:
  193. var dateStart, dataEnd time.Time
  194. switch ruleInfo.LeftValueBack {
  195. case "today":
  196. dateStart = utils.Today()
  197. if tableTime == dateStart {
  198. return true
  199. }
  200. case "tomorrow":
  201. dateStart = utils.Tomorrow()
  202. if tableTime == dateStart {
  203. return true
  204. }
  205. case "last7days":
  206. dateStart, dataEnd = utils.Last7Days()
  207. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  208. return true
  209. }
  210. case "lastweek":
  211. dateStart, dataEnd = utils.LastWeek()
  212. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  213. return true
  214. }
  215. case "thisweek":
  216. dateStart, dataEnd = utils.ThisWeek()
  217. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  218. return true
  219. }
  220. case "nextweek":
  221. dateStart, dataEnd = utils.NextWeek()
  222. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  223. return true
  224. }
  225. case "lastmonth":
  226. dateStart, dataEnd = utils.LastMonth()
  227. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  228. return true
  229. }
  230. case "thismonth":
  231. dateStart, dataEnd = utils.ThisMonth()
  232. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  233. return true
  234. }
  235. case "nextmonth":
  236. dateStart, dataEnd = utils.NextMonth()
  237. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  238. return true
  239. }
  240. default:
  241. return false
  242. }
  243. }
  244. return false
  245. }
  246. func generateRuleScopeIndexMap(items []*excel.ExcelInfoRuleMappingView) (ruleScopeMap map[int]map[int]int) {
  247. ruleScopeMap = make(map[int]map[int]int)
  248. for _, item := range items {
  249. coords := strings.Split(item.ScopeCoord, ",")
  250. var coordIntArr []int
  251. for _, v := range coords {
  252. t, _ := strconv.Atoi(v)
  253. coordIntArr = append(coordIntArr, t)
  254. }
  255. if len(coords) == 4 {
  256. xmin, ymin, xmax, ymax := coordIntArr[0]-1, coordIntArr[1]-1, coordIntArr[2]-1, coordIntArr[3]-1
  257. for i := ymin; i <= ymax; i++ {
  258. for j := xmin; j <= xmax; j++ {
  259. if _, ok := ruleScopeMap[i]; !ok {
  260. ruleScopeMap[i] = make(map[int]int)
  261. }
  262. ruleScopeMap[i][j] = item.ExcelInfoRuleMappingId
  263. }
  264. }
  265. }
  266. if len(coords) == 2 {
  267. x, y := coordIntArr[0]-1, coordIntArr[1]-1
  268. if _, ok := ruleScopeMap[y]; !ok {
  269. ruleScopeMap[y] = make(map[int]int)
  270. }
  271. ruleScopeMap[y][x] = item.ExcelInfoRuleMappingId
  272. }
  273. }
  274. return
  275. }