store.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package controller
  2. import (
  3. "gas-cylinder-api/app/admin/model"
  4. "gas-cylinder-api/app/admin/service"
  5. "gas-cylinder-api/app/admin/service/dto"
  6. "gas-cylinder-api/common/actions"
  7. "github.com/gin-gonic/gin"
  8. "github.com/gin-gonic/gin/binding"
  9. "gogs.baozhida.cn/zoie/OAuth-core/api"
  10. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  11. _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  12. "gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
  13. "strconv"
  14. "strings"
  15. )
  16. type StoreController struct {
  17. api.Api
  18. }
  19. // GetPage 获取销售门店列表
  20. // @Summary 获取销售门店列表
  21. // @Description 获取销售门店列表(用户公司及子公司)
  22. // @Tags 销售门店
  23. // @Param name query string false "销售门店名称"
  24. // @Success 200 {object} response.Response{data=response.Page{list=[]model.SysDept}} "{"code": 200, "data": [...]}"
  25. // @Router /api/store [get]
  26. // @Security Bearer
  27. func (e StoreController) GetPage(c *gin.Context) {
  28. s := service.Store{}
  29. req := dto.StoreGetPageReq{}
  30. err := e.MakeContext(c).
  31. MakeOrm().
  32. Bind(&req, binding.Query).
  33. MakeService(&s.Service).
  34. Errors
  35. if err != nil {
  36. e.Logger.Error(err)
  37. e.Error(500, err, err.Error())
  38. return
  39. }
  40. //数据权限检查
  41. p := actions.GetPermissionFromContext(c)
  42. p.DeptId = user.GetDeptId(c)
  43. req.DeptId = user.GetDeptId(c)
  44. req.Type = model.TypeStore
  45. list := make([]model.SysDept, 0)
  46. list, err = s.SetStorePage(&req, p)
  47. if err != nil {
  48. e.Error(500, err, err.Error())
  49. return
  50. }
  51. e.OK(list, "查询成功")
  52. }
  53. // GetDeliveryPage 获取配送门店列表
  54. // @Summary 获取配送门店列表
  55. // @Description 获取配送门店列表(进入公司后的公司列表)
  56. // @Tags 销售门店
  57. // @Param name query string false "销售门店名称"
  58. // @Success 200 {object} response.Response{data=response.Page{list=[]model.SysDept}} "{"code": 200, "data": [...]}"
  59. // @Router /api/store/delivery [get]
  60. // @Security Bearer
  61. func (e StoreController) GetDeliveryPage(c *gin.Context) {
  62. s := service.Store{}
  63. req := dto.StoreGetPageReq{}
  64. err := e.MakeContext(c).
  65. MakeOrm().
  66. Bind(&req, binding.Query).
  67. MakeService(&s.Service).
  68. Errors
  69. if err != nil {
  70. e.Logger.Error(err)
  71. e.Error(500, err, err.Error())
  72. return
  73. }
  74. //数据权限检查
  75. p := actions.GetPermissionFromContext(c)
  76. req.DeptId = p.DeptId
  77. req.Type = model.TypeStore
  78. list := make([]model.SysDept, 0)
  79. list, err = s.SetStorePage(&req, p)
  80. if err != nil {
  81. e.Error(500, err, err.Error())
  82. return
  83. }
  84. e.OK(list, "查询成功")
  85. }
  86. // GetAllPage 获取配送门店列表(根公司下所有公司)
  87. // @Summary 获取配送门店列表
  88. // @Description 获取配送门店列表(进入公司后的公司列表)
  89. // @Tags 销售门店
  90. // @Param name query string false "销售门店名称"
  91. // @Success 200 {object} response.Response{data=response.Page{list=[]model.SysDept}} "{"code": 200, "data": [...]}"
  92. // @Router /api/store/delivery [get]
  93. // @Security Bearer
  94. func (e StoreController) GetAllPage(c *gin.Context) {
  95. s := service.Store{}
  96. req := dto.StoreGetPageReq{}
  97. err := e.MakeContext(c).
  98. MakeOrm().
  99. Bind(&req, binding.Query).
  100. MakeService(&s.Service).
  101. Errors
  102. if err != nil {
  103. e.Logger.Error(err)
  104. e.Error(500, err, err.Error())
  105. return
  106. }
  107. var dept model.SysDept
  108. //数据权限检查
  109. err = s.Get(&dto.StoreGetReq{
  110. Id: user.GetDeptId(c),
  111. }, &dept)
  112. if err != nil {
  113. e.Error(500, err, err.Error())
  114. return
  115. }
  116. deptId := user.GetDeptId(c)
  117. deptIdList := strings.Split(strings.Trim(dept.Path, "/"), "/")
  118. if len(deptIdList) > 1 {
  119. deptId, _ = strconv.Atoi(deptIdList[1])
  120. }
  121. //数据权限检查
  122. p := actions.GetPermissionFromContext(c)
  123. req.DeptId = deptId
  124. req.Type = model.TypeStore
  125. list := make([]model.SysDept, 0)
  126. p.DeptId = deptId
  127. list, err = s.SetStorePage(&req, p)
  128. if err != nil {
  129. e.Error(500, err, err.Error())
  130. return
  131. }
  132. e.OK(list, "查询成功")
  133. }
  134. // Get 通过id获取销售门店
  135. // @Summary 通过id获取销售门店
  136. // @Description 通过id获取销售门店
  137. // @Tags 销售门店
  138. // @Param id path string true "销售门店id"
  139. // @Success 200 {object} response.Response{data=model.SysDept} "{"code": 200, "data": [...]}"
  140. // @Router /api/store/{id} [get]
  141. // @Security Bearer
  142. func (e StoreController) Get(c *gin.Context) {
  143. s := service.Store{}
  144. req := dto.StoreGetReq{}
  145. err := e.MakeContext(c).
  146. MakeOrm().
  147. Bind(&req, nil).
  148. MakeService(&s.Service).
  149. Errors
  150. if err != nil {
  151. e.Logger.Error(err)
  152. e.Error(500, err, err.Error())
  153. return
  154. }
  155. var object model.SysDept
  156. //数据权限检查
  157. err = s.Get(&req, &object)
  158. if err != nil {
  159. e.Error(500, err, err.Error())
  160. return
  161. }
  162. e.OK(object, "查询成功")
  163. }
  164. // Insert 添加销售门店
  165. // @Summary 添加销售门店
  166. // @Description 添加销售门店
  167. // @Tags 销售门店
  168. // @Accept application/json
  169. // @Product application/json
  170. // @Param data body dto.StoreInsertReq true "data"
  171. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  172. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  173. // @Router /api/store [post]
  174. // @Security Bearer
  175. func (e StoreController) Insert(c *gin.Context) {
  176. s := service.Store{}
  177. req := dto.StoreInsertReq{}
  178. err := e.MakeContext(c).
  179. MakeOrm().
  180. Bind(&req, binding.JSON).
  181. MakeService(&s.Service).
  182. Errors
  183. if err != nil {
  184. e.Logger.Error(err)
  185. e.Error(500, err, err.Error())
  186. return
  187. }
  188. // 设置创建人
  189. req.SetCreateBy(user.GetUserId(c))
  190. deptId := user.GetDeptId(c)
  191. err = s.Insert(&req, deptId)
  192. if err != nil {
  193. e.Error(500, err, err.Error())
  194. return
  195. }
  196. e.OK(req.GetId(), "创建成功")
  197. }
  198. // Update 修改销售门店
  199. // @Summary 修改销售门店
  200. // @Description 修改销售门店
  201. // @Tags 销售门店
  202. // @Accept application/json
  203. // @Product application/json
  204. // @Param data body dto.StoreUpdateReq true "body"
  205. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  206. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  207. // @Router /api/store [put]
  208. // @Security Bearer
  209. func (e StoreController) Update(c *gin.Context) {
  210. s := service.Store{}
  211. req := dto.StoreUpdateReq{}
  212. err := e.MakeContext(c).
  213. MakeOrm().
  214. Bind(&req).
  215. MakeService(&s.Service).
  216. Errors
  217. if err != nil {
  218. e.Logger.Error(err)
  219. e.Error(500, err, err.Error())
  220. return
  221. }
  222. req.SetUpdateBy(user.GetUserId(c))
  223. err = s.Update(&req)
  224. if err != nil {
  225. e.Error(500, err, err.Error())
  226. return
  227. }
  228. e.OK(req.GetId(), "更新成功")
  229. }
  230. // Delete 删除销售门店
  231. // @Summary 删除销售门店
  232. // @Description 删除销售门店
  233. // @Tags 销售门店
  234. // @Accept application/json
  235. // @Product application/json
  236. // @Param data body dto.StoreDeleteReq true "body"
  237. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  238. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  239. // @Router /api/store [delete]
  240. // @Security Bearer
  241. func (e StoreController) Delete(c *gin.Context) {
  242. s := service.Store{}
  243. req := dto.StoreDeleteReq{}
  244. userSvc := service.SysUser{}
  245. err := e.MakeContext(c).
  246. MakeOrm().
  247. Bind(&req, binding.JSON, nil).
  248. MakeService(&s.Service).
  249. MakeService(&userSvc.Service).
  250. Errors
  251. if err != nil {
  252. e.Logger.Error(err)
  253. e.Error(500, err, err.Error())
  254. return
  255. }
  256. var count int64
  257. err = userSvc.GetCount(&dto.SysUserGetCountReq{DeptIds: []int{req.Id}}, &count)
  258. if err != nil {
  259. e.Error(500, err, err.Error())
  260. return
  261. }
  262. if count > 0 {
  263. e.Error(500, err, "有用户关联,禁止删除!")
  264. return
  265. }
  266. //数据权限检查
  267. //p := actions.GetPermissionFromContext(c)
  268. err = s.Remove(&req, nil)
  269. if err != nil {
  270. e.Error(500, err, err.Error())
  271. return
  272. }
  273. e.OK(req.GetId(), "删除成功")
  274. }
  275. // Enter 进入销售门店
  276. // @Summary 删除销售门店
  277. // @Description 删除销售门店
  278. // @Tags 销售门店
  279. // @Accept application/json
  280. // @Product application/json
  281. // @Param data body dto.StoreEnterReq true "body"
  282. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  283. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  284. // @Router /api/store/enter [post]
  285. // @Security Bearer
  286. func (e StoreController) Enter(c *gin.Context) {
  287. s := service.Store{}
  288. req := dto.StoreEnterReq{}
  289. err := e.MakeContext(c).
  290. MakeOrm().
  291. Bind(&req).
  292. MakeService(&s.Service).
  293. Errors
  294. if err != nil {
  295. e.Logger.Error(err)
  296. e.Error(500, err, err.Error())
  297. return
  298. }
  299. var object model.SysDept
  300. //数据权限检查
  301. err = s.Get(&dto.StoreGetReq{
  302. Id: req.Id,
  303. }, &object)
  304. if err != nil {
  305. e.Error(500, err, err.Error())
  306. return
  307. }
  308. err = e.Cache.Set(model.GetEnterDeptCacheKey(c), req.GetId(), int(config.JwtConfig.Timeout)+7200)
  309. err = e.Cache.Set(model.GetEnterDeptNameCacheKey(c), object.Name, int(config.JwtConfig.Timeout)+7200)
  310. if err != nil {
  311. e.Logger.Error(err)
  312. e.Error(500, err, "进入失败")
  313. return
  314. }
  315. e.OK(nil, "进入成功")
  316. }