rent_set_meal.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 ServiceRentSetMeal struct {
  14. }
  15. func (s *ServiceRentSetMeal) RentSetMealList(ctx context.Context, api rentComputer.RentSetMeal, 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.RentSetMeal{})
  24. var total int64
  25. db = db.Select("rent_set_meal.id, rent_set_meal.update_time,rent_set_meal.create_time,rent_set_meal.name," +
  26. "rent_set_meal.remark,rent_set_meal.rent_price,rent_set_meal.price_type,rent_set_meal.rent_price_day,rent_set_meal.shop_id,s.name as shop_name")
  27. db = db.Joins("left join rent_computer_shop s on s.id = rent_set_meal.shop_id")
  28. //db = db.Distinct("pc_code", "id", "operator", "create_date", "GROUP_CONCAT(game_id) AS game_ids")
  29. //db = db.Where("create_date = ?", date)
  30. //if api.Operator != "" {
  31. // db = db.Where("operator like ?", "%"+api.Operator+"%")
  32. //}
  33. //if api.PcCode != "" {
  34. // db = db.Where("pc_code = ?", api.PcCode)
  35. //}
  36. //if api.GameId != 0 {
  37. // db = db.Where("game_id = ?", api.GameId)
  38. //}
  39. //.Select("count(distinct(name))").Count(&count)
  40. //err := db.Count(&total).Error
  41. //err := db.Select("count(distinct(pc_code))").Count(&total).Error
  42. //db = db.Group("pc_code")
  43. //db2 := global.GVA_DB.Model(&log.LogComputer{})
  44. //db2 = db2.Select("count(distinct(pc_code))")
  45. //db2 = db2.Where("create_date = ?", date)
  46. //if api.Operator != "" {
  47. // db2 = db2.Where("operator like ?", "%"+api.Operator+"%")
  48. //}
  49. //if api.PcCode != "" {
  50. // db2 = db2.Where("pc_code = ?", api.PcCode)
  51. //}
  52. err := db.Count(&total).Error
  53. //if err != nil {
  54. // return nil, 0, err
  55. //}
  56. limit := info.PageSize
  57. offset := info.PageSize * (info.Page - 1)
  58. //var statisticsLogs []*log.LogComputer
  59. //var statisticsLogs []*rentComputer.RentSetMeal
  60. var statisticsLogs []*response.SetMealStatisticsReply1
  61. db = db.Limit(limit).Offset(offset)
  62. if order != "" {
  63. var OrderStr string
  64. // 设置有效排序key 防止sql注入
  65. // 感谢 Tom4t0 提交漏洞信息
  66. orderMap := make(map[string]bool, 3)
  67. orderMap["id"] = true
  68. if orderMap[order] {
  69. if desc {
  70. OrderStr = order + " desc"
  71. } else {
  72. OrderStr = order
  73. }
  74. } else { // didn't matched any order key in `orderMap`
  75. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  76. return statisticsLogs, total, err
  77. }
  78. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  79. } else {
  80. err = db.Order("id").Find(&statisticsLogs).Error
  81. }
  82. if err != nil {
  83. return nil, 0, err
  84. }
  85. var statisticsSetMeals []*response.SetMealStatisticsReply1
  86. for _, statisticsLog := range statisticsLogs {
  87. //gameIdStr:=strconv.Itoa(statisticsLog.GameId)
  88. //taskStatistics := s.GameTargetInfo(ctx,s.LogicalLog.CurrentDate(),gameIdStr)
  89. statisticsSetMeal := new(response.SetMealStatisticsReply1)
  90. statisticsSetMeal.Id = statisticsLog.Id
  91. //updateTime, _ := time.ParseInLocation("2006-01-02 15:04:05", statisticsLog.UpdateTime, time.Local)
  92. statisticsSetMeal.UpdateTime = statisticsLog.UpdateTime
  93. //global.GVA_LOG.Info(statisticsLog.CreateTime.String())
  94. //createTime, _ := time.ParseInLocation("2006-01-02 15:04:05", statisticsLog.CreateTime, time.Local)
  95. //global.GVA_LOG.Info(createTime.String())
  96. statisticsSetMeal.CreateTime = statisticsLog.CreateTime
  97. statisticsSetMeal.Name = statisticsLog.Name
  98. statisticsSetMeal.Remark = statisticsLog.Remark
  99. statisticsSetMeal.PriceType = statisticsLog.PriceType
  100. statisticsSetMeal.RentPriceDay = statisticsLog.RentPriceDay
  101. statisticsSetMeal.RentPrice = statisticsLog.RentPrice
  102. statisticsSetMeal.ShopId = statisticsLog.ShopId
  103. statisticsSetMeal.ShopName = statisticsLog.ShopName
  104. //if isCurrentDate {
  105. // statisticsLogComputer.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, statisticsLog.PcCode, statisticsLog.GameId)
  106. //}
  107. //statisticsLogComputer.ComputerHourAverageRate = statisticsLog.ComputerHourAverageRate
  108. statisticsSetMeals = append(statisticsSetMeals, statisticsSetMeal)
  109. }
  110. return statisticsSetMeals, total, err
  111. }
  112. func (s *ServiceRentSetMeal) GetRentSetMealNum() int64 {
  113. var total int64
  114. db := global.GVA_DB.Model(&rentComputer.RentSetMeal{})
  115. db = db.Distinct("id")
  116. _ = db.Count(&total).Error
  117. return total
  118. }
  119. func (s *ServiceRentSetMeal) GetRentSetMealById(id int) (computerShop response.SetMealStatisticsReply1, err error) {
  120. var api rentComputer.RentSetMeal
  121. err = global.GVA_DB.Where("id = ?", id).First(&api).Error
  122. if err != nil {
  123. return
  124. }
  125. computerShop.Id = api.Id
  126. computerShop.Name = api.Name
  127. computerShop.Remark = api.Remark
  128. computerShop.RentPrice = api.RentPrice
  129. computerShop.PriceType = api.PriceType
  130. computerShop.RentPriceDay = api.RentPriceDay
  131. computerShop.ShopId = api.ShopId
  132. computerShop.UpdateTime = api.UpdateTime
  133. computerShop.CreateTime = api.CreateTime
  134. return
  135. }
  136. func (s *ServiceRentSetMeal) AddRentSetMeal(requestCoding request.SetMealRequest) (err error) {
  137. //fmt.Printf("%#v\n", &requestCoding)
  138. if !errors.Is(global.GVA_DB.Where("name = ? and price_type = ? and shop_id = ?", requestCoding.Name, requestCoding.PriceType, requestCoding.ShopId).First(&rentComputer.RentSetMeal{}).Error, gorm.ErrRecordNotFound) {
  139. return errors.New("存在相同套餐")
  140. }
  141. computerShop := new(rentComputer.RentSetMeal)
  142. priceType := requestCoding.PriceType
  143. rentPrice := requestCoding.RentPrice
  144. rentPriceDay := float64(0)
  145. computerShop.Name = requestCoding.Name
  146. computerShop.Remark = requestCoding.Remark
  147. if priceType == 0 {
  148. rentPriceDay = float64(int(rentPrice))
  149. } else if priceType == 1 {
  150. rentPriceDay = rentPrice / 7
  151. } else if priceType == 2 {
  152. rentPriceDay = rentPrice / 30
  153. }
  154. computerShop.PriceType = priceType
  155. computerShop.RentPrice = rentPrice
  156. computerShop.RentPriceDay = rentPriceDay
  157. computerShop.ShopId = requestCoding.ShopId
  158. computerShop.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  159. computerShop.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
  160. //key := codeListCacheKey
  161. //_ = s.cache.DelBatheHsCache(context.Background(), key)
  162. return global.GVA_DB.Create(&computerShop).Error
  163. }
  164. func (s *ServiceRentSetMeal) EditRentSetMeal(setMeal rentComputer.RentSetMeal) (err error) {
  165. priceType := setMeal.PriceType
  166. rentPrice := setMeal.RentPrice
  167. rentPriceDay := float64(0)
  168. if priceType == 0 {
  169. rentPriceDay = float64(int(rentPrice))
  170. } else if priceType == 1 {
  171. rentPriceDay = rentPrice / 7
  172. } else if priceType == 2 {
  173. rentPriceDay = rentPrice / 30
  174. }
  175. setMeal.RentPriceDay = rentPriceDay
  176. err = global.GVA_DB.Save(setMeal).Error
  177. return err
  178. }
  179. func (s *ServiceRentSetMeal) DeleteRentSetMealByIds(ids request.IdsReq) (err error) {
  180. err = global.GVA_DB.Delete(&[]rentComputer.RentSetMeal{}, "id in ?", ids.Ids).Error
  181. return err
  182. }
  183. func (s *ServiceRentSetMeal) DeleteRentSetMealById(api rentComputer.RentSetMeal) (err error) {
  184. var entity rentComputer.RentSetMeal
  185. err = global.GVA_DB.Where("id = ?", api.Id).First(&entity).Error // 根据id查询api记录
  186. if errors.Is(err, gorm.ErrRecordNotFound) { // api记录不存在
  187. return err
  188. }
  189. return global.GVA_DB.Delete(&entity).Error
  190. }