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

472 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. "platform_commission": v.PlatformCommission,
  131. "commission": v.Commission,
  132. "state": utils.IntToStr(v.State),
  133. "create_at": v.CreateAt.Format("2006-01-02 15:04:05"),
  134. "confirm_at": utils.Int64ToStr(v.Oid),
  135. }
  136. if v.ConfirmAt.IsZero() == false {
  137. tmp["confirm_at"] = v.ConfirmAt.Format("2006-01-02 15:04:05")
  138. }
  139. user1, _ := db.UserFindByID(svc.MasterDb(c), v.Uid)
  140. if user1 != nil {
  141. tmp["phone"] = user1.Phone
  142. tmp["nickname"] = user1.Nickname
  143. }
  144. store := db.GetUserStore(svc.MasterDb(c), v.StoreUid)
  145. if store != nil {
  146. tmp["store_name"] = store.Name
  147. }
  148. list = append(list, tmp)
  149. }
  150. }
  151. res := map[string]interface{}{
  152. "total": total,
  153. "state": []map[string]string{
  154. {"name": "待付款", "value": "0"},
  155. {"name": "已支付", "value": "1"},
  156. {"name": "已提货", "value": "2"},
  157. {"name": "已取消", "value": "3"},
  158. },
  159. "list": list,
  160. }
  161. e.OutSuc(c, res, nil)
  162. }
  163. // UserStoreOrderDetail 门店订单详情-订单管理共用一个
  164. // @Summary 门店-门店订单详情
  165. // @Tags 门店
  166. // @Description 门店-门店订单详情
  167. // @Accept json
  168. // @Produce json
  169. // @Param req body md.StoreOrderDetail true "请求参数"
  170. // @Success 200 {string} ""
  171. // @Failure 400 {object} md.Response "具体错误"
  172. // @Router /api/v1/communityTeam/ownStore/store/order/detail [POST]
  173. func UserStoreOrderDetail(c *gin.Context) {
  174. var arg md.StoreOrderDetail
  175. if err := c.ShouldBindJSON(&arg); err != nil {
  176. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  177. return
  178. }
  179. order := db.GetOrderByOid(svc.MasterDb(c), arg.Oid)
  180. if order == nil {
  181. e.OutErr(c, 400, e.NewErr(400, "订单不存在"))
  182. return
  183. }
  184. stateList := []string{"待付款", "已支付", "已提货", "已取消"}
  185. payMethodStr := []string{"余额支付", "支付宝支付", "微信支付"}
  186. orderInfoMap := make([]map[string]string, 0)
  187. res := map[string]interface{}{
  188. "oid": utils.Int64ToStr(order.Oid),
  189. "uid": utils.IntToStr(order.Uid),
  190. "phone": order.Phone,
  191. "nickname": "",
  192. "store_name": "",
  193. "memo": order.Memo,
  194. "coupon": order.Coupon,
  195. "code": order.Code,
  196. "amount": order.Amount,
  197. "all_amount": utils.Float64ToStr(utils.StrToFloat64(order.Amount) + utils.StrToFloat64(order.Coupon)),
  198. "pay_method_str": payMethodStr[order.PayMethod],
  199. "state_str": stateList[order.State],
  200. "state": utils.IntToStr(order.State),
  201. "create_at": order.CreateAt.Format("2006-01-02 15:04:05"),
  202. "confirm_at": "",
  203. "pay_at": "",
  204. "order_info": orderInfoMap,
  205. }
  206. if order.ConfirmAt.IsZero() == false {
  207. res["confirm_at"] = order.ConfirmAt.Format("2006-01-02 15:04:05")
  208. } else {
  209. res["confirm_at"] = order.Timer
  210. if order.IsNow == 1 {
  211. res["confirm_at"] = "立即提货"
  212. }
  213. }
  214. if order.PayAt.IsZero() == false {
  215. res["pay_at"] = order.PayAt.Format("2006-01-02 15:04:05")
  216. }
  217. user, _ := db.UserFindByID(svc.MasterDb(c), order.Uid)
  218. if user != nil {
  219. res["nickname"] = user.Nickname
  220. if order.Phone == "" {
  221. res["phone"] = user.Phone
  222. }
  223. }
  224. store := db.GetUserStore(svc.MasterDb(c), order.StoreUid)
  225. if store != nil {
  226. res["store_name"] = store.Name
  227. }
  228. orderInfo := db.GetOrderInfoAllEg(svc.MasterDb(c), arg.Oid)
  229. if orderInfo != nil {
  230. for _, v := range *orderInfo {
  231. skuData := make([]md.Sku, 0)
  232. json.Unmarshal([]byte(v.SkuInfo), &skuData)
  233. skuStr := ""
  234. for _, v1 := range skuData {
  235. if skuStr != "" {
  236. skuStr += ";"
  237. }
  238. skuStr += v1.Value
  239. }
  240. tmp := map[string]string{
  241. "sku_str": skuStr,
  242. "goods_title": v.Title,
  243. "goods_img": v.Img,
  244. "num": utils.IntToStr(v.Num),
  245. "amount": utils.Float64ToStr(float64(v.Num) * utils.StrToFloat64(v.Price)),
  246. }
  247. orderInfoMap = append(orderInfoMap, tmp)
  248. }
  249. res["order_info"] = orderInfoMap
  250. }
  251. e.OutSuc(c, res, nil)
  252. return
  253. }
  254. func StoreOrderConfirm(c *gin.Context) {
  255. storeSvc.StoreOrderConfirm(c)
  256. }
  257. func UserStoreInfo(c *gin.Context) {
  258. user := svc.GetUser(c)
  259. data, _ := db.SysModFindBySkipIdentifierWithUid(c, svc.MasterDb(c), "pub.flutter.user_store", user.Info.Uid)
  260. if data == nil {
  261. data = &model.SysModule{
  262. ModName: "user_store",
  263. Position: "user_store",
  264. SkipIdentifier: "pub.flutter.user_store",
  265. Title: "店铺模板",
  266. Uid: user.Info.Uid,
  267. State: 1,
  268. Data: "{\"icon\":\"\",\"icon_url\":\"\",\"name\":\"\",\"store_detail_url\":\"\",\"address\":\"\",\"phone\":\"\",\"time_str\":\"\",\"notice_str\":\"\",\"banner_title\":\"\",\"activity_title\":\"\"}",
  269. Platform: 1,
  270. CreateAt: time.Now(),
  271. UpdateAt: time.Now(),
  272. }
  273. svc.MasterDb(c).Insert(data)
  274. }
  275. var tmp = make(map[string]interface{})
  276. json.Unmarshal([]byte(data.Data), &tmp)
  277. h5Domain := svc.GetWebSiteDomainInfo(c, "wap")
  278. store := db.GetStoreIdEg(svc.MasterDb(c), utils.IntToStr(user.Info.Uid))
  279. if store != nil {
  280. tmp["icon"] = store.Logo
  281. tmp["icon_url"] = store.Logo
  282. tmp["name"] = store.Name
  283. tmp["address"] = store.Address
  284. tmp["phone"] = store.Phone
  285. tmp["lat"] = store.Lat
  286. tmp["lng"] = store.Lng
  287. tmp["province"] = store.Province
  288. tmp["city"] = store.City
  289. tmp["district"] = store.District
  290. tmp["province_id"] = store.ProvinceId
  291. tmp["city_id"] = store.CityId
  292. tmp["district_id"] = store.DistrictId
  293. tmp["work_state"] = utils.IntToStr(store.WorkState)
  294. }
  295. tmp["share_url"] = h5Domain + "/#/zy-user-store/pages/store-page/index?uid=" + utils.IntToStr(user.Info.Uid)
  296. e.OutSuc(c, tmp, nil)
  297. return
  298. }
  299. func UserStoreInfoSet(c *gin.Context) {
  300. var arg map[string]interface{}
  301. if err := c.ShouldBindJSON(&arg); err != nil {
  302. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  303. return
  304. }
  305. user := svc.GetUser(c)
  306. data, _ := db.SysModFindBySkipIdentifierWithUid(c, svc.MasterDb(c), "pub.flutter.user_store", user.Info.Uid)
  307. data.Data = utils.SerializeStr(arg)
  308. svc.MasterDb(c).Where("mod_id=?", data.ModId).Cols("data").Update(data)
  309. store := db.GetStoreIdEg(svc.MasterDb(c), utils.IntToStr(user.Info.Uid))
  310. if store == nil {
  311. store = &model.CommunityTeamStore{
  312. Uid: user.Info.Uid,
  313. CreateAt: time.Now(),
  314. UpdateAt: time.Now(),
  315. State: 1,
  316. }
  317. svc.MasterDb(c).Insert(store)
  318. }
  319. if gjson.Get(data.Data, "address").String() != "" {
  320. store.Address = gjson.Get(data.Data, "address").String()
  321. }
  322. if gjson.Get(data.Data, "lat").String() != "" {
  323. store.Lat = gjson.Get(data.Data, "lat").String()
  324. }
  325. if gjson.Get(data.Data, "lng").String() != "" {
  326. store.Lng = gjson.Get(data.Data, "lng").String()
  327. }
  328. if gjson.Get(data.Data, "province").String() != "" {
  329. store.Province = gjson.Get(data.Data, "province").String()
  330. }
  331. if gjson.Get(data.Data, "city").String() != "" {
  332. store.City = gjson.Get(data.Data, "city").String()
  333. }
  334. if gjson.Get(data.Data, "district").String() != "" {
  335. store.District = gjson.Get(data.Data, "district").String()
  336. }
  337. if gjson.Get(data.Data, "province_id").String() != "" {
  338. store.ProvinceId = utils.StrToInt(gjson.Get(data.Data, "province_id").String())
  339. }
  340. if gjson.Get(data.Data, "city_id").String() != "" {
  341. store.CityId = utils.StrToInt(gjson.Get(data.Data, "city_id").String())
  342. }
  343. if gjson.Get(data.Data, "district_id").String() != "" {
  344. store.DistrictId = utils.StrToInt(gjson.Get(data.Data, "district_id").String())
  345. }
  346. if gjson.Get(data.Data, "name").String() != "" {
  347. store.Name = gjson.Get(data.Data, "name").String()
  348. }
  349. if gjson.Get(data.Data, "work_state").String() != "" {
  350. store.WorkState = utils.StrToInt(gjson.Get(data.Data, "work_state").String())
  351. }
  352. if gjson.Get(data.Data, "time_str").String() != "" {
  353. store.Timer = gjson.Get(data.Data, "time_str").String()
  354. }
  355. if gjson.Get(data.Data, "phone").String() != "" {
  356. store.Phone = gjson.Get(data.Data, "phone").String()
  357. }
  358. if gjson.Get(data.Data, "icon_url").String() != "" {
  359. store.Logo = gjson.Get(data.Data, "icon_url").String()
  360. }
  361. 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)
  362. e.OutSuc(c, "success", nil)
  363. return
  364. }
  365. func UserStoreBanner(c *gin.Context) {
  366. var arg map[string]string
  367. if err := c.ShouldBindJSON(&arg); err != nil {
  368. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  369. return
  370. }
  371. user := svc.GetUser(c)
  372. uid := user.Info.Uid
  373. activity := db.GetUserStoreActivity(svc.MasterDb(c), arg["type"], uid)
  374. data := make([]map[string]string, 0)
  375. if activity != nil {
  376. scheme := svc.SysCfgGet(c, "file_bucket_scheme")
  377. domain := svc.SysCfgGet(c, "file_bucket_host")
  378. linkTypeStr := []string{"内链分享", "外链分享"}
  379. for _, v := range *activity {
  380. tmp := map[string]string{
  381. "id": utils.IntToStr(v.Id),
  382. "title": v.Title,
  383. "link_type_str": linkTypeStr[v.LinkType],
  384. "img": v.Img,
  385. "img_url": svc.ImageFormatWithBucket(scheme, domain, v.Img),
  386. "label": v.Label,
  387. "uid": utils.IntToStr(v.Uid),
  388. }
  389. data = append(data, tmp)
  390. }
  391. }
  392. e.OutSuc(c, data, nil)
  393. return
  394. }
  395. func UserStoreBannerDetail(c *gin.Context) {
  396. var arg map[string]string
  397. if err := c.ShouldBindJSON(&arg); err != nil {
  398. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  399. return
  400. }
  401. data := db.GetUserStoreActivityById(svc.MasterDb(c), arg["id"])
  402. if data == nil {
  403. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  404. return
  405. }
  406. scheme := svc.SysCfgGet(c, "file_bucket_scheme")
  407. domain := svc.SysCfgGet(c, "file_bucket_host")
  408. res := map[string]string{
  409. "id": utils.IntToStr(data.Id),
  410. "uid": utils.IntToStr(data.Uid),
  411. "title": data.Title,
  412. "img": data.Img,
  413. "img_url": svc.ImageFormatWithBucket(scheme, domain, data.Img),
  414. "label": data.Label,
  415. "content": data.Content,
  416. "type": data.Type,
  417. "url": data.Url,
  418. "link_type": utils.IntToStr(data.LinkType),
  419. "sort": utils.IntToStr(data.Sort),
  420. }
  421. e.OutSuc(c, res, nil)
  422. return
  423. }
  424. func UserStoreBannerSave(c *gin.Context) {
  425. var arg map[string]string
  426. if err := c.ShouldBindJSON(&arg); err != nil {
  427. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  428. return
  429. }
  430. user := svc.GetUser(c)
  431. data := model.UserStoreActivity{}
  432. if arg["id"] == "" {
  433. data = model.UserStoreActivity{
  434. Uid: user.Info.Uid,
  435. Type: arg["type"],
  436. Time: time.Now(),
  437. }
  438. svc.MasterDb(c).Insert(&data)
  439. } else {
  440. svc.MasterDb(c).Where("id=?", arg["id"]).Get(&data)
  441. }
  442. data.Label = arg["label"]
  443. data.Img = arg["img"]
  444. data.Title = arg["title"]
  445. data.Content = arg["content"]
  446. data.LinkType = utils.StrToInt(arg["link_type"])
  447. data.Url = arg["url"]
  448. data.Sort = utils.StrToInt(arg["sort"])
  449. svc.MasterDb(c).Where("id=?", data.Id).Update(&data)
  450. e.OutSuc(c, "success", nil)
  451. return
  452. }
  453. func UserStoreBannerDel(c *gin.Context) {
  454. var arg map[string]string
  455. if err := c.ShouldBindJSON(&arg); err != nil {
  456. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  457. return
  458. }
  459. svc.MasterDb(c).Where("id=?", arg["id"]).Delete(&model.UserStoreActivity{})
  460. e.OutSuc(c, "success", nil)
  461. return
  462. }