sys_dict_data.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package sys_dict_data
  2. import (
  3. "gfast/library/service"
  4. "github.com/gogf/gf/errors/gerror"
  5. "github.com/gogf/gf/frame/g"
  6. "github.com/gogf/gf/os/gtime"
  7. "github.com/gogf/gf/util/gconv"
  8. )
  9. // Fill with you ideas below.
  10. //新增字典数据页面请求参数
  11. type AddDataReq struct {
  12. DictLabel string `p:"dictLabel" v:"required#字典标签不能为空"`
  13. DictValue string `p:"dictValue" v:"required#字典键值不能为空"`
  14. DictType string `p:"dictType" v:"required#字典类型不能为空"`
  15. DictSort int `p:"dictSort" v:"integer#排序只能为整数"`
  16. CssClass string `p:"cssClass"`
  17. ListClass string `p:"listClass"`
  18. IsDefault int `p:"isDefault" v:"required|in:0,1#系统默认不能为空|默认值只能为0或1"`
  19. Status int `p:"status" v:"required|in:0,1#状态不能为空|状态只能为0或1"`
  20. Remark string `p:"remark"`
  21. }
  22. type EditDataReq struct {
  23. DictCode int `p:"dictCode" v:"required|min:1#主键ID不能为空|主键ID不能小于1"`
  24. AddDataReq
  25. }
  26. //分页请求参数
  27. type SelectDataPageReq struct {
  28. DictType string `p:"dictType"` //字典类型
  29. DictLabel string `p:"dictLabel"` //字典标签
  30. Status string `p:"status"` //状态
  31. PageNum int `p:"pageNum"` //当前页码
  32. PageSize int `p:"pageSize"` //每页数
  33. }
  34. //添加字典数据操作
  35. func AddSaveData(req *AddDataReq, userId int) (int64, error) {
  36. var entity Entity
  37. entity.DictType = req.DictType
  38. entity.Status = req.Status
  39. entity.DictLabel = req.DictLabel
  40. entity.CssClass = req.CssClass
  41. entity.DictSort = req.DictSort
  42. entity.DictValue = req.DictValue
  43. entity.IsDefault = req.IsDefault
  44. entity.ListClass = req.ListClass
  45. entity.Remark = req.Remark
  46. time := gconv.Uint64(gtime.Timestamp())
  47. entity.CreateTime = time
  48. entity.UpdateTime = time
  49. entity.CreateBy = userId
  50. result, err := entity.Insert()
  51. if err != nil {
  52. g.Log().Error(err)
  53. return 0, gerror.New("添加失败")
  54. }
  55. id, err := result.LastInsertId()
  56. if err != nil {
  57. g.Log().Error(err)
  58. return 0, gerror.New("添加失败")
  59. }
  60. return id, nil
  61. }
  62. //通过字典数据主键获取数据
  63. func GetById(dictCode int) (*Entity, error) {
  64. entity, err := Model.FindOne("dict_code", dictCode)
  65. if err != nil {
  66. g.Log().Error(err)
  67. return nil, gerror.New("获取字典数据失败")
  68. }
  69. if entity == nil {
  70. return nil, gerror.New("获取字典数据失败")
  71. }
  72. return entity, nil
  73. }
  74. //修改字典数据操作
  75. func EditSaveData(req *EditDataReq, userId int) (int64, error) {
  76. entity, err := GetById(req.DictCode)
  77. if err != nil {
  78. return 0, err
  79. }
  80. entity.DictType = req.DictType
  81. entity.Status = req.Status
  82. entity.DictLabel = req.DictLabel
  83. entity.CssClass = req.CssClass
  84. entity.DictSort = req.DictSort
  85. entity.DictValue = req.DictValue
  86. entity.IsDefault = req.IsDefault
  87. entity.ListClass = req.ListClass
  88. entity.Remark = req.Remark
  89. entity.UpdateTime = gconv.Uint64(gtime.Timestamp())
  90. entity.UpdateBy = userId
  91. result, err := entity.Update()
  92. if err != nil {
  93. g.Log().Error(err)
  94. return 0, gerror.New("修改失败")
  95. }
  96. return result.RowsAffected()
  97. }
  98. //字典数据列表查询分页
  99. func SelectDataListByPage(req *SelectDataPageReq) (total, page int, list []*Entity, err error) {
  100. model := Model
  101. if req != nil {
  102. if req.DictLabel != "" {
  103. model = model.Where("dict_label like ?", "%"+req.DictLabel+"%")
  104. }
  105. if req.Status != "" {
  106. model = model.Where("status = ", gconv.Int(req.Status))
  107. }
  108. if req.DictType != "" {
  109. model = model.Where("dict_type = ?", req.DictType)
  110. }
  111. total, err = model.Count()
  112. if err != nil {
  113. g.Log().Error(err)
  114. err = gerror.New("获取总行数失败")
  115. return
  116. }
  117. if req.PageNum == 0 {
  118. req.PageNum = 1
  119. }
  120. }
  121. page = req.PageNum
  122. if req.PageSize == 0 {
  123. req.PageSize = service.AdminPageNum
  124. }
  125. list, err = model.Page(page, req.PageSize).Order("dict_sort asc,dict_code asc").All()
  126. if err != nil {
  127. g.Log().Error(err)
  128. err = gerror.New("获取数据失败")
  129. return
  130. }
  131. return
  132. }
  133. //删除字典数据
  134. func DeleteByIds(ids []int) error {
  135. _, err := Model.Delete("dict_code in (?)", ids)
  136. if err != nil {
  137. g.Log().Error(err)
  138. return gerror.New("删除失败")
  139. }
  140. return nil
  141. }