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

470 lines
15 KiB

  1. package hdl
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/e"
  6. "applet/app/md"
  7. storeSvc "applet/app/store/svc"
  8. "applet/app/svc"
  9. "applet/app/utils"
  10. "encoding/json"
  11. "github.com/gin-gonic/gin"
  12. "github.com/tidwall/gjson"
  13. "time"
  14. )
  15. // UserStoreList 门店列表
  16. // @Summary 门店-列表
  17. // @Tags 门店
  18. // @Description 门店-列表
  19. // @Accept json
  20. // @Produce json
  21. // @Param req body md.Store true "请求参数"
  22. // @Success 200 {string} ""
  23. // @Failure 400 {object} md.Response "具体错误"
  24. // @Router /api/v1/communityTeam/ownStore/store/list [POST]
  25. func UserStoreList(c *gin.Context) {
  26. var arg md.Store
  27. if err := c.ShouldBindJSON(&arg); err != nil {
  28. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  29. return
  30. }
  31. user := svc.GetUser(c)
  32. list, count := db.GetUserList(svc.MasterDb(c), arg, user.Info.Uid)
  33. data := make([]map[string]interface{}, 0)
  34. if list != nil {
  35. currentMonth := utils.GetTimeRange("current_month")
  36. for _, v := range *list {
  37. store := v.CommunityTeamStore
  38. tmp := map[string]interface{}{
  39. "uid": utils.IntToStr(v.User.Uid),
  40. "create_at": store.CreateAt.Format("2006-01-02 15:04:05"),
  41. "name": "",
  42. "address": "",
  43. "logo": "",
  44. "order_count": "0",
  45. "order_amount": "0",
  46. "agent_commission": "0",
  47. "platform_commission": "0",
  48. "base_commission": "0",
  49. "work_state": "",
  50. }
  51. count1, _ := svc.MasterDb(c).Where("store_uid=? and parent_uid=?", v.User.Uid, user.Info.Uid).In("state", []string{"1", "2"}).Count(&model.CommunityTeamOrder{})
  52. sum, _ := svc.MasterDb(c).Where("store_uid=? and parent_uid=? and create_at>=?", v.User.Uid, user.Info.Uid, time.Unix(currentMonth["start"], 0).Format("2006-01-02 15:04:05")).In("state", []string{"1", "2"}).Sum(&model.CommunityTeamOrder{}, "amount")
  53. tmp["order_count"] = utils.Int64ToStr(count1)
  54. tmp["order_amount"] = svc.GetCommissionPrec(c, utils.Float64ToStr(sum), "2", "")
  55. tmp["name"] = store.Name
  56. tmp["address"] = store.Address
  57. tmp["logo"] = store.Logo
  58. tmp["work_state"] = "营业中"
  59. if store.WorkState == 1 {
  60. tmp["work_state"] = "休息中"
  61. }
  62. tmp["agent_commission"] = store.AgentCommission
  63. data = append(data, tmp)
  64. }
  65. }
  66. res := map[string]interface{}{
  67. "total": count,
  68. "list": data,
  69. }
  70. e.OutSuc(c, res, nil)
  71. }
  72. // UserStoreSave 门店编辑
  73. // @Summary 门店-门店编辑
  74. // @Tags 门店
  75. // @Description 门店-门店编辑
  76. // @Accept json
  77. // @Produce json
  78. // @Param req body md.StoreSave true "请求参数"
  79. // @Success 200 {string} ""
  80. // @Failure 400 {object} md.Response "具体错误"
  81. // @Router /api/v1/communityTeam/ownStore/store/save [POST]
  82. func UserStoreSave(c *gin.Context) {
  83. var arg md.StoreSave
  84. if err := c.ShouldBindJSON(&arg); err != nil {
  85. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  86. return
  87. }
  88. store := db.GetUserStore(svc.MasterDb(c), utils.StrToInt(arg.Uid))
  89. if store == nil {
  90. e.OutErr(c, 400, e.NewErr(400, "门店不存在"))
  91. return
  92. }
  93. store.AgentCommission = arg.AgentCommission
  94. svc.MasterDb(c).Where("id=?", store.Id).Cols("agent_commission").Update(store)
  95. e.OutSuc(c, "success", nil)
  96. return
  97. }
  98. // UserStoreOrder 门店订单-订单管理共用一个
  99. // @Summary 门店-门店订单
  100. // @Tags 门店
  101. // @Description 门店-门店订单
  102. // @Accept json
  103. // @Produce json
  104. // @Param req body md.StoreOrder true "请求参数"
  105. // @Success 200 {string} ""
  106. // @Failure 400 {object} md.Response "具体错误"
  107. // @Router /api/v1/communityTeam/ownStore/store/order [POST]
  108. func UserStoreOrder(c *gin.Context) {
  109. var arg md.StoreOrder
  110. if err := c.ShouldBindJSON(&arg); err != nil {
  111. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  112. return
  113. }
  114. user := svc.GetUser(c)
  115. arg.StoreUid = utils.IntToStr(user.Info.Uid)
  116. data, total, _ := db.GetStoreOrderList(svc.MasterDb(c), arg, 0)
  117. list := make([]map[string]string, 0)
  118. if data != nil {
  119. stateList := []string{"待付款", "已支付", "已提货", "已取消"}
  120. for _, v := range *data {
  121. tmp := map[string]string{
  122. "oid": utils.Int64ToStr(v.Oid),
  123. "uid": utils.IntToStr(v.Uid),
  124. "phone": "",
  125. "nickname": "",
  126. "store_name": "",
  127. "state_str": stateList[v.State],
  128. "amount": v.Amount,
  129. "agent_commission": v.AgentCommission,
  130. "state": utils.IntToStr(v.State),
  131. "create_at": v.CreateAt.Format("2006-01-02 15:04:05"),
  132. "confirm_at": utils.Int64ToStr(v.Oid),
  133. }
  134. if v.ConfirmAt.IsZero() == false {
  135. tmp["confirm_at"] = v.ConfirmAt.Format("2006-01-02 15:04:05")
  136. }
  137. user1, _ := db.UserFindByID(svc.MasterDb(c), v.Uid)
  138. if user1 != nil {
  139. tmp["phone"] = user1.Phone
  140. tmp["nickname"] = user1.Nickname
  141. }
  142. store := db.GetUserStore(svc.MasterDb(c), v.StoreUid)
  143. if store != nil {
  144. tmp["store_name"] = store.Name
  145. }
  146. list = append(list, tmp)
  147. }
  148. }
  149. res := map[string]interface{}{
  150. "total": total,
  151. "state": []map[string]string{
  152. {"name": "待付款", "value": "0"},
  153. {"name": "已支付", "value": "1"},
  154. {"name": "已提货", "value": "2"},
  155. {"name": "已取消", "value": "3"},
  156. },
  157. "list": list,
  158. }
  159. e.OutSuc(c, res, nil)
  160. }
  161. // UserStoreOrderDetail 门店订单详情-订单管理共用一个
  162. // @Summary 门店-门店订单详情
  163. // @Tags 门店
  164. // @Description 门店-门店订单详情
  165. // @Accept json
  166. // @Produce json
  167. // @Param req body md.StoreOrderDetail true "请求参数"
  168. // @Success 200 {string} ""
  169. // @Failure 400 {object} md.Response "具体错误"
  170. // @Router /api/v1/communityTeam/ownStore/store/order/detail [POST]
  171. func UserStoreOrderDetail(c *gin.Context) {
  172. var arg md.StoreOrderDetail
  173. if err := c.ShouldBindJSON(&arg); err != nil {
  174. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  175. return
  176. }
  177. order := db.GetOrderByOid(svc.MasterDb(c), arg.Oid)
  178. if order == nil {
  179. e.OutErr(c, 400, e.NewErr(400, "订单不存在"))
  180. return
  181. }
  182. stateList := []string{"待付款", "已支付", "已提货", "已取消"}
  183. payMethodStr := []string{"余额支付", "支付宝支付", "微信支付"}
  184. orderInfoMap := make([]map[string]string, 0)
  185. res := map[string]interface{}{
  186. "oid": utils.Int64ToStr(order.Oid),
  187. "uid": utils.IntToStr(order.Uid),
  188. "phone": order.Phone,
  189. "nickname": "",
  190. "store_name": "",
  191. "memo": order.Memo,
  192. "coupon": order.Coupon,
  193. "code": order.Code,
  194. "amount": order.Amount,
  195. "all_amount": utils.Float64ToStr(utils.StrToFloat64(order.Amount) + utils.StrToFloat64(order.Coupon)),
  196. "pay_method_str": payMethodStr[order.PayMethod],
  197. "state_str": stateList[order.State],
  198. "state": utils.IntToStr(order.State),
  199. "create_at": order.CreateAt.Format("2006-01-02 15:04:05"),
  200. "confirm_at": "",
  201. "pay_at": "",
  202. "order_info": orderInfoMap,
  203. }
  204. if order.ConfirmAt.IsZero() == false {
  205. res["confirm_at"] = order.ConfirmAt.Format("2006-01-02 15:04:05")
  206. } else {
  207. res["confirm_at"] = order.Timer
  208. if order.IsNow == 1 {
  209. res["confirm_at"] = "立即提货"
  210. }
  211. }
  212. if order.PayAt.IsZero() == false {
  213. res["pay_at"] = order.PayAt.Format("2006-01-02 15:04:05")
  214. }
  215. user, _ := db.UserFindByID(svc.MasterDb(c), order.Uid)
  216. if user != nil {
  217. res["nickname"] = user.Nickname
  218. if order.Phone == "" {
  219. res["phone"] = user.Phone
  220. }
  221. }
  222. store := db.GetUserStore(svc.MasterDb(c), order.StoreUid)
  223. if store != nil {
  224. res["store_name"] = store.Name
  225. }
  226. orderInfo := db.GetOrderInfoAllEg(svc.MasterDb(c), arg.Oid)
  227. if orderInfo != nil {
  228. for _, v := range *orderInfo {
  229. skuData := make([]md.Sku, 0)
  230. json.Unmarshal([]byte(v.SkuInfo), &skuData)
  231. skuStr := ""
  232. for _, v1 := range skuData {
  233. if skuStr != "" {
  234. skuStr += ";"
  235. }
  236. skuStr += v1.Value
  237. }
  238. tmp := map[string]string{
  239. "sku_str": skuStr,
  240. "goods_title": v.Title,
  241. "goods_img": v.Img,
  242. "num": utils.IntToStr(v.Num),
  243. "amount": utils.Float64ToStr(float64(v.Num) * utils.StrToFloat64(v.Price)),
  244. }
  245. orderInfoMap = append(orderInfoMap, tmp)
  246. }
  247. res["order_info"] = orderInfoMap
  248. }
  249. e.OutSuc(c, res, nil)
  250. return
  251. }
  252. func StoreOrderConfirm(c *gin.Context) {
  253. storeSvc.StoreOrderConfirm(c)
  254. }
  255. func UserStoreInfo(c *gin.Context) {
  256. var arg map[string]string
  257. if err := c.ShouldBindJSON(&arg); err != nil {
  258. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  259. return
  260. }
  261. user := svc.GetUser(c)
  262. data, _ := db.SysModFindBySkipIdentifierWithUid(c, svc.MasterDb(c), "pub.flutter.user_store", user.Info.Uid)
  263. if data == nil {
  264. data = &model.SysModule{
  265. ModName: "user_store",
  266. Position: "user_store",
  267. SkipIdentifier: "pub.flutter.user_store",
  268. Title: "店铺模板",
  269. Uid: utils.StrToInt(arg["uid"]),
  270. State: 1,
  271. Data: "{\"icon\":\"\",\"icon_url\":\"\",\"name\":\"\",\"store_detail_url\":\"\",\"address\":\"\",\"phone\":\"\",\"time_str\":\"\",\"notice_str\":\"\",\"banner_title\":\"\",\"activity_title\":\"\"}",
  272. Platform: 1,
  273. CreateAt: time.Now(),
  274. UpdateAt: time.Now(),
  275. }
  276. svc.MasterDb(c).Insert(data)
  277. }
  278. var tmp = make(map[string]interface{})
  279. json.Unmarshal([]byte(data.Data), &tmp)
  280. h5Domain := svc.GetWebSiteDomainInfo(c, "wap")
  281. store := db.GetStoreIdEg(svc.MasterDb(c), utils.IntToStr(user.Info.Uid))
  282. if store != nil {
  283. tmp["lat"] = store.Lat
  284. tmp["lng"] = store.Lng
  285. tmp["province"] = store.Province
  286. tmp["city"] = store.City
  287. tmp["district"] = store.District
  288. tmp["province_id"] = store.ProvinceId
  289. tmp["city_id"] = store.CityId
  290. tmp["district_id"] = store.DistrictId
  291. tmp["work_state"] = utils.IntToStr(store.WorkState)
  292. }
  293. tmp["share_url"] = h5Domain + "/#/zy-user-store/pages/store-page/index?uid=" + arg["uid"]
  294. e.OutSuc(c, tmp, nil)
  295. return
  296. }
  297. func UserStoreInfoSet(c *gin.Context) {
  298. var arg map[string]interface{}
  299. if err := c.ShouldBindJSON(&arg); err != nil {
  300. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  301. return
  302. }
  303. user := svc.GetUser(c)
  304. data, _ := db.SysModFindBySkipIdentifierWithUid(c, svc.MasterDb(c), "pub.flutter.user_store", user.Info.Uid)
  305. data.Data = utils.SerializeStr(arg)
  306. svc.MasterDb(c).Where("mod_id=?", data.ModId).Cols("data").Update(data)
  307. store := db.GetStoreIdEg(svc.MasterDb(c), utils.IntToStr(user.Info.Uid))
  308. if store == nil {
  309. store = &model.CommunityTeamStore{
  310. Uid: user.Info.Uid,
  311. CreateAt: time.Now(),
  312. UpdateAt: time.Now(),
  313. State: 1,
  314. }
  315. svc.MasterDb(c).Insert(store)
  316. }
  317. if gjson.Get(data.Data, "address").String() != "" {
  318. store.Address = gjson.Get(data.Data, "address").String()
  319. }
  320. if gjson.Get(data.Data, "lat").String() != "" {
  321. store.Lat = gjson.Get(data.Data, "lat").String()
  322. }
  323. if gjson.Get(data.Data, "lng").String() != "" {
  324. store.Lng = gjson.Get(data.Data, "lng").String()
  325. }
  326. if gjson.Get(data.Data, "province").String() != "" {
  327. store.Province = gjson.Get(data.Data, "province").String()
  328. }
  329. if gjson.Get(data.Data, "city").String() != "" {
  330. store.City = gjson.Get(data.Data, "city").String()
  331. }
  332. if gjson.Get(data.Data, "district").String() != "" {
  333. store.District = gjson.Get(data.Data, "district").String()
  334. }
  335. if gjson.Get(data.Data, "province_id").String() != "" {
  336. store.ProvinceId = utils.StrToInt(gjson.Get(data.Data, "province_id").String())
  337. }
  338. if gjson.Get(data.Data, "city_id").String() != "" {
  339. store.CityId = utils.StrToInt(gjson.Get(data.Data, "city_id").String())
  340. }
  341. if gjson.Get(data.Data, "district_id").String() != "" {
  342. store.DistrictId = utils.StrToInt(gjson.Get(data.Data, "district_id").String())
  343. }
  344. if gjson.Get(data.Data, "name").String() != "" {
  345. store.Name = gjson.Get(data.Data, "name").String()
  346. }
  347. if gjson.Get(data.Data, "work_state").String() != "" {
  348. store.WorkState = utils.StrToInt(gjson.Get(data.Data, "work_state").String())
  349. }
  350. if gjson.Get(data.Data, "time_str").String() != "" {
  351. store.Timer = gjson.Get(data.Data, "time_str").String()
  352. }
  353. if gjson.Get(data.Data, "phone").String() != "" {
  354. store.Phone = gjson.Get(data.Data, "phone").String()
  355. }
  356. if gjson.Get(data.Data, "icon_url").String() != "" {
  357. store.Logo = gjson.Get(data.Data, "icon_url").String()
  358. }
  359. svc.MasterDb(c).Where("id=?", store.Id).Cols("province_id,city_id,district_id,timer,phone,logo,work_state,name,province,city,district,address,lat,lng").Update(store)
  360. e.OutSuc(c, "success", nil)
  361. return
  362. }
  363. func UserStoreBanner(c *gin.Context) {
  364. var arg map[string]string
  365. if err := c.ShouldBindJSON(&arg); err != nil {
  366. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  367. return
  368. }
  369. user := svc.GetUser(c)
  370. uid := user.Info.Uid
  371. activity := db.GetUserStoreActivity(svc.MasterDb(c), arg["type"], uid)
  372. data := make([]map[string]string, 0)
  373. if activity != nil {
  374. scheme := svc.SysCfgGet(c, "file_bucket_scheme")
  375. domain := svc.SysCfgGet(c, "file_bucket_host")
  376. linkTypeStr := []string{"内链分享", "外链分享"}
  377. for _, v := range *activity {
  378. tmp := map[string]string{
  379. "id": utils.IntToStr(v.Id),
  380. "title": v.Title,
  381. "link_type_str": linkTypeStr[v.LinkType],
  382. "img": v.Img,
  383. "img_url": svc.ImageFormatWithBucket(scheme, domain, v.Img),
  384. "label": v.Label,
  385. "uid": utils.IntToStr(v.Uid),
  386. }
  387. data = append(data, tmp)
  388. }
  389. }
  390. e.OutSuc(c, data, nil)
  391. return
  392. }
  393. func UserStoreBannerDetail(c *gin.Context) {
  394. var arg map[string]string
  395. if err := c.ShouldBindJSON(&arg); err != nil {
  396. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  397. return
  398. }
  399. data := db.GetUserStoreActivityById(svc.MasterDb(c), arg["id"])
  400. if data == nil {
  401. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  402. return
  403. }
  404. scheme := svc.SysCfgGet(c, "file_bucket_scheme")
  405. domain := svc.SysCfgGet(c, "file_bucket_host")
  406. res := map[string]string{
  407. "id": utils.IntToStr(data.Id),
  408. "uid": utils.IntToStr(data.Uid),
  409. "title": data.Title,
  410. "img": data.Img,
  411. "img_url": svc.ImageFormatWithBucket(scheme, domain, data.Img),
  412. "label": data.Label,
  413. "content": data.Content,
  414. "type": data.Type,
  415. "url": data.Url,
  416. "link_type": utils.IntToStr(data.LinkType),
  417. "sort": utils.IntToStr(data.Sort),
  418. }
  419. e.OutSuc(c, res, nil)
  420. return
  421. }
  422. func UserStoreBannerSave(c *gin.Context) {
  423. var arg map[string]string
  424. if err := c.ShouldBindJSON(&arg); err != nil {
  425. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  426. return
  427. }
  428. user := svc.GetUser(c)
  429. data := model.UserStoreActivity{}
  430. if arg["id"] == "" {
  431. data = model.UserStoreActivity{
  432. Uid: user.Info.Uid,
  433. Type: arg["type"],
  434. Time: time.Now(),
  435. }
  436. svc.MasterDb(c).Insert(&data)
  437. } else {
  438. svc.MasterDb(c).Where("id=?", arg["id"]).Get(&data)
  439. }
  440. data.Label = arg["label"]
  441. data.Img = arg["img"]
  442. data.Title = arg["title"]
  443. data.Content = arg["content"]
  444. data.LinkType = utils.StrToInt(arg["link_type"])
  445. data.Url = arg["url"]
  446. data.Sort = utils.StrToInt(arg["sort"])
  447. svc.MasterDb(c).Where("id=?", data.Id).Update(&data)
  448. e.OutSuc(c, "success", nil)
  449. return
  450. }
  451. func UserStoreBannerDel(c *gin.Context) {
  452. var arg map[string]string
  453. if err := c.ShouldBindJSON(&arg); err != nil {
  454. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  455. return
  456. }
  457. svc.MasterDb(c).Where("id=?", arg["id"]).Delete(&model.UserStoreActivity{})
  458. e.OutSuc(c, "success", nil)
  459. return
  460. }