package service

import (
	"strconv"
	"sws_xcx/models"

	"github.com/jinzhu/gorm"
)

type SysDicService struct {
	rdb *gorm.DB
	wdb *gorm.DB
}

func NewSysDicService() *SysDicService {
	dic := &models.SysDictionary{}
	return &SysDicService{

		rdb: ReadDB().Model(dic),
		wdb: WriteDB().Model(dic),
	}
}

func (s *SysDicService) GetDicsByType(t string) ([]*models.SysDictionary, error) {
	var dics []*models.SysDictionary
	err := s.rdb.Where("name_en = ? and parent_id>0", t).Scan(&dics).Error
	return dics, err
}

func (s *SysDicService) Transform(dics []*models.SysDictionary) []*models.DicResp {
	var resps []*models.DicResp
	for _, dic := range dics {
		var resp models.DicResp
		resp.Type = dic.NameEn
		resp.Name = dic.NameCh
		t, _ := strconv.Atoi(dic.Type)
		resp.Value = t
		resps = append(resps, &resp)
	}
	return resps
}
