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

276 lines
7.3 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. 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. "commission": v.Commission,
  99. "username": nickname,
  100. "head_img": headImg,
  101. "amount": v.Amount,
  102. "num": utils.IntToStr(v.Num),
  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. } else {
  114. if v.IsNow == 1 {
  115. tmp["confirm_at"] = "立即提货"
  116. } else if v.Timer != "" {
  117. tmp["confirm_at"] = v.Timer
  118. }
  119. }
  120. if v.Type == 1 {
  121. tmp["label"] = "外卖"
  122. }
  123. list = append(list, tmp)
  124. }
  125. }
  126. e.OutSuc(c, list, nil)
  127. return
  128. }
  129. func StoreOrderDetail(c *gin.Context) {
  130. var arg map[string]string
  131. if err := c.ShouldBindJSON(&arg); err != nil {
  132. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  133. return
  134. }
  135. data := db.GetOrderEg(MasterDb(c), arg["oid"])
  136. var state = []string{"待付款", "待提货", "已完成", "已取消"}
  137. now := time.Now().Unix()
  138. downTime := "0"
  139. if data.State == 0 {
  140. downTime = utils.IntToStr(int(data.CreateAt.Unix() + 15*60 - now))
  141. if now > data.CreateAt.Unix()+15*60 {
  142. data.State = 3
  143. }
  144. if utils.StrToInt(downTime) < 0 {
  145. downTime = "0"
  146. }
  147. }
  148. confirmAt := ""
  149. if data.ConfirmAt.IsZero() == false {
  150. confirmAt = data.ConfirmAt.Format("2006-01-02 15:04:05")
  151. }
  152. payAt := ""
  153. if data.PayAt.IsZero() == false {
  154. payAt = data.PayAt.Format("2006-01-02 15:04:05")
  155. }
  156. timer := ""
  157. if data.IsNow == 1 {
  158. timer = "立即提货"
  159. } else if data.Timer != "" {
  160. timer = data.Timer
  161. }
  162. orderInfo := []map[string]string{
  163. {"title": "订单编号", "content": utils.Int64ToStr(data.Oid)},
  164. {"title": "提货码", "content": data.Code},
  165. {"title": "下单时间", "content": data.CreateAt.Format("2006-01-02 15:04:05")},
  166. {"title": "付款时间", "content": payAt},
  167. {"title": "预计提货时间", "content": timer},
  168. {"title": "提货时间", "content": confirmAt},
  169. {"title": "预留电话", "content": data.Phone},
  170. {"title": "备注信息", "content": data.Memo},
  171. }
  172. goodsInfo := make([]map[string]string, 0)
  173. info := db.GetOrderInfoAllEg(MasterDb(c), utils.Int64ToStr(data.Oid))
  174. if info != nil {
  175. for _, v := range *info {
  176. tmp := map[string]string{
  177. "img": v.Img,
  178. "title": v.Title,
  179. "price": v.Price,
  180. "num": utils.IntToStr(v.Num),
  181. "sku_str": "",
  182. }
  183. skuData := make([]md.Sku, 0)
  184. json.Unmarshal([]byte(v.SkuInfo), &skuData)
  185. skuStr := ""
  186. for _, v1 := range skuData {
  187. if skuStr != "" {
  188. skuStr += ";"
  189. }
  190. skuStr += v1.Value
  191. }
  192. tmp["sku_str"] = skuStr
  193. goodsInfo = append(goodsInfo, tmp)
  194. }
  195. }
  196. user1, _ := db.UserFindByID(MasterDb(c), data.Uid)
  197. userProfile, _ := db.UserProfileFindByID(MasterDb(c), data.Uid)
  198. nickname := ""
  199. headImg := ""
  200. if userProfile != nil {
  201. headImg = userProfile.AvatarUrl
  202. }
  203. if user1 != nil {
  204. if user1.Nickname != user1.Phone {
  205. user1.Nickname += " " + user1.Phone
  206. }
  207. nickname = user1.Nickname
  208. }
  209. tmp := map[string]interface{}{
  210. "oid": utils.Int64ToStr(data.Oid),
  211. "label": "自提",
  212. "username": nickname,
  213. "head_img": headImg,
  214. "state": utils.IntToStr(data.State),
  215. "state_str": state[data.State],
  216. "amount": data.Amount,
  217. "all_amount": utils.Float64ToStr(utils.StrToFloat64(data.Amount) + utils.StrToFloat64(data.Coupon)),
  218. "coupon": data.Coupon,
  219. "num": utils.IntToStr(data.Num),
  220. "code": data.Code,
  221. "down_time": downTime,
  222. "order_info": orderInfo,
  223. "goods_info": goodsInfo,
  224. "goods_count": utils.IntToStr(len(goodsInfo)),
  225. }
  226. if data.Type == 1 {
  227. tmp["label"] = "外卖"
  228. }
  229. e.OutSuc(c, tmp, nil)
  230. return
  231. }
  232. func StoreOrderConfirm(c *gin.Context) {
  233. var arg map[string]string
  234. if err := c.ShouldBindJSON(&arg); err != nil {
  235. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  236. return
  237. }
  238. // 加锁 防止并发提取
  239. mutexKey := fmt.Sprintf("%s:team.StoreOrderConfirm:%s", c.GetString("mid"), arg["oid"])
  240. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  241. if err != nil {
  242. e.OutErr(c, e.ERR, err)
  243. return
  244. }
  245. if withdrawAvailable != "OK" {
  246. e.OutErr(c, e.ERR, e.NewErr(400000, "请求过于频繁,请稍后再试"))
  247. return
  248. }
  249. sess := MasterDb(c).NewSession()
  250. defer sess.Close()
  251. sess.Begin()
  252. order := db.GetOrder(sess, arg["oid"])
  253. if order == nil {
  254. sess.Rollback()
  255. e.OutErr(c, 400, e.NewErr(400, "订单不存在"))
  256. return
  257. }
  258. if order.State != 1 {
  259. sess.Rollback()
  260. e.OutErr(c, 400, e.NewErr(400, "订单不能确认"))
  261. return
  262. }
  263. order.State = 2
  264. order.UpdateAt = time.Now()
  265. order.ConfirmAt = time.Now()
  266. update, err := sess.Where("id=?", order.Id).Cols("state,confirm_at,update_at").Update(order)
  267. if update == 0 || err != nil {
  268. e.OutErr(c, 400, e.NewErr(400, "订单确认失败"))
  269. return
  270. }
  271. e.OutSuc(c, "success", nil)
  272. return
  273. }