互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
7.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package client
  2. import (
  3. "encoding/json"
  4. "github.com/ouxuanserver/osmanthuswine/src/core"
  5. "hudongzhuanjia/controllers"
  6. "hudongzhuanjia/models"
  7. pay_service "hudongzhuanjia/services/pay"
  8. "hudongzhuanjia/utils/code"
  9. "time"
  10. )
  11. type GoodCtl struct {
  12. controllers.AuthorCtl
  13. }
  14. func (t *GoodCtl) GoodOption() {
  15. activityId := t.MustGetInt64("activity_id")
  16. //areaId := t.MustGetInt64("area_id")
  17. option := new(models.CustomerOrderOption)
  18. exist, err := option.GetByActivityId(activityId)
  19. t.CheckErr(err)
  20. if !exist {
  21. t.JSON([]interface{}{})
  22. return
  23. }
  24. //t.Assert(exist, code.MSG_ORDER_NOT_EXIST, "订单活动不存在")
  25. t.JSON(option)
  26. }
  27. // 商品列表
  28. func (t *GoodCtl) ListGood() {
  29. activityId := t.MustGetInt64("activity_id")
  30. areaId := t.MustGetInt64("area_id")
  31. option := new(models.CustomerOrderOption)
  32. exist, err := option.GetByActivityId(activityId)
  33. t.CheckErr(err)
  34. if !exist {
  35. t.JSON([]interface{}{})
  36. return
  37. }
  38. //t.Assert(exist, code.MSG_ORDER_NOT_EXIST, "订单活动不存在")
  39. goods, err := models.GetGoodsByActivityId(activityId, areaId)
  40. t.CheckErr(err)
  41. //
  42. //for index := range goods {
  43. // goods[index].GoodType = option.PostFeeType
  44. // goods[index].Postage = option.PostFee
  45. //}
  46. t.JSON(goods)
  47. return
  48. }
  49. func (t *GoodCtl) ListOrder() {
  50. activityId := t.MustGetInt64("activity_id")
  51. status := t.MustGetInt("status")
  52. areaId := t.MustGetInt64("area_id")
  53. uid := t.MustGetUID()
  54. activity := new(models.Activity)
  55. exist, err := models.GetById(activity, activityId)
  56. t.CheckErr(err)
  57. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  58. orders, err := models.GetCustomerOrdersByActivityId(uid, activity.Id, activity.RehearsalId,
  59. areaId, status, t.Page, t.PageSize)
  60. t.CheckErr(err)
  61. outTradeNos := make([]string, 0)
  62. for _, order := range orders {
  63. outTradeNos = append(outTradeNos, order.OutTradeNo)
  64. }
  65. subs, err := models.GetCustomerOrderSubByOutTradeNos(outTradeNos)
  66. t.CheckErr(err)
  67. for index, order := range orders {
  68. for _, sub := range subs {
  69. if order.OutTradeNo == sub["out_trade_no"] {
  70. orders[index].SubOrders = append(orders[index].SubOrders, sub)
  71. }
  72. }
  73. }
  74. t.JSON(orders)
  75. }
  76. type OrderParam struct {
  77. ActivityId int64 `json:"activity_id"`
  78. AreaId int64 `json:"area_id"`
  79. Name string `json:"name"`
  80. Phone string `json:"phone"`
  81. Address string `json:"address"`
  82. Goods []map[string]int `json:"goods"`
  83. }
  84. // 下订单
  85. func (t *GoodCtl) Order() {
  86. userId := t.MustGetUID() //
  87. param := OrderParam{}
  88. if t.Request.OriginRequest.Method == "POST" {
  89. t.CheckErr(t.Bind(&param))
  90. } else if t.Request.OriginRequest.Method == "GET" {
  91. param.ActivityId = t.MustGetInt64("activity_id")
  92. param.AreaId = t.MustGetInt64("area_id")
  93. param.Name = t.MustGet("name")
  94. param.Phone = t.MustGet("phone")
  95. param.Address = t.MustGet("address")
  96. goods := t.MustGet("goods")
  97. err := json.Unmarshal([]byte(goods), &param.Goods)
  98. t.CheckErr(err)
  99. }
  100. activity := new(models.Activity)
  101. exist, err := models.GetById(activity, param.ActivityId)
  102. t.CheckErr(err)
  103. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  104. area := new(models.AreaStore)
  105. exist, err = models.GetById(area, param.AreaId)
  106. t.CheckErr(err)
  107. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  108. user := new(models.User)
  109. exist, err = models.GetById(user, userId)
  110. t.CheckErr(err)
  111. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  112. option := new(models.CustomerOrderOption)
  113. exist, err = option.GetByActivityId(param.ActivityId)
  114. t.CheckErr(err)
  115. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "订单活动不存在")
  116. //if option.Status == 0 {
  117. // t.ERROR("订单活动尚未开启", code.MSG_ORDER_RULE_NOT_EXIST)
  118. // return
  119. //}
  120. // 注意库存 --> 开启事务
  121. session := core.GetXormAuto().NewSession()
  122. defer session.Close()
  123. session.Begin()
  124. subOrders := make([]*models.CustomerOrderSub, 0)
  125. var price = 0
  126. for _, g := range param.Goods {
  127. good := new(models.CustomerGoods)
  128. exist, err = session.Where("is_delete=0 and id=?", g["id"]).Get(good)
  129. if err != nil || !exist {
  130. session.Rollback()
  131. t.ERROR("商品信息异常", code.MSG_DATA_NOT_EXIST)
  132. }
  133. if good.Stock-g["num"] < 0 {
  134. session.Rollback()
  135. t.ERROR("商品库存不足", code.MSG_DATA_NOT_EXIST)
  136. }
  137. if good.Stock > 0 {
  138. _, err = session.ID(good.Id).Decr("stock", g["num"]).Cols("stock").Update(good)
  139. if err != nil {
  140. session.Rollback()
  141. t.CheckErr(err)
  142. }
  143. }
  144. price += int(good.Price*100) * g["num"]
  145. subOrders = append(subOrders, &models.CustomerOrderSub{
  146. IsDelete: false,
  147. CreatedAt: time.Now(),
  148. UpdatedAt: time.Now(),
  149. OutTradeNo: "",
  150. GoodsId: good.Id,
  151. GoodsNum: g["num"],
  152. GoodName: good.Name,
  153. GoodPrice: good.Price,
  154. })
  155. }
  156. res, err := pay_service.UnifiedOrder("欧轩互动-直播商品", user.Openid,
  157. int64(price+int(option.PostFee*100)), 4, userId, activity.Id)
  158. if err != nil {
  159. session.Rollback()
  160. t.CheckErr(err)
  161. }
  162. order := &models.CustomerOrder{
  163. IsDelete: false,
  164. CreatedAt: time.Now(),
  165. UpdatedAt: time.Now(),
  166. //UserPrizeId: , // 奖品
  167. ActivityId: activity.Id,
  168. AreaId: area.Id,
  169. AreaName: area.Name,
  170. RehearsalId: activity.RehearsalId,
  171. OutTradeNo: res["out_trade_no"].(string),
  172. //OrderEntryPersonId: ,
  173. BuyerId: user.Id,
  174. //GoodsId: ,
  175. //GoodsName: "",
  176. //GoodsNum: 0,
  177. TotalAmount: float64(price)/100 + option.PostFee,
  178. PayAmount: float64(price)/100 + option.PostFee,
  179. Status: 0,
  180. Receiver: param.Name,
  181. Address: param.Address,
  182. Phone: param.Phone,
  183. IsDrawCash: 0,
  184. Postage: option.PostFee,
  185. //ExpressId: 0,
  186. //ExpressName: "",
  187. //ExpressNo: "",
  188. //CancelTime: time.Time{},
  189. AutoReceiveTime: time.Now().AddDate(0, 0, 15),
  190. //OrderTime: time.Now(),
  191. //Good: nil,
  192. //User: nil,
  193. //OrderEntryPersonName: "",
  194. //SubOrders: nil,
  195. }
  196. _, err = session.InsertOne(order)
  197. if err != nil {
  198. session.Rollback()
  199. t.CheckErr(err)
  200. }
  201. for _, subOrder := range subOrders {
  202. subOrder.OutTradeNo = order.OutTradeNo
  203. _, err = session.InsertOne(subOrder)
  204. if err != nil {
  205. session.Rollback()
  206. t.CheckErr(err)
  207. }
  208. }
  209. session.Commit()
  210. res["order_id"] = order.Id
  211. t.JSON(res)
  212. }
  213. // 为支付的重新支付
  214. func (t *GoodCtl) Reorder() {
  215. outTradeNo := t.MustGet("out_trade_no")
  216. res, err := pay_service.ReOrder(outTradeNo)
  217. t.CheckErr(err)
  218. t.JSON(res)
  219. }
  220. // 订单状态[0未支付1已支付即待发货3已发货4确认收货5申请退款6已退款7申请退货8已退货9已取消]
  221. // 申请退款
  222. func (t *GoodCtl) RefundOrder() {
  223. outTradeNo := t.MustGet("out_trade_no")
  224. order := new(models.CustomerOrder)
  225. exist, err := order.GetByOutTradeNO(outTradeNo)
  226. t.CheckErr(err)
  227. t.Assert(exist, code.MSG_CUSTOMER_ORDER_NOT_EXIST, "用户订单不存在")
  228. if order.Status == 1 { // 退款
  229. _, err = order.UpdateStatusBy(outTradeNo, 1, 5)
  230. } else if order.Status == 3 {
  231. _, err = order.UpdateStatusBy(outTradeNo, 3, 7)
  232. } else {
  233. t.ERROR("订单完成状态不能申请退款", code.MSG_CUSTOMER_ORDER_ERROR)
  234. return
  235. }
  236. t.CheckErr(err)
  237. t.SUCCESS("成功申请退款")
  238. }
  239. func (t *GoodCtl) CancelOrder() {
  240. outTradeNo := t.MustGet("out_trade_no")
  241. order := new(models.CustomerOrder)
  242. exist, err := order.GetByOutTradeNO(outTradeNo)
  243. t.CheckErr(err)
  244. t.Assert(exist, code.MSG_CUSTOMER_ORDER_NOT_EXIST, "用户订单不存在")
  245. if order.Status != 0 {
  246. t.ERROR("非待支付状态不能取消", code.MSG_CUSTOMER_ORDER_ERROR)
  247. }
  248. _, err = order.UpdateStatusBy(outTradeNo, 0, 9)
  249. t.CheckErr(err)
  250. t.SUCCESS("成功取消订单")
  251. }