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

344 lines
9.1 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
  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.OrderNo {
  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. res, err := pay_service.UnifiedOrder("欧轩互动-直播商品", user.Openid, int64(price+int(option.PostFee*100)),
  178. 4, userId, activity.Id, time.Now().Add(30*time.Second).Unix())
  179. if err != nil {
  180. session.Rollback()
  181. t.CheckErr(err)
  182. }
  183. count, err := session.Where("is_delete=0").Count(&models.CustomerOrder{})
  184. if err != nil {
  185. session.Rollback()
  186. t.CheckErr(err)
  187. }
  188. order := models.CustomerOrder{
  189. OrderNo: fmt.Sprint(define.DefaultOrderNo + int(count)),
  190. ActivityId: activity.Id,
  191. AreaId: area.Id,
  192. AreaName: area.Name,
  193. RehearsalId: activity.RehearsalId,
  194. OutTradeNo: res["out_trade_no"].(string),
  195. BuyerId: user.Id,
  196. TotalAmount: float64(price)/100 + option.PostFee,
  197. PayAmount: float64(price)/100 + option.PostFee,
  198. Status: 0,
  199. Receiver: param.Name,
  200. Address: param.Address,
  201. Phone: param.Phone,
  202. IsDrawCash: 0,
  203. Postage: option.PostFee,
  204. }
  205. _, err = session.InsertOne(&order)
  206. if err != nil {
  207. session.Rollback()
  208. t.CheckErr(err)
  209. }
  210. // 批量插入
  211. beans := make([]interface{}, 0)
  212. for _, subOrder := range subOrders {
  213. subOrder.OrderNo = order.OrderNo
  214. beans = append(beans, subOrder)
  215. }
  216. session.Insert(beans...)
  217. if err != nil {
  218. session.Rollback()
  219. t.CheckErr(err)
  220. }
  221. err = session.Commit()
  222. t.CheckErr(err)
  223. res["order_id"] = order.Id
  224. t.JSON(res)
  225. }
  226. // 为支付的重新支付
  227. func (t *GoodCtl) Reorder() {
  228. outTradeNo := t.MustGet("out_trade_no")
  229. res, err := pay_service.ReOrder(outTradeNo)
  230. t.CheckErr(err)
  231. t.JSON(res)
  232. }
  233. // 订单状态[0未支付1已支付即待发货3已发货4确认收货5申请退款6已退款7申请退货8已退货9已取消]
  234. // 申请退款
  235. func (t *GoodCtl) RefundOrder() {
  236. outTradeNo := t.MustGet("out_trade_no")
  237. order := new(models.CustomerOrder)
  238. exist, err := order.GetByOutTradeNO(outTradeNo)
  239. t.CheckErr(err)
  240. t.Assert(exist, code.MSG_CUSTOMER_ORDER_NOT_EXIST, "用户订单不存在")
  241. if order.Status == 1 { // 退款
  242. _, err = order.UpdateStatusBy(outTradeNo, 1, 5)
  243. } else if order.Status == 3 {
  244. _, err = order.UpdateStatusBy(outTradeNo, 3, 7)
  245. } else {
  246. t.ERROR("订单非已支付或者已发货状态不能申请退款", code.MSG_CUSTOMER_ORDER_ERROR)
  247. return
  248. }
  249. t.CheckErr(err)
  250. t.SUCCESS("成功申请退款")
  251. }
  252. // 取消订单
  253. func (t *GoodCtl) CancelOrder() {
  254. outTradeNo := t.MustGet("out_trade_no")
  255. order := new(models.CustomerOrder)
  256. exist, err := order.GetByOutTradeNO(outTradeNo)
  257. t.CheckErr(err)
  258. t.Assert(exist, code.MSG_CUSTOMER_ORDER_NOT_EXIST, "用户订单不存在")
  259. if order.Status != 0 {
  260. t.ERROR("非待支付状态不能取消", code.MSG_CUSTOMER_ORDER_ERROR)
  261. }
  262. session := core.GetXormAuto().NewSession()
  263. defer session.Close()
  264. err = session.Begin()
  265. t.CheckErr(err)
  266. order.Status = 9
  267. order.CancelTime = time.Now()
  268. _, err = session.Where("is_delete=0 and status=? and out_trade_no=?",
  269. 0, outTradeNo).Cols("status, cancel_time").Update(order)
  270. if err != nil {
  271. session.Rollback()
  272. t.CheckErr(err)
  273. }
  274. subs := make([]*models.CustomerOrderSub, 0)
  275. err = session.Where("is_delete=0 and order_no=?", order.OrderNo).Find(&subs)
  276. if err != nil {
  277. session.Rollback()
  278. t.CheckErr(err)
  279. }
  280. for _, sub := range subs {
  281. _, err = session.Where("id=?", sub.GoodsId).
  282. Incr("stock", sub.GoodsNum).Update(&models.CustomerGoods{})
  283. if err != nil {
  284. session.Rollback()
  285. t.CheckErr(err)
  286. }
  287. }
  288. err = session.Commit()
  289. t.CheckErr(err)
  290. pay_service.Close(outTradeNo)
  291. t.SUCCESS("成功取消订单")
  292. }
  293. func (t *GoodCtl) VerifyOrder() {
  294. outTradeNo := t.MustGet("out_trade_no")
  295. order := new(models.CustomerOrder)
  296. exist, err := order.GetByOutTradeNO(outTradeNo)
  297. t.CheckErr(err)
  298. t.Assert(exist, code.MSG_CUSTOMER_ORDER_NOT_EXIST, "用户订单不存在")
  299. if order.Status != 3 && order.Status != 1 {
  300. t.ERROR("非已支付或已发货状态不能确认", code.MSG_CUSTOMER_ORDER_ERROR)
  301. }
  302. _, err = order.UpdateStatusBy(outTradeNo, 3, 4)
  303. t.CheckErr(err)
  304. t.SUCCESS("成功确认已收货")
  305. }