lucky_sheet_table.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. package excel
  2. import (
  3. "eta/eta_api/models/data_manage/excel"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // HandleTableCell 前端d毛需要我根据合并单元格处理掉多余的单元格
  12. func HandleTableCell(oldTableData TableData) (newTableData TableData) {
  13. newTableData = oldTableData
  14. mergeList := oldTableData.MergeList
  15. lenMergeList := len(mergeList)
  16. if lenMergeList <= 0 {
  17. return
  18. }
  19. deleteRowMap := make(map[int]map[int]int)
  20. deleteRowList := make([]int, 0)
  21. deleteColumnMap := make(map[int][]int, 0)
  22. for i := lenMergeList - 1; i >= 0; i-- {
  23. tmpMerge := mergeList[i]
  24. //
  25. for rowIndex := tmpMerge.MergeRowNum; rowIndex >= 0; rowIndex-- {
  26. tmpColumnMap := make(map[int]int)
  27. if columnMap, ok := deleteRowMap[tmpMerge.StartRowIndex+rowIndex]; ok {
  28. tmpColumnMap = columnMap
  29. } else {
  30. deleteRowList = append(deleteRowList, tmpMerge.StartRowIndex+rowIndex)
  31. }
  32. deleteColumnList := make([]int, 0)
  33. if columnList, ok := deleteColumnMap[tmpMerge.StartRowIndex+rowIndex]; ok {
  34. deleteColumnList = columnList
  35. }
  36. for columnIndex := tmpMerge.MergeColumnNum; columnIndex >= 0; columnIndex-- {
  37. if rowIndex == 0 && columnIndex == 0 {
  38. continue
  39. }
  40. tmpColumnMap[tmpMerge.StartColumnIndex+columnIndex] = tmpMerge.StartColumnIndex + columnIndex
  41. deleteColumnList = append(deleteColumnList, tmpMerge.StartColumnIndex+columnIndex)
  42. }
  43. // 待删除的行
  44. deleteRowMap[tmpMerge.StartRowIndex+rowIndex] = tmpColumnMap
  45. // 该行待删除的列
  46. deleteColumnMap[tmpMerge.StartRowIndex+rowIndex] = deleteColumnList
  47. }
  48. }
  49. sort.Ints(deleteRowList)
  50. for i := len(deleteRowList) - 1; i >= 0; i-- {
  51. rowIndex := deleteRowList[i]
  52. deleteColumnList := deleteColumnMap[rowIndex]
  53. sort.Ints(deleteColumnList)
  54. for i := len(deleteColumnList) - 1; i >= 0; i-- {
  55. columnIndex := deleteColumnList[i]
  56. // 最后一行合并单元格时,就不再次移除合并的单元格,避免数组越界
  57. if rowIndex >= len(newTableData.TableDataList) {
  58. continue
  59. }
  60. tmpColumnDataList := newTableData.TableDataList[rowIndex]
  61. // 最后一列合并单元格时,就不再次移除合并的单元格,避免数组越界
  62. if columnIndex >= len(tmpColumnDataList) {
  63. continue
  64. }
  65. newTableData.TableDataList[rowIndex] = append(tmpColumnDataList[:columnIndex], tmpColumnDataList[columnIndex+1:]...) // 删除开头N个元素
  66. //fmt.Println("row:", rowIndex, "===column:", columnIndex)
  67. }
  68. }
  69. // 数据值处理
  70. for rowIndex, colList := range newTableData.TableDataList {
  71. for colIndex, v := range colList {
  72. v.Monitor = handleCellVal(v)
  73. colList[colIndex] = v
  74. }
  75. newTableData.TableDataList[rowIndex] = colList
  76. }
  77. return
  78. }
  79. // handleCellVal 处理单元格数据
  80. func handleCellVal(tmpTableColData LuckySheetDataValue) (valueStr string) {
  81. valueStr = tmpTableColData.Monitor
  82. if valueStr == `` {
  83. //valueStr = fmt.Sprint(cellInfo.Value)
  84. if valueStr == `` && tmpTableColData.CellType.S != nil {
  85. //不是设置在单元格上面,而是设置在文本上
  86. for _, cellS := range tmpTableColData.CellType.S {
  87. valueStr += fmt.Sprint(cellS.Value)
  88. }
  89. }
  90. }
  91. return
  92. }
  93. // HandleRuleToTableCell 根据管理规则渲染单元格数据
  94. func HandleRuleToTableCell(excelInfoId int, oldTableData TableData) (newTableData TableData, err error) {
  95. newTableData = oldTableData
  96. excelRuleMappingList, err := excel.GetExcelRuleMappingByExcelInfoId(excelInfoId)
  97. if err != nil {
  98. return
  99. }
  100. if len(excelRuleMappingList) == 0 {
  101. return
  102. }
  103. tableDataList := oldTableData.TableDataList
  104. excelRuleMap := make(map[int]*excel.ExcelInfoRuleMappingView)
  105. for _, v := range excelRuleMappingList {
  106. excelRuleMap[v.ExcelInfoRuleMappingId] = v
  107. }
  108. ruleScopeMap := generateRuleScopeIndexMap(excelRuleMappingList)
  109. for row, scopeValues := range ruleScopeMap {
  110. for col, ruleId := range scopeValues {
  111. if v, ok := excelRuleMap[ruleId]; ok {
  112. if len(tableDataList) > row && len(tableDataList[row]) > col {
  113. // 符合管理规则要求,则进行字体和背景颜色的渲染
  114. if checkCellRule(v, tableDataList[row][col].Monitor, tableDataList) {
  115. tableDataList[row][col].Background = v.BackgroundColor
  116. tableDataList[row][col].FontColor = v.FontColor
  117. }
  118. } else {
  119. continue
  120. }
  121. } else {
  122. continue
  123. }
  124. }
  125. }
  126. return
  127. }
  128. func getCellValueByType(value string, valueType int, tableDataList [][]LuckySheetDataValue) (float64, bool) {
  129. if valueType == 2 {
  130. coords := strings.Split(value, ",")
  131. var coordIntArr []int
  132. for _, v := range coords {
  133. t, _ := strconv.Atoi(v)
  134. coordIntArr = append(coordIntArr, t)
  135. }
  136. if len(coordIntArr) == 2 {
  137. x, y := coordIntArr[0]-1, coordIntArr[1]-1
  138. conditionValue, err := strconv.ParseFloat(tableDataList[y][x].Monitor, 64)
  139. if err != nil {
  140. return 0, false
  141. }
  142. return conditionValue, true
  143. }
  144. } else {
  145. conditionValue, err := strconv.ParseFloat(value, 64)
  146. if err != nil {
  147. return 0, false
  148. }
  149. return conditionValue, true
  150. }
  151. return 0, false
  152. }
  153. func checkCellRule(ruleInfo *excel.ExcelInfoRuleMappingView, value string, tableDataList [][]LuckySheetDataValue) bool {
  154. var tableValue float64
  155. var tableTime time.Time
  156. var err error
  157. if ruleInfo.RuleType == 5 {
  158. tableTime, err = time.ParseInLocation(utils.FormatDate, value, time.Local)
  159. if err != nil {
  160. return false
  161. }
  162. } else {
  163. tableValue, err = strconv.ParseFloat(value, 64)
  164. if err != nil {
  165. return false
  166. }
  167. }
  168. switch ruleInfo.RuleType {
  169. // 大于
  170. case 1:
  171. conditionValue, ok := getCellValueByType(ruleInfo.LeftValueBack, ruleInfo.LeftValueType, tableDataList)
  172. if !ok {
  173. return false
  174. }
  175. if tableValue > conditionValue {
  176. return true
  177. }
  178. // 小于
  179. case 2:
  180. conditionValue, ok := getCellValueByType(ruleInfo.LeftValueBack, ruleInfo.LeftValueType, tableDataList)
  181. if !ok {
  182. return false
  183. }
  184. if tableValue < conditionValue {
  185. return true
  186. }
  187. // 介于
  188. case 3:
  189. leftcondValue, ok := getCellValueByType(ruleInfo.LeftValueBack, ruleInfo.LeftValueType, tableDataList)
  190. if !ok {
  191. return false
  192. }
  193. rightcondValue, ok := getCellValueByType(ruleInfo.RightValueBack, ruleInfo.RightValueType, tableDataList)
  194. if !ok {
  195. return false
  196. }
  197. if leftcondValue <= tableValue && tableValue <= rightcondValue {
  198. return true
  199. }
  200. // 等于
  201. case 4:
  202. conditionValue, ok := getCellValueByType(ruleInfo.LeftValueBack, ruleInfo.LeftValueType, tableDataList)
  203. if !ok {
  204. return false
  205. }
  206. if tableValue == conditionValue {
  207. return true
  208. }
  209. // 发生日期
  210. case 5:
  211. // 发生日期
  212. var dateStart, dataEnd time.Time
  213. switch ruleInfo.LeftValueBack {
  214. // 今天
  215. case "today":
  216. // 获得今天的零点零分零秒
  217. dateStart = utils.Today()
  218. if tableTime == dateStart {
  219. return true
  220. }
  221. // 明天
  222. case "tomorrow":
  223. dateStart = utils.Tomorrow()
  224. if tableTime == dateStart {
  225. return true
  226. }
  227. // 最近7天
  228. case "last7days":
  229. dateStart, dataEnd = utils.Last7Days()
  230. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  231. return true
  232. }
  233. // 上周
  234. case "lastweek":
  235. dateStart, dataEnd = utils.LastWeek()
  236. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  237. return true
  238. }
  239. // 本周
  240. case "thisweek":
  241. dateStart, dataEnd = utils.ThisWeek()
  242. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  243. return true
  244. }
  245. // 下周
  246. case "nextweek":
  247. dateStart, dataEnd = utils.NextWeek()
  248. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  249. return true
  250. }
  251. // 上月
  252. case "lastmonth":
  253. dateStart, dataEnd = utils.LastMonth()
  254. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  255. return true
  256. }
  257. // 本月
  258. case "thismonth":
  259. dateStart, dataEnd = utils.ThisMonth()
  260. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  261. return true
  262. }
  263. // 下月
  264. case "nextmonth":
  265. dateStart, dataEnd = utils.NextMonth()
  266. if tableTime.After(dateStart) && tableTime.Before(dataEnd) {
  267. return true
  268. }
  269. default:
  270. return false
  271. }
  272. }
  273. return false
  274. }
  275. func generateRuleScopeIndexMap(items []*excel.ExcelInfoRuleMappingView) (ruleScopeMap map[int]map[int]int) {
  276. ruleScopeMap = make(map[int]map[int]int)
  277. for _, item := range items {
  278. coords := strings.Split(item.ScopeCoord, ",")
  279. var coordIntArr []int
  280. for _, v := range coords {
  281. t, _ := strconv.Atoi(v)
  282. coordIntArr = append(coordIntArr, t)
  283. }
  284. if len(coords) == 4 {
  285. xmin, ymin, xmax, ymax := coordIntArr[0]-1, coordIntArr[1]-1, coordIntArr[2]-1, coordIntArr[3]-1
  286. for i := ymin; i <= ymax; i++ {
  287. for j := xmin; j <= xmax; j++ {
  288. if _, ok := ruleScopeMap[i]; !ok {
  289. ruleScopeMap[i] = make(map[int]int)
  290. }
  291. ruleScopeMap[i][j] = item.ExcelInfoRuleMappingId
  292. }
  293. }
  294. }
  295. if len(coords) == 2 {
  296. x, y := coordIntArr[0]-1, coordIntArr[1]-1
  297. if _, ok := ruleScopeMap[y]; !ok {
  298. ruleScopeMap[y] = make(map[int]int)
  299. }
  300. ruleScopeMap[y][x] = item.ExcelInfoRuleMappingId
  301. }
  302. }
  303. return
  304. }