附近小店
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

svc_store_order.go 9.4 KiB

1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
1 miesiąc temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. "amount": v.Amount,
  105. "num": utils.IntToStr(v.Num),
  106. "down_time": downTime,
  107. "create_at": v.CreateAt.Format("2006-01-02 15:04:05"),
  108. "pay_at": "",
  109. "confirm_at": "",
  110. }
  111. if v.PayAt.IsZero() == false {
  112. tmp["pay_at"] = v.PayAt.Format("2006-01-02 15:04:05")
  113. }
  114. if v.ConfirmAt.IsZero() == false {
  115. tmp["confirm_at"] = v.ConfirmAt.Format("2006-01-02 15:04:05")
  116. } else {
  117. if v.IsNow == 1 {
  118. tmp["confirm_at"] = "立即提货"
  119. } else if v.Timer != "" {
  120. tmp["confirm_at"] = v.Timer
  121. }
  122. }
  123. if v.Type == 1 {
  124. tmp["label"] = "外卖"
  125. }
  126. list = append(list, tmp)
  127. }
  128. }
  129. e.OutSuc(c, list, nil)
  130. return
  131. }
  132. func StoreOrderDetail(c *gin.Context) {
  133. var arg map[string]string
  134. if err := c.ShouldBindJSON(&arg); err != nil {
  135. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  136. return
  137. }
  138. data := db.GetOrderEg(MasterDb(c), arg["oid"])
  139. var state = []string{"待付款", "待提货", "已完成", "已取消"}
  140. now := time.Now().Unix()
  141. downTime := "0"
  142. if data.State == 0 {
  143. downTime = utils.IntToStr(int(data.CreateAt.Unix() + 15*60 - now))
  144. if now > data.CreateAt.Unix()+15*60 {
  145. data.State = 3
  146. }
  147. if utils.StrToInt(downTime) < 0 {
  148. downTime = "0"
  149. }
  150. }
  151. confirmAt := ""
  152. if data.ConfirmAt.IsZero() == false {
  153. confirmAt = data.ConfirmAt.Format("2006-01-02 15:04:05")
  154. }
  155. payAt := ""
  156. if data.PayAt.IsZero() == false {
  157. payAt = data.PayAt.Format("2006-01-02 15:04:05")
  158. }
  159. timer := ""
  160. if data.IsNow == 1 {
  161. timer = "立即提货"
  162. } else if data.Timer != "" {
  163. timer = data.Timer
  164. }
  165. orderInfo := []map[string]string{
  166. {"title": "订单编号", "content": utils.Int64ToStr(data.Oid)},
  167. {"title": "提货码", "content": data.Code},
  168. {"title": "下单时间", "content": data.CreateAt.Format("2006-01-02 15:04:05")},
  169. {"title": "付款时间", "content": payAt},
  170. {"title": "预计提货时间", "content": timer},
  171. {"title": "提货时间", "content": confirmAt},
  172. {"title": "预留电话", "content": data.Phone},
  173. {"title": "备注信息", "content": data.Memo},
  174. }
  175. goodsInfo := make([]map[string]string, 0)
  176. info := db.GetOrderInfoAllEg(MasterDb(c), utils.Int64ToStr(data.Oid))
  177. if info != nil {
  178. scheme, host := ImageBucket(c)
  179. for _, v := range *info {
  180. tmp := map[string]string{
  181. "img": ImageFormatWithBucket(scheme, host, v.Img),
  182. "title": v.Title,
  183. "price": v.Price,
  184. "num": utils.IntToStr(v.Num),
  185. "sku_str": "",
  186. }
  187. skuData := make([]md.Sku, 0)
  188. json.Unmarshal([]byte(v.SkuInfo), &skuData)
  189. skuStr := ""
  190. for _, v1 := range skuData {
  191. if skuStr != "" {
  192. skuStr += ";"
  193. }
  194. skuStr += v1.Value
  195. }
  196. tmp["sku_str"] = skuStr
  197. goodsInfo = append(goodsInfo, tmp)
  198. }
  199. }
  200. user1, _ := db.UserFindByID(MasterDb(c), data.Uid)
  201. userProfile, _ := db.UserProfileFindByID(MasterDb(c), data.Uid)
  202. nickname := ""
  203. headImg := ""
  204. if userProfile != nil {
  205. headImg = userProfile.AvatarUrl
  206. }
  207. if user1 != nil {
  208. if user1.Nickname != user1.Phone {
  209. user1.Nickname += " " + user1.Phone
  210. }
  211. nickname = user1.Nickname
  212. }
  213. tmp := map[string]interface{}{
  214. "oid": utils.Int64ToStr(data.Oid),
  215. "label": "自提",
  216. "username": nickname,
  217. "head_img": headImg,
  218. "commission": data.Commission,
  219. "state": utils.IntToStr(data.State),
  220. "state_str": state[data.State],
  221. "amount": data.Amount,
  222. "all_amount": utils.Float64ToStr(utils.StrToFloat64(data.Amount) + utils.StrToFloat64(data.Coupon)),
  223. "coupon": data.Coupon,
  224. "num": utils.IntToStr(data.Num),
  225. "code": data.Code,
  226. "down_time": downTime,
  227. "order_info": orderInfo,
  228. "goods_info": goodsInfo,
  229. "goods_count": utils.IntToStr(len(goodsInfo)),
  230. }
  231. if data.Type == 1 {
  232. tmp["label"] = "外卖"
  233. }
  234. e.OutSuc(c, tmp, nil)
  235. return
  236. }
  237. func StoreOrderConfirm(c *gin.Context) {
  238. var arg map[string]string
  239. if err := c.ShouldBindJSON(&arg); err != nil {
  240. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  241. return
  242. }
  243. // 加锁 防止并发提取
  244. mutexKey := fmt.Sprintf("%s:team.StoreOrderConfirm:%s", c.GetString("mid"), arg["oid"])
  245. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  246. if err != nil {
  247. e.OutErr(c, e.ERR, err)
  248. return
  249. }
  250. if withdrawAvailable != "OK" {
  251. e.OutErr(c, e.ERR, e.NewErr(400000, "请求过于频繁,请稍后再试"))
  252. return
  253. }
  254. sess := MasterDb(c).NewSession()
  255. defer sess.Close()
  256. sess.Begin()
  257. order := db.GetOrder(sess, arg["oid"])
  258. if order == nil {
  259. sess.Rollback()
  260. e.OutErr(c, 400, e.NewErr(400, "订单不存在"))
  261. return
  262. }
  263. if order.State != 1 {
  264. sess.Rollback()
  265. e.OutErr(c, 400, e.NewErr(400, "订单不能确认"))
  266. return
  267. }
  268. order.State = 2
  269. order.UpdateAt = time.Now()
  270. order.ConfirmAt = time.Now()
  271. update, err := sess.Where("id=?", order.Id).Cols("state,confirm_at,update_at").Update(order)
  272. if update == 0 || err != nil {
  273. sess.Rollback()
  274. e.OutErr(c, 400, e.NewErr(400, "订单确认失败"))
  275. return
  276. }
  277. money := utils.StrToFloat64(order.Commission)
  278. if order.ParentUid > 0 {
  279. money = utils.StrToFloat64(order.Amount) - utils.StrToFloat64(order.AgentCommission)
  280. }
  281. if order.StoreType == 1 {
  282. money = utils.StrToFloat64(order.Amount) - utils.StrToFloat64(order.PlatformCommission)
  283. }
  284. bools := MoneyCheck(c, sess, order.StoreUid, order.ParentUid, order.StoreType, 0, 1, money, "订单核销", order.Oid)
  285. if bools == false {
  286. sess.Rollback()
  287. e.OutErr(c, 400, e.NewErr(400, "订单确认失败"))
  288. return
  289. }
  290. sess.Commit()
  291. e.OutSuc(c, "success", nil)
  292. return
  293. }
  294. func MoneyCheck(c *gin.Context, sess *xorm.Session, storeId, ParentUid, StoreType, types, ordType int, money float64, title string, oid int64) bool {
  295. amountData := db.GetStoreAmount(sess, storeId, ParentUid, StoreType)
  296. if amountData == nil {
  297. amountData = &model.CommunityTeamStoreAmount{
  298. Uid: storeId,
  299. ParentUid: ParentUid,
  300. StoreType: StoreType,
  301. }
  302. insert, err := sess.Insert(amountData)
  303. if insert == 0 || err != nil {
  304. return false
  305. }
  306. }
  307. before := amountData.Amount
  308. if types == 1 {
  309. amountData.Amount = utils.Float64ToStr(utils.StrToFloat64(amountData.Amount) - money)
  310. if utils.StrToFloat64(amountData.Amount) < 0 {
  311. return false
  312. }
  313. } else {
  314. amountData.Amount = utils.Float64ToStr(utils.StrToFloat64(amountData.Amount) + money)
  315. }
  316. update, err := sess.Where("id=?", amountData.Id).Cols("amount").Update(amountData)
  317. if update == 0 || err != nil {
  318. return false
  319. }
  320. var flow = &model.CommunityTeamStoreAmountFlow{
  321. Uid: storeId,
  322. StoreType: StoreType,
  323. ParentUid: ParentUid,
  324. Amount: utils.Float64ToStr(money),
  325. BeforeAmount: before,
  326. AfterAmount: amountData.Amount,
  327. Oid: oid,
  328. Type: types,
  329. Title: title,
  330. OrdType: ordType,
  331. CreateAt: time.Now(),
  332. }
  333. insert, err := sess.Insert(flow)
  334. if insert == 0 || err != nil {
  335. return false
  336. }
  337. return true
  338. }