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

311 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
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, activity.ArchId)
  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. ArchId: activity.ArchId,
  193. AreaId: area.Id,
  194. AreaName: area.Name,
  195. RehearsalId: activity.RehearsalId,
  196. OutTradeNo: res["out_trade_no"].(string),
  197. BuyerId: user.Id,
  198. TotalAmount: float64(price)/100 + option.PostFee,
  199. PayAmount: float64(price)/100 + option.PostFee,
  200. Status: 0,
  201. Type: 1, // 直播订单
  202. Receiver: param.Name,
  203. Address: param.Address,
  204. Phone: param.Phone,
  205. IsDrawCash: 0,
  206. ExpireTime: expireAt,
  207. Postage: option.PostFee,
  208. }
  209. _, err = session.InsertOne(&order)
  210. if err != nil {
  211. session.Rollback()
  212. t.CheckErr(err)
  213. }
  214. // 批量插入
  215. beans := make([]interface{}, 0)
  216. for _, subOrder := range subOrders {
  217. subOrder.OrderNo = order.OrderNo
  218. beans = append(beans, subOrder)
  219. }
  220. session.Insert(beans...)
  221. if err != nil {
  222. session.Rollback()
  223. t.CheckErr(err)
  224. }
  225. err = session.Commit()
  226. t.CheckErr(err)
  227. res["order_id"] = order.Id
  228. t.JSON(res)
  229. }
  230. // 为支付的重新支付
  231. func (t *GoodCtl) Reorder() {
  232. outTradeNo := t.MustGet("out_trade_no")
  233. res, err := pay_service.ReOrder(outTradeNo)
  234. t.CheckErr(err)
  235. t.JSON(res)
  236. }
  237. // 订单状态[0未支付1已支付即待发货3已发货4确认收货5申请退款6已退款7申请退货8已退货9已取消]
  238. // 申请退款
  239. func (t *GoodCtl) RefundOrder() {
  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 == 1 { // 退款
  246. _, err = order.UpdateStatusBy(outTradeNo, 1, 5)
  247. } else if order.Status == 3 {
  248. _, err = order.UpdateStatusBy(outTradeNo, 3, 7)
  249. } else {
  250. t.ERROR("订单非已支付或者已发货状态不能申请退款", code.MSG_CUSTOMER_ORDER_ERROR)
  251. return
  252. }
  253. t.CheckErr(err)
  254. t.SUCCESS("成功申请退款")
  255. }
  256. // 取消订单
  257. func (t *GoodCtl) CancelOrder() {
  258. outTradeNo := t.MustGet("out_trade_no")
  259. pay_service.HandleCancelOrder(outTradeNo)
  260. pay_service.Close(outTradeNo)
  261. t.SUCCESS("成功取消订单")
  262. }
  263. func (t *GoodCtl) VerifyOrder() {
  264. outTradeNo := t.MustGet("out_trade_no")
  265. order := new(models.CustomerOrder)
  266. exist, err := order.GetByOutTradeNO(outTradeNo)
  267. t.CheckErr(err)
  268. t.Assert(exist, code.MSG_CUSTOMER_ORDER_NOT_EXIST, "用户订单不存在")
  269. if order.Status != 3 && order.Status != 1 {
  270. t.ERROR("非已支付或已发货状态不能确认", code.MSG_CUSTOMER_ORDER_ERROR)
  271. }
  272. _, err = order.UpdateStatusBy(outTradeNo, 3, 4)
  273. t.CheckErr(err)
  274. t.SUCCESS("成功确认已收货")
  275. }