|
|
@@ -7,6 +7,8 @@ import (
|
|
|
comService "dashoo.cn/micro/app/common/service"
|
|
|
"dashoo.cn/micro/app/dao"
|
|
|
"dashoo.cn/micro/app/model"
|
|
|
+ "dashoo.cn/opms_libary/micro_srv"
|
|
|
+ "github.com/gogf/gf/os/glog"
|
|
|
|
|
|
"github.com/gogf/gf/container/garray"
|
|
|
"github.com/gogf/gf/database/gdb"
|
|
|
@@ -15,24 +17,35 @@ import (
|
|
|
"github.com/gogf/gf/util/gconv"
|
|
|
)
|
|
|
|
|
|
-type sysDictType struct {
|
|
|
+type dictTypeService struct {
|
|
|
+ Dao *dao.SysDictTypeDao
|
|
|
}
|
|
|
|
|
|
-var SysDictType = new(sysDictType)
|
|
|
+func NewDictTypeService(ctx context.Context) (*dictTypeService, error) {
|
|
|
+ dict := new(dictTypeService)
|
|
|
+ // 获取租户码
|
|
|
+ tenant, err := micro_srv.GetTenant(ctx)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ reqMethod, _ := micro_srv.GetReqMethod(ctx)
|
|
|
+ glog.Info("Received " + reqMethod + " request @ " + tenant)
|
|
|
+ dict.Dao = dao.NewSysDictTypeDao(tenant)
|
|
|
+ return dict, err
|
|
|
+}
|
|
|
|
|
|
-func (s *sysDictType) SelectList(req *model.ListSysDictTypeReq) (total, page int,
|
|
|
- list []*model.SysDictTypeInfoRes, err error) {
|
|
|
- d := dao.SysDictType.Ctx(req.Ctx)
|
|
|
+func (s *dictTypeService) GetList(req *model.ListSysDictTypeReq) (total int, list []*model.SysDictType, err error) {
|
|
|
+ db := s.Dao.M
|
|
|
if req.DictName != "" {
|
|
|
- d = d.Where(dao.SysDictType.Columns.DictName+" like ?", "%"+req.DictName+"%")
|
|
|
+ db = db.Where(dao.SysDictType.Columns.DictName+" like ?", "%"+req.DictName+"%")
|
|
|
}
|
|
|
if req.DictType != "" {
|
|
|
- d = d.Where(dao.SysDictType.Columns.DictType+" like ?", "%"+req.DictType+"%")
|
|
|
+ db = db.Where(dao.SysDictType.Columns.DictType+" like ?", "%"+req.DictType+"%")
|
|
|
}
|
|
|
if req.Status != "" {
|
|
|
- d = d.Where(dao.SysDictType.Columns.Status+" = ", gconv.Int(req.Status))
|
|
|
+ db = db.Where(dao.SysDictType.Columns.Status+" = ", gconv.Int(req.Status))
|
|
|
}
|
|
|
- total, err = d.Count()
|
|
|
+ total, err = db.Count()
|
|
|
if err != nil {
|
|
|
g.Log().Error(err)
|
|
|
err = gerror.New("获取总行数失败")
|
|
|
@@ -41,12 +54,10 @@ func (s *sysDictType) SelectList(req *model.ListSysDictTypeReq) (total, page int
|
|
|
if req.PageNum == 0 {
|
|
|
req.PageNum = 1
|
|
|
}
|
|
|
- page = req.PageNum
|
|
|
if req.PageSize == 0 {
|
|
|
req.PageSize = comModel.PageSize
|
|
|
}
|
|
|
- err = d.Fields(model.SysDictTypeInfoRes{}).Page(page, req.PageSize).
|
|
|
- Order(dao.SysDictType.Columns.Id + " asc").Scan(&list)
|
|
|
+ err = db.Page(req.PageNum, req.PageSize).Order(dao.SysDictType.Columns.Id + " asc").Scan(&list)
|
|
|
if err != nil {
|
|
|
g.Log().Error(err)
|
|
|
err = gerror.New("获取数据失败")
|
|
|
@@ -54,8 +65,42 @@ func (s *sysDictType) SelectList(req *model.ListSysDictTypeReq) (total, page int
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+// GetDictById 获取字典类型
|
|
|
+func (s *dictTypeService) GetDictById(id int64) (dict *model.SysDictType, err error) {
|
|
|
+ dict, err = dao.SysDictType.FindOne(dao.SysDictType.Columns.Id, id)
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Error(err)
|
|
|
+ err = gerror.New("获取字典类型失败")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if dict == nil {
|
|
|
+ err = gerror.New("不存在的字典类型")
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetAllDictType 获取所有正常状态下的字典类型
|
|
|
+func (s *dictTypeService) GetAllDictType() (list []*model.SysDictType, err error) {
|
|
|
+ cache := comService.Cache.New()
|
|
|
+ //从缓存获取
|
|
|
+ data := cache.Get(global.SysDict + "_dict_type_all")
|
|
|
+ if data != nil {
|
|
|
+ err = gconv.Structs(data, &list)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = dao.SysDictType.Where("status", 1).Order("dict_id ASC").Scan(&list)
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Error(err)
|
|
|
+ err = gerror.New("获取字典类型数据出错")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //缓存
|
|
|
+ cache.Set(global.SysDict+"_dict_type_all", list, 0, global.SysDictTag)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
// ExistsDictType 检查类型是否已经存在
|
|
|
-func (s *sysDictType) ExistsDictType(dictType string, dictId ...int64) bool {
|
|
|
+func (s *dictTypeService) ExistsDictType(dictType string, dictId ...int64) bool {
|
|
|
d := dao.SysDictType.Fields(dao.SysDictType.Columns.Id).
|
|
|
Where(dao.SysDictType.Columns.DictType, dictType)
|
|
|
if len(dictId) > 0 {
|
|
|
@@ -72,8 +117,13 @@ func (s *sysDictType) ExistsDictType(dictType string, dictId ...int64) bool {
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
-func (s *sysDictType) Add(req *model.SysDictTypeAddReq) error {
|
|
|
- _, err := dao.SysDictType.Insert(req)
|
|
|
+func (s *dictTypeService) Create(req *model.SysDictTypeAddReq) error {
|
|
|
+ data := new(model.SysDictType)
|
|
|
+ if err := gconv.Struct(req, data); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ SetCreatedInfo(data, 1, "")
|
|
|
+ _, err := dao.SysDictType.Insert(data)
|
|
|
if err != nil {
|
|
|
g.Log().Debug(err)
|
|
|
err = gerror.New("保存到数据库失败")
|
|
|
@@ -81,15 +131,15 @@ func (s *sysDictType) Add(req *model.SysDictTypeAddReq) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
-func (s *sysDictType) Edit(ctx context.Context, req *model.SysDictTypeEditReq) error {
|
|
|
- err := g.DB().Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
|
|
|
- dt, err := dao.SysDictType.Fields(dao.SysDictType.Columns.DictType).FindOne(req.DictId)
|
|
|
+func (s *dictTypeService) UpdateById(req *model.SysDictTypeEditReq) error {
|
|
|
+ err := s.Dao.Transaction(s.Dao.GetCtx(), func(ctx context.Context, tx *gdb.TX) error {
|
|
|
+ dt, err := dao.SysDictType.Fields(dao.SysDictType.Columns.DictType).FindOne(req.Id)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
//修改字典类型
|
|
|
_, err = dao.SysDictType.TX(tx).FieldsEx(dao.SysDictType.Columns.CreatedBy,
|
|
|
- dao.SysDictType.Columns.Id).WherePri(req.DictId).Update(req)
|
|
|
+ dao.SysDictType.Columns.Id).WherePri(req.Id).Update(req)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
@@ -105,7 +155,7 @@ func (s *sysDictType) Edit(ctx context.Context, req *model.SysDictTypeEditReq) e
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
-func (s *sysDictType) Delete(ctx context.Context, dictIds []int) (err error) {
|
|
|
+func (s *dictTypeService) Delete(dictIds []int64) (err error) {
|
|
|
discs := ([]*model.SysDictType)(nil)
|
|
|
discs, err = dao.SysDictType.Fields(dao.SysDictType.Columns.DictType).
|
|
|
Where(dao.SysDictType.Columns.Id+" in (?) ", dictIds).All()
|
|
|
@@ -119,7 +169,7 @@ func (s *sysDictType) Delete(ctx context.Context, dictIds []int) (err error) {
|
|
|
types.Append(dt.DictType)
|
|
|
}
|
|
|
if types.Len() > 0 {
|
|
|
- err = g.DB().Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
|
|
|
+ err = g.DB().Transaction(s.Dao.GetCtx(), func(ctx context.Context, tx *gdb.TX) error {
|
|
|
_, err = dao.SysDictType.TX(tx).Delete(dao.SysDictType.Columns.Id+" in (?) ", dictIds)
|
|
|
if err != nil {
|
|
|
g.Log().Error(err)
|
|
|
@@ -137,37 +187,3 @@ func (s *sysDictType) Delete(ctx context.Context, dictIds []int) (err error) {
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
-// GetDictById 获取字典类型
|
|
|
-func (s *sysDictType) GetDictById(id int) (dict *model.SysDictType, err error) {
|
|
|
- dict, err = dao.SysDictType.FindOne(dao.SysDictType.Columns.Id, id)
|
|
|
- if err != nil {
|
|
|
- g.Log().Error(err)
|
|
|
- err = gerror.New("获取字典类型失败")
|
|
|
- return
|
|
|
- }
|
|
|
- if dict == nil {
|
|
|
- err = gerror.New("不存在的字典类型")
|
|
|
- }
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
-// GetAllDictType 获取所有正常状态下的字典类型
|
|
|
-func (s *sysDictType) GetAllDictType() (list []*model.SysDictType, err error) {
|
|
|
- cache := comService.Cache.New()
|
|
|
- //从缓存获取
|
|
|
- data := cache.Get(global.SysDict + "_dict_type_all")
|
|
|
- if data != nil {
|
|
|
- err = gconv.Structs(data, &list)
|
|
|
- return
|
|
|
- }
|
|
|
- err = dao.SysDictType.Where("status", 1).Order("dict_id ASC").Scan(&list)
|
|
|
- if err != nil {
|
|
|
- g.Log().Error(err)
|
|
|
- err = gerror.New("获取字典类型数据出错")
|
|
|
- return
|
|
|
- }
|
|
|
- //缓存
|
|
|
- cache.Set(global.SysDict+"_dict_type_all", list, 0, global.SysDictTag)
|
|
|
- return
|
|
|
-}
|