完成字典模块改造

This commit is contained in:
linwenxiang committed 2021-02-20 11:06:59 +08:00
1 parent e04b5559bd
commit 4979fd659f
21 files changed
+1176 -635

No files matched your search

-154
View File
@@ -1,154 +0,0 @@
package dict
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"go-admin/app/admin/models"
"go-admin/tools"
"go-admin/tools/app"
)
// @Summary 字典数据列表
// @Description 获取JSON
// @Tags 字典数据
// @Param status query string false "status"
// @Param dictCode query string false "dictCode"
// @Param dictType query string false "dictType"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/dict/data/list [get]
// @Security Bearer
func GetDictDataList(c *gin.Context) {
var data models.DictData
var err error
var pageSize = 10
var pageIndex = 1
if size := c.Request.FormValue("pageSize"); size != "" {
pageSize, err = tools.StringToInt(size)
}
if index := c.Request.FormValue("pageIndex"); index != "" {
pageIndex, err = tools.StringToInt(index)
}
data.DictLabel = c.Request.FormValue("dictLabel")
data.Status = c.Request.FormValue("status")
data.DictType = c.Request.FormValue("dictType")
id := c.Request.FormValue("dictCode")
data.DictCode, _ = tools.StringToInt(id)
data.DataScope = tools.GetUserIdStr(c)
result, count, err := data.GetPage(pageSize, pageIndex)
tools.HasError(err, "", -1)
var mp = make(map[string]interface{}, 3)
mp["list"] = result
mp["count"] = count
mp["pageIndex"] = pageIndex
mp["pageSize"] = pageSize
var res app.Response
res.Data = mp
c.JSON(http.StatusOK, res.ReturnOK())
}
// @Summary 通过编码获取字典数据
// @Description 获取JSON
// @Tags 字典数据
// @Param dictCode path int true "字典编码"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/dict/data/{dictCode} [get]
// @Security Bearer
func GetDictData(c *gin.Context) {
var DictData models.DictData
DictData.DictLabel = c.Request.FormValue("dictLabel")
DictData.DictCode, _ = tools.StringToInt(c.Param("dictCode"))
result, err := DictData.GetByCode()
tools.HasError(err, "抱歉未找到相关信息", -1)
var res app.Response
res.Data = result
c.JSON(http.StatusOK, res.ReturnOK())
}
// @Summary 通过字典类型获取字典数据
// @Description 获取JSON
// @Tags 字典数据
// @Param dictType path int true "dictType"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/dict/databyType/{dictType} [get]
// @Security Bearer
func GetDictDataByDictType(c *gin.Context) {
var DictData models.DictData
DictData.DictType = c.Param("dictType")
result, err := DictData.Get()
tools.HasError(err, "抱歉未找到相关信息", -1)
var res app.Response
res.Data = result
c.JSON(http.StatusOK, res.ReturnOK())
}
// @Summary 添加字典数据
// @Description 获取JSON
// @Tags 字典数据
// @Accept application/json
// @Product application/json
// @Param data body models.DictType true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /api/v1/dict/data [post]
// @Security Bearer
func InsertDictData(c *gin.Context) {
var data models.DictData
err := c.ShouldBindJSON(&data)
data.CreateBy = tools.GetUserIdStr(c)
tools.HasError(err, "", 500)
result, err := data.Create()
tools.HasError(err, "", -1)
var res app.Response
res.Data = result
c.JSON(http.StatusOK, res.ReturnOK())
}
// @Summary 修改字典数据
// @Description 获取JSON
// @Tags 字典数据
// @Accept application/json
// @Product application/json
// @Param data body models.DictType true "body"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /api/v1/dict/data [put]
// @Security Bearer
func UpdateDictData(c *gin.Context) {
var data models.DictData
err := c.BindWith(&data, binding.JSON)
data.UpdateBy = tools.GetUserIdStr(c)
tools.HasError(err, "", -1)
result, err := data.Update(data.DictCode)
tools.HasError(err, "", -1)
var res app.Response
res.Data = result
c.JSON(http.StatusOK, res.ReturnOK())
}
// @Summary 删除字典数据
// @Description 删除数据
// @Tags 字典数据
// @Param dictCode path int true "dictCode"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
// @Router /api/v1/dict/data/{dictCode} [delete]
func DeleteDictData(c *gin.Context) {
var data models.DictData
data.UpdateBy = tools.GetUserIdStr(c)
IDS := tools.IdsStrToIdsIntGroup("dictCode", c)
result, err := data.BatchDelete(IDS)
tools.HasError(err, "修改失败", 500)
app.OK(c, result, "删除成功")
}
-137
View File
@@ -1,137 +0,0 @@
package dict
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"go-admin/app/admin/models"
"go-admin/tools"
"go-admin/tools/app"
)
// @Summary 字典类型列表数据
// @Description 获取JSON
// @Tags 字典类型
// @Param dictName query string false "dictName"
// @Param dictId query string false "dictId"
// @Param dictType query string false "dictType"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/dict/type/list [get]
// @Security Bearer
func GetDictTypeList(c *gin.Context) {
var data models.DictType
var err error
var pageSize = 10
var pageIndex = 1
if size := c.Request.FormValue("pageSize"); size != "" {
pageSize, err = tools.StringToInt(size)
}
if index := c.Request.FormValue("pageIndex"); index != "" {
pageIndex, err = tools.StringToInt(index)
}
data.DictName = c.Request.FormValue("dictName")
id := c.Request.FormValue("dictId")
data.DictId, _ = tools.StringToInt(id)
data.DictType = c.Request.FormValue("dictType")
data.DataScope = tools.GetUserIdStr(c)
result, count, err := data.GetPage(pageSize, pageIndex)
tools.HasError(err, "", -1)
app.PageOK(c, result, count, pageIndex, pageSize, "")
}
// @Summary 通过字典id获取字典类型
// @Description 获取JSON
// @Tags 字典类型
// @Param dictId path int true "字典类型编码"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/dict/type/{dictId} [get]
// @Security Bearer
func GetDictType(c *gin.Context) {
var DictType models.DictType
DictType.DictName = c.Request.FormValue("dictName")
DictType.DictId, _ = tools.StringToInt(c.Param("dictId"))
result, err := DictType.Get()
tools.HasError(err, "抱歉未找到相关信息", -1)
var res app.Response
res.Data = result
c.JSON(http.StatusOK, res.ReturnOK())
}
func GetDictTypeOptionSelect(c *gin.Context) {
var DictType models.DictType
DictType.DictName = c.Request.FormValue("dictName")
DictType.DictId, _ = tools.StringToInt(c.Param("dictId"))
result, err := DictType.GetList()
tools.HasError(err, "抱歉未找到相关信息", -1)
var res app.Response
res.Data = result
c.JSON(http.StatusOK, res.ReturnOK())
}
// @Summary 添加字典类型
// @Description 获取JSON
// @Tags 字典类型
// @Accept application/json
// @Product application/json
// @Param data body models.DictType true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /api/v1/dict/type [post]
// @Security Bearer
func InsertDictType(c *gin.Context) {
var data models.DictType
err := c.BindWith(&data, binding.JSON)
data.CreateBy = tools.GetUserIdStr(c)
tools.HasError(err, "", 500)
result, err := data.Create()
tools.HasError(err, "", -1)
var res app.Response
res.Data = result
c.JSON(http.StatusOK, res.ReturnOK())
}
// @Summary 修改字典类型
// @Description 获取JSON
// @Tags 字典类型
// @Accept application/json
// @Product application/json
// @Param data body models.DictType true "body"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /api/v1/dict/type [put]
// @Security Bearer
func UpdateDictType(c *gin.Context) {
var data models.DictType
err := c.BindWith(&data, binding.JSON)
data.UpdateBy = tools.GetUserIdStr(c)
tools.HasError(err, "", -1)
result, err := data.Update(data.DictId)
tools.HasError(err, "", -1)
var res app.Response
res.Data = result
c.JSON(http.StatusOK, res.ReturnOK())
}
// @Summary 删除字典类型
// @Description 删除数据
// @Tags 字典类型
// @Param dictId path int true "dictId"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
// @Router /api/v1/dict/type/{dictId} [delete]
func DeleteDictType(c *gin.Context) {
var data models.DictType
data.UpdateBy = tools.GetUserIdStr(c)
IDS := tools.IdsStrToIdsIntGroup("dictId", c)
result, err := data.BatchDelete(IDS)
tools.HasError(err, "修改失败", 500)
app.OK(c, result, "删除成功")
}
+253
View File
@@ -0,0 +1,253 @@
package dict
import (
"net/http"
"github.com/gin-gonic/gin"
"go-admin/app/admin/models/system"
"go-admin/app/admin/service"
"go-admin/app/admin/service/dto"
"go-admin/common/apis"
"go-admin/common/log"
common "go-admin/common/models"
"go-admin/tools"
)
type SysDictData struct {
apis.Api
}
// @Summary 字典数据列表
// @Description 获取JSON
// @Tags 字典数据
// @Param status query string false "status"
// @Param dictCode query string false "dictCode"
// @Param dictType query string false "dictType"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/dict/data [get]
// @Security Bearer
func (e *SysDictData) GetSysDictDataList(c *gin.Context) {
msgID := tools.GenerateMsgIDFromContext(c)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
req := &dto.SysDictDataSearch{}
//查询列表
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
list := make([]system.SysDictData, 0)
var count int64
s := service.SysDictData{}
s.MsgID = msgID
s.Orm = db.Debug()
err = s.GetPage(req, &list, &count)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "查询失败")
return
}
e.PageOK(c, list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}
// @Summary 通过编码获取字典数据
// @Description 获取JSON
// @Tags 字典数据
// @Param dictCode path int true "字典编码"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/dict/data/{dictCode} [get]
// @Security Bearer
func (e *SysDictData) GetSysDictData(c *gin.Context) {
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
//查看详情
req := &dto.SysDictDataById{}
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object system.SysDictData
s := service.SysDictData{}
s.MsgID = msgID
s.Orm = db
err = s.Get(req, &object)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "查询失败")
return
}
e.OK(c, object, "查看成功")
}
// @Summary 添加字典数据
// @Description 获取JSON
// @Tags 字典数据
// @Accept application/json
// @Product application/json
// @Param data body system.DictType true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /api/v1/dict/data [post]
// @Security Bearer
func (e *SysDictData) InsertSysDictData(c *gin.Context) {
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
//新增操作
req := &dto.SysDictDataControl{}
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
object, _ := req.GenerateM()
// 设置创建人
object.SetCreateBy(tools.GetUserId(c))
s := service.SysDictData{}
s.Orm = db
s.MsgID = msgID
err = s.Insert(object.(*system.SysDictData))
if err != nil {
log.Error(err)
e.Error(c, http.StatusInternalServerError, err, "创建失败")
return
}
e.OK(c, object.GetId(), "创建成功")
}
// @Summary 修改字典数据
// @Description 获取JSON
// @Tags 字典数据
// @Accept application/json
// @Product application/json
// @Param data body models.DictType true "body"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /api/v1/dict/data/{dictCode} [put]
// @Security Bearer
func (e *SysDictData) UpdateSysDictData(c *gin.Context) {
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
req := &dto.SysDictDataControl{}
//更新操作
err = req.Bind(c)
if err != nil {
log.Errorf("msgID[%s] request validate error, %s", msgID, err.Error())
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
object, _ := req.GenerateM()
object.SetUpdateBy(tools.GetUserId(c))
s := service.SysDictData{}
s.Orm = db
s.MsgID = msgID
err = s.Update(object.(*system.SysDictData))
if err != nil {
log.Error(err)
return
}
e.OK(c, object.GetId(), "更新成功")
}
// @Summary 删除字典数据
// @Description 删除数据
// @Tags 字典数据
// @Param dictCode path int true "dictCode"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
// @Router /api/v1/dict/data/{dictCode} [delete]
func (e *SysDictData) DeleteSysDictData(c *gin.Context) {
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
//删除操作
req := new(dto.SysDictDataById)
err = req.Bind(c)
if err != nil {
log.Errorf("MsgID[%s] Bind error: %s", msgID, err)
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object common.ActiveRecord
object, err = req.GenerateM()
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
// 设置编辑人
object.SetUpdateBy(tools.GetUserId(c))
s := service.SysDictData{}
s.Orm = db
s.MsgID = msgID
err = s.Remove(req, object.(*system.SysDictData))
if err != nil {
log.Error(err)
return
}
e.OK(c, object.GetId(), "删除成功")
}
func (e *SysDictData) GetSysDictDataAll(c *gin.Context) {
msgID := tools.GenerateMsgIDFromContext(c)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
req := &dto.SysDictDataSearch{}
//查询列表
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
list := make([]system.SysDictData, 0)
s := service.SysDictData{}
s.MsgID = msgID
s.Orm = db
err = s.GetAll(req, &list)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "查询失败")
return
}
e.OK(c, list, "查询成功")
}
+261
View File
@@ -0,0 +1,261 @@
package dict
import (
"net/http"
"github.com/gin-gonic/gin"
"go-admin/app/admin/models/system"
"go-admin/app/admin/service"
"go-admin/app/admin/service/dto"
"go-admin/common/apis"
"go-admin/common/log"
common "go-admin/common/models"
"go-admin/tools"
)
type SysDictType struct {
apis.Api
}
// @Summary 字典类型列表数据
// @Description 获取JSON
// @Tags 字典类型
// @Param dictName query string false "dictName"
// @Param dictId query string false "dictId"
// @Param dictType query string false "dictType"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/dict/type [get]
// @Security Bearer
func (e *SysDictType) GetSysDictTypeList(c *gin.Context) {
msgID := tools.GenerateMsgIDFromContext(c)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
req := &dto.SysDictTypeSearch{}
//查询列表
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
list := make([]system.SysDictType, 0)
var count int64
s := service.SysDictType{}
s.MsgID = msgID
s.Orm = db.Debug()
err = s.GetPage(req, &list, &count)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "查询失败")
return
}
e.PageOK(c, list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}
// @Summary 通过字典id获取字典类型
// @Description 获取JSON
// @Tags 字典类型
// @Param dictId path int true "字典类型编码"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/dict/type/{dictId} [get]
// @Security Bearer
func (e *SysDictType) GetSysDictType(c *gin.Context) {
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
//查看详情
req := &dto.SysDictTypeById{}
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object system.SysDictType
s := service.SysDictType{}
s.MsgID = msgID
s.Orm = db
err = s.Get(req, &object)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "查询失败")
return
}
e.OK(c, object, "查看成功")
}
// @Summary 添加字典类型
// @Description 获取JSON
// @Tags 字典类型
// @Accept application/json
// @Product application/json
// @Param data body models.DictType true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /api/v1/dict/type [post]
// @Security Bearer
func (e *SysDictType) InsertSysDictType(c *gin.Context) {
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
//新增操作
req := &dto.SysDictTypeControl{}
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
object, _ := req.GenerateM()
// 设置创建人
object.SetCreateBy(tools.GetUserId(c))
s := service.SysDictType{}
s.Orm = db
s.MsgID = msgID
err = s.Insert(object.(*system.SysDictType))
if err != nil {
log.Error(err)
e.Error(c, http.StatusInternalServerError, err, "创建失败")
return
}
e.OK(c, object.GetId(), "创建成功")
}
// @Summary 修改字典类型
// @Description 获取JSON
// @Tags 字典类型
// @Accept application/json
// @Product application/json
// @Param data body models.DictType true "body"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /api/v1/dict/type/{dictId} [put]
// @Security Bearer
func (e *SysDictType) UpdateSysDictType(c *gin.Context) {
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
req := &dto.SysDictTypeControl{}
//更新操作
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
object, _ := req.GenerateM()
object.SetUpdateBy(tools.GetUserId(c))
s := service.SysDictType{}
s.Orm = db
s.MsgID = msgID
err = s.Update(object.(*system.SysDictType))
if err != nil {
log.Error(err)
return
}
e.OK(c, object.GetId(), "更新成功")
}
// @Summary 删除字典类型
// @Description 删除数据
// @Tags 字典类型
// @Param dictId path int true "dictId"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
// @Router /api/v1/dict/type/{dictId} [delete]
func (e *SysDictType) DeleteSysDictType(c *gin.Context) {
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
//删除操作
req := new(dto.SysDictTypeById)
err = req.Bind(c)
if err != nil {
log.Errorf("MsgID[%s] Bind error: %s", msgID, err)
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object common.ActiveRecord
object, err = req.GenerateM()
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
// 设置编辑人
object.SetUpdateBy(tools.GetUserId(c))
s := service.SysDictType{}
s.Orm = db
s.MsgID = msgID
err = s.Remove(req, object.(*system.SysDictType))
if err != nil {
log.Error(err)
return
}
e.OK(c, object.GetId(), "删除成功")
}
// @Summary 字典类型全部数据
// @Description 获取JSON
// @Tags 字典类型
// @Param dictName query string false "dictName"
// @Param dictId query string false "dictId"
// @Param dictType query string false "dictType"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/dict/type-option-select [get]
// @Security Bearer
func (e *SysDictType) GetSysDictTypeAll(c *gin.Context) {
msgID := tools.GenerateMsgIDFromContext(c)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
req := &dto.SysDictTypeSearch{}
//查询列表
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
list := make([]system.SysDictType, 0)
s := service.SysDictType{}
s.MsgID = msgID
s.Orm = db
err = s.GetAll(req, &list)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "查询失败")
return
}
e.OK(c, list, "查询成功")
}
-168
View File
@@ -1,168 +0,0 @@
package models
import (
"errors"
orm "go-admin/common/global"
"go-admin/tools"
)
type DictData struct {
DictCode int `gorm:"primary_key;auto_increment;" json:"dictCode" example:"1"` //字典编码
DictSort int `gorm:"" json:"dictSort"` //显示顺序
DictLabel string `gorm:"size:128;" json:"dictLabel"` //数据标签
DictValue string `gorm:"size:255;" json:"dictValue"` //数据键值
DictType string `gorm:"size:64;" json:"dictType"` //字典类型
CssClass string `gorm:"size:128;" json:"cssClass"` //
ListClass string `gorm:"size:128;" json:"listClass"` //
IsDefault string `gorm:"size:8;" json:"isDefault"` //
Status string `gorm:"size:4;" json:"status"` //状态
Default string `gorm:"size:8;" json:"default"` //
CreateBy string `gorm:"size:64;" json:"createBy"` //
UpdateBy string `gorm:"size:64;" json:"updateBy"` //
Remark string `gorm:"size:255;" json:"remark"` //备注
BaseModel
Params string `gorm:"-" json:"params"`
DataScope string `gorm:"-" json:"dataScope"`
}
func (DictData) TableName() string {
return "sys_dict_data"
}
func (e *DictData) Create() (DictData, error) {
var doc DictData
var i int64
if err := orm.Eloquent.Table(e.TableName()).
Where("dict_type = ?", e.DictType).
Where("dict_label=? or (dict_label=? and dict_value = ?)", e.DictLabel, e.DictLabel, e.DictValue).
Count(&i).Error; err != nil {
return doc, err
}
if i > 0 {
return doc, errors.New("字典标签或者字典键值已经存在!")
}
result := orm.Eloquent.Table(e.TableName()).Create(&e)
if result.Error != nil {
err := result.Error
return doc, err
}
doc = *e
return doc, nil
}
func (e *DictData) GetByCode() (DictData, error) {
var doc DictData
table := orm.Eloquent.Table(e.TableName())
if e.DictCode != 0 {
table = table.Where("dict_code = ?", e.DictCode)
}
if e.DictLabel != "" {
table = table.Where("dict_label = ?", e.DictLabel)
}
if e.DictType != "" {
table = table.Where("dict_type = ?", e.DictType)
}
if err := table.First(&doc).Error; err != nil {
return doc, err
}
return doc, nil
}
func (e *DictData) Get() ([]DictData, error) {
var doc []DictData
table := orm.Eloquent.Table(e.TableName())
if e.DictCode != 0 {
table = table.Where("dict_code = ?", e.DictCode)
}
if e.DictLabel != "" {
table = table.Where("dict_label = ?", e.DictLabel)
}
if e.DictType != "" {
table = table.Where("dict_type = ?", e.DictType)
}
table = table.Where("status = ?", 2)
if err := table.Order("dict_sort").Find(&doc).Error; err != nil {
return doc, err
}
return doc, nil
}
func (e *DictData) GetPage(pageSize int, pageIndex int) ([]DictData, int, error) {
var doc []DictData
table := orm.Eloquent.Table(e.TableName())
if e.DictCode != 0 {
table = table.Where("dict_code = ?", e.DictCode)
}
if e.DictType != "" {
table = table.Where("dict_type = ?", e.DictType)
}
if e.DictLabel != "" {
table = table.Where("dict_label = ?", e.DictLabel)
}
if e.Status != "" {
table = table.Where("status = ?", e.Status)
}
// 数据权限控制
dataPermission := new(DataPermission)
dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
table, err := dataPermission.GetDataScope("sys_dict_data", table)
if err != nil {
return nil, 0, err
}
var count int64
if err := table.Order("dict_sort").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
return nil, 0, err
}
//table.Where("`deleted_at` IS NULL").Count(&count)
return doc, int(count), nil
}
func (e *DictData) Update(id int) (update DictData, err error) {
if err = orm.Eloquent.Table(e.TableName()).Where("dict_code = ?", id).First(&update).Error; err != nil {
return
}
if e.DictLabel != "" && e.DictLabel != update.DictLabel {
return update, errors.New("标签不允许修改!")
}
if e.DictValue != "" && e.DictValue != update.DictValue {
return update, errors.New("键值不允许修改!")
}
//参数1:是要修改的数据
//参数2:是修改的数据
if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
return
}
return
}
func (e *DictData) Delete(id int) (success bool, err error) {
if err = orm.Eloquent.Table(e.TableName()).Where("dict_code = ?", id).Delete(&DictData{}).Error; err != nil {
success = false
return
}
success = true
return
}
func (e *DictData) BatchDelete(id []int) (Result bool, err error) {
if err = orm.Eloquent.Table(e.TableName()).Where("dict_code in (?)", id).Delete(&DictData{}).Error; err != nil {
return
}
Result = true
return
}
-152
View File
@@ -1,152 +0,0 @@
package models
import (
"errors"
"gorm.io/gorm"
orm "go-admin/common/global"
"go-admin/tools"
)
type DictType struct {
DictId int `gorm:"primary_key;auto_increment;" json:"dictId"`
DictName string `gorm:"size:128;" json:"dictName"` //字典名称
DictType string `gorm:"size:128;" json:"dictType"` //字典类型
Status string `gorm:"size:4;" json:"status"` //状态
CreateBy string `gorm:"size:11;" json:"createBy"` //创建者
UpdateBy string `gorm:"size:11;" json:"updateBy"` //更新者
Remark string `gorm:"size:255;" json:"remark"` //备注
BaseModel
DataScope string `gorm:"-" json:"dataScope"` //
Params string `gorm:"-" json:"params"` //
}
func (DictType) TableName() string {
return "sys_dict_type"
}
func (e *DictType) Create() (DictType, error) {
var doc DictType
var i int64
orm.Eloquent.Table(e.TableName()).Where("dict_name=? or dict_type = ?", e.DictName, e.DictType).Count(&i)
if i > 0 {
return doc, errors.New("字典名称或者字典类型已经存在!")
}
result := orm.Eloquent.Table(e.TableName()).Create(&e)
if result.Error != nil {
err := result.Error
return doc, err
}
doc = *e
return doc, nil
}
func (e *DictType) Get() (DictType, error) {
var doc DictType
table := orm.Eloquent.Table(e.TableName())
if e.DictId != 0 {
table = table.Where("dict_id = ?", e.DictId)
}
if e.DictName != "" {
table = table.Where("dict_name = ?", e.DictName)
}
if e.DictType != "" {
table = table.Where("dict_type = ?", e.DictType)
}
if err := table.First(&doc).Error; err != nil {
return doc, err
}
return doc, nil
}
func (e *DictType) GetList() ([]DictType, error) {
var doc []DictType
table := orm.Eloquent.Table(e.TableName())
if e.DictId != 0 {
table = table.Where("dict_id = ?", e.DictId)
}
if e.DictName != "" {
table = table.Where("dict_name = ?", e.DictName)
}
if e.DictType != "" {
table = table.Where("dict_type = ?", e.DictType)
}
if err := table.Find(&doc).Error; err != nil {
return doc, err
}
return doc, nil
}
func (e *DictType) GetPage(pageSize int, pageIndex int) ([]DictType, int, error) {
var doc []DictType
var db *gorm.DB
table := orm.Eloquent.Table(e.TableName())
if e.DictId != 0 {
db = db.Where("dict_id = ?", e.DictId)
}
if e.DictName != "" {
db = db.Where("dict_name = ?", e.DictName)
}
// 数据权限控制
dataPermission := new(DataPermission)
dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
table, err := dataPermission.GetDataScope("sys_dict_type", table)
if err != nil {
return nil, 0, err
}
var count int64
if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
return nil, 0, err
}
//table.Count(&count)
return doc, int(count), nil
}
func (e *DictType) Update(id int) (update DictType, err error) {
if err = orm.Eloquent.Table(e.TableName()).First(&update, id).Error; err != nil {
return
}
if e.DictName != "" && e.DictName != update.DictName {
return update, errors.New("名称不允许修改!")
}
if e.DictType != "" && e.DictType != update.DictType {
return update, errors.New("类型不允许修改!")
}
//参数1:是要修改的数据
//参数2:是修改的数据
if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
return
}
return
}
func (e *DictType) Delete(id int) (success bool, err error) {
if err = orm.Eloquent.Table(e.TableName()).Where("dict_id = ?", id).Delete(&DictData{}).Error; err != nil {
success = false
return
}
success = true
return
}
func (e *DictType) BatchDelete(id []int) (Result bool, err error) {
if err = orm.Eloquent.Table(e.TableName()).Where("dict_id in (?)", id).Delete(&DictType{}).Error; err != nil {
return
}
Result = true
return
}
+35
View File
@@ -0,0 +1,35 @@
package system
import (
"go-admin/common/models"
)
type SysDictData struct {
models.ControlBy
models.ModelTime
DictCode int `json:"dictCode" gorm:"primaryKey;column:dict_code;autoIncrement;comment:主键编码"`
DictSort int `json:"dictSort" gorm:"type:bigint(20);comment:DictSort"`
DictLabel string `json:"dictLabel" gorm:"type:varchar(128);comment:DictLabel"`
DictValue string `json:"dictValue" gorm:"type:varchar(255);comment:DictValue"`
DictType string `json:"dictType" gorm:"type:varchar(64);comment:DictType"`
CssClass string `json:"cssClass" gorm:"type:varchar(128);comment:CssClass"`
ListClass string `json:"listClass" gorm:"type:varchar(128);comment:ListClass"`
IsDefault string `json:"isDefault" gorm:"type:varchar(8);comment:IsDefault"`
Status string `json:"status" gorm:"type:varchar(4);comment:Status"`
Default string `json:"default" gorm:"type:varchar(8);comment:Default"`
Remark string `json:"remark" gorm:"type:varchar(255);comment:Remark"`
}
func (SysDictData) TableName() string {
return "sys_dict_data"
}
func (e *SysDictData) Generate() models.ActiveRecord {
o := *e
return &o
}
func (e *SysDictData) GetId() interface{} {
return e.DictCode
}
+29
View File
@@ -0,0 +1,29 @@
package system
import (
"go-admin/common/models"
)
type SysDictType struct {
models.ControlBy
models.ModelTime
ID int `json:"id" gorm:"primaryKey;column:dict_id;autoIncrement;comment:主键编码"`
DictName string `json:"dictName" gorm:"type:varchar(128);comment:DictName"`
DictType string `json:"dictType" gorm:"type:varchar(128);comment:DictType"`
Status string `json:"status" gorm:"type:varchar(4);comment:Status"`
Remark string `json:"remark" gorm:"type:varchar(255);comment:Remark"`
}
func (SysDictType) TableName() string {
return "sys_dict_type"
}
func (e *SysDictType) Generate() models.ActiveRecord {
o := *e
return &o
}
func (e *SysDictType) GetId() interface{} {
return e.ID
}
+14 -12
View File
@@ -63,7 +63,6 @@ func sysNoCheckRoleRouter(r *gin.RouterGroup) {
v1.GET("/gen/todb/:tableId", GenMenuAndApi)
v1.GET("/gen/tabletree", GetSysTablesTree)
v1.GET("/menuTreeselect", system.GetMenuTreeelect)
v1.GET("/dict/databytype/:dictType", dict.GetDictDataByDictType)
registerDBRouter(v1)
registerSysTableRouter(v1)
@@ -191,21 +190,24 @@ func registerSysUserRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddle
}
func registerDictRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
dictApi := &dict.SysDictType{}
dataApi := &dict.SysDictData{}
dicts := v1.Group("/dict").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
{
dicts.GET("/datalist", dict.GetDictDataList)
dicts.GET("/typelist", dict.GetDictTypeList)
dicts.GET("/typeoptionselect", dict.GetDictTypeOptionSelect)
dicts.GET("/data/:dictCode", dict.GetDictData)
dicts.POST("/data", dict.InsertDictData)
dicts.PUT("/data/", dict.UpdateDictData)
dicts.DELETE("/data/:dictCode", dict.DeleteDictData)
dicts.GET("/data-all", dataApi.GetSysDictDataAll)
dicts.GET("/data", dataApi.GetSysDictDataList)
dicts.GET("/data/:dictCode", dataApi.GetSysDictData)
dicts.POST("/data", dataApi.InsertSysDictData)
dicts.PUT("/data/:dictCode", dataApi.UpdateSysDictData)
dicts.DELETE("/data/:dictCode", dataApi.DeleteSysDictData)
dicts.GET("/type/:dictId", dict.GetDictType)
dicts.POST("/type", dict.InsertDictType)
dicts.PUT("/type", dict.UpdateDictType)
dicts.DELETE("/type/:dictId", dict.DeleteDictType)
dicts.GET("/type-option-select", dictApi.GetSysDictTypeAll)
dicts.GET("/type", dictApi.GetSysDictTypeList)
dicts.GET("/type/:id", dictApi.GetSysDictType)
dicts.POST("/type", dictApi.InsertSysDictType)
dicts.PUT("/type/:id", dictApi.UpdateSysDictType)
dicts.DELETE("/type/:id", dictApi.DeleteSysDictType)
}
}
+126
View File
@@ -0,0 +1,126 @@
package dto
import (
"github.com/gin-gonic/gin"
"net/http"
"go-admin/app/admin/models/system"
"go-admin/common/dto"
"go-admin/common/log"
common "go-admin/common/models"
"go-admin/tools"
)
type SysDictDataSearch struct {
dto.Pagination `search:"-"`
Id int `form:"id" search:"type:exact;column:dict_code;table:sys_dict_data" comment:""`
DictLabel string `form:"dictLabel" search:"type:contains;column:dict_label;table:sys_dict_data" comment:""`
DictValue string `form:"dictValue" search:"type:contains;column:dict_value;table:sys_dict_data" comment:""`
DictType string `form:"dictType" search:"type:contains;column:dict_type;table:sys_dict_data" comment:""`
Status string `form:"status" search:"type:exact;column:status;table:sys_dict_data" comment:""`
}
func (m *SysDictDataSearch) GetNeedSearch() interface{} {
return *m
}
func (m *SysDictDataSearch) Bind(ctx *gin.Context) error {
msgID := tools.GenerateMsgIDFromContext(ctx)
err := ctx.ShouldBind(m)
if err != nil {
log.Debugf("MsgID[%s] ShouldBind error: %s", msgID, err.Error())
}
return err
}
func (m *SysDictDataSearch) Generate() dto.Index {
o := *m
return &o
}
type SysDictDataControl struct {
Id int `uri:"dictCode" comment:""`
DictSort int `json:"dictSort" comment:""`
DictLabel string `json:"dictLabel" comment:""`
DictValue string `json:"dictValue" comment:""`
DictType string `json:"dictType" comment:""`
CssClass string `json:"cssClass" comment:""`
ListClass string `json:"listClass" comment:""`
IsDefault string `json:"isDefault" comment:""`
Status string `json:"status" comment:""`
Default string `json:"default" comment:""`
Remark string `json:"remark" comment:""`
}
func (s *SysDictDataControl) Bind(ctx *gin.Context) error {
msgID := tools.GenerateMsgIDFromContext(ctx)
err := ctx.ShouldBindUri(s)
if err != nil {
log.Debugf("MsgID[%s] ShouldBindUri error: %s", msgID, err.Error())
return err
}
err = ctx.ShouldBind(s)
if err != nil {
log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error())
}
return err
}
func (s *SysDictDataControl) Generate() dto.Control {
cp := *s
return &cp
}
func (s *SysDictDataControl) GenerateM() (common.ActiveRecord, error) {
return &system.SysDictData{
DictCode: s.Id,
DictSort: s.DictSort,
DictLabel: s.DictLabel,
DictValue: s.DictValue,
DictType: s.DictType,
CssClass: s.CssClass,
ListClass: s.ListClass,
IsDefault: s.IsDefault,
Status: s.Status,
Default: s.Default,
Remark: s.Remark,
}, nil
}
func (s *SysDictDataControl) GetId() interface{} {
return s.Id
}
type SysDictDataById struct {
Id int `uri:"dictCode"`
Ids []int `json:"ids"`
}
func (s *SysDictDataById) Bind(ctx *gin.Context) error {
if ctx.Request.Method == http.MethodDelete {
err := ctx.ShouldBind(&s.Ids)
if err != nil {
return err
}
if len(s.Ids) > 0 {
return nil
}
}
return ctx.ShouldBindUri(s)
}
func (s *SysDictDataById) GetId() interface{} {
if len(s.Ids) > 0 {
return s.Ids
}
return s.Id
}
func (s *SysDictDataById) Generate() dto.Control {
cp := *s
return &cp
}
func (s *SysDictDataById) GenerateM() (common.ActiveRecord, error) {
return &system.SysDictData{}, nil
}
+96
View File
@@ -0,0 +1,96 @@
package dto
import (
"github.com/gin-gonic/gin"
"go-admin/app/admin/models/system"
"go-admin/common/dto"
"go-admin/common/log"
common "go-admin/common/models"
"go-admin/tools"
)
type SysDictTypeSearch struct {
dto.Pagination `search:"-"`
DictId []int `form:"dictId" search:"type:in;column:dict_id;table:sys_dict_type"`
DictName string `form:"dictName" search:"type:icontains;column:dict_name;table:sys_dict_type"`
DictType string `form:"dictType" search:"type:icontains;column:dict_type;table:sys_dict_type"`
Status int `form:"status" search:"type:exact;column:status;table:sys_dict_type"`
}
func (m *SysDictTypeSearch) GetNeedSearch() interface{} {
return *m
}
func (m *SysDictTypeSearch) Bind(ctx *gin.Context) error {
msgID := tools.GenerateMsgIDFromContext(ctx)
err := ctx.ShouldBind(m)
if err != nil {
log.Debugf("MsgID[%s] ShouldBind error: %s", msgID, err.Error())
}
return err
}
func (m *SysDictTypeSearch) Generate() dto.Index {
o := *m
return &o
}
type SysDictTypeControl struct {
Id int `uri:"id" comment:""` //
DictName string `json:"dictName" comment:""`
DictType string `json:"dictType" comment:""`
Status string `json:"status" comment:""`
Remark string `json:"remark" comment:""`
}
func (s *SysDictTypeControl) Bind(ctx *gin.Context) error {
msgID := tools.GenerateMsgIDFromContext(ctx)
err := ctx.ShouldBindUri(s)
if err != nil {
log.Debugf("MsgID[%s] ShouldBindUri error: %s", msgID, err.Error())
return err
}
err = ctx.ShouldBind(s)
if err != nil {
log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error())
}
return err
}
func (s *SysDictTypeControl) Generate() dto.Control {
cp := *s
return &cp
}
func (s *SysDictTypeControl) GenerateM() (common.ActiveRecord, error) {
return &system.SysDictType{
ID: s.Id,
DictName: s.DictName,
DictType: s.DictType,
Status: s.Status,
Remark: s.Remark,
}, nil
}
func (s *SysDictTypeControl) GetId() interface{} {
return s.Id
}
type SysDictTypeById struct {
dto.ObjectById
}
func (s *SysDictTypeById) Generate() dto.Control {
cp := *s
return &cp
}
func (s *SysDictTypeById) GenerateM() (common.ActiveRecord, error) {
return &system.SysDictType{}, nil
}
+130
View File
@@ -0,0 +1,130 @@
package service
import (
"errors"
"gorm.io/gorm"
"go-admin/app/admin/models/system"
"go-admin/app/admin/service/dto"
cDto "go-admin/common/dto"
"go-admin/common/log"
"go-admin/common/service"
)
type SysDictData struct {
service.Service
}
// GetPage 获取列表
func (e *SysDictData) GetPage(c *dto.SysDictDataSearch, list *[]system.SysDictData, count *int64) error {
var err error
var data system.SysDictData
msgID := e.MsgID
err = e.Orm.Model(&data).
Scopes(
cDto.MakeCondition(c.GetNeedSearch()),
cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
).
Find(list).Limit(-1).Offset(-1).
Count(count).Error
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// Get 获取对象
func (e *SysDictData) Get(d *dto.SysDictDataById, model *system.SysDictData) error {
var err error
var data system.SysDictData
msgID := e.MsgID
db := e.Orm.Model(&data).
First(model, d.GetId())
err = db.Error
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
err = errors.New("查看对象不存在或无权查看")
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
if db.Error != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// Insert 创建对象
func (e *SysDictData) Insert(model *system.SysDictData) error {
var err error
var data system.SysDictData
msgID := e.MsgID
err = e.Orm.Model(&data).
Create(model).Error
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// Update 修改对象
func (e *SysDictData) Update(c *system.SysDictData) error {
var err error
var data system.SysDictData
msgID := e.MsgID
db := e.Orm.Model(&data).
Where(c.GetId()).Updates(c)
if db.Error != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
if db.RowsAffected == 0 {
return errors.New("无权更新该数据")
}
return nil
}
// Remove 删除
func (e *SysDictData) Remove(d *dto.SysDictDataById, c *system.SysDictData) error {
var err error
var data system.SysDictData
msgID := e.MsgID
db := e.Orm.Model(&data).
Where(d.GetId()).Delete(c)
if db.Error != nil {
err = db.Error
log.Errorf("MsgID[%s] Delete error: %s", msgID, err)
return err
}
if db.RowsAffected == 0 {
err = errors.New("无权删除该数据")
return err
}
return nil
}
// GetAll 获取所有
func (e *SysDictData) GetAll(c *dto.SysDictDataSearch, list *[]system.SysDictData) error {
var err error
var data system.SysDictData
msgID := e.MsgID
err = e.Orm.Model(&data).
Scopes(
cDto.MakeCondition(c.GetNeedSearch()),
).
Find(list).Error
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
+130
View File
@@ -0,0 +1,130 @@
package service
import (
"errors"
"gorm.io/gorm"
"go-admin/app/admin/models/system"
"go-admin/app/admin/service/dto"
cDto "go-admin/common/dto"
"go-admin/common/log"
"go-admin/common/service"
)
type SysDictType struct {
service.Service
}
// GetPage 获取列表
func (e *SysDictType) GetPage(c *dto.SysDictTypeSearch, list *[]system.SysDictType, count *int64) error {
var err error
var data system.SysDictType
msgID := e.MsgID
err = e.Orm.Model(&data).
Scopes(
cDto.MakeCondition(c.GetNeedSearch()),
cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
).
Find(list).Limit(-1).Offset(-1).
Count(count).Error
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// Get 获取对象
func (e *SysDictType) Get(d *dto.SysDictTypeById, model *system.SysDictType) error {
var err error
var data system.SysDictType
msgID := e.MsgID
db := e.Orm.Model(&data).
First(model, d.GetId())
err = db.Error
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
err = errors.New("查看对象不存在或无权查看")
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
if db.Error != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// Insert 创建对象
func (e *SysDictType) Insert(model *system.SysDictType) error {
var err error
var data system.SysDictType
msgID := e.MsgID
err = e.Orm.Model(&data).
Create(model).Error
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// Update 修改对象
func (e *SysDictType) Update(c *system.SysDictType) error {
var err error
var data system.SysDictType
msgID := e.MsgID
db := e.Orm.Model(&data).
Where(c.GetId()).Updates(c)
if db.Error != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
if db.RowsAffected == 0 {
return errors.New("无权更新该数据")
}
return nil
}
// Remove 删除
func (e *SysDictType) Remove(d *dto.SysDictTypeById, c *system.SysDictType) error {
var err error
var data system.SysDictType
msgID := e.MsgID
db := e.Orm.Model(&data).
Where(d.GetId()).Delete(c)
if db.Error != nil {
err = db.Error
log.Errorf("MsgID[%s] Delete error: %s", msgID, err)
return err
}
if db.RowsAffected == 0 {
err = errors.New("无权删除该数据")
return err
}
return nil
}
// GetAll 获取所有
func (e *SysDictType) GetAll(c *dto.SysDictTypeSearch, list *[]system.SysDictType) error {
var err error
var data system.SysDictType
msgID := e.MsgID
err = e.Orm.Model(&data).
Scopes(
cDto.MakeCondition(c.GetNeedSearch()),
).
Find(list).Error
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
@@ -32,8 +32,8 @@ func _1599190683659Tables(db *gorm.DB, version string) error {
new(models.SysUser),
new(system.SysRole),
new(models.Post),
new(models.DictData),
new(models.DictType),
new(DictData),
new(DictType),
new(models.SysJob),
new(system.SysConfig),
new(models.SysSetting),
@@ -44,7 +44,7 @@ func _1599190683670Test(db *gorm.DB, version string) error {
{1, "系统管理员", "2", "admin", 1, "", "1", "", "", true, "", models.BaseModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, "", []int{}, []int{}},
}
list7 := []models.DictType{
list7 := []DictType{
{DictId: 1, DictName: "系统开关", DictType: "sys_normal_disable", Status: "2", CreateBy: "1", UpdateBy: "1", Remark: "系统开关列表", BaseModel: models.BaseModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}},
{DictId: 2, DictName: "用户性别", DictType: "sys_user_sex", Status: "2", CreateBy: "1", UpdateBy: "", Remark: "用户性别列表", BaseModel: models.BaseModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}},
{DictId: 3, DictName: "菜单状态", DictType: "sys_show_hide", Status: "2", CreateBy: "1", UpdateBy: "", Remark: "菜单状态列表", BaseModel: models.BaseModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}},
@@ -61,7 +61,7 @@ func _1599190683670Test(db *gorm.DB, version string) error {
{models.SysUserId{1}, models.LoginM{models.UserName{"admin"}, models.PassWord{"$2a$10$cKFFTCzGOvaIHHJY2K45Zuwt8TD6oPzYi4s5MzYIBAWCLL6ZhouP2"}}, models.SysUserB{"zhangwj", "13818888888", 1, "", "", "0", "1@qq.com", 1, 1, "1", "1", "", "2", models.BaseModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, "", ""}},
}
list9 := []models.DictData{
list9 := []DictData{
{DictCode: 1, DictSort: 0, DictLabel: "正常", DictValue: "2", DictType: "sys_normal_disable", CssClass: "", ListClass: "", IsDefault: "", Status: "2", Default: "", CreateBy: "1", UpdateBy: "", Remark: "系统正常", BaseModel: models.BaseModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}},
{DictCode: 2, DictSort: 0, DictLabel: "停用", DictValue: "1", DictType: "sys_normal_disable", CssClass: "", ListClass: "", IsDefault: "", Status: "2", Default: "", CreateBy: "1", UpdateBy: "", Remark: "系统停用", BaseModel: models.BaseModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}},
{DictCode: 3, DictSort: 0, DictLabel: "男", DictValue: "0", DictType: "sys_user_sex", CssClass: "", ListClass: "", IsDefault: "", Status: "2", Default: "", CreateBy: "1", UpdateBy: "", Remark: "性别男", BaseModel: models.BaseModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}},
@@ -18,13 +18,13 @@ func init() {
func _1606579402398Test(db *gorm.DB, version string) error {
return db.Transaction(func(tx *gorm.DB) error {
dicData := models.DictData{}
dicData := DictData{}
err := tx.Model(&dicData).Where("dict_code = ?", 1).Update("dict_value", 2).Error
if err != nil {
return err
}
dicType := models.DictType{}
dicType := DictType{}
err = tx.Model(&dicType).Where("status = ?", 0).Update("status", 2).Error
if err != nil {
return err
@@ -24,7 +24,7 @@ func _1606582844753Test(db *gorm.DB, version string) error {
return err
}
dictData := models.DictData{}
dictData := DictData{}
err = tx.Model(&dictData).Where("status = ?", 0).Update("status", 2).Error
if err != nil {
return err
@@ -1,7 +1,6 @@
package version
import (
"go-admin/app/admin/models"
//"go-admin/app/admin/models"
"gorm.io/gorm"
"runtime"
@@ -19,7 +18,7 @@ func _1613720892819Test(db *gorm.DB, version string) error {
return db.Transaction(func(tx *gorm.DB) error {
// TODO: 这里开始写入要变更的内容
t := &models.DictType{
t := &DictType{
DictName: "内容状态",
DictType: "sys_content_status",
Status: "2",
@@ -31,8 +30,8 @@ func _1613720892819Test(db *gorm.DB, version string) error {
return err
}
data := make([]models.DictData, 2)
data[0] = models.DictData{
data := make([]DictData, 2)
data[0] = DictData{
DictSort: 0,
DictLabel: "正常",
DictValue: "1",
@@ -41,7 +40,7 @@ func _1613720892819Test(db *gorm.DB, version string) error {
CreateBy: "1",
UpdateBy: "1",
}
data[1] = models.DictData{
data[1] = DictData{
DictSort: 1,
DictLabel: "禁用",
DictValue: "2",
@@ -0,0 +1,45 @@
package version
import (
"go-admin/app/admin/models/system"
//"go-admin/app/admin/models"
"gorm.io/gorm"
"runtime"
"go-admin/cmd/migrate/migration"
common "go-admin/common/models"
)
func init() {
_, fileName, _, _ := runtime.Caller(0)
migration.Migrate.SetVersion(migration.GetFilename(fileName), _1613783303115Test)
}
func _1613783303115Test(db *gorm.DB, version string) error {
return db.Transaction(func(tx *gorm.DB) error {
//修改字段类型
err := tx.Migrator().AlterColumn(&system.SysDictType{}, "create_by")
if err != nil {
return err
}
err = tx.Migrator().AlterColumn(&system.SysDictType{}, "update_by")
if err != nil {
return err
}
err = tx.Migrator().AlterColumn(&system.SysDictData{}, "create_by")
if err != nil {
return err
}
err = tx.Migrator().AlterColumn(&system.SysDictData{}, "update_by")
if err != nil {
return err
}
return tx.Create(&common.Migration{
Version: version,
}).Error
})
}
+26
View File
@@ -0,0 +1,26 @@
package version
import (
"go-admin/app/admin/models"
)
type DictData struct {
DictCode int `gorm:"primary_key;auto_increment;" json:"dictCode" example:"1"` //字典编码
DictSort int `gorm:"" json:"dictSort"` //显示顺序
DictLabel string `gorm:"size:128;" json:"dictLabel"` //数据标签
DictValue string `gorm:"size:255;" json:"dictValue"` //数据键值
DictType string `gorm:"size:64;" json:"dictType"` //字典类型
CssClass string `gorm:"size:128;" json:"cssClass"` //
ListClass string `gorm:"size:128;" json:"listClass"` //
IsDefault string `gorm:"size:8;" json:"isDefault"` //
Status string `gorm:"size:4;" json:"status"` //状态
Default string `gorm:"size:8;" json:"default"` //
CreateBy string `gorm:"size:64;" json:"createBy"` //
UpdateBy string `gorm:"size:64;" json:"updateBy"` //
Remark string `gorm:"size:255;" json:"remark"` //备注
models.BaseModel
}
func (DictData) TableName() string {
return "sys_dict_data"
}
+20
View File
@@ -0,0 +1,20 @@
package version
import (
"go-admin/app/admin/models"
)
type DictType struct {
DictId int `gorm:"primary_key;auto_increment;" json:"dictId"`
DictName string `gorm:"size:128;" json:"dictName"` //字典名称
DictType string `gorm:"size:128;" json:"dictType"` //字典类型
Status string `gorm:"size:4;" json:"status"` //状态
CreateBy string `gorm:"size:11;" json:"createBy"` //创建者
UpdateBy string `gorm:"size:11;" json:"updateBy"` //更新者
Remark string `gorm:"size:255;" json:"remark"` //备注
models.BaseModel
}
func (DictType) TableName() string {
return "sys_dict_type"
}