rent_computer_shop.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package rentComputer
  2. import (
  3. "context"
  4. "errors"
  5. "go.uber.org/zap"
  6. "gorm.io/gorm"
  7. "log-server/global"
  8. "log-server/model/rentComputer"
  9. "log-server/model/rentComputer/request"
  10. "log-server/model/rentComputer/response"
  11. "time"
  12. )
  13. type ServiceRentComputerShop struct {
  14. }
  15. func (s *ServiceRentComputerShop) RentComputerShopList(ctx context.Context, api rentComputer.RentComputerShop, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  16. //global.GVA_LOG.Info("service----GetComputerShopList----strat")
  17. //date := api.CreateTime
  18. //isCurrentDate := false
  19. //if date == "" {
  20. // date = s.CurrentDate()
  21. // isCurrentDate = true
  22. //}
  23. db := global.GVA_DB.Model(&rentComputer.RentComputerShop{})
  24. var total int64
  25. //db = db.Distinct("pc_code", "id", "operator", "create_date", "GROUP_CONCAT(game_id) AS game_ids")
  26. //db = db.Where("create_date = ?", date)
  27. //if api.Operator != "" {
  28. // db = db.Where("operator like ?", "%"+api.Operator+"%")
  29. //}
  30. //if api.PcCode != "" {
  31. // db = db.Where("pc_code = ?", api.PcCode)
  32. //}
  33. //if api.GameId != 0 {
  34. // db = db.Where("game_id = ?", api.GameId)
  35. //}
  36. //.Select("count(distinct(name))").Count(&count)
  37. //err := db.Count(&total).Error
  38. //err := db.Select("count(distinct(pc_code))").Count(&total).Error
  39. //db = db.Group("pc_code")
  40. //db2 := global.GVA_DB.Model(&log.LogComputer{})
  41. //db2 = db2.Select("count(distinct(pc_code))")
  42. //db2 = db2.Where("create_date = ?", date)
  43. //if api.Operator != "" {
  44. // db2 = db2.Where("operator like ?", "%"+api.Operator+"%")
  45. //}
  46. //if api.PcCode != "" {
  47. // db2 = db2.Where("pc_code = ?", api.PcCode)
  48. //}
  49. err := db.Count(&total).Error
  50. //if err != nil {
  51. // return nil, 0, err
  52. //}
  53. limit := info.PageSize
  54. offset := info.PageSize * (info.Page - 1)
  55. //var statisticsLogs []*log.LogComputer
  56. var statisticsLogs []*rentComputer.RentComputerShop
  57. db = db.Limit(limit).Offset(offset)
  58. if order != "" {
  59. var OrderStr string
  60. // 设置有效排序key 防止sql注入
  61. // 感谢 Tom4t0 提交漏洞信息
  62. orderMap := make(map[string]bool, 3)
  63. orderMap["pc_code"] = true
  64. orderMap["game_id"] = true
  65. orderMap["operator"] = true
  66. if orderMap[order] {
  67. if desc {
  68. OrderStr = order + " desc"
  69. } else {
  70. OrderStr = order
  71. }
  72. } else { // didn't matched any order key in `orderMap`
  73. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  74. return statisticsLogs, total, err
  75. }
  76. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  77. } else {
  78. err = db.Order("id").Find(&statisticsLogs).Error
  79. }
  80. if err != nil {
  81. return nil, 0, err
  82. }
  83. var statisticsLogsComputer []*response.ComputerStatisticsReply1
  84. for _, statisticsLog := range statisticsLogs {
  85. /*var gameInfo []*response.GameInfo
  86. err = json.Unmarshal([]byte(statisticsLog.GameInfo), &gameInfo)
  87. for i, _ := range gameInfo {
  88. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  89. statisticsLogComputer.GameInfo = gameInfo
  90. statisticsLogComputer.PcCode = statisticsLog.PcCode
  91. statisticsLogComputer.Operator = statisticsLog.Operator
  92. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  93. statisticsLogComputer.GameId = gameInfo[i].GameId
  94. statisticsLogComputer.PullAccountNum = gameInfo[i].PullAccountNum
  95. statisticsLogComputer.TaskSuccessNum = gameInfo[i].TaskSuccessNum
  96. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  97. }*/
  98. //gameIdStr:=strconv.Itoa(statisticsLog.GameId)
  99. //taskStatistics := s.GameTargetInfo(ctx,s.LogicalLog.CurrentDate(),gameIdStr)
  100. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  101. statisticsLogComputer.Id = statisticsLog.Id
  102. //updateTime, _ := time.ParseInLocation("2006-01-02 15:04:05", statisticsLog.UpdateTime, time.Local)
  103. statisticsLogComputer.UpdateTime = statisticsLog.UpdateTime
  104. //global.GVA_LOG.Info(statisticsLog.CreateTime.String())
  105. //createTime, _ := time.ParseInLocation("2006-01-02 15:04:05", statisticsLog.CreateTime, time.Local)
  106. //global.GVA_LOG.Info(createTime.String())
  107. statisticsLogComputer.PayMethod = statisticsLog.PayMethod
  108. statisticsLogComputer.CreateTime = statisticsLog.CreateTime
  109. statisticsLogComputer.Name = statisticsLog.Name
  110. statisticsLogComputer.Description = statisticsLog.Description
  111. //if isCurrentDate {
  112. // statisticsLogComputer.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, statisticsLog.PcCode, statisticsLog.GameId)
  113. //}
  114. //statisticsLogComputer.ComputerHourAverageRate = statisticsLog.ComputerHourAverageRate
  115. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  116. }
  117. return statisticsLogsComputer, total, err
  118. }
  119. func (s *ServiceRentComputerShop) GetRentComputerShopNum() int64 {
  120. var total int64
  121. db := global.GVA_DB.Model(&rentComputer.RentComputerShop{})
  122. db = db.Distinct("name")
  123. _ = db.Count(&total).Error
  124. return total
  125. }
  126. func (s *ServiceRentComputerShop) GetRentComputerShopById(id int) (computerShop response.ComputerStatisticsReply1, err error) {
  127. var api rentComputer.RentComputerShop
  128. err = global.GVA_DB.Where("id = ?", id).First(&api).Error
  129. if err != nil {
  130. return
  131. }
  132. computerShop.Id = api.Id
  133. computerShop.Name = api.Name
  134. computerShop.PayMethod = api.PayMethod
  135. computerShop.Description = api.Description
  136. computerShop.UpdateTime = api.UpdateTime
  137. computerShop.CreateTime = api.CreateTime
  138. return
  139. }
  140. func (s *ServiceRentComputerShop) CurrentDate() (current string) {
  141. current = time.Now().Format("2006-01-02")
  142. return
  143. }
  144. func (s *ServiceRentComputerShop) AddRentComputerShop(requestCoding request.ComputerShopRequest) (err error) {
  145. if !errors.Is(global.GVA_DB.Where("name = ?", requestCoding.Name).First(&rentComputer.RentComputerShop{}).Error, gorm.ErrRecordNotFound) {
  146. return errors.New("存在相同供应商名")
  147. }
  148. computerShop := new(rentComputer.RentComputerShop)
  149. computerShop.Name = requestCoding.Name
  150. computerShop.PayMethod = requestCoding.PayMethod
  151. computerShop.Description = requestCoding.Description
  152. computerShop.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  153. computerShop.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
  154. //key := codeListCacheKey
  155. //_ = s.cache.DelBatheHsCache(context.Background(), key)
  156. return global.GVA_DB.Create(&computerShop).Error
  157. }
  158. func (s *ServiceRentComputerShop) EditRentComputerShop(computerShop rentComputer.RentComputerShop) (err error) {
  159. err = global.GVA_DB.Save(computerShop).Error
  160. return err
  161. }
  162. func (s *ServiceRentComputerShop) DeleteRentComputerShopByIds(ids request.IdsReq) (err error) {
  163. err = global.GVA_DB.Delete(&[]rentComputer.RentComputerShop{}, "id in ?", ids.Ids).Error
  164. return err
  165. }
  166. func (s *ServiceRentComputerShop) DeleteRentComputerShopById(api rentComputer.RentComputerShop) (err error) {
  167. var entity rentComputer.RentComputerShop
  168. err = global.GVA_DB.Where("id = ?", api.Id).First(&entity).Error // 根据id查询api记录
  169. if errors.Is(err, gorm.ErrRecordNotFound) { // api记录不存在
  170. return err
  171. }
  172. return global.GVA_DB.Delete(&entity).Error
  173. }