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

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