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

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