store.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. // GetAllPage2 获取配送门店列表(根公司下所有公司)
  135. // @Summary 获取配送门店列表
  136. // @Description 获取配送门店列表(进入公司后的公司列表)
  137. // @Tags 销售门店
  138. // @Param name query string false "销售门店名称"
  139. // @Success 200 {object} response.Response{data=response.Page{list=[]model.SysDept}} "{"code": 200, "data": [...]}"
  140. // @Router /api/store/all2 [get]
  141. // @Security Bearer
  142. func (e StoreController) GetAllPageByUser(c *gin.Context) {
  143. s := service.Store{}
  144. req := dto.StoreGetPageReq{}
  145. err := e.MakeContext(c).
  146. MakeOrm().
  147. Bind(&req, binding.Query).
  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 dept model.SysDept
  156. //数据权限检查
  157. err = s.Get(&dto.StoreGetReq{
  158. Id: user.GetDeptId(c),
  159. }, &dept)
  160. if err != nil {
  161. e.Error(500, err, err.Error())
  162. return
  163. }
  164. deptId := user.GetDeptId(c)
  165. deptIdList := strings.Split(strings.Trim(dept.Path, "/"), "/")
  166. if len(deptIdList) > 1 {
  167. deptId, _ = strconv.Atoi(deptIdList[1])
  168. }
  169. //数据权限检查
  170. p := actions.GetPermissionFromContext(c)
  171. req.DeptId = deptId
  172. req.Type = model.TypeStore
  173. list := make([]model.SysDept, 0)
  174. p.DeptId = deptId
  175. list, err = s.SetStorePage(&req, p)
  176. if err != nil {
  177. e.Error(500, err, err.Error())
  178. return
  179. }
  180. e.OK(list, "查询成功")
  181. }
  182. // Get 通过id获取销售门店
  183. // @Summary 通过id获取销售门店
  184. // @Description 通过id获取销售门店
  185. // @Tags 销售门店
  186. // @Param id path string true "销售门店id"
  187. // @Success 200 {object} response.Response{data=model.SysDept} "{"code": 200, "data": [...]}"
  188. // @Router /api/store/{id} [get]
  189. // @Security Bearer
  190. func (e StoreController) Get(c *gin.Context) {
  191. s := service.Store{}
  192. req := dto.StoreGetReq{}
  193. err := e.MakeContext(c).
  194. MakeOrm().
  195. Bind(&req, nil).
  196. MakeService(&s.Service).
  197. Errors
  198. if err != nil {
  199. e.Logger.Error(err)
  200. e.Error(500, err, err.Error())
  201. return
  202. }
  203. var object model.SysDept
  204. //数据权限检查
  205. err = s.Get(&req, &object)
  206. if err != nil {
  207. e.Error(500, err, err.Error())
  208. return
  209. }
  210. e.OK(object, "查询成功")
  211. }
  212. // Insert 添加销售门店
  213. // @Summary 添加销售门店
  214. // @Description 添加销售门店
  215. // @Tags 销售门店
  216. // @Accept application/json
  217. // @Product application/json
  218. // @Param data body dto.StoreInsertReq true "data"
  219. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  220. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  221. // @Router /api/store [post]
  222. // @Security Bearer
  223. func (e StoreController) Insert(c *gin.Context) {
  224. s := service.Store{}
  225. req := dto.StoreInsertReq{}
  226. err := e.MakeContext(c).
  227. MakeOrm().
  228. Bind(&req, binding.JSON).
  229. MakeService(&s.Service).
  230. Errors
  231. if err != nil {
  232. e.Logger.Error(err)
  233. e.Error(500, err, err.Error())
  234. return
  235. }
  236. // 设置创建人
  237. req.SetCreateBy(user.GetUserId(c))
  238. deptId := user.GetDeptId(c)
  239. err = s.Insert(&req, deptId)
  240. if err != nil {
  241. e.Error(500, err, err.Error())
  242. return
  243. }
  244. e.OK(req.GetId(), "创建成功")
  245. }
  246. // Update 修改销售门店
  247. // @Summary 修改销售门店
  248. // @Description 修改销售门店
  249. // @Tags 销售门店
  250. // @Accept application/json
  251. // @Product application/json
  252. // @Param data body dto.StoreUpdateReq true "body"
  253. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  254. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  255. // @Router /api/store [put]
  256. // @Security Bearer
  257. func (e StoreController) Update(c *gin.Context) {
  258. s := service.Store{}
  259. req := dto.StoreUpdateReq{}
  260. err := e.MakeContext(c).
  261. MakeOrm().
  262. Bind(&req).
  263. MakeService(&s.Service).
  264. Errors
  265. if err != nil {
  266. e.Logger.Error(err)
  267. e.Error(500, err, err.Error())
  268. return
  269. }
  270. req.SetUpdateBy(user.GetUserId(c))
  271. err = s.Update(&req)
  272. if err != nil {
  273. e.Error(500, err, err.Error())
  274. return
  275. }
  276. e.OK(req.GetId(), "更新成功")
  277. }
  278. // Delete 删除销售门店
  279. // @Summary 删除销售门店
  280. // @Description 删除销售门店
  281. // @Tags 销售门店
  282. // @Accept application/json
  283. // @Product application/json
  284. // @Param data body dto.StoreDeleteReq true "body"
  285. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  286. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  287. // @Router /api/store [delete]
  288. // @Security Bearer
  289. func (e StoreController) Delete(c *gin.Context) {
  290. s := service.Store{}
  291. req := dto.StoreDeleteReq{}
  292. userSvc := service.SysUser{}
  293. err := e.MakeContext(c).
  294. MakeOrm().
  295. Bind(&req, binding.JSON, nil).
  296. MakeService(&s.Service).
  297. MakeService(&userSvc.Service).
  298. Errors
  299. if err != nil {
  300. e.Logger.Error(err)
  301. e.Error(500, err, err.Error())
  302. return
  303. }
  304. var count int64
  305. err = userSvc.GetCount(&dto.SysUserGetCountReq{DeptIds: []int{req.Id}}, &count)
  306. if err != nil {
  307. e.Error(500, err, err.Error())
  308. return
  309. }
  310. if count > 0 {
  311. e.Error(500, err, "有用户关联,禁止删除!")
  312. return
  313. }
  314. //数据权限检查
  315. //p := actions.GetPermissionFromContext(c)
  316. err = s.Remove(&req, nil)
  317. if err != nil {
  318. e.Error(500, err, err.Error())
  319. return
  320. }
  321. e.OK(req.GetId(), "删除成功")
  322. }
  323. // Enter 进入销售门店
  324. // @Summary 删除销售门店
  325. // @Description 删除销售门店
  326. // @Tags 销售门店
  327. // @Accept application/json
  328. // @Product application/json
  329. // @Param data body dto.StoreEnterReq true "body"
  330. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  331. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  332. // @Router /api/store/enter [post]
  333. // @Security Bearer
  334. func (e StoreController) Enter(c *gin.Context) {
  335. s := service.Store{}
  336. req := dto.StoreEnterReq{}
  337. err := e.MakeContext(c).
  338. MakeOrm().
  339. Bind(&req).
  340. MakeService(&s.Service).
  341. Errors
  342. if err != nil {
  343. e.Logger.Error(err)
  344. e.Error(500, err, err.Error())
  345. return
  346. }
  347. var object model.SysDept
  348. //数据权限检查
  349. err = s.Get(&dto.StoreGetReq{
  350. Id: req.Id,
  351. }, &object)
  352. if err != nil {
  353. e.Error(500, err, err.Error())
  354. return
  355. }
  356. err = e.Cache.Set(model.GetEnterDeptCacheKey(c), req.GetId(), int(config.JwtConfig.Timeout)+7200)
  357. err = e.Cache.Set(model.GetEnterDeptNameCacheKey(c), object.Name, int(config.JwtConfig.Timeout)+7200)
  358. if err != nil {
  359. e.Logger.Error(err)
  360. e.Error(500, err, "进入失败")
  361. return
  362. }
  363. e.OK(nil, "进入成功")
  364. }