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

547 lines
15 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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. invitation_service "hudongzhuanjia/services/invitation"
  8. "hudongzhuanjia/utils"
  9. "hudongzhuanjia/utils/code"
  10. "hudongzhuanjia/utils/define"
  11. "github.com/ouxuanserver/osmanthuswine/src/core"
  12. )
  13. type OrderEntryCtl struct {
  14. controllers.AuthorCtl
  15. }
  16. // 用户查看所有商品
  17. func (t *OrderEntryCtl) List() {
  18. _type := t.GetAccountType()
  19. uid := t.GetAccountId()
  20. activityId := t.MustGetInt("activity_id")
  21. areaId := 0
  22. if _type == define.TYPE_ENTRYPEOPLE {
  23. entryPerson := models.OrderEntryPerson{}
  24. exist, err := models.Get(&entryPerson, uid)
  25. t.CheckErr(err)
  26. t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员信息异常")
  27. areaId = entryPerson.AreaId
  28. } else {
  29. areaId = t.MustGetInt("area_id")
  30. }
  31. goods, err := models.GetGoodsByActivityId(activityId, areaId)
  32. t.CheckErr(err)
  33. if _type == define.TYPE_H5USER {
  34. for index := range goods {
  35. url := fmt.Sprintf("%s/PcClient/Client/OrderEntryCtl/order?"+
  36. "user_id=%d&activity_id=%d&good_id=%d", define.HOST, uid, activityId, goods[index].Id)
  37. qrcode, err := utils.Qrcode2Base64(url)
  38. t.CheckErr(err)
  39. goods[index].Qrcode = qrcode
  40. }
  41. }
  42. t.JSON(map[string]interface{}{
  43. "list": goods,
  44. "total": len(goods),
  45. })
  46. }
  47. // 扫二维码下单
  48. func (t *OrderEntryCtl) Order() {
  49. userId := t.MustGetInt("user_id")
  50. goodId := t.MustGetInt("good_id")
  51. entryId := t.GetAccountId()
  52. entryPerson := models.OrderEntryPerson{}
  53. exist, err := models.Get(&entryPerson, entryId)
  54. t.CheckErr(err)
  55. t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员不存在")
  56. activity := models.Activity{}
  57. exist, err = models.Get(&activity, entryPerson.ActivityId)
  58. t.CheckErr(err)
  59. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  60. t.CheckRunning(activity.Status)
  61. area := models.AreaStore{}
  62. exist, err = models.Get(&area, entryPerson.AreaId)
  63. t.CheckErr(err)
  64. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  65. letter := &models.InvitationLetter{}
  66. exist, err = letter.GetByUserIdAndActivityId(userId, activity.Id, activity.ArchId, activity.RehearsalId)
  67. t.CheckErr(err)
  68. t.Assert(exist, code.MSG_INVITE_LETTER_NOT_EXIST, "邀请函不存在")
  69. items, err := invitation_service.GetOptionItem(activity.Id)
  70. t.CheckErr(err)
  71. values, err := invitation_service.GetOptionValue(items, letter.ExtraData)
  72. t.CheckErr(err)
  73. var name, phone, address = "", "", ""
  74. for _, value := range values {
  75. if v, ok := value["姓名"]; ok {
  76. name = fmt.Sprint(v)
  77. } else if v, ok := value["手机"]; ok {
  78. phone = fmt.Sprint(v)
  79. } else if v, ok := value["地址"]; ok {
  80. address = fmt.Sprint(v)
  81. }
  82. }
  83. s := core.GetXormAuto().NewSession()
  84. defer s.Close()
  85. err = s.Begin()
  86. if err != nil {
  87. s.Rollback()
  88. t.CheckErr(err)
  89. }
  90. good := models.CustomerGoods{}
  91. exist, err = s.Where("is_delete=0 and id=?", goodId).Get(&good)
  92. if err != nil || !exist {
  93. s.Rollback()
  94. t.ERROR("商品信息异常", code.MSG_ERR_Param)
  95. return
  96. }
  97. // 找出商品库存
  98. ms := make([]map[string]int, 0)
  99. err = s.Table(&models.CustomerOrderSub{}).Select("goods_id, sum(goods_num) as goods_num").
  100. Where("goods_id=?", good.Id).GroupBy("goods_id").Find(&ms)
  101. if err != nil {
  102. s.Rollback()
  103. t.CheckErr(err)
  104. }
  105. for _, m := range ms {
  106. if m["goods_id"] == int(good.Id) && m["goods_num"] >= good.Stock {
  107. s.Rollback()
  108. t.ERROR(good.Name+"商品库存不足", code.MSG_CUSTOMER_GOOD_NOT_ENOUGH)
  109. return
  110. }
  111. }
  112. order := models.CustomerOrder{}
  113. // 查询库存
  114. total, err := s.Where("is_delete=0").Count(&order) // 订单总数
  115. if err != nil {
  116. s.Rollback()
  117. t.CheckErr(err)
  118. }
  119. order.AreaId = entryPerson.AreaId
  120. order.ArchId = activity.ArchId
  121. order.AreaName = area.Name
  122. order.BuyerId = userId
  123. order.Type = 0
  124. order.ActivityId = activity.Id
  125. order.RehearsalId = activity.RehearsalId
  126. order.OrderEntryPersonId = entryPerson.Id
  127. order.OrderEntryPersonName = entryPerson.Name
  128. order.TotalAmount = good.Price
  129. order.Receiver = name
  130. order.Phone = phone
  131. order.Address = address
  132. order.OutTradeNo = utils.RandomStr(32)
  133. order.OrderNo = fmt.Sprint(define.DefaultOrderNo + int(total))
  134. order.Status = 1
  135. _, err = s.InsertOne(&order)
  136. if err != nil {
  137. s.Rollback()
  138. t.CheckErr(err)
  139. }
  140. sub := models.CustomerOrderSub{}
  141. sub.GoodsId = good.Id
  142. sub.GoodName = good.Name
  143. sub.GoodPrice = good.Price
  144. sub.GoodsNum = 1
  145. sub.OrderId = order.Id
  146. _, err = models.Add(&sub) // 存入子订单
  147. if err != nil {
  148. s.Rollback()
  149. t.CheckErr(err)
  150. }
  151. gift := models.OrderGift{}
  152. exist, err = s.Where("is_delete=0 and activity_id=?", activity.Id).Get(&gift)
  153. if err != nil {
  154. s.Rollback()
  155. t.CheckErr(err)
  156. }
  157. if exist {
  158. prize := models.UserPrize{}
  159. prize.UserId = userId
  160. prize.ActivityId = activity.Id
  161. prize.RehearsalId = activity.RehearsalId
  162. prize.ActivityName = activity.Name
  163. prize.PrizeName = gift.GiftName
  164. prize.PrizeImg = gift.GiftPicUrl
  165. prize.ArchId = activity.ArchId
  166. prize.CustomerOrderId = order.Id
  167. prize.PrizeType = 3
  168. if gift.Num == 0 {
  169. _, err = s.InsertOne(&prize)
  170. if err != nil {
  171. s.Rollback()
  172. t.CheckErr(err)
  173. }
  174. } else if gift.Num > 0 {
  175. count, err := s.Where("activity_id=? and rehearsal_id=? and arch_id=? and is_delete=0",
  176. activity.Id, activity.RehearsalId, activity.ArchId).NoAutoCondition().Count(&order)
  177. if err != nil {
  178. s.Rollback()
  179. t.CheckErr(err)
  180. }
  181. if gift.Num >= int(count) { // 大于等于
  182. _, err = s.InsertOne(&prize)
  183. if err != nil {
  184. s.Rollback()
  185. t.CheckErr(err)
  186. }
  187. }
  188. }
  189. if prize.Id > 0 {
  190. order.UserPrizeId = prize.Id
  191. _, err = s.ID(order.Id).NoAutoCondition().Cols("user_prize_id").Update(order)
  192. if err != nil {
  193. s.Rollback()
  194. t.CheckErr(err)
  195. }
  196. }
  197. }
  198. err = s.Commit()
  199. if err != nil {
  200. s.Rollback()
  201. t.CheckErr(err)
  202. }
  203. t.SUCCESS("成功录入订单")
  204. }
  205. // 手动下单
  206. type ManualOrderGood struct {
  207. GoodId int `json:"good_id"`
  208. GoodNum int `json:"good_num"`
  209. }
  210. type ManualOrderParam struct {
  211. Name string `json:"name"`
  212. Phone string `json:"phone"`
  213. Goods []*ManualOrderGood `json:"goods"`
  214. }
  215. func (t *OrderEntryCtl) ManualOrder() {
  216. entryId := t.GetAccountId() // 录入人员id
  217. param := &ManualOrderParam{}
  218. t.Bind(param)
  219. param.Goods = make([]*ManualOrderGood, 0)
  220. err := json.Unmarshal([]byte(t.Request.REQUEST["goods"]), &param.Goods)
  221. t.CheckErr(err)
  222. if len(param.Goods) <= 0 {
  223. t.ERROR("商品不能为空", code.MSG_CUSTOMER_GOOD_NOT_EXIST)
  224. return
  225. }
  226. entryPerson := models.OrderEntryPerson{}
  227. exist, err := models.Get(&entryPerson, entryId)
  228. t.CheckErr(err)
  229. t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员不存在")
  230. activity := models.Activity{}
  231. exist, err = models.Get(&activity, entryPerson.ActivityId)
  232. t.CheckErr(err)
  233. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  234. t.CheckRunning(activity.Status)
  235. area := models.AreaStore{}
  236. exist, err = models.Get(&area, entryPerson.AreaId)
  237. t.CheckErr(err)
  238. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  239. goodIds := make([]int, 0)
  240. for _, g := range param.Goods {
  241. goodIds = append(goodIds, g.GoodId)
  242. }
  243. s := core.GetXormAuto().NewSession()
  244. defer s.Close()
  245. err = s.Begin()
  246. if err != nil {
  247. s.Rollback()
  248. t.CheckErr(err)
  249. }
  250. user := models.User{
  251. Phone: param.Phone,
  252. Nickname: param.Name,
  253. Password: utils.RandomStr(6),
  254. }
  255. _, err = s.InsertOne(&user)
  256. if err != nil {
  257. s.Rollback()
  258. t.CheckErr(err)
  259. }
  260. // 校验库存
  261. goods := make([]*models.CustomerGoods, 0)
  262. err = s.Where("is_delete=0").In("id", goodIds).Find(&goods)
  263. if err != nil {
  264. s.Rollback()
  265. t.CheckErr(err)
  266. }
  267. if len(goods) != len(param.Goods) {
  268. s.Rollback()
  269. t.ERROR("商品信息异常", code.MSG_ERR_Param)
  270. return
  271. }
  272. subs := make([]*models.CustomerOrderSub, 0)
  273. totalAmount := 0.00
  274. for _, g := range param.Goods {
  275. for _, good := range goods {
  276. if g.GoodId == int(good.Id) {
  277. subs = append(subs, &models.CustomerOrderSub{
  278. GoodsId: good.Id,
  279. GoodsNum: g.GoodNum,
  280. GoodName: good.Name,
  281. GoodPrice: good.Price,
  282. })
  283. totalAmount += good.Price * float64(g.GoodNum)
  284. }
  285. }
  286. }
  287. ms := make([]map[string]int, 0)
  288. err = s.Table(&models.CustomerOrderSub{}).Select("goods_id, sum(goods_num) as goods_num").
  289. In("goods_id", goodIds).GroupBy("goods_id").Find(&ms)
  290. if err != nil {
  291. s.Rollback()
  292. t.CheckErr(err)
  293. }
  294. for _, m := range ms {
  295. for _, g := range goods {
  296. if g.Stock == -1 { // 无上限
  297. break
  298. }
  299. if m["goods_id"] == int(g.Id) && m["goods_num"] >= g.Stock {
  300. s.Rollback()
  301. t.ERROR(g.Name+"商品库存不足", code.MSG_CUSTOMER_GOOD_NOT_ENOUGH)
  302. return
  303. }
  304. }
  305. }
  306. order := models.CustomerOrder{}
  307. count, err := s.Where("is_delete=0").Count(&order) // 查看订单所在
  308. if err != nil {
  309. s.Rollback()
  310. t.CheckErr(err)
  311. }
  312. order.AreaId = entryPerson.AreaId
  313. order.AreaName = area.Name
  314. order.ArchId = activity.ArchId
  315. order.BuyerId = user.Id
  316. order.Type = 0
  317. order.ActivityId = activity.Id
  318. order.RehearsalId = activity.RehearsalId
  319. order.OrderEntryPersonId = entryPerson.Id
  320. order.OrderEntryPersonName = entryPerson.Name
  321. order.TotalAmount = totalAmount
  322. order.Receiver = param.Name
  323. order.Phone = param.Phone
  324. order.OutTradeNo = utils.RandomStr(32)
  325. order.OrderNo = fmt.Sprint(define.DefaultOrderNo + int(count))
  326. order.Status = 1
  327. _, err = s.InsertOne(&order)
  328. if err != nil {
  329. s.Rollback()
  330. t.CheckErr(err)
  331. }
  332. for _, sub := range subs {
  333. sub.OrderId = order.Id
  334. _, err = models.Add(sub) // 存入子订单
  335. if err != nil {
  336. s.Rollback()
  337. t.CheckErr(err)
  338. }
  339. }
  340. gift := models.OrderGift{}
  341. exist, err = s.Where("is_delete=0 and activity_id=?", activity.Id).Get(&gift)
  342. if err != nil {
  343. s.Rollback()
  344. t.CheckErr(err)
  345. }
  346. if exist {
  347. prize := models.UserPrize{}
  348. prize.UserId = user.Id
  349. prize.ActivityId = activity.Id
  350. prize.RehearsalId = activity.RehearsalId
  351. prize.ActivityName = activity.Name
  352. prize.PrizeName = gift.GiftName
  353. prize.PrizeImg = gift.GiftPicUrl
  354. prize.ArchId = activity.ArchId
  355. prize.CustomerOrderId = order.Id
  356. prize.PrizeType = 3
  357. if gift.Num == 0 {
  358. _, err = s.InsertOne(&prize)
  359. if err != nil {
  360. s.Rollback()
  361. t.CheckErr(err)
  362. }
  363. } else if gift.Num > 0 {
  364. count, err := s.Where("activity_id=? and rehearsal_id=? and arch_id=? and is_delete=0",
  365. activity.Id, activity.RehearsalId, activity.ArchId).NoAutoCondition().Count(&order)
  366. if err != nil {
  367. s.Rollback()
  368. t.CheckErr(err)
  369. }
  370. if gift.Num >= int(count) { // 大于等于
  371. _, err = s.InsertOne(&prize)
  372. if err != nil {
  373. s.Rollback()
  374. t.CheckErr(err)
  375. }
  376. }
  377. }
  378. if prize.Id > 0 {
  379. order.UserPrizeId = prize.Id
  380. _, err = s.ID(order.Id).NoAutoCondition().Cols("user_prize_id").Update(&order)
  381. if err != nil {
  382. s.Rollback()
  383. t.CheckErr(err)
  384. }
  385. }
  386. }
  387. err = s.Commit()
  388. if err != nil {
  389. s.Rollback()
  390. t.CheckErr(err)
  391. }
  392. t.SUCCESS("成功录入订单")
  393. }
  394. // 订单列表
  395. func (t *OrderEntryCtl) ListOrder() {
  396. }
  397. func (t *OrderEntryCtl) DeleteOrder() {
  398. orderId := t.MustGetInt("order_id")
  399. order := new(models.CustomerOrder)
  400. exist, err := models.Get(order, orderId)
  401. t.CheckErr(err)
  402. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "订单不存在")
  403. _, err = models.Del(order, order.Id)
  404. t.CheckErr(err)
  405. if order.UserPrizeId > 0 {
  406. _, err = models.Del(&models.UserPrize{}, order.UserPrizeId)
  407. t.CheckErr(err)
  408. }
  409. t.SUCCESS("删除成功")
  410. }
  411. type OrderListResult struct {
  412. OrderId int `json:"order_id"`
  413. UserId int `json:"user_id"`
  414. EntryName string `json:"entry_name"`
  415. GoodName string `json:"good_name"`
  416. OrderTime string `json:"order_time"`
  417. OrderMoney float64 `json:"order_money"`
  418. Receiver string `json:"receiver"`
  419. Phone string `json:"phone"`
  420. Address string `json:"address"`
  421. Extra []map[string]interface{} `json:"extra"` // 额外信息
  422. ExtraData string `json:"-"`
  423. }
  424. func (t *OrderEntryCtl) EntryOrders() {
  425. uid := t.GetAccountId()
  426. activityId := t.MustGetInt("activity_id")
  427. entry := new(models.OrderEntryPerson)
  428. exist, err := models.Get(entry, uid)
  429. t.CheckErr(err)
  430. t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员不存在")
  431. activity := new(models.Activity)
  432. exist, err = models.Get(activity, activityId)
  433. t.CheckErr(err)
  434. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  435. list := make([]*OrderListResult, 0)
  436. err = core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o").
  437. Select("o.receiver as receiver, o.phone as phone, o.address as address,l.extra_data as extra_data, "+
  438. " g.name as good_name, o.buyer_id as user_id, o.total_amount as order_money, o.id as order_id, "+
  439. " DATE_FORMAT(o.created_at, '%Y-%m-%d %H:%i:%S') AS order_time").
  440. Join("LEFT", new(models.CustomerGoods).Alias("g"),
  441. "o.goods_id=g.id and o.activity_id=g.activity_id and g.is_delete=0").
  442. Join("LEFT", new(models.InvitationLetter).Alias("l"),
  443. "l.user_id = o.buyer_id and o.activity_id=l.activity_id and o.arch_id=l.arch_id and l.is_delete=0").
  444. Where("o.activity_id=? and o.order_entry_person_id=? and o.rehearsal_id=? and o.arch_id=? and o.is_delete=0",
  445. activityId, uid, activity.RehearsalId, activity.ArchId).Desc("o.created_at").Find(&list)
  446. t.CheckErr(err)
  447. // 添加邀请函的内容
  448. optionItems, err := invitation_service.GetOptionItem(activityId)
  449. t.CheckErr(err)
  450. for i := range list {
  451. data, err := invitation_service.GetOptionValue(optionItems, list[i].ExtraData)
  452. t.CheckErr(err)
  453. list[i].Extra = data
  454. list[i].EntryName = entry.Name
  455. }
  456. t.JSON(map[string]interface{}{
  457. "list": list,
  458. "total": len(list),
  459. })
  460. }
  461. // 用户订单列表
  462. type UserOrdersResult struct {
  463. models.CustomerOrder `xorm:"extends"`
  464. Good *models.CustomerGoods `json:"good" xorm:"extends"`
  465. }
  466. // 用户查看订单列表
  467. func (t *OrderEntryCtl) UserOrders() {
  468. uid := t.GetAccountId()
  469. activityId := t.MustGetInt("activity_id")
  470. activity := new(models.Activity)
  471. exist, err := models.Get(activity, activityId)
  472. t.CheckErr(err)
  473. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  474. orders := make([]UserOrdersResult, 0)
  475. s := core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o").
  476. Join("LEFT", new(models.CustomerGoods).Alias("g"),
  477. "o.goods_id=g.id and g.is_delete=0").
  478. Where("o.buyer_id=? and o.is_delete=0 and o.activity_id=? and o.rehearsal_id=? and o.arch_id=?",
  479. uid, activity.Id, activity.RehearsalId, activity.ArchId)
  480. if t.PageSize > 0 {
  481. s = s.Limit(t.PageSize, t.Page*t.PageSize)
  482. }
  483. total, err := s.Desc("o.created_at").FindAndCount(&orders)
  484. t.CheckErr(err)
  485. t.JSON(map[string]interface{}{
  486. "list": orders,
  487. "total": total,
  488. })
  489. }
  490. // 二维码
  491. func (t *OrderEntryCtl) Qrcode() {
  492. userId := t.MustGetInt("user_id")
  493. activityId := t.MustGetInt("activity_id")
  494. goodId := t.MustGetInt("good_id")
  495. Url := fmt.Sprintf("%s/PcClient/Client/OrderEntryCtl/order?user_id=%d&activity_id=%d&good_id=%d",
  496. define.HOST, userId, activityId, goodId)
  497. qr, err := utils.Qrcode2Base64(Url)
  498. t.CheckErr(err)
  499. t.JSON(map[string]interface{}{
  500. "qrcode": qr,
  501. })
  502. }