OrderDao.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. update, err := configs.Engine.Table("goods_order").Where("id = ?", orderId).Update(map[string]any{"state": 1})
  134. if err != nil {
  135. fmt.Println(update, err)
  136. }
  137. //获取价格
  138. var price = order.TotalPrice
  139. //给推荐人分成
  140. user, err := manage.GetUserById(userId)
  141. if user.InviterBy != 0 {
  142. wallet := domain.UserWallet{UserId: user.InviterBy}
  143. list, err := manage.GetUserWalletList(wallet, 0, 1)
  144. if err != nil {
  145. return err
  146. }
  147. userWallet, ok := list.List[0].(domain.UserWallet)
  148. if ok {
  149. //userWallet.Balance = userWallet.Balance + (price * 0.1)
  150. userWallet.PromotionAmount = userWallet.PromotionAmount + (price * 0.1)
  151. configs.Engine.Table("user_wallet").ID(userWallet.Id).Cols("balance").
  152. Update(map[string]any{"promotion_amount": userWallet.PromotionAmount})
  153. }
  154. }
  155. return nil
  156. }
  157. // GetOrderByUserId 获取订单集合
  158. func GetOrderByUserId(userId int64) ([]domain.GoodsOrder, error) {
  159. order := domain.GoodsOrder{}
  160. rows, err := configs.Engine.Table("goods_order").Where("create_by = ?", userId).Desc("create_time").Rows(&order)
  161. if err != nil {
  162. return nil, err
  163. }
  164. defer rows.Close()
  165. var orders []domain.GoodsOrder
  166. for rows.Next() {
  167. var order domain.GoodsOrder
  168. err := rows.Scan(&order)
  169. if err != nil {
  170. return nil, err
  171. }
  172. orders = append(orders, order)
  173. }
  174. return orders, nil
  175. }
  176. // GetUserWalletByUserId 获取用户的钱包信息
  177. func GetUserWalletByUserId(userId int64) (domain.UserWallet, error) {
  178. var wallet domain.UserWallet
  179. _, err := configs.Engine.Where("user_id = ?", userId).Get(&wallet)
  180. if err != nil {
  181. return wallet, err
  182. }
  183. return wallet, nil
  184. }
  185. // GetUserCouponByUserId 获取用户的优惠券信息
  186. func GetUserCouponByUserId(userId int64) ([]domain.CouponUserJDM, error) {
  187. var couponUserJDMs []domain.CouponUserJDM
  188. err := configs.Engine.Table("goods_coupon_user").
  189. Join("INNER", "goods_coupon", "goods_coupon_user.coupon_id = goods_coupon.id").
  190. Where("goods_coupon_user.user_id = ?", userId).
  191. Find(&couponUserJDMs)
  192. if err != nil {
  193. return couponUserJDMs, err
  194. }
  195. return couponUserJDMs, nil
  196. }
  197. // GetListGoodsOrder 获取用户的订单信息
  198. func GetListGoodsOrder(state string, userId int64, pageNum, pageSize int) (vo.BaseListVo, error) {
  199. var goodsOrder domain.GoodsOrder
  200. Session := configs.Engine.Table("goods_order")
  201. if state != "" {
  202. Session.Where("state = ?", state)
  203. }
  204. rows, err := Session.Where("create_by = ?", userId).Limit(pageSize, (pageNum-1)*pageSize).Desc("id").Rows(&goodsOrder)
  205. var vo vo.BaseListVo
  206. if err != nil {
  207. return vo, err
  208. }
  209. vo.PageNum = pageNum
  210. vo.PageSize = pageSize
  211. vo.List = make([]any, 0)
  212. for rows.Next() {
  213. var u domain.GoodsOrder
  214. rows.Scan(&u)
  215. vo.List = append(vo.List, u)
  216. }
  217. CountSess := configs.Engine.Table("goods_order")
  218. if state != "" {
  219. CountSess.Where("state = ?", state)
  220. }
  221. //查询数量
  222. count, err := CountSess.Where("create_by = ?", userId).Count(&goodsOrder)
  223. vo.Total = int(count)
  224. return vo, nil
  225. }
  226. // 领取优惠券
  227. //func pickUpTheCoupon(couponId int, userId int) (bool, error) {
  228. // gcu := domain.GoodsCouponUser{
  229. // UserId: userId,
  230. // CouponId: couponId,
  231. // CollectionTime: time.Now(),
  232. // State: "1",
  233. // }
  234. // //判断优惠券数量是否充足
  235. // coupon, err := manage.GetByIdGoodsCoupon(couponId)
  236. // if err != nil {
  237. // return false, err
  238. // }
  239. // //判断条件
  240. // if coupon.Count > 0 {
  241. // if coupon.Count == 1 {
  242. // //优惠券数量为1时,直接领取
  243. // _, err := manage.PostGoodsCouponUser(&gcu)
  244. // if err != nil {
  245. // return false, err
  246. // }
  247. // manage.PutGoodsCoupon(map[string]any{"count": coupon.Count - 1}, couponId)
  248. // return true, nil
  249. // } else {
  250. // //优惠券数量大于1时,判断是否满足领取条件
  251. // if coupon.Condition == 1 {
  252. // //满足领取条件
  253. // _, err := manage.PostGoodsCouponUser(&gcu)
  254. // if err != nil {
  255. // }
  256. // }
  257. // }
  258. //
  259. // }
  260. //
  261. //}