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

354 lines
9.2 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
5 years ago
4 years ago
4 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
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. for _, g := range param.Goods {
  178. if good.Id == g["id"] {
  179. for _, v := range orderGoodNum { // 库存计算
  180. if v.GoodsId == good.Id {
  181. good.Stock -= v.GoodsNum
  182. }
  183. }
  184. if good.Stock-g["num"] < 0 {
  185. s.Rollback()
  186. t.ERROR("商品库存不足", code.MSG_DATA_NOT_EXIST)
  187. } else {
  188. // 需要减掉库存
  189. //_, err = s.ID(good.Id).Decr("stock", g["num"]).Cols("stock").Update(good)
  190. //if err != nil {
  191. // s.Rollback()
  192. // t.CheckErr(err)
  193. //}
  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. order := models.CustomerOrder{
  216. OrderNo: fmt.Sprint(define.DefaultOrderNo + int(count)),
  217. ActivityId: activity.Id,
  218. ArchId: activity.ArchId,
  219. AreaId: area.Id,
  220. AreaName: area.Name,
  221. RehearsalId: activity.RehearsalId,
  222. BuyerId: user.Id,
  223. TotalAmount: float64(price)/100 + option.PostFee,
  224. PayAmount: float64(price)/100 + option.PostFee,
  225. Status: 1,
  226. Type: 1, // 直播订单
  227. Receiver: param.Name,
  228. Address: param.Address,
  229. Phone: param.Phone,
  230. GoodsId: subOrders[0].GoodsId,
  231. GoodsName: subOrders[0].GoodName,
  232. IsDrawCash: 0,
  233. ExpireTime: expireAt,
  234. Postage: option.PostFee,
  235. }
  236. amount := price + int(option.PostFee*100)
  237. res := make(map[string]interface{})
  238. if amount > 0.00 {
  239. res, err = pay_service.UnifiedOrder("欧轩互动-直播商品", user.Openid, amount, 4, userId,
  240. activity.Id, expireAt)
  241. if err != nil {
  242. s.Rollback()
  243. t.CheckErr(err)
  244. }
  245. order.OutTradeNo = res["out_trade_no"].(string)
  246. order.Status = 0
  247. }
  248. _, err = s.InsertOne(&order)
  249. if err != nil {
  250. s.Rollback()
  251. t.CheckErr(err)
  252. }
  253. // 批量插入
  254. beans := make([]interface{}, 0)
  255. for _, subOrder := range subOrders {
  256. subOrder.OrderId = order.Id
  257. beans = append(beans, subOrder)
  258. }
  259. s.Insert(beans...)
  260. if err != nil {
  261. s.Rollback()
  262. t.CheckErr(err)
  263. }
  264. err = s.Commit()
  265. t.CheckErr(err)
  266. res["order_id"] = order.Id
  267. t.JSON(res)
  268. }
  269. // 为支付的重新支付
  270. func (t *GoodCtl) Reorder() {
  271. outTradeNo := t.MustGet("out_trade_no")
  272. res, err := pay_service.ReOrder(outTradeNo)
  273. t.CheckErr(err)
  274. t.JSON(res)
  275. }
  276. // 订单状态[0未支付1已支付即待发货3已发货4确认收货5申请退款6已退款7申请退货8已退货9已取消]
  277. // 申请退款
  278. func (t *GoodCtl) RefundOrder() {
  279. outTradeNo := t.MustGet("out_trade_no")
  280. order := new(models.CustomerOrder)
  281. exist, err := order.GetByOutTradeNO(outTradeNo)
  282. t.CheckErr(err)
  283. t.Assert(exist, code.MSG_CUSTOMER_ORDER_NOT_EXIST, "用户订单不存在")
  284. if order.Status == 1 { // 退款
  285. _, err = order.UpdateStatusBy(outTradeNo, 1, 5)
  286. } else if order.Status == 3 {
  287. _, err = order.UpdateStatusBy(outTradeNo, 3, 7)
  288. } else {
  289. t.ERROR("订单非已支付或者已发货状态不能申请退款", code.MSG_CUSTOMER_ORDER_ERROR)
  290. return
  291. }
  292. t.CheckErr(err)
  293. t.SUCCESS("成功申请退款")
  294. }
  295. // 取消订单
  296. func (t *GoodCtl) CancelOrder() {
  297. outTradeNo := t.MustGet("out_trade_no")
  298. pay_service.HandleCancelOrder(outTradeNo)
  299. pay_service.Close(outTradeNo)
  300. t.SUCCESS("成功取消订单")
  301. }
  302. func (t *GoodCtl) VerifyOrder() {
  303. outTradeNo := t.MustGet("out_trade_no")
  304. order := new(models.CustomerOrder)
  305. exist, err := order.GetByOutTradeNO(outTradeNo)
  306. t.CheckErr(err)
  307. t.Assert(exist, code.MSG_CUSTOMER_ORDER_NOT_EXIST, "用户订单不存在")
  308. if order.Status != 3 && order.Status != 1 {
  309. t.ERROR("非已支付或已发货状态不能确认", code.MSG_CUSTOMER_ORDER_ERROR)
  310. }
  311. _, err = order.UpdateStatusBy(outTradeNo, 3, 4)
  312. t.CheckErr(err)
  313. t.SUCCESS("成功确认已收货")
  314. }