sys_dictionaries.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package system
  2. import (
  3. "fmt"
  4. "github.com/demdxx/gocast"
  5. "github.com/gin-gonic/gin"
  6. "go_server/model/common/response"
  7. system2 "go_server/model/system"
  8. "go_server/service/base"
  9. )
  10. type DictionaryService struct {
  11. base.SysCommonService
  12. }
  13. // 字典服务 增 删 改 查
  14. // 所有字典信息
  15. func (s *DictionaryService) DictionariesTree(c *gin.Context) {
  16. type Detail struct {
  17. DictionaryId int64 `json:"dictionaryId" gorm:"comment:字典ID"`
  18. Label string `json:"label" gorm:"type:varchar(50);comment:展示值"`
  19. Value string `json:"value" gorm:"type:varchar(50);comment:字典值"`
  20. Extend string `json:"extend" gorm:"type:varchar(100);comment:扩展值"`
  21. Sort int64 `json:"sort" gorm:"comment:排序"`
  22. }
  23. type DictionaryNode struct {
  24. Key string `json:"key"`
  25. Name string `json:"name"`
  26. Desc string `json:"desc"`
  27. Details []*Detail `json:"details"`
  28. }
  29. dictionaryMap := make(map[int64]*DictionaryNode)
  30. dictionaries, err := base.GetMore[system2.Dictionaries](s.DB(), "enable", true)
  31. if err != nil {
  32. response.Resp(c, err.Error())
  33. return
  34. }
  35. for _, item := range dictionaries {
  36. dictionaryMap[item.ID] = &DictionaryNode{
  37. Key: item.Key,
  38. Name: item.Name,
  39. Desc: item.Desc,
  40. Details: make([]*Detail, 0),
  41. }
  42. }
  43. dictionaryDetails, err := base.GetMore[system2.DictionaryDetail](s.DB().Order("sort"), "enable", true)
  44. if err != nil {
  45. response.Resp(c, err.Error())
  46. return
  47. }
  48. for _, item := range dictionaryDetails {
  49. if _, ok := dictionaryMap[item.DictionaryId]; ok {
  50. dictionaryMap[item.DictionaryId].Details = append(dictionaryMap[item.DictionaryId].Details, &Detail{
  51. DictionaryId: item.DictionaryId,
  52. Label: item.Label,
  53. Value: item.Value,
  54. Extend: item.Extend,
  55. Sort: item.Sort,
  56. })
  57. }
  58. }
  59. res := make(map[string]interface{})
  60. res["dictionaryMap"] = dictionaryMap
  61. response.Resp(c, res)
  62. return
  63. }
  64. // --finish
  65. func (s *DictionaryService) FindDictionaries(c *gin.Context) {
  66. type request[T any] struct {
  67. base.ListRequest[T]
  68. Id interface{} `form:"id"`
  69. Name string `json:"name" gorm:"column:name;type:varchar(50);comment:名称"`
  70. Key string `json:"key" gorm:"column:key;;unique;type:varchar(50);comment:关键词"`
  71. Enable interface{} `json:"enable" gorm:"column:enable;type:tinyint(1);comment:是否有效"`
  72. }
  73. req := new(request[system2.Dictionaries])
  74. if err := c.BindQuery(req); err != nil {
  75. response.Resp(c, err.Error())
  76. return
  77. }
  78. db := s.DB()
  79. if req.Id != nil && gocast.ToInt64(req.Id) != 0 {
  80. db = db.Where("id", req.Id)
  81. }
  82. if req.Name != "" {
  83. db = db.Where("label LIKE ?", "%"+req.Name+"%")
  84. }
  85. if req.Key != "" {
  86. db = db.Where("`key` LIKE ?", "%"+req.Key+"%")
  87. }
  88. if req.Enable != nil {
  89. db = db.Where("enable = ?", gocast.ToBool(req.Enable))
  90. }
  91. resp, err := base.NewQueryBaseHandler(system2.NewDictionaries()).List(db, req)
  92. if err != nil {
  93. response.Resp(c, err.Error())
  94. return
  95. }
  96. response.Resp(c, resp)
  97. }
  98. func (s *DictionaryService) CreateDictionary(c *gin.Context) {
  99. userId := c.GetInt64("userId")
  100. // 限制用户增加必须管理员才可以操作
  101. if userId != system2.AdminId {
  102. response.Resp(c, "不允许操作")
  103. return
  104. }
  105. type request struct {
  106. Name string `json:"name" gorm:"column:name;type:varchar(50);comment:名称"`
  107. Key string `json:"key" gorm:"column:key;;unique;type:varchar(50);comment:关键词"`
  108. Desc string `json:"desc" gorm:"column:desc;type:varchar(255);comment:说明"`
  109. }
  110. req := new(request)
  111. if err := c.BindJSON(req); err != nil {
  112. response.Resp(c, err.Error())
  113. return
  114. }
  115. if base.CountRow[system2.Dictionaries](s.DB(), "key", req.Key) > 0 {
  116. response.Resp(c, "key 不可重复")
  117. return
  118. }
  119. row := &system2.Dictionaries{
  120. Name: req.Name,
  121. Key: req.Key,
  122. Enable: true,
  123. Desc: req.Desc,
  124. }
  125. if err := s.DB().Create(&row).Error; err != nil {
  126. response.Resp(c, "添加失败")
  127. return
  128. }
  129. response.Resp(c)
  130. return
  131. }
  132. func (s *DictionaryService) UpdateDictionary(c *gin.Context) {
  133. userId := c.GetInt64("userId")
  134. // 限制用户增加必须管理员才可以操作
  135. if userId != system2.AdminId {
  136. response.Resp(c, "不允许操作")
  137. return
  138. }
  139. type request struct {
  140. Name string `json:"name" gorm:"column:name;type:varchar(50);comment:名称"`
  141. Key string `json:"key" gorm:"column:key;;unique;type:varchar(50);comment:关键词"`
  142. Enable interface{} `json:"enable"`
  143. Desc string `json:"desc" gorm:"column:desc;type:varchar(255);comment:说明"`
  144. }
  145. req := new(request)
  146. if err := c.BindJSON(req); err != nil {
  147. response.Resp(c, err.Error())
  148. return
  149. }
  150. if req.Key == "" || req.Name == "" {
  151. response.Resp(c, "key, name 必填")
  152. return
  153. }
  154. row, ok := base.GetOne[system2.Dictionaries](s.DB(), "key", req.Key)
  155. if !ok {
  156. response.Resp(c, fmt.Sprintf("字典:%s不存在", req.Key))
  157. return
  158. }
  159. row.Name = req.Name
  160. row.Desc = req.Desc
  161. row.Enable = gocast.ToBool(req.Enable)
  162. if err := s.DB().Save(&row).Error; err != nil {
  163. response.Resp(c, fmt.Sprintf("修改失败:%s", err.Error()))
  164. return
  165. }
  166. response.Resp(c)
  167. return
  168. }
  169. func (s *DictionaryService) DelDictionary(c *gin.Context) {
  170. userId := c.GetInt64("userId")
  171. // 限制用户增加必须管理员才可以操作
  172. if userId != system2.AdminId {
  173. response.Resp(c, "不允许操作")
  174. return
  175. }
  176. type request struct {
  177. Key string `json:"key" gorm:"column:key;;unique;type:varchar(50);comment:关键词"`
  178. }
  179. req := new(request)
  180. if err := c.BindJSON(req); err != nil {
  181. response.Resp(c, err.Error())
  182. return
  183. }
  184. row, ok := base.GetOne[system2.Dictionaries](s.DB(), "key", req.Key)
  185. if !ok {
  186. response.Resp(c, fmt.Sprintf("字典:%s不存在", req.Key))
  187. return
  188. }
  189. txDb := s.DB().Begin()
  190. if err := txDb.Delete(&row).Error; err != nil {
  191. response.Resp(c, "删除失败")
  192. txDb.Rollback()
  193. return
  194. }
  195. if err := txDb.
  196. Where("dictionary_id", row.ID).
  197. Delete(&system2.DictionaryDetail{}).Error; err != nil {
  198. response.Resp(c, "删除失败")
  199. txDb.Rollback()
  200. return
  201. }
  202. txDb.Commit()
  203. response.Resp(c)
  204. return
  205. }
  206. // --finish
  207. func (s *DictionaryService) FindDictionaryDetail(c *gin.Context) {
  208. type request[T any] struct {
  209. base.ListRequest[T]
  210. Id interface{} `form:"id"`
  211. Label string `json:"label" gorm:"type:varchar(50);comment:展示值"`
  212. Value string `json:"value" gorm:"type:varchar(50);comment:字典值"`
  213. Enable interface{} `json:"enable" gorm:"column:enable;type:tinyint(1);comment:是否有效"`
  214. }
  215. req := new(request[system2.DictionaryDetail])
  216. if err := c.BindQuery(req); err != nil {
  217. response.Resp(c, err.Error())
  218. return
  219. }
  220. db := s.DB()
  221. if req.Id != nil && gocast.ToInt64(req.Id) != 0 {
  222. db = db.Where("id", req.Id)
  223. }
  224. if req.Label != "" {
  225. db = db.Where("label LIKE ?", "%"+req.Label+"%")
  226. }
  227. if req.Value != "" {
  228. db = db.Where("value LIKE ?", "%"+req.Value+"%")
  229. }
  230. if req.Enable != nil {
  231. db = db.Where("enable = ?", gocast.ToBool(req.Enable))
  232. }
  233. resp, err := base.NewQueryBaseHandler(system2.NewDictionaryDetail()).List(db, req)
  234. if err != nil {
  235. response.Resp(c, err.Error())
  236. return
  237. }
  238. response.Resp(c, resp)
  239. }
  240. func (s *DictionaryService) CreateDictionaryDetail(c *gin.Context) {
  241. userId := c.GetInt64("userId")
  242. // 限制用户增加必须管理员才可以操作
  243. if userId != system2.AdminId {
  244. response.Resp(c, "不允许操作")
  245. return
  246. }
  247. type request struct {
  248. DictionaryId interface{} `json:"dictionaryId" gorm:"comment:字典ID"`
  249. Label string `json:"label" gorm:"type:varchar(50);comment:展示值"`
  250. Value string `json:"value" gorm:"type:varchar(50);comment:字典值"`
  251. Extend string `json:"extend" gorm:"type:varchar(100);comment:扩展值"`
  252. Enable interface{} `json:"enable" gorm:"column:enable;type:tinyint(1);comment:是否有效"`
  253. Sort interface{} `json:"sort" gorm:"comment:排序"`
  254. }
  255. req := new(request)
  256. if err := c.BindJSON(req); err != nil {
  257. response.Resp(c, err.Error())
  258. return
  259. }
  260. if req.DictionaryId == nil || req.Label == "" || req.Value == "" {
  261. response.Resp(c, "DictionaryId,Label,Value 必填")
  262. return
  263. }
  264. if base.CountRow[system2.Dictionaries](s.DB(), "id", req.DictionaryId) == 0 {
  265. response.Resp(c, "DictionaryId不存在")
  266. return
  267. }
  268. row := &system2.DictionaryDetail{
  269. DictionaryId: gocast.ToInt64(req.DictionaryId),
  270. Label: req.Label,
  271. Value: req.Value,
  272. Extend: req.Extend,
  273. Enable: gocast.ToBool(req.Enable),
  274. Sort: gocast.ToInt64(req.Sort),
  275. }
  276. if err := s.DB().Create(&row).Error; err != nil {
  277. response.Resp(c, "添加失败")
  278. return
  279. }
  280. response.Resp(c)
  281. return
  282. }
  283. func (s *DictionaryService) UpdateDictionaryDetail(c *gin.Context) {
  284. userId := c.GetInt64("userId")
  285. // 限制用户增加必须管理员才可以操作
  286. if err := s.CheckIsAdmin(userId); err != nil {
  287. response.Resp(c, err.Error())
  288. return
  289. }
  290. type request struct {
  291. Id interface{} `json:"id"`
  292. Label string `json:"label" gorm:"type:varchar(50);comment:展示值"`
  293. Value string `json:"value" gorm:"type:varchar(50);comment:字典值"`
  294. Extend string `json:"extend" gorm:"type:varchar(100);comment:扩展值"`
  295. Enable interface{} `json:"enable" gorm:"column:enable;type:tinyint(1);comment:是否有效"`
  296. Sort interface{} `json:"sort" gorm:"comment:排序"`
  297. }
  298. req := new(request)
  299. if err := c.BindJSON(req); err != nil {
  300. response.Resp(c, err.Error())
  301. return
  302. }
  303. if req.Id == nil || req.Id == 0 || req.Label == "" || req.Value == "" {
  304. response.Resp(c, "Id、label、value必填")
  305. return
  306. }
  307. row, ok := base.GetOne[system2.DictionaryDetail](s.DB(), "id", req.Id)
  308. if !ok {
  309. response.Resp(c, fmt.Sprintf("字典值:%s不存在", req.Id))
  310. return
  311. }
  312. row.Label = req.Label
  313. row.Value = req.Value
  314. row.Extend = req.Extend
  315. row.Sort = gocast.ToInt64(req.Sort)
  316. row.Enable = gocast.ToBool(req.Enable)
  317. if err := s.DB().Save(&row).Error; err != nil {
  318. response.Resp(c, fmt.Sprintf("修改失败:%s", err.Error()))
  319. return
  320. }
  321. response.Resp(c)
  322. return
  323. }
  324. func (s *DictionaryService) DelDictionaryDetail(c *gin.Context) {
  325. userId := c.GetInt64("userId")
  326. if err := s.CheckIsAdmin(userId); err != nil {
  327. response.Resp(c, err.Error())
  328. return
  329. }
  330. type request struct {
  331. Id interface{} `json:"id"`
  332. }
  333. req := new(request)
  334. if err := c.BindJSON(req); err != nil {
  335. response.Resp(c, err.Error())
  336. return
  337. }
  338. if req.Id == nil || gocast.ToInt64(req.Id) == 0 {
  339. response.Resp(c, "Id必填")
  340. return
  341. }
  342. row, ok := base.GetOne[system2.DictionaryDetail](s.DB(), "id", req.Id)
  343. if !ok {
  344. response.Resp(c, fmt.Sprintf("字典值:%s不存在", req.Id))
  345. return
  346. }
  347. if err := s.DB().Delete(&row).Error; err != nil {
  348. response.Resp(c, fmt.Sprintf("修改失败:%s", err.Error()))
  349. return
  350. }
  351. response.Resp(c)
  352. }