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

hdl_store.go 15 KiB

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