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

339 lines
9.0 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
  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. goodIds := make([]interface{}, 0)
  131. for _, g := range param.Goods {
  132. goodIds = append(goodIds, g["id"])
  133. }
  134. goods := make([]*models.CustomerGoods, 0)
  135. err = session.Where("is_delete=0").In("id", goodIds).Find(&goods)
  136. if err != nil || len(goods) != len(goodIds) || len(goods) <= 0 {
  137. session.Rollback()
  138. t.ERROR("商品信息异常", code.MSG_DATA_NOT_EXIST)
  139. }
  140. subOrders := make([]*models.CustomerOrderSub, 0)
  141. var price = 0
  142. // 检测库存
  143. for _, good := range goods {
  144. for _, g := range param.Goods {
  145. if good.Id == int64(g["id"]) {
  146. if good.Stock-g["num"] < 0 {
  147. session.Rollback()
  148. t.ERROR("商品库存不足", code.MSG_DATA_NOT_EXIST)
  149. } else {
  150. _, err = session.ID(good.Id).Decr("stock", g["num"]).Cols("stock").Update(good)
  151. if err != nil {
  152. session.Rollback()
  153. t.CheckErr(err)
  154. }
  155. price += int(good.Price*100) * g["num"]
  156. subOrders = append(subOrders, &models.CustomerOrderSub{
  157. IsDelete: false,
  158. CreatedAt: time.Now(),
  159. UpdatedAt: time.Now(),
  160. OrderNo: "",
  161. GoodsId: good.Id,
  162. GoodsNum: g["num"],
  163. GoodName: good.Name,
  164. GoodPrice: good.Price,
  165. })
  166. break
  167. }
  168. }
  169. }
  170. }
  171. res, err := pay_service.UnifiedOrder("欧轩互动-直播商品", user.Openid,
  172. int64(price+int(option.PostFee*100)), 4, userId, activity.Id)
  173. if err != nil {
  174. session.Rollback()
  175. t.CheckErr(err)
  176. }
  177. count, err := session.Where("is_delete=0").Count(&models.CustomerOrder{})
  178. if err != nil {
  179. session.Rollback()
  180. t.CheckErr(err)
  181. }
  182. order := models.CustomerOrder{
  183. IsDelete: false,
  184. CreatedAt: time.Now(),
  185. UpdatedAt: time.Now(),
  186. OrderNo: fmt.Sprint(define.DefaultOrderNo + int(count)),
  187. ActivityId: activity.Id,
  188. AreaId: area.Id,
  189. AreaName: area.Name,
  190. RehearsalId: activity.RehearsalId,
  191. OutTradeNo: res["out_trade_no"].(string),
  192. BuyerId: user.Id,
  193. TotalAmount: float64(price)/100 + option.PostFee,
  194. PayAmount: float64(price)/100 + option.PostFee,
  195. Status: 0,
  196. Receiver: param.Name,
  197. Address: param.Address,
  198. Phone: param.Phone,
  199. IsDrawCash: 0,
  200. Postage: option.PostFee,
  201. }
  202. _, err = session.InsertOne(&order)
  203. if err != nil {
  204. session.Rollback()
  205. t.CheckErr(err)
  206. }
  207. // 批量插入
  208. beans := make([]interface{}, 0)
  209. for _, subOrder := range subOrders {
  210. subOrder.OrderNo = order.OrderNo
  211. beans = append(beans, subOrder)
  212. }
  213. session.Insert(beans...)
  214. if err != nil {
  215. session.Rollback()
  216. t.CheckErr(err)
  217. }
  218. err = session.Commit()
  219. t.CheckErr(err)
  220. res["order_id"] = order.Id
  221. t.JSON(res)
  222. }
  223. // 为支付的重新支付
  224. func (t *GoodCtl) Reorder() {
  225. outTradeNo := t.MustGet("out_trade_no")
  226. res, err := pay_service.ReOrder(outTradeNo)
  227. t.CheckErr(err)
  228. t.JSON(res)
  229. }
  230. // 订单状态[0未支付1已支付即待发货3已发货4确认收货5申请退款6已退款7申请退货8已退货9已取消]
  231. // 申请退款
  232. func (t *GoodCtl) RefundOrder() {
  233. outTradeNo := t.MustGet("out_trade_no")
  234. order := new(models.CustomerOrder)
  235. exist, err := order.GetByOutTradeNO(outTradeNo)
  236. t.CheckErr(err)
  237. t.Assert(exist, code.MSG_CUSTOMER_ORDER_NOT_EXIST, "用户订单不存在")
  238. if order.Status == 1 { // 退款
  239. _, err = order.UpdateStatusBy(outTradeNo, 1, 5)
  240. } else if order.Status == 3 {
  241. _, err = order.UpdateStatusBy(outTradeNo, 3, 7)
  242. } else {
  243. t.ERROR("订单非已支付或者已发货状态不能申请退款", code.MSG_CUSTOMER_ORDER_ERROR)
  244. return
  245. }
  246. t.CheckErr(err)
  247. t.SUCCESS("成功申请退款")
  248. }
  249. // 取消订单
  250. func (t *GoodCtl) CancelOrder() {
  251. outTradeNo := t.MustGet("out_trade_no")
  252. order := new(models.CustomerOrder)
  253. exist, err := order.GetByOutTradeNO(outTradeNo)
  254. t.CheckErr(err)
  255. t.Assert(exist, code.MSG_CUSTOMER_ORDER_NOT_EXIST, "用户订单不存在")
  256. if order.Status != 0 {
  257. t.ERROR("非待支付状态不能取消", code.MSG_CUSTOMER_ORDER_ERROR)
  258. }
  259. session := core.GetXormAuto().NewSession()
  260. defer session.Close()
  261. err = session.Begin()
  262. t.CheckErr(err)
  263. order.Status = 9
  264. order.CancelTime = time.Now()
  265. _, err = session.Where("is_delete=0 and status=? and out_trade_no=?",
  266. 0, outTradeNo).Cols("status, cancel_time").Update(order)
  267. if err != nil {
  268. session.Rollback()
  269. t.CheckErr(err)
  270. }
  271. subs := make([]*models.CustomerOrderSub, 0)
  272. err = session.Where("is_delete=0 and order_no=?", order.OrderNo).Find(&subs)
  273. if err != nil {
  274. session.Rollback()
  275. t.CheckErr(err)
  276. }
  277. for _, sub := range subs {
  278. _, err = session.Where("id=?", sub.GoodsId).
  279. Incr("stock", sub.GoodsNum).Update(&models.CustomerGoods{})
  280. if err != nil {
  281. session.Rollback()
  282. t.CheckErr(err)
  283. }
  284. }
  285. err = session.Commit()
  286. t.CheckErr(err)
  287. pay_service.Close(outTradeNo)
  288. t.SUCCESS("成功取消订单")
  289. }
  290. func (t *GoodCtl) VerifyOrder() {
  291. outTradeNo := t.MustGet("out_trade_no")
  292. order := new(models.CustomerOrder)
  293. exist, err := order.GetByOutTradeNO(outTradeNo)
  294. t.CheckErr(err)
  295. t.Assert(exist, code.MSG_CUSTOMER_ORDER_NOT_EXIST, "用户订单不存在")
  296. if order.Status != 3 && order.Status != 1 {
  297. t.ERROR("非已支付或已发货状态不能确认", code.MSG_CUSTOMER_ORDER_ERROR)
  298. }
  299. _, err = order.UpdateStatusBy(outTradeNo, 3, 4)
  300. t.CheckErr(err)
  301. t.SUCCESS("成功确认已收货")
  302. }