lucky_sheet_table.go 9.1 KB

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