附近小店
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

svc_store_order.go 9.4 KiB

2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
1 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
2 ay önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/e"
  6. "applet/app/md"
  7. "applet/app/utils"
  8. "applet/app/utils/cache"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/gin-gonic/gin"
  12. "time"
  13. "xorm.io/xorm"
  14. )
  15. func StoreOrderCate(c *gin.Context) {
  16. var cate = []map[string]string{
  17. {"name": "全部", "value": ""},
  18. {"name": "待付款", "value": "0"},
  19. {"name": "待提货", "value": "1"},
  20. {"name": "已完成", "value": "2"},
  21. {"name": "已取消", "value": "3"},
  22. }
  23. e.OutSuc(c, cate, nil)
  24. return
  25. }
  26. func StoreOrderList(c *gin.Context) {
  27. var arg map[string]string
  28. if err := c.ShouldBindJSON(&arg); err != nil {
  29. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  30. return
  31. }
  32. user := GetUser(c)
  33. arg["store_uid"] = utils.IntToStr(user.Info.Uid)
  34. data := db.GetOrderList(MasterDb(c), arg)
  35. var state = []string{"待付款", "待提货", "已完成", "已取消"}
  36. list := make([]map[string]interface{}, 0)
  37. if data != nil {
  38. scheme, host := ImageBucket(c)
  39. now := time.Now().Unix()
  40. for _, v := range *data {
  41. store := db.GetStoreIdEg(MasterDb(c), utils.IntToStr(v.StoreUid))
  42. info := db.GetOrderInfoAllEg(MasterDb(c), utils.Int64ToStr(v.Oid))
  43. downTime := "0"
  44. if v.State == 0 {
  45. downTime = utils.IntToStr(int(v.CreateAt.Unix() + 15*60 - now))
  46. if now > v.CreateAt.Unix()+15*60 {
  47. v.State = 3
  48. }
  49. if utils.StrToInt(downTime) < 0 {
  50. downTime = "0"
  51. }
  52. }
  53. storeName := ""
  54. if store != nil {
  55. storeName = store.Name
  56. }
  57. goodsInfo := make([]map[string]string, 0)
  58. if info != nil {
  59. for _, v1 := range *info {
  60. skuData := make([]md.Sku, 0)
  61. json.Unmarshal([]byte(v1.SkuInfo), &skuData)
  62. skuStr := ""
  63. for _, v2 := range skuData {
  64. if skuStr != "" {
  65. skuStr += ";"
  66. }
  67. skuStr += v2.Value
  68. }
  69. if skuStr != "" {
  70. skuStr = "(" + skuStr + ")"
  71. }
  72. tmp := map[string]string{
  73. "title": v1.Title + skuStr,
  74. "num": utils.IntToStr(v1.Num),
  75. "img": ImageFormatWithBucket(scheme, host, v1.Img),
  76. }
  77. goodsInfo = append(goodsInfo, tmp)
  78. }
  79. }
  80. user1, _ := db.UserFindByID(MasterDb(c), v.Uid)
  81. userProfile, _ := db.UserProfileFindByID(MasterDb(c), v.Uid)
  82. nickname := ""
  83. headImg := ""
  84. if userProfile != nil {
  85. headImg = userProfile.AvatarUrl
  86. }
  87. if user1 != nil {
  88. if user1.Nickname != user1.Phone {
  89. user1.Nickname += " " + user1.Phone
  90. }
  91. nickname = user1.Nickname
  92. }
  93. tmp := map[string]interface{}{
  94. "goods_info": goodsInfo,
  95. "oid": utils.Int64ToStr(v.Oid),
  96. "label": "自提",
  97. "state": utils.IntToStr(v.State),
  98. "state_str": state[v.State],
  99. "store_name": storeName,
  100. "coupon": v.Coupon,
  101. "commission": v.Commission,
  102. "username": nickname,
  103. "head_img": headImg,
  104. "table_num": v.TableNum,
  105. "amount": v.Amount,
  106. "num": utils.IntToStr(v.Num),
  107. "down_time": downTime,
  108. "create_at": v.CreateAt.Format("2006-01-02 15:04:05"),
  109. "pay_at": "",
  110. "confirm_at": "",
  111. }
  112. if v.PayAt.IsZero() == false {
  113. tmp["pay_at"] = v.PayAt.Format("2006-01-02 15:04:05")
  114. }
  115. if v.ConfirmAt.IsZero() == false {
  116. tmp["confirm_at"] = v.ConfirmAt.Format("2006-01-02 15:04:05")
  117. } else {
  118. if v.IsNow == 1 {
  119. tmp["confirm_at"] = "立即提货"
  120. } else if v.Timer != "" {
  121. tmp["confirm_at"] = v.Timer
  122. }
  123. }
  124. if v.Type == 1 {
  125. tmp["label"] = "外卖"
  126. }
  127. list = append(list, tmp)
  128. }
  129. }
  130. e.OutSuc(c, list, nil)
  131. return
  132. }
  133. func StoreOrderDetail(c *gin.Context) {
  134. var arg map[string]string
  135. if err := c.ShouldBindJSON(&arg); err != nil {
  136. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  137. return
  138. }
  139. data := db.GetOrderEg(MasterDb(c), arg["oid"])
  140. var state = []string{"待付款", "待提货", "已完成", "已取消"}
  141. now := time.Now().Unix()
  142. downTime := "0"
  143. if data.State == 0 {
  144. downTime = utils.IntToStr(int(data.CreateAt.Unix() + 15*60 - now))
  145. if now > data.CreateAt.Unix()+15*60 {
  146. data.State = 3
  147. }
  148. if utils.StrToInt(downTime) < 0 {
  149. downTime = "0"
  150. }
  151. }
  152. confirmAt := ""
  153. if data.ConfirmAt.IsZero() == false {
  154. confirmAt = data.ConfirmAt.Format("2006-01-02 15:04:05")
  155. }
  156. payAt := ""
  157. if data.PayAt.IsZero() == false {
  158. payAt = data.PayAt.Format("2006-01-02 15:04:05")
  159. }
  160. timer := ""
  161. if data.IsNow == 1 {
  162. timer = "立即提货"
  163. } else if data.Timer != "" {
  164. timer = data.Timer
  165. }
  166. orderInfo := []map[string]string{
  167. {"title": "订单编号", "content": utils.Int64ToStr(data.Oid)},
  168. {"title": "提货码", "content": data.Code},
  169. {"title": "下单时间", "content": data.CreateAt.Format("2006-01-02 15:04:05")},
  170. {"title": "付款时间", "content": payAt},
  171. {"title": "预计提货时间", "content": timer},
  172. {"title": "提货时间", "content": confirmAt},
  173. {"title": "预留电话", "content": data.Phone},
  174. {"title": "备注信息", "content": data.Memo},
  175. }
  176. goodsInfo := make([]map[string]string, 0)
  177. info := db.GetOrderInfoAllEg(MasterDb(c), utils.Int64ToStr(data.Oid))
  178. if info != nil {
  179. scheme, host := ImageBucket(c)
  180. for _, v := range *info {
  181. tmp := map[string]string{
  182. "img": ImageFormatWithBucket(scheme, host, v.Img),
  183. "title": v.Title,
  184. "price": v.Price,
  185. "num": utils.IntToStr(v.Num),
  186. "sku_str": "",
  187. }
  188. skuData := make([]md.Sku, 0)
  189. json.Unmarshal([]byte(v.SkuInfo), &skuData)
  190. skuStr := ""
  191. for _, v1 := range skuData {
  192. if skuStr != "" {
  193. skuStr += ";"
  194. }
  195. skuStr += v1.Value
  196. }
  197. tmp["sku_str"] = skuStr
  198. goodsInfo = append(goodsInfo, tmp)
  199. }
  200. }
  201. user1, _ := db.UserFindByID(MasterDb(c), data.Uid)
  202. userProfile, _ := db.UserProfileFindByID(MasterDb(c), data.Uid)
  203. nickname := ""
  204. headImg := ""
  205. if userProfile != nil {
  206. headImg = userProfile.AvatarUrl
  207. }
  208. if user1 != nil {
  209. if user1.Nickname != user1.Phone {
  210. user1.Nickname += " " + user1.Phone
  211. }
  212. nickname = user1.Nickname
  213. }
  214. tmp := map[string]interface{}{
  215. "oid": utils.Int64ToStr(data.Oid),
  216. "label": "自提",
  217. "username": nickname,
  218. "head_img": headImg,
  219. "commission": data.Commission,
  220. "state": utils.IntToStr(data.State),
  221. "state_str": state[data.State],
  222. "amount": data.Amount,
  223. "all_amount": utils.Float64ToStr(utils.StrToFloat64(data.Amount) + utils.StrToFloat64(data.Coupon)),
  224. "coupon": data.Coupon,
  225. "num": utils.IntToStr(data.Num),
  226. "code": data.Code,
  227. "down_time": downTime,
  228. "order_info": orderInfo,
  229. "goods_info": goodsInfo,
  230. "goods_count": utils.IntToStr(len(goodsInfo)),
  231. }
  232. if data.Type == 1 {
  233. tmp["label"] = "外卖"
  234. }
  235. e.OutSuc(c, tmp, nil)
  236. return
  237. }
  238. func StoreOrderConfirm(c *gin.Context) {
  239. var arg map[string]string
  240. if err := c.ShouldBindJSON(&arg); err != nil {
  241. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  242. return
  243. }
  244. // 加锁 防止并发提取
  245. mutexKey := fmt.Sprintf("%s:team.StoreOrderConfirm:%s", c.GetString("mid"), arg["oid"])
  246. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  247. if err != nil {
  248. e.OutErr(c, e.ERR, err)
  249. return
  250. }
  251. if withdrawAvailable != "OK" {
  252. e.OutErr(c, e.ERR, e.NewErr(400000, "请求过于频繁,请稍后再试"))
  253. return
  254. }
  255. sess := MasterDb(c).NewSession()
  256. defer sess.Close()
  257. sess.Begin()
  258. order := db.GetOrder(sess, arg["oid"])
  259. if order == nil {
  260. sess.Rollback()
  261. e.OutErr(c, 400, e.NewErr(400, "订单不存在"))
  262. return
  263. }
  264. if order.State != 1 {
  265. sess.Rollback()
  266. e.OutErr(c, 400, e.NewErr(400, "订单不能确认"))
  267. return
  268. }
  269. order.State = 2
  270. order.UpdateAt = time.Now()
  271. order.ConfirmAt = time.Now()
  272. update, err := sess.Where("id=?", order.Id).Cols("state,confirm_at,update_at").Update(order)
  273. if update == 0 || err != nil {
  274. sess.Rollback()
  275. e.OutErr(c, 400, e.NewErr(400, "订单确认失败"))
  276. return
  277. }
  278. money := utils.StrToFloat64(order.Commission)
  279. if order.ParentUid > 0 {
  280. money = utils.StrToFloat64(order.Amount) - utils.StrToFloat64(order.AgentCommission)
  281. }
  282. if order.StoreType == 1 {
  283. money = utils.StrToFloat64(order.Amount) - utils.StrToFloat64(order.PlatformCommission)
  284. }
  285. bools := MoneyCheck(c, sess, order.StoreUid, order.ParentUid, order.StoreType, 0, 1, money, "订单核销", order.Oid)
  286. if bools == false {
  287. sess.Rollback()
  288. e.OutErr(c, 400, e.NewErr(400, "订单确认失败"))
  289. return
  290. }
  291. sess.Commit()
  292. e.OutSuc(c, "success", nil)
  293. return
  294. }
  295. func MoneyCheck(c *gin.Context, sess *xorm.Session, storeId, ParentUid, StoreType, types, ordType int, money float64, title string, oid int64) bool {
  296. amountData := db.GetStoreAmount(sess, storeId, ParentUid, StoreType)
  297. if amountData == nil {
  298. amountData = &model.CommunityTeamStoreAmount{
  299. Uid: storeId,
  300. ParentUid: ParentUid,
  301. StoreType: StoreType,
  302. }
  303. insert, err := sess.Insert(amountData)
  304. if insert == 0 || err != nil {
  305. return false
  306. }
  307. }
  308. before := amountData.Amount
  309. if types == 1 {
  310. amountData.Amount = utils.Float64ToStr(utils.StrToFloat64(amountData.Amount) - money)
  311. if utils.StrToFloat64(amountData.Amount) < 0 {
  312. return false
  313. }
  314. } else {
  315. amountData.Amount = utils.Float64ToStr(utils.StrToFloat64(amountData.Amount) + money)
  316. }
  317. update, err := sess.Where("id=?", amountData.Id).Cols("amount").Update(amountData)
  318. if update == 0 || err != nil {
  319. return false
  320. }
  321. var flow = &model.CommunityTeamStoreAmountFlow{
  322. Uid: storeId,
  323. StoreType: StoreType,
  324. ParentUid: ParentUid,
  325. Amount: utils.Float64ToStr(money),
  326. BeforeAmount: before,
  327. AfterAmount: amountData.Amount,
  328. Oid: oid,
  329. Type: types,
  330. Title: title,
  331. OrdType: ordType,
  332. CreateAt: time.Now(),
  333. }
  334. insert, err := sess.Insert(flow)
  335. if insert == 0 || err != nil {
  336. return false
  337. }
  338. return true
  339. }