package service import ( "context" "dashoo.cn/micro/app/common/global" comService "dashoo.cn/micro/app/common/service" "dashoo.cn/micro/app/dao" "dashoo.cn/micro/app/model" "database/sql" "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/text/gstr" "github.com/gogf/gf/util/gconv" ) type dictDataService struct { *contextService Dao *dao.SysDictDataDao } func NewDictDataService(ctx context.Context) (svc *dictDataService, err error) { svc = new(dictDataService) if svc.contextService, err = svc.Init(ctx); err != nil { return nil, err } svc.Dao = dao.NewSysDictDataDao(svc.Tenant) svc.Table = svc.Dao.Table return svc, nil } func (s *dictDataService) GetDictDataList(req *model.SelectDictPageReq) (total int, list []*model.SysDictData, err error) { db := s.Dao.Ctx(req.Ctx) if req != nil { if req.DictLabel != "" { db = db.Where(s.Dao.Columns.DictLabel+" like ?", "%"+req.DictLabel+"%") } if req.Status != "" { db = db.Where(s.Dao.Columns.Status+" = ", gconv.Int(req.Status)) } if req.DictType != "" { db = db.Where(s.Dao.Columns.DictType+" = ?", req.DictType) } total, err = db.Count() if err != nil { g.Log().Error(err) err = gerror.New("获取总行数失败") return } } list, err = db.Page(req.GetPage()).Order(s.Dao.Columns.DictSort + " asc," + s.Dao.Columns.DictCode + " asc").All() if err != nil { g.Log().Error(err) err = gerror.New("获取数据失败") return } return } // GetDictDataById 通过字典数据id获取字典数据 func (s *dictDataService) GetDictDataById(id int64) (data *model.SysDictData, err error) { data, err = s.Dao.FindOne(s.Dao.Columns.DictCode, id) if err != nil { g.Log().Error(err) err = gerror.New("获取字典数据失败") return } if data == nil { err = gerror.New("获取字典数据失败") } return } // GetDictWithDataByType 通过字典键类型获取选项 func (s *dictDataService) GetDictWithDataByType(req *model.GetDictReq) (dict *model.DictRes, err error) { cache := comService.Cache.New() cacheKey := global.SysDict + "_" + req.DictType //从缓存获取 iDict := cache.Get(cacheKey) if iDict != nil { err = gconv.Struct(iDict, &dict) if err != nil { return } } else { //从数据库获取 dict = &model.DictRes{} //获取类型数据 err = s.Dao.DB.Model("sys_dict_type").Where(dao.SysDictType.Columns.DictType, req.DictType). Where(dao.SysDictType.Columns.Status, "10").Fields(model.DictTypeRes{}).Scan(&dict.Info) if err != nil { g.Log().Error(err) err = gerror.New("获取字典类型失败") } err = s.Dao.Where(s.Dao.Columns.DictType, req.DictType).Where(s.Dao.Columns.Status, "10"). Order(s.Dao.Columns.DictSort + " asc," + s.Dao.Columns.DictCode + " asc"). Scan(&dict.Values) if err != nil { g.Log().Error(err) err = gerror.New("获取字典数据失败") } //缓存菜单 if dict.Info != nil && dict.Values != nil { cache.Set(cacheKey, dict, 0, global.SysDictTag) } } //设置给定的默认值 for _, v := range dict.Values { if req.DefaultValue != "" { if gstr.Equal(req.DefaultValue, v.DictValue) { v.IsDefault = 1 } else { v.IsDefault = 0 } } } return } // CheckDictTypeUniqueAll 检查字典类型是否唯一 func (s *dictDataService) CheckDictTypeUniqueAll(dictType string) bool { dict, err := s.Dao.FindOne(s.Dao.Columns.DictType+"=?", dictType) if err != nil { g.Log().Error(err) return false } if dict != nil { return false } return true } // Create 添加保存字典数据 func (s *dictDataService) Create(req *model.DictDataAddReq) (id int64, err error) { var res sql.Result data := new(model.SysDictData) if err := gconv.Struct(req, data); err != nil { return -1, err } SetCreatedInfo(data, s.GetCxtUserId(), s.GetCxtUserName()) res, err = s.Dao.Data(data).Insert() if err != nil { g.Log().Error(err) err = gerror.New("添加字典数据失败") return } id, err = res.LastInsertId() return } // UpdateByDict 修改字典数据 func (s *dictDataService) UpdateByDict(req *model.EditDictDataReq) (err error) { data := new(model.SysDictData) if err := gconv.Struct(req, data); err != nil { return err } SetUpdatedInfo(data, s.GetCxtUserId(), s.GetCxtUserName()) updateFieldEx := append(CommonUpdateFieldEx, dao.SysDictData.Columns.DictCode) _, err = s.Dao.FieldsEx(updateFieldEx...).WherePri(req.DictCode).Update(data) return } // DeleteDictDataByIds 删除字典数据 func (s *dictDataService) DeleteDictDataByIds(ids []int64) error { _, err := s.Dao.Where(s.Dao.Columns.DictCode+" in(?)", ids).Delete() if err != nil { g.Log().Error(err) return gerror.New("删除失败") } return nil }