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

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