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

2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. "time"
  12. )
  13. // UserStoreList 门店列表
  14. // @Summary 门店-列表
  15. // @Tags 门店
  16. // @Description 门店-列表
  17. // @Accept json
  18. // @Produce json
  19. // @Param req body md.Store true "请求参数"
  20. // @Success 200 {string} ""
  21. // @Failure 400 {object} md.Response "具体错误"
  22. // @Router /api/v1/communityTeam/agent/store/list [POST]
  23. func UserStoreList(c *gin.Context) {
  24. var arg md.Store
  25. if err := c.ShouldBindJSON(&arg); err != nil {
  26. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  27. return
  28. }
  29. user := svc.GetUser(c)
  30. list, count := db.GetUserList(svc.MasterDb(c), arg, user.Info.Uid)
  31. data := make([]map[string]interface{}, 0)
  32. if list != nil {
  33. currentMonth := utils.GetTimeRange("current_month")
  34. for _, v := range *list {
  35. store := v.CommunityTeamStore
  36. tmp := map[string]interface{}{
  37. "uid": utils.IntToStr(v.User.Uid),
  38. "create_at": store.CreateAt.Format("2006-01-02 15:04:05"),
  39. "name": "",
  40. "address": "",
  41. "logo": "",
  42. "order_count": "0",
  43. "order_amount": "0",
  44. "agent_commission": "0",
  45. "platform_commission": "0",
  46. "base_commission": "0",
  47. "work_state": "",
  48. }
  49. count1, _ := svc.MasterDb(c).Where("store_uid=? and parent_uid=?", v.User.Uid, user.Info.Uid).In("state", []string{"1", "2"}).Count(&model.CommunityTeamOrder{})
  50. 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")
  51. tmp["order_count"] = utils.Int64ToStr(count1)
  52. tmp["order_amount"] = svc.GetCommissionPrec(c, utils.Float64ToStr(sum), "2", "")
  53. tmp["name"] = store.Name
  54. tmp["address"] = store.Address
  55. tmp["logo"] = store.Logo
  56. tmp["work_state"] = "营业中"
  57. if store.WorkState == 1 {
  58. tmp["work_state"] = "休息中"
  59. }
  60. tmp["agent_commission"] = store.AgentCommission
  61. data = append(data, tmp)
  62. }
  63. }
  64. res := map[string]interface{}{
  65. "total": count,
  66. "list": data,
  67. }
  68. e.OutSuc(c, res, nil)
  69. }
  70. // UserStoreSave 门店编辑
  71. // @Summary 门店-门店编辑
  72. // @Tags 门店
  73. // @Description 门店-门店编辑
  74. // @Accept json
  75. // @Produce json
  76. // @Param req body md.StoreSave true "请求参数"
  77. // @Success 200 {string} ""
  78. // @Failure 400 {object} md.Response "具体错误"
  79. // @Router /api/v1/communityTeam/agent/store/save [POST]
  80. func UserStoreSave(c *gin.Context) {
  81. var arg md.StoreSave
  82. if err := c.ShouldBindJSON(&arg); err != nil {
  83. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  84. return
  85. }
  86. store := db.GetUserStore(svc.MasterDb(c), utils.StrToInt(arg.Uid))
  87. if store == nil {
  88. e.OutErr(c, 400, e.NewErr(400, "门店不存在"))
  89. return
  90. }
  91. store.AgentCommission = arg.AgentCommission
  92. svc.MasterDb(c).Where("id=?", store.Id).Cols("agent_commission").Update(store)
  93. e.OutSuc(c, "success", nil)
  94. return
  95. }
  96. // UserStoreOrder 门店订单-订单管理共用一个
  97. // @Summary 门店-门店订单
  98. // @Tags 门店
  99. // @Description 门店-门店订单
  100. // @Accept json
  101. // @Produce json
  102. // @Param req body md.StoreOrder true "请求参数"
  103. // @Success 200 {string} ""
  104. // @Failure 400 {object} md.Response "具体错误"
  105. // @Router /api/v1/communityTeam/agent/store/order [POST]
  106. func UserStoreOrder(c *gin.Context) {
  107. var arg md.StoreOrder
  108. if err := c.ShouldBindJSON(&arg); err != nil {
  109. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  110. return
  111. }
  112. user := svc.GetUser(c)
  113. data, total, _ := db.GetStoreOrderList(svc.MasterDb(c), arg, user.Info.Uid)
  114. list := make([]map[string]string, 0)
  115. if data != nil {
  116. stateList := []string{"待付款", "已支付", "已提货", "已取消"}
  117. for _, v := range *data {
  118. tmp := map[string]string{
  119. "oid": utils.Int64ToStr(v.Oid),
  120. "uid": utils.IntToStr(v.Uid),
  121. "phone": "",
  122. "nickname": "",
  123. "store_name": "",
  124. "state_str": stateList[v.State],
  125. "amount": v.Amount,
  126. "agent_commission": v.AgentCommission,
  127. "state": utils.IntToStr(v.State),
  128. "create_at": v.CreateAt.Format("2006-01-02 15:04:05"),
  129. "confirm_at": utils.Int64ToStr(v.Oid),
  130. }
  131. if v.ConfirmAt.IsZero() == false {
  132. tmp["confirm_at"] = v.ConfirmAt.Format("2006-01-02 15:04:05")
  133. }
  134. user1, _ := db.UserFindByID(svc.MasterDb(c), v.Uid)
  135. if user1 != nil {
  136. tmp["phone"] = user1.Phone
  137. tmp["nickname"] = user1.Nickname
  138. }
  139. store := db.GetUserStore(svc.MasterDb(c), v.StoreUid)
  140. if store != nil {
  141. tmp["store_name"] = store.Name
  142. }
  143. list = append(list, tmp)
  144. }
  145. }
  146. res := map[string]interface{}{
  147. "total": total,
  148. "state": []map[string]string{
  149. {"name": "待付款", "value": "0"},
  150. {"name": "已支付", "value": "1"},
  151. {"name": "已提货", "value": "2"},
  152. {"name": "已取消", "value": "3"},
  153. },
  154. "list": list,
  155. }
  156. e.OutSuc(c, res, nil)
  157. }
  158. // UserStoreOrderDetail 门店订单详情-订单管理共用一个
  159. // @Summary 门店-门店订单详情
  160. // @Tags 门店
  161. // @Description 门店-门店订单详情
  162. // @Accept json
  163. // @Produce json
  164. // @Param req body md.StoreOrderDetail true "请求参数"
  165. // @Success 200 {string} ""
  166. // @Failure 400 {object} md.Response "具体错误"
  167. // @Router /api/v1/communityTeam/agent/store/order/detail [POST]
  168. func UserStoreOrderDetail(c *gin.Context) {
  169. var arg md.StoreOrderDetail
  170. if err := c.ShouldBindJSON(&arg); err != nil {
  171. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  172. return
  173. }
  174. order := db.GetOrderByOid(svc.MasterDb(c), arg.Oid)
  175. if order == nil {
  176. e.OutErr(c, 400, e.NewErr(400, "订单不存在"))
  177. return
  178. }
  179. stateList := []string{"待付款", "已支付", "已提货", "已取消"}
  180. payMethodStr := []string{"余额支付", "支付宝支付", "微信支付"}
  181. orderInfoMap := make([]map[string]string, 0)
  182. res := map[string]interface{}{
  183. "oid": utils.Int64ToStr(order.Oid),
  184. "uid": utils.IntToStr(order.Uid),
  185. "phone": order.Phone,
  186. "nickname": "",
  187. "store_name": "",
  188. "memo": order.Memo,
  189. "coupon": order.Coupon,
  190. "code": order.Code,
  191. "amount": order.Amount,
  192. "all_amount": utils.Float64ToStr(utils.StrToFloat64(order.Amount) + utils.StrToFloat64(order.Coupon)),
  193. "pay_method_str": payMethodStr[order.PayMethod],
  194. "state_str": stateList[order.State],
  195. "state": utils.IntToStr(order.State),
  196. "create_at": order.CreateAt.Format("2006-01-02 15:04:05"),
  197. "confirm_at": "",
  198. "pay_at": "",
  199. "order_info": orderInfoMap,
  200. }
  201. if order.ConfirmAt.IsZero() == false {
  202. res["confirm_at"] = order.ConfirmAt.Format("2006-01-02 15:04:05")
  203. } else {
  204. res["confirm_at"] = order.Timer
  205. if order.IsNow == 1 {
  206. res["confirm_at"] = "立即提货"
  207. }
  208. }
  209. if order.PayAt.IsZero() == false {
  210. res["pay_at"] = order.PayAt.Format("2006-01-02 15:04:05")
  211. }
  212. user, _ := db.UserFindByID(svc.MasterDb(c), order.Uid)
  213. if user != nil {
  214. res["nickname"] = user.Nickname
  215. if order.Phone == "" {
  216. res["phone"] = user.Phone
  217. }
  218. }
  219. store := db.GetUserStore(svc.MasterDb(c), order.StoreUid)
  220. if store != nil {
  221. res["store_name"] = store.Name
  222. }
  223. orderInfo := db.GetOrderInfoAllEg(svc.MasterDb(c), arg.Oid)
  224. if orderInfo != nil {
  225. for _, v := range *orderInfo {
  226. skuData := make([]md.Sku, 0)
  227. json.Unmarshal([]byte(v.SkuInfo), &skuData)
  228. skuStr := ""
  229. for _, v1 := range skuData {
  230. if skuStr != "" {
  231. skuStr += ";"
  232. }
  233. skuStr += v1.Value
  234. }
  235. tmp := map[string]string{
  236. "sku_str": skuStr,
  237. "goods_title": v.Title,
  238. "goods_img": v.Img,
  239. "num": utils.IntToStr(v.Num),
  240. "amount": utils.Float64ToStr(float64(v.Num) * utils.StrToFloat64(v.Price)),
  241. }
  242. orderInfoMap = append(orderInfoMap, tmp)
  243. }
  244. res["order_info"] = orderInfoMap
  245. }
  246. e.OutSuc(c, res, nil)
  247. return
  248. }
  249. // UserStoreTotal 销售额统计
  250. // @Summary 门店-销售额统计
  251. // @Tags 门店
  252. // @Description 门店-销售额统计
  253. // @Accept json
  254. // @Produce json
  255. // @Param req body md.StoreOrderTotal true "请求参数"
  256. // @Success 200 {string} ""
  257. // @Failure 400 {object} md.Response "具体错误"
  258. // @Router /api/v1/communityTeam/agent/store/total [POST]
  259. func UserStoreTotal(c *gin.Context) {
  260. var arg md.StoreOrderTotal
  261. if err := c.ShouldBindJSON(&arg); err != nil {
  262. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  263. return
  264. }
  265. user := svc.GetUser(c)
  266. currentMonth := utils.GetTimeRange("current_month")
  267. today := utils.GetTimeRange("today")
  268. withinSevenDays := utils.GetTimeRange("within_seven_days")
  269. monthSum, _ := svc.MasterDb(c).Where("store_uid=? and parent_uid=? and create_at>=?", arg.StoreUid, user.Info.Uid, time.Unix(currentMonth["start"], 0).Format("2006-01-02 15:04:05")).In("state", []string{"1", "2"}).Sum(&model.CommunityTeamOrder{}, "amount")
  270. daySum, _ := svc.MasterDb(c).Where("store_uid=? and parent_uid=? and create_at>=?", arg.StoreUid, user.Info.Uid, time.Unix(today["start"], 0).Format("2006-01-02 15:04:05")).In("state", []string{"1", "2"}).Sum(&model.CommunityTeamOrder{}, "amount")
  271. withinSevenDaysSum, _ := svc.MasterDb(c).Where("store_uid=? and parent_uid=? and create_at>=?", arg.StoreUid, user.Info.Uid, time.Unix(withinSevenDays["start"], 0).Format("2006-01-02 15:04:05")).In("state", []string{"1", "2"}).Sum(&model.CommunityTeamOrder{}, "amount")
  272. list := []map[string]string{
  273. {"name": "本日销售额", "value": utils.Float64ToStr(daySum)},
  274. {"name": "近七日销售额", "value": utils.Float64ToStr(withinSevenDaysSum)},
  275. {"name": "本月销售额", "value": utils.Float64ToStr(monthSum)},
  276. }
  277. commission, _ := svc.MasterDb(c).Where("store_uid=? and parent_uid=? ", arg.StoreUid, user.Info.Uid).Sum(&model.CommunityTeamOrder{}, "agent_commission")
  278. store := db.GetStoreIdEg(svc.MasterDb(c), arg.StoreUid)
  279. res := map[string]interface{}{
  280. "list": list,
  281. "platform_bili": "-",
  282. "platform_amount": "-",
  283. "base_bili": "-",
  284. "base_amount": "-",
  285. "agent_bili": store.AgentCommission + "%",
  286. "agent_amount": utils.Float64ToStr(commission),
  287. }
  288. e.OutSuc(c, res, nil)
  289. return
  290. }