附近小店
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.

svc_store_order.go 7.3 KiB

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