sys_dict_data.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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" v:"required#回显样式不能为空"`
  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:"page"` //当前页码
  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. entity.CreateTime = gconv.Uint64(gtime.Timestamp())
  47. entity.CreateBy = userId
  48. result, err := entity.Insert()
  49. if err != nil {
  50. g.Log().Error(err)
  51. return 0, gerror.New("添加失败")
  52. }
  53. id, err := result.LastInsertId()
  54. if err != nil {
  55. g.Log().Error(err)
  56. return 0, gerror.New("添加失败")
  57. }
  58. return id, nil
  59. }
  60. //通过字典数据主键获取数据
  61. func GetById(dictCode int) (*Entity, error) {
  62. entity, err := Model.FindOne("dict_code", dictCode)
  63. if err != nil {
  64. g.Log().Error(err)
  65. return nil, gerror.New("获取字典数据失败")
  66. }
  67. if entity == nil {
  68. return nil, gerror.New("获取字典数据失败")
  69. }
  70. return entity, nil
  71. }
  72. //修改字典数据操作
  73. func EditSaveData(req *EditDataReq, userId int) (int64, error) {
  74. entity, err := GetById(req.DictCode)
  75. if err != nil {
  76. return 0, err
  77. }
  78. entity.DictType = req.DictType
  79. entity.Status = req.Status
  80. entity.DictLabel = req.DictLabel
  81. entity.CssClass = req.CssClass
  82. entity.DictSort = req.DictSort
  83. entity.DictValue = req.DictValue
  84. entity.IsDefault = req.IsDefault
  85. entity.ListClass = req.ListClass
  86. entity.Remark = req.Remark
  87. entity.UpdateTime = gconv.Uint64(gtime.Timestamp())
  88. entity.UpdateBy = userId
  89. result, err := entity.Update()
  90. if err != nil {
  91. g.Log().Error(err)
  92. return 0, gerror.New("修改失败")
  93. }
  94. return result.RowsAffected()
  95. }
  96. //字典数据列表查询分页
  97. func SelectDataListByPage(req *SelectDataPageReq) (total, page int, list []*Entity, err error) {
  98. model := Model
  99. if req != nil {
  100. if req.DictLabel != "" {
  101. model = model.Where("dict_label like ?", "%"+req.DictLabel+"%")
  102. }
  103. if req.Status != "" {
  104. model = model.Where("status = ", gconv.Int(req.Status))
  105. }
  106. if req.DictType != "" {
  107. model = model.Where("dict_type = ?", req.DictType)
  108. }
  109. total, err = model.Count()
  110. if err != nil {
  111. g.Log().Error(err)
  112. err = gerror.New("获取总行数失败")
  113. return
  114. }
  115. if req.PageNum == 0 {
  116. req.PageNum = 1
  117. }
  118. }
  119. page = req.PageNum
  120. if req.PageSize == 0 {
  121. req.PageSize = service.AdminPageNum
  122. }
  123. list, err = model.Page(page, req.PageSize).Order("dict_sort asc,dict_code asc").All()
  124. if err != nil {
  125. g.Log().Error(err)
  126. err = gerror.New("获取数据失败")
  127. return
  128. }
  129. return
  130. }
  131. //删除字典数据
  132. func DeleteByIds(ids []int) error {
  133. _, err := Model.Delete("dict_code in (?)", ids)
  134. if err != nil {
  135. g.Log().Error(err)
  136. return gerror.New("删除失败")
  137. }
  138. return nil
  139. }