sysdicservice.go 832B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package service
  2. import (
  3. "strconv"
  4. "sws_xcx/models"
  5. "github.com/jinzhu/gorm"
  6. )
  7. type SysDicService struct {
  8. rdb *gorm.DB
  9. wdb *gorm.DB
  10. }
  11. func NewSysDicService() *SysDicService {
  12. dic := &models.SysDictionary{}
  13. return &SysDicService{
  14. rdb: ReadDB().Model(dic),
  15. wdb: WriteDB().Model(dic),
  16. }
  17. }
  18. func (s *SysDicService) GetDicsByType(t string) ([]*models.SysDictionary, error) {
  19. var dics []*models.SysDictionary
  20. err := s.rdb.Where("name_en = ? and parent_id>0", t).Scan(&dics).Error
  21. return dics, err
  22. }
  23. func (s *SysDicService) Transform(dics []*models.SysDictionary) []*models.DicResp {
  24. var resps []*models.DicResp
  25. for _, dic := range dics {
  26. var resp models.DicResp
  27. resp.Type = dic.NameEn
  28. resp.Name = dic.NameCh
  29. t, _ := strconv.Atoi(dic.Type)
  30. resp.Value = t
  31. resps = append(resps, &resp)
  32. }
  33. return resps
  34. }