integralService.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package service
  2. import (
  3. "fmt"
  4. "log"
  5. "points_service/entity"
  6. "time"
  7. )
  8. type IntegralService struct{}
  9. //新增积分流水
  10. func (service *IntegralService) IntegralAddService(userId, businessType, endDate string, pointType, businessTypeId, point, appId int) (int, string) {
  11. orm := entity.Engine
  12. flow := entity.Flow{}
  13. flow.UserId = userId
  14. flow.PointType = pointType
  15. flow.BusinessTypeId = businessTypeId
  16. flow.BusinessType = businessType
  17. flow.Point = point
  18. flow.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  19. flow.EndDate = endDate
  20. flow.AppId = appId
  21. //查询积分余额是否充足
  22. balance := entity.Balance{}
  23. balance.UserId = userId
  24. balance.AppId = appId
  25. b, err := orm.Table("integral_balance").Select("countPoints").
  26. Where("userId = ? AND appId = ?", userId, appId).
  27. Get(&balance)
  28. if !b || err != nil {
  29. log.Printf("积分余额查询出错,userId:[%s],err:[%v]", userId, err)
  30. return entity.ErrorCode, "积分余额不足"
  31. }
  32. if balance.CountPoints < point {
  33. return entity.ErrorCode, "积分余额不足"
  34. }
  35. return entity.SuccessCode, "消耗积分成功"
  36. }
  37. //消耗积分流水
  38. func (service *IntegralService) IntegralConsumeService(userId, businessType, createTime, endDate string, pointType, businessTypeId, point, appId int) (int, string) {
  39. orm := entity.Engine
  40. flow := entity.Flow{}
  41. flow.UserId = userId
  42. flow.PointType = pointType
  43. flow.BusinessTypeId = businessTypeId
  44. flow.BusinessType = businessType
  45. flow.Point = point
  46. flow.CreateTime = createTime //time.Now().Format("2006-01-02 15:04:05")
  47. flow.EndDate = endDate
  48. flow.AppId = appId
  49. //查询积分余额是否充足
  50. balance := entity.Balance{}
  51. balance.UserId = userId
  52. balance.AppId = appId
  53. b, err := orm.Table("integral_balance").Select("countPoints").
  54. Where("userId = ? AND appId = ?", userId, appId).
  55. Get(&balance)
  56. if !b || err != nil {
  57. log.Printf("积分余额查询出错,userId:[%s],err:[%v]", userId, err)
  58. return entity.ErrorCode, "积分余额不足"
  59. }
  60. if balance.CountPoints < point {
  61. return entity.ErrorCode, "积分余额不足"
  62. }
  63. af, err := entity.Engine.Table("integral_flow").Insert(&flow)
  64. if err != nil && af>0 {
  65. log.Print("消耗积分记录失败")
  66. return entity.ErrorCode,"消耗积分记录失败"
  67. }
  68. return entity.SuccessCode, "消耗积分成功"
  69. }
  70. //结存新增
  71. func (service *IntegralService) IntegralSoldeService(model entity.SoldeUpdate) (bool, string) {
  72. solde := entity.Solde{}
  73. solde.AppId = model.AppId
  74. solde.UserId = model.UserId
  75. solde.EndDate = model.EndDate
  76. var err error
  77. var numb = int64(0)
  78. //新增积分
  79. if (model.PointsType) {
  80. //永久积分
  81. solde.PerManEntPoints = model.Points
  82. numb, err = entity.Engine.Table("integral_solde").Insert(&solde)
  83. if err != nil && numb == 0 {
  84. log.Print("新增永久积分失败")
  85. return false, "新增永久积分失败"
  86. }
  87. return true, "新增永久积分成功"
  88. }
  89. //失效积分
  90. //先查看是否有EndDate的积分
  91. soldelist := []entity.Solde{}
  92. err = entity.Engine.Table("integral_solde").Where("appId=? and userId=? and endDate=? ", model.AppId, model.UserId, model.EndDate).Find(&soldelist)
  93. if len(soldelist) > 0 {
  94. soldelist[0].TimePoints += model.Points
  95. numb, err = entity.Engine.Table("integral_solde").ID(soldelist[0].Id).Cols("timePoints").Update(&soldelist[0])
  96. if err != nil && numb == 0 {
  97. log.Print("修改时效积分失败")
  98. return false, "修改时效积分失败"
  99. }
  100. return true, "修改时效积分成功"
  101. } else {
  102. solde.TimePoints = model.Points
  103. numb, err = entity.Engine.Table("integral_solde").Insert(&solde)
  104. if err != nil && numb == 0 {
  105. log.Print("新增时效积分失败")
  106. return false, "新增时效积分失败"
  107. }
  108. return true, "新增时效积分成功"
  109. }
  110. }
  111. //结存扣除
  112. func (service *IntegralService) IntegralSoldeReduceService(model entity.SoldeUpdate) (bool, string) {
  113. var err error
  114. var numb = int64(0)
  115. soldelist := []entity.Solde{}
  116. err = entity.Engine.Table("integral_solde").Where("appId=? and userId=? and endDate> ? ( perManEntPoints != 0 AND timePoints = 0 ) OR ( perManEntPoints = 0 AND timePoints != 0 )", model.AppId, model.UserId, time.Now().Format("2006/01/02/")).Desc("endDate,timePoints").Find(&soldelist)
  117. if len(soldelist) > 0 {
  118. var point = model.Points
  119. for _, solde := range soldelist {
  120. if point == 0 {
  121. return true, "积分消耗成功"
  122. }
  123. if (solde.TimePoints == 0) {
  124. //消耗永久积分
  125. if (solde.PerManEntPoints >= point) {
  126. //够消耗
  127. point = 0
  128. solde.PerManEntPoints = solde.PerManEntPoints - point
  129. numb, err = entity.Engine.Table("integral_solde").ID(solde.Id).Cols("perManEntPoints").Update(&solde)
  130. if err != nil && numb == 0 {
  131. log.Print("消耗永久积分失败")
  132. return false, "消耗永久积分失败"
  133. }
  134. }
  135. }
  136. //消耗时效积分
  137. if (solde.TimePoints > point) {
  138. //够消耗
  139. point = 0
  140. solde.TimePoints = solde.TimePoints - point
  141. numb, err = entity.Engine.Table("integral_solde").ID(solde.Id).Cols("timePoints").Update(&solde)
  142. if err != nil && numb == 0 {
  143. log.Print("消耗时效积分失败")
  144. return false, "消耗时效积分失败"
  145. }
  146. return true, "消耗时效积分成功"
  147. }
  148. //不够消耗
  149. point = point - solde.TimePoints
  150. solde.TimePoints = 0
  151. numb, err = entity.Engine.Table("integral_solde").ID(solde.Id).Cols("timePoints").Update(&solde)
  152. if err != nil && numb == 0 {
  153. log.Print("消耗时效积分失败")
  154. return false, "消耗时效积分失败"
  155. }
  156. }
  157. }
  158. return false, "没有积分可以扣除"
  159. }
  160. //调整余额
  161. func (service *IntegralService) IntegralBalanceService(model entity.BalanceUpdate) (bool, string) {
  162. var err error
  163. var numb = int64(0)
  164. fmt.Println(numb)
  165. balanceList := []*entity.Balance{}
  166. //查看是否存在本人的余额
  167. err = entity.Engine.Table("integral_balance").Where("appId=? and userId=? ", model.AppId, model.UserId).Find(&balanceList)
  168. if model.Change {
  169. //新增
  170. if len(balanceList) == 0 {
  171. balance := entity.Balance{}
  172. balance.UserId = model.UserId
  173. balance.AppId = model.AppId
  174. balance.CountPoints = model.CountPoints
  175. numb, err = entity.Engine.Table("integral_balance").Insert(&balance)
  176. if err != nil && numb == 0 {
  177. log.Print("新增余额失败")
  178. return false, "新增余额失败"
  179. }
  180. }
  181. //修改余额
  182. balanceList[0].CountPoints = balanceList[0].CountPoints + model.CountPoints
  183. }
  184. //消耗
  185. if len(balanceList) == 0 {
  186. log.Println("没有积分可以扣除")
  187. return false, "没有积分可以扣除"
  188. }
  189. if (balanceList[0].CountPoints < model.CountPoints) {
  190. log.Println("积分余额不足")
  191. return false, "积分余额不足"
  192. }
  193. balanceList[0].CountPoints = balanceList[0].CountPoints - model.CountPoints
  194. numb, err = entity.Engine.Table("integral_balance").ID(balanceList[0].Id).Cols("countPoints").Update(&balanceList[0])
  195. if err != nil && numb == 0 {
  196. log.Print("余额扣除失败")
  197. return false, "余额扣除失败"
  198. }
  199. return true, "余额扣除失败"
  200. }