match.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package admin
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "zhiyuan/models/match"
  7. "zhiyuan/pkg/app"
  8. "zhiyuan/pkg/db"
  9. "zhiyuan/pkg/utils"
  10. "github.com/gin-gonic/gin"
  11. "github.com/tealeg/xlsx/v3"
  12. )
  13. type MatchCell struct {
  14. Value string
  15. Half bool
  16. }
  17. func isMatchTrue(value string) bool {
  18. value = strings.Trim(value, " ")
  19. if value == "是" || value == "✔" || value == "√" {
  20. return true
  21. }
  22. b := true
  23. m := false
  24. for _, s := range []rune(value) {
  25. if s == '是' {
  26. b = true
  27. m = true
  28. } else if s == '否' {
  29. b = false
  30. m = false
  31. } else if s == '✔' || s == '√' {
  32. return b
  33. }
  34. }
  35. return m
  36. }
  37. func MyMatch(c *gin.Context) {
  38. now := time.Now()
  39. offset := int(time.Monday - now.Weekday())
  40. if offset > 0 {
  41. offset -= 7
  42. }
  43. monday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  44. info, _ := db.GetOneModelMap(db.Type(match.MatchOrder{}), map[string]interface{}{
  45. "adminId": c.GetInt("adminID"),
  46. "created_at >=": monday.Unix(),
  47. "order by": "`created_at` DESC",
  48. }, nil)
  49. app.Success(c, info)
  50. }
  51. func MatchImport(c *gin.Context) {
  52. file, err := c.FormFile("file")
  53. if err != nil {
  54. app.Error(c, fmt.Sprintf("get form err: %s", err.Error()))
  55. return
  56. }
  57. fileExt := utils.FileExt(file.Filename)
  58. attachKey := utils.RandomStr() + fileExt
  59. file.Filename = "uploads/" + attachKey
  60. if err := c.SaveUploadedFile(file, file.Filename); err != nil {
  61. app.Error(c, fmt.Sprintf("上传文件失败%s", err.Error()))
  62. return
  63. }
  64. wb, err := xlsx.OpenFile(file.Filename)
  65. if err != nil {
  66. app.Error(c, err.Error())
  67. return
  68. }
  69. halfs := 0
  70. rows := make([][]MatchCell, 0)
  71. for _, sheet := range wb.Sheet {
  72. err = sheet.ForEachRow(func(r *xlsx.Row) error {
  73. if r.GetCoordinate() < 4 || r.GetCell(0).HMerge > 0 {
  74. return nil
  75. }
  76. cells := make([]MatchCell, 0)
  77. for i := 0; i < 61; i++ {
  78. cells = append(cells, MatchCell{
  79. Value: r.GetCell(i).String(),
  80. Half: false,
  81. })
  82. }
  83. if cells[0].Value == "" && cells[1].Value == "" && cells[2].Value == "" && cells[3].Value == "" {
  84. return nil
  85. }
  86. for _, i := range []int{0, 1, 2, 3, 58, 59, 60} {
  87. if cells[i].Value == "" {
  88. cells[i].Half = true
  89. }
  90. }
  91. if cells[10].Value != "" || cells[11].Value != "" {
  92. for _, i := range []int{12, 14, 16, 18} {
  93. if !isMatchTrue(cells[i].Value) {
  94. if cells[i+1].Value == "" {
  95. cells[i+1].Half = true
  96. }
  97. }
  98. }
  99. if !isMatchTrue(cells[20].Value) {
  100. if cells[25].Value == "" {
  101. cells[25].Half = true
  102. }
  103. }
  104. }
  105. cause := false
  106. for i := 27; i < 37; i++ {
  107. if cells[i].Value != "" {
  108. cause = true
  109. }
  110. }
  111. if !cause && cells[37].Value == "" {
  112. cells[37].Half = true
  113. }
  114. visit := false
  115. for _, i := range []int{9, 10, 11} {
  116. if cells[i].Value != "" {
  117. visit = true
  118. }
  119. }
  120. if visit {
  121. for _, i := range []int{38, 39, 40} {
  122. if cells[i].Value == "" {
  123. cells[i].Half = true
  124. }
  125. }
  126. }
  127. for _, i := range []int{49, 51, 53, 55} {
  128. if isMatchTrue(cells[i].Value) {
  129. if cells[i+1].Value == "" {
  130. cells[i+1].Half = true
  131. }
  132. if i == 55 && cells[i+2].Value == "" {
  133. cells[i+2].Half = true
  134. }
  135. }
  136. }
  137. for _, c := range cells {
  138. if c.Half {
  139. halfs++
  140. }
  141. }
  142. rows = append(rows, cells)
  143. return nil
  144. })
  145. }
  146. data := map[string]interface{}{
  147. "adminId": int64(c.GetInt("adminID")),
  148. "halfs": halfs,
  149. "content": utils.JsonEncode(rows),
  150. "path": file.Filename,
  151. }
  152. id, err := db.InsertModel(db.Type(match.MatchOrder{}), data)
  153. if err != nil {
  154. app.Error(c, err.Error())
  155. return
  156. }
  157. app.Success(c, gin.H{"id": id})
  158. }