OrderDao.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package dao
  2. import (
  3. "demo/configs"
  4. "demo/data/dao/manage"
  5. "demo/data/domain"
  6. "demo/data/domain/vo"
  7. "fmt"
  8. "strings"
  9. "time"
  10. )
  11. // SelectSkuBySkuId 根据id查询sku
  12. func SelectSkuBySkuId(skuId int) (domain.GoodsSku, error) {
  13. var sku domain.GoodsSku
  14. _, err := configs.Engine.Table("goods_sku").Where("id = ?", skuId).Get(&sku)
  15. if err != nil {
  16. return sku, err
  17. }
  18. return sku, nil
  19. }
  20. // GetSkuBelong 查询sku所属topic,goods,type
  21. func GetSkuBelong(skuId, couponUserId int64, tp float64) (bool, string, error) {
  22. //时间内可用
  23. availableTime := false
  24. //范围内可用
  25. availableField := false
  26. //金额条件可用
  27. availableMatch := false
  28. var couponUser domain.GoodsCouponUser
  29. _, err := configs.Engine.Table("goods_coupon_user").Where("id = ?", couponUserId).Get(&couponUser)
  30. if err != nil {
  31. return false, "", err
  32. }
  33. coupon, err := manage.GetGoodsCouponById(couponUser.CouponId)
  34. if err != nil {
  35. return false, "", err
  36. }
  37. //判断是否过期
  38. //领取后开始计时
  39. if coupon.Validity == "ClaimTiming" {
  40. if couponUser.CollectionTime.UnixMilli()+coupon.ValidityPeriod.UnixMilli() < time.Now().UnixMilli() {
  41. return false, "优惠券已过期", nil
  42. }
  43. //固定时间段内可用
  44. } else if coupon.Validity == "FixedTime" {
  45. if coupon.ValidityPeriod.UnixMilli() < time.Now().UnixMilli() {
  46. return false, "优惠券不在可使用时间段内", nil
  47. }
  48. } else {
  49. availableTime = true
  50. }
  51. if !availableTime {
  52. return availableTime, "", nil
  53. }
  54. sku, err := manage.GetGoodsSkuById(skuId)
  55. if err != nil {
  56. return false, "", err
  57. }
  58. //goods
  59. if coupon.ConditionByGoods != "" {
  60. split := strings.Split(coupon.ConditionByGoods, ",")
  61. for i := range split {
  62. if fmt.Sprint(skuId) == split[i] {
  63. availableField = true
  64. break
  65. }
  66. }
  67. }
  68. m := make(map[string]interface{})
  69. _, err = configs.Engine.Select("goods_type.id as tid, goods.id as gid").Table("goods_type").
  70. Join("INNER ", "goods", "goods_type.id = goods.type_id").
  71. Join("INNER ", "goods_sku", "goods.id = goods_sku.goods_id").
  72. Where("goods_sku.id = ?", sku.GoodsId).Get(&m)
  73. if err != nil {
  74. return false, "type 查询错误", err
  75. }
  76. //type
  77. if !availableField && coupon.ConditionByType != "" && m["tid"] != "" {
  78. split := strings.Split(coupon.ConditionByType, ",")
  79. for i := range split {
  80. if fmt.Sprint(m["tid"]) == split[i] {
  81. availableField = true
  82. break
  83. }
  84. }
  85. }
  86. //topic
  87. var topic domain.ShopTopic
  88. _, err = configs.Engine.Table("shop_topic").Where("find_in_set(?,type_ids)", m["tid"]).Get(&topic)
  89. if err != nil {
  90. return false, "topic 查询错误", err
  91. }
  92. if !availableField && topic.Id != 0 {
  93. availableField = true
  94. }
  95. if tp > coupon.CashBackPoint {
  96. availableMatch = true
  97. }
  98. if !availableTime || !availableField || !availableMatch {
  99. return false, "不满足条件", nil
  100. }
  101. return availableTime && availableField && availableMatch, "", nil
  102. }
  103. // CreateOrder 创建订单
  104. func CreateOrder(order *domain.GoodsOrder) (*domain.GoodsOrder, error) {
  105. var couponUser domain.GoodsCouponUser
  106. _, err := configs.Engine.Table("goods_coupon_user").Where("id = ?", order.CouponUserId).Get(&couponUser)
  107. if err != nil {
  108. return order, err
  109. }
  110. coupon, err := manage.GetGoodsCouponById(couponUser.CouponId)
  111. if err != nil {
  112. return order, err
  113. }
  114. order.TotalPrice = order.TotalPrice - coupon.CashBackPrice
  115. //优惠券使用
  116. configs.Engine.ID(order.CouponUserId).Cols("state").Update(map[string]any{"state": 2})
  117. //创建订单
  118. id, err := configs.Engine.Table("goods_order").Insert(&order)
  119. fmt.Println("is id? ", id)
  120. if err != nil {
  121. return order, err
  122. }
  123. fmt.Println("order is ", order)
  124. return order, err
  125. }
  126. // OrderPaySuccess 订单支付成功
  127. func OrderPaySuccess(orderId int64, userId int64) error {
  128. //查询订单
  129. order, err := manage.GetGoodsOrderById(orderId)
  130. if err != nil {
  131. return err
  132. }
  133. configs.Engine.Table("goods_order").ID(orderId).Cols("state").Update(map[string]any{"state": 1})
  134. //获取价格
  135. var price = order.TotalPrice
  136. //给推荐人分成
  137. user, err := manage.GetUserById(userId)
  138. wallet := domain.UserWallet{UserId: user.InviterBy}
  139. list, err := manage.GetUserWalletList(wallet, 0, 1)
  140. if err != nil {
  141. return err
  142. }
  143. userWallet, ok := list.List[0].(domain.UserWallet)
  144. if ok {
  145. //userWallet.Balance = userWallet.Balance + (price * 0.1)
  146. userWallet.PromotionAmount = userWallet.PromotionAmount + (price * 0.1)
  147. configs.Engine.Table("user_wallet").ID(userWallet.Id).Cols("balance").Update(map[string]any{"promotion_amount": userWallet.PromotionAmount})
  148. }
  149. return nil
  150. }
  151. // GetOrderByUserId 获取订单集合
  152. func GetOrderByUserId(userId int64) ([]domain.GoodsOrder, error) {
  153. order := domain.GoodsOrder{}
  154. rows, err := configs.Engine.Table("goods_order").Where("create_by = ?", userId).Desc("create_time").Rows(&order)
  155. if err != nil {
  156. return nil, err
  157. }
  158. defer rows.Close()
  159. var orders []domain.GoodsOrder
  160. for rows.Next() {
  161. var order domain.GoodsOrder
  162. err := rows.Scan(&order)
  163. if err != nil {
  164. return nil, err
  165. }
  166. orders = append(orders, order)
  167. }
  168. return orders, nil
  169. }
  170. // GetUserWalletByUserId 获取用户的钱包信息
  171. func GetUserWalletByUserId(userId int64) (domain.UserWallet, error) {
  172. var wallet domain.UserWallet
  173. _, err := configs.Engine.Where("user_id = ?", userId).Get(&wallet)
  174. if err != nil {
  175. return wallet, err
  176. }
  177. return wallet, nil
  178. }
  179. // GetUserCouponByUserId 获取用户的优惠券信息
  180. func GetUserCouponByUserId(userId int64) ([]domain.CouponUserJDM, error) {
  181. var couponUserJDMs []domain.CouponUserJDM
  182. err := configs.Engine.Table("goods_coupon_user").
  183. Join("INNER", "goods_coupon", "goods_coupon_user.coupon_id = goods_coupon.id").
  184. Where("goods_coupon_user.user_id = ?", userId).
  185. Find(&couponUserJDMs)
  186. if err != nil {
  187. return couponUserJDMs, err
  188. }
  189. return couponUserJDMs, nil
  190. }
  191. // GetListGoodsOrder 获取用户的订单信息
  192. func GetListGoodsOrder(state string, userId int64, pageNum, pageSize int) (vo.BaseListVo, error) {
  193. var goodsOrder domain.GoodsOrder
  194. Session := configs.Engine.Table("goods_order")
  195. if state != "" {
  196. Session.Where("state = ?", state)
  197. }
  198. rows, err := Session.Where("create_by = ?", userId).Limit(pageSize, (pageNum-1)*pageSize).Desc("id").Rows(&goodsOrder)
  199. var vo vo.BaseListVo
  200. if err != nil {
  201. return vo, err
  202. }
  203. vo.PageNum = pageNum
  204. vo.PageSize = pageSize
  205. vo.List = make([]any, 0)
  206. for rows.Next() {
  207. var u domain.GoodsOrder
  208. rows.Scan(&u)
  209. vo.List = append(vo.List, u)
  210. }
  211. CountSess := configs.Engine.Table("goods_order")
  212. if state != "" {
  213. CountSess.Where("state = ?", state)
  214. }
  215. //查询数量
  216. count, err := CountSess.Where("create_by = ?", userId).Count(&goodsOrder)
  217. vo.Total = int(count)
  218. return vo, nil
  219. }
  220. // 领取优惠券
  221. //func pickUpTheCoupon(couponId int, userId int) (bool, error) {
  222. // gcu := domain.GoodsCouponUser{
  223. // UserId: userId,
  224. // CouponId: couponId,
  225. // CollectionTime: time.Now(),
  226. // State: "1",
  227. // }
  228. // //判断优惠券数量是否充足
  229. // coupon, err := manage.GetByIdGoodsCoupon(couponId)
  230. // if err != nil {
  231. // return false, err
  232. // }
  233. // //判断条件
  234. // if coupon.Count > 0 {
  235. // if coupon.Count == 1 {
  236. // //优惠券数量为1时,直接领取
  237. // _, err := manage.PostGoodsCouponUser(&gcu)
  238. // if err != nil {
  239. // return false, err
  240. // }
  241. // manage.PutGoodsCoupon(map[string]any{"count": coupon.Count - 1}, couponId)
  242. // return true, nil
  243. // } else {
  244. // //优惠券数量大于1时,判断是否满足领取条件
  245. // if coupon.Condition == 1 {
  246. // //满足领取条件
  247. // _, err := manage.PostGoodsCouponUser(&gcu)
  248. // if err != nil {
  249. // }
  250. // }
  251. // }
  252. //
  253. // }
  254. //
  255. //}