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

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