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

281 lines
7.5 KiB

  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. scheme, host := ImageBucket(c)
  37. now := time.Now().Unix()
  38. for _, v := range *data {
  39. store := db.GetStoreIdEg(MasterDb(c), utils.IntToStr(v.StoreUid))
  40. info := db.GetOrderInfoAllEg(MasterDb(c), utils.Int64ToStr(v.Oid))
  41. downTime := "0"
  42. if v.State == 0 {
  43. downTime = utils.IntToStr(int(v.CreateAt.Unix() + 15*60 - now))
  44. if now > v.CreateAt.Unix()+15*60 {
  45. v.State = 3
  46. }
  47. if utils.StrToInt(downTime) < 0 {
  48. downTime = "0"
  49. }
  50. }
  51. storeName := ""
  52. if store != nil {
  53. storeName = store.Name
  54. }
  55. goodsInfo := make([]map[string]string, 0)
  56. if info != nil {
  57. for _, v1 := range *info {
  58. skuData := make([]md.Sku, 0)
  59. json.Unmarshal([]byte(v1.SkuInfo), &skuData)
  60. skuStr := ""
  61. for _, v2 := range skuData {
  62. if skuStr != "" {
  63. skuStr += ";"
  64. }
  65. skuStr += v2.Value
  66. }
  67. if skuStr != "" {
  68. skuStr = "(" + skuStr + ")"
  69. }
  70. tmp := map[string]string{
  71. "title": v1.Title + skuStr,
  72. "num": utils.IntToStr(v1.Num),
  73. "img": ImageFormatWithBucket(scheme, host, v1.Img),
  74. }
  75. goodsInfo = append(goodsInfo, tmp)
  76. }
  77. }
  78. user1, _ := db.UserFindByID(MasterDb(c), v.Uid)
  79. userProfile, _ := db.UserProfileFindByID(MasterDb(c), v.Uid)
  80. nickname := ""
  81. headImg := ""
  82. if userProfile != nil {
  83. headImg = userProfile.AvatarUrl
  84. }
  85. if user1 != nil {
  86. if user1.Nickname != user1.Phone {
  87. user1.Nickname += " " + user1.Phone
  88. }
  89. nickname = user1.Nickname
  90. }
  91. tmp := map[string]interface{}{
  92. "goods_info": goodsInfo,
  93. "oid": utils.Int64ToStr(v.Oid),
  94. "label": "自提",
  95. "state": utils.IntToStr(v.State),
  96. "state_str": state[v.State],
  97. "store_name": storeName,
  98. "coupon": v.Coupon,
  99. "commission": v.Commission,
  100. "username": nickname,
  101. "head_img": headImg,
  102. "amount": v.Amount,
  103. "num": utils.IntToStr(v.Num),
  104. "down_time": downTime,
  105. "create_at": v.CreateAt.Format("2006-01-02 15:04:05"),
  106. "pay_at": "",
  107. "confirm_at": "",
  108. }
  109. if v.PayAt.IsZero() == false {
  110. tmp["pay_at"] = v.PayAt.Format("2006-01-02 15:04:05")
  111. }
  112. if v.ConfirmAt.IsZero() == false {
  113. tmp["confirm_at"] = v.ConfirmAt.Format("2006-01-02 15:04:05")
  114. } else {
  115. if v.IsNow == 1 {
  116. tmp["confirm_at"] = "立即提货"
  117. } else if v.Timer != "" {
  118. tmp["confirm_at"] = v.Timer
  119. }
  120. }
  121. if v.Type == 1 {
  122. tmp["label"] = "外卖"
  123. }
  124. list = append(list, tmp)
  125. }
  126. }
  127. e.OutSuc(c, list, nil)
  128. return
  129. }
  130. func StoreOrderDetail(c *gin.Context) {
  131. var arg map[string]string
  132. if err := c.ShouldBindJSON(&arg); err != nil {
  133. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  134. return
  135. }
  136. data := db.GetOrderEg(MasterDb(c), arg["oid"])
  137. var state = []string{"待付款", "待提货", "已完成", "已取消"}
  138. now := time.Now().Unix()
  139. downTime := "0"
  140. if data.State == 0 {
  141. downTime = utils.IntToStr(int(data.CreateAt.Unix() + 15*60 - now))
  142. if now > data.CreateAt.Unix()+15*60 {
  143. data.State = 3
  144. }
  145. if utils.StrToInt(downTime) < 0 {
  146. downTime = "0"
  147. }
  148. }
  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. scheme, host := ImageBucket(c)
  177. for _, v := range *info {
  178. tmp := map[string]string{
  179. "img": ImageFormatWithBucket(scheme, host, v.Img),
  180. "title": v.Title,
  181. "price": v.Price,
  182. "num": utils.IntToStr(v.Num),
  183. "sku_str": "",
  184. }
  185. skuData := make([]md.Sku, 0)
  186. json.Unmarshal([]byte(v.SkuInfo), &skuData)
  187. skuStr := ""
  188. for _, v1 := range skuData {
  189. if skuStr != "" {
  190. skuStr += ";"
  191. }
  192. skuStr += v1.Value
  193. }
  194. tmp["sku_str"] = skuStr
  195. goodsInfo = append(goodsInfo, tmp)
  196. }
  197. }
  198. user1, _ := db.UserFindByID(MasterDb(c), data.Uid)
  199. userProfile, _ := db.UserProfileFindByID(MasterDb(c), data.Uid)
  200. nickname := ""
  201. headImg := ""
  202. if userProfile != nil {
  203. headImg = userProfile.AvatarUrl
  204. }
  205. if user1 != nil {
  206. if user1.Nickname != user1.Phone {
  207. user1.Nickname += " " + user1.Phone
  208. }
  209. nickname = user1.Nickname
  210. }
  211. tmp := map[string]interface{}{
  212. "oid": utils.Int64ToStr(data.Oid),
  213. "label": "自提",
  214. "username": nickname,
  215. "head_img": headImg,
  216. "commission": data.Commission,
  217. "state": utils.IntToStr(data.State),
  218. "state_str": state[data.State],
  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. order.ConfirmAt = time.Now()
  269. update, err := sess.Where("id=?", order.Id).Cols("state,confirm_at,update_at").Update(order)
  270. if update == 0 || err != nil {
  271. e.OutErr(c, 400, e.NewErr(400, "订单确认失败"))
  272. return
  273. }
  274. e.OutSuc(c, "success", nil)
  275. return
  276. }