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

469 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: int(utils.AnyToInt64(arg["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. uid := utils.StrToInt(arg["uid"])
  370. activity := db.GetUserStoreActivity(svc.MasterDb(c), arg["type"], uid)
  371. data := make([]map[string]string, 0)
  372. if activity != nil {
  373. scheme := svc.SysCfgGet(c, "file_bucket_scheme")
  374. domain := svc.SysCfgGet(c, "file_bucket_host")
  375. linkTypeStr := []string{"内链分享", "外链分享"}
  376. for _, v := range *activity {
  377. tmp := map[string]string{
  378. "id": utils.IntToStr(v.Id),
  379. "title": v.Title,
  380. "link_type_str": linkTypeStr[v.LinkType],
  381. "img": v.Img,
  382. "img_url": svc.ImageFormatWithBucket(scheme, domain, v.Img),
  383. "label": v.Label,
  384. "uid": utils.IntToStr(v.Uid),
  385. }
  386. data = append(data, tmp)
  387. }
  388. }
  389. e.OutSuc(c, data, nil)
  390. return
  391. }
  392. func UserStoreBannerDetail(c *gin.Context) {
  393. var arg map[string]string
  394. if err := c.ShouldBindJSON(&arg); err != nil {
  395. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  396. return
  397. }
  398. data := db.GetUserStoreActivityById(svc.MasterDb(c), arg["id"])
  399. if data == nil {
  400. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  401. return
  402. }
  403. scheme := svc.SysCfgGet(c, "file_bucket_scheme")
  404. domain := svc.SysCfgGet(c, "file_bucket_host")
  405. res := map[string]string{
  406. "id": utils.IntToStr(data.Id),
  407. "uid": utils.IntToStr(data.Uid),
  408. "title": data.Title,
  409. "img": data.Img,
  410. "img_url": svc.ImageFormatWithBucket(scheme, domain, data.Img),
  411. "label": data.Label,
  412. "content": data.Content,
  413. "type": data.Type,
  414. "url": data.Url,
  415. "link_type": utils.IntToStr(data.LinkType),
  416. "sort": utils.IntToStr(data.Sort),
  417. }
  418. e.OutSuc(c, res, nil)
  419. return
  420. }
  421. func UserStoreBannerSave(c *gin.Context) {
  422. var arg map[string]string
  423. if err := c.ShouldBindJSON(&arg); err != nil {
  424. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  425. return
  426. }
  427. data := model.UserStoreActivity{}
  428. if arg["id"] == "" {
  429. data = model.UserStoreActivity{
  430. Uid: utils.StrToInt(arg["uid"]),
  431. Type: arg["type"],
  432. Time: time.Now(),
  433. }
  434. svc.MasterDb(c).Insert(&data)
  435. } else {
  436. svc.MasterDb(c).Where("id=?", arg["id"]).Get(&data)
  437. }
  438. data.Label = arg["label"]
  439. data.Img = arg["img"]
  440. data.Title = arg["title"]
  441. data.Content = arg["content"]
  442. data.LinkType = utils.StrToInt(arg["link_type"])
  443. data.Url = arg["url"]
  444. data.Sort = utils.StrToInt(arg["sort"])
  445. svc.MasterDb(c).Where("id=?", data.Id).Update(&data)
  446. e.OutSuc(c, "success", nil)
  447. return
  448. }
  449. func UserStoreBannerDel(c *gin.Context) {
  450. var arg map[string]string
  451. if err := c.ShouldBindJSON(&arg); err != nil {
  452. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  453. return
  454. }
  455. svc.MasterDb(c).Where("id=?", arg["id"]).Delete(&model.UserStoreActivity{})
  456. e.OutSuc(c, "success", nil)
  457. return
  458. }