互动
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.

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