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

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