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

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