service-order.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package api
  2. import (
  3. "fmt"
  4. "sku3dweb/db/model"
  5. "sku3dweb/db/repo"
  6. "time"
  7. "github.com/gin-gonic/gin"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. )
  11. //订单
  12. func Order(r *GinRouter) {
  13. // 查看订单
  14. r.GETJWTKEY("/order/detail/:id", GetOrder, SKU3DUSER, SKU3DADMIN)
  15. // 查看订单列表
  16. r.GETJWTKEY("/order/list", GetOrders, SKU3DUSER, SKU3DADMIN)
  17. // 添加
  18. r.POSTJWTKEY("/order/create", CreateOrder, SKU3DUSER, SKU3DADMIN)
  19. // 修改订单
  20. r.POSTJWTKEY("/order/update", UpdateOrder, SKU3DUSER, SKU3DADMIN)
  21. // 删除订单
  22. r.POSTJWTKEY("/order/delete/:id", DeleteOrder, SKU3DUSER, SKU3DADMIN)
  23. }
  24. func GetOrder(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  25. idStr := c.Param("id")
  26. id, err := primitive.ObjectIDFromHex(idStr)
  27. if err != nil {
  28. return false, err
  29. }
  30. query := repo.Map{"_id": id}
  31. // 非管理员只能看自己的订单
  32. if apictx.User.Key == SKU3DUSER {
  33. pid, err := primitive.ObjectIDFromHex(apictx.User.Parent)
  34. if err != nil {
  35. return false, err
  36. }
  37. query["userId"] = pid
  38. }
  39. option := &repo.DocSearchOptions{
  40. CollectName: repo.CollectionOrder,
  41. Query: query,
  42. }
  43. found, m := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), option)
  44. if !found {
  45. return false, NewError("没有找到该订单")
  46. }
  47. return m, nil
  48. }
  49. func GetOrders(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  50. page, size, query := UtilQueryPageSize(c)
  51. option := &repo.PageSearchOptions{
  52. CollectName: repo.CollectionOrder,
  53. Query: query,
  54. Page: page,
  55. Size: size,
  56. Sort: bson.M{"createTime": -1},
  57. }
  58. // 非管理员只能看自己的订单
  59. if apictx.User.Key == SKU3DUSER {
  60. pid, err := primitive.ObjectIDFromHex(apictx.User.Parent)
  61. if err != nil {
  62. return false, err
  63. }
  64. query["userId"] = pid
  65. }
  66. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  67. }
  68. func CreateOrder(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  69. var order model.Order
  70. err := c.ShouldBindJSON(&order)
  71. if err != nil {
  72. fmt.Println(err)
  73. return false, err
  74. }
  75. ctx := apictx.CreateRepoCtx()
  76. pid, err := primitive.ObjectIDFromHex(apictx.User.Parent)
  77. if err != nil {
  78. return false, err
  79. }
  80. query := repo.Map{"userId": pid}
  81. // 申请免费版,或者购买企业版
  82. if order.Apply != nil && order.Member == nil {
  83. query["status"] = model.Applying
  84. option := &repo.DocSearchOptions{
  85. CollectName: repo.CollectionOrder,
  86. Query: query,
  87. }
  88. found, _ := repo.RepoSeachDocMap(ctx, option)
  89. if found {
  90. return false, NewError("已申请")
  91. }
  92. order.Status = &model.Applying
  93. } else if order.Apply == nil && order.Member != nil {
  94. query["status"] = model.WatiPay
  95. option := &repo.DocSearchOptions{
  96. CollectName: repo.CollectionOrder,
  97. Query: query,
  98. }
  99. found, _ := repo.RepoSeachDocMap(ctx, option)
  100. if found {
  101. return false, NewError("请先完成已有支付")
  102. }
  103. order.Status = &model.WatiPay
  104. // 查询member信息
  105. var m model.Member
  106. option1 := &repo.DocSearchOptions{
  107. CollectName: repo.CollectionMember,
  108. Query: repo.Map{"_id": order.Member.Id},
  109. }
  110. found, _ = repo.RepoSeachDoc(ctx, option1, &m)
  111. if !found {
  112. return false, NewError("不存在该会员信息")
  113. }
  114. m.Day = order.Member.Day
  115. order.Member.Member = &m
  116. // 直接购买/续费
  117. var total int
  118. if order.Member.Day != nil { //直接购买默认是5个席位
  119. // 席数*价格*年
  120. order.Member.UserNum = IntValue(5)
  121. m.UserNum = order.Member.UserNum
  122. total = 2000
  123. } else {
  124. // 添加席位
  125. // 查询多少过期时间
  126. var mu model.MemberUser
  127. option := &repo.DocSearchOptions{
  128. CollectName: repo.CollectionMemberUser,
  129. Query: repo.Map{"parentId": pid, "memberId": m.Id},
  130. Project: []string{"deadTime"},
  131. }
  132. found, _ := repo.RepoSeachDoc(ctx, option, &mu)
  133. if !found {
  134. return false, NewError("未购买过企业会员")
  135. }
  136. // 剩余天数
  137. day := int(time.Until(mu.DeadTime) / (time.Hour * 24))
  138. if day <= 0 {
  139. day = 1
  140. }
  141. // 计算一天需要多少钱
  142. dayPrice := *order.Member.Price / 365
  143. total = (*order.Member.UserNum) * dayPrice * day
  144. }
  145. order.Amount = &total
  146. } else {
  147. return false, NewError("请求错误")
  148. }
  149. if apictx.User.Parent != "" {
  150. pid, err := primitive.ObjectIDFromHex(apictx.User.Parent)
  151. if err != nil {
  152. return false, err
  153. }
  154. order.UserId = &pid
  155. } else {
  156. order.UserId = &pid
  157. }
  158. order.UserName = apictx.User.Name
  159. order.CreateTime = time.Now()
  160. order.UpdateTime = time.Now()
  161. return repo.RepoAddDoc(ctx, repo.CollectionOrder, &order)
  162. }
  163. func UpdateOrder(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  164. var order model.Order
  165. err := c.ShouldBindJSON(&order)
  166. if err != nil {
  167. return false, nil
  168. }
  169. if order.Id == nil {
  170. return false, NewError("Id不能为空")
  171. }
  172. ctx := apictx.CreateRepoCtx()
  173. option := &repo.DocSearchOptions{
  174. CollectName: repo.CollectionOrder,
  175. Query: repo.Map{"_id": order.Id},
  176. Project: []string{"userId"},
  177. }
  178. found, _ := repo.RepoSeachDoc(ctx, option, &order)
  179. if !found {
  180. return false, NewError("不存在该订单")
  181. }
  182. if apictx.User.Key == SKU3DUSER {
  183. order.Status = &model.Applying
  184. }
  185. // 免费版加一年时间
  186. if *order.Status == model.ApplySuccess {
  187. // 获取免费版
  188. var member model.Member
  189. option := &repo.DocSearchOptions{
  190. CollectName: repo.CollectionMember,
  191. Query: repo.Map{"level": model.FREE},
  192. }
  193. found, _ := repo.RepoSeachDoc(ctx, option, &member)
  194. if !found {
  195. return false, err
  196. }
  197. // 更新增加时间
  198. data := repo.Map{"deadTime": time.Now().Add(365 * time.Hour * 24)}
  199. res, err := repo.RepoUpdateOneDoc(apictx.CreateRepoCtx(), repo.CollectionMemberUser, bson.M{"parentId": order.UserId, "memberId": member.Id}, data)
  200. fmt.Println(res, err)
  201. order.Status = &model.ApplySuccess
  202. }
  203. order.UpdateTime = time.Now()
  204. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, order.Id.Hex(), &order)
  205. }
  206. func DeleteOrder(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  207. idStr := c.Param("id")
  208. id, err := primitive.ObjectIDFromHex(idStr)
  209. if err != nil {
  210. return false, nil
  211. }
  212. // 1、查询是否存在
  213. query := repo.Map{"_id": id}
  214. if apictx.User.Key == SKU3DUSER {
  215. userId, err := primitive.ObjectIDFromHex(apictx.User.ID)
  216. if err != nil {
  217. return false, err
  218. }
  219. query["userId"] = userId
  220. }
  221. var order model.Order
  222. ctx := apictx.CreateRepoCtx()
  223. option := &repo.DocSearchOptions{
  224. CollectName: repo.CollectionOrder,
  225. Query: query,
  226. }
  227. found, _ := repo.RepoSeachDoc(ctx, option, &order)
  228. if !found {
  229. return false, NewError("不存在该订单")
  230. }
  231. // 2、关闭订单
  232. // om := &model.OrderMsg{Id: order.Id.Hex(), PayMode: order.PayMod, ModName: "SKU3D"}
  233. // res, err := bus.CloseOrder(om)
  234. // if err != nil {
  235. // return false, err
  236. // }
  237. // if res.Code != model.SUCCESS.Code {
  238. // return false, NewError(res.Message)
  239. // }
  240. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, idStr)
  241. }