package service

import (
	"context"
	"sim-server/app/main/model"
	"errors"
)

type DictService struct {
	adminDictTypeModel model.AdminDictTypeModel
	adminDictDataModel model.AdminDictDataModel
}

func NewDictService(adminDictTypeModel model.AdminDictTypeModel, adminDictDataModel model.AdminDictDataModel) *DictService {
	return &DictService{adminDictTypeModel: adminDictTypeModel, adminDictDataModel: adminDictDataModel}
}
func (s *DictService) GetDictLabel(ctx context.Context, dictType string, dictValue int64) (string, error) {
	dictTypeModel, err := s.adminDictTypeModel.FindOneByDictType(ctx, dictType)
	if err != nil {
		return "", err
	}
	if dictTypeModel.Status != 1 {
		return "", errors.New("字典类型未启用")
	}
	dictData, err := s.adminDictDataModel.FindOneByDictTypeDictValue(ctx, dictTypeModel.DictType, dictValue)
	if err != nil {
		return "", err
	}
	if dictData.Status != 1 {
		return "", errors.New("字典数据未启用")
	}
	return dictData.DictLabel, nil
}
func (s *DictService) GetDictValue(ctx context.Context, dictType string, dictLabel string) (int64, error) {
	dictTypeModel, err := s.adminDictTypeModel.FindOneByDictType(ctx, dictType)
	if err != nil {
		return 0, err
	}
	if dictTypeModel.Status != 1 {
		return 0, errors.New("字典类型未启用")
	}
	dictData, err := s.adminDictDataModel.FindOneByDictTypeDictLabel(ctx, dictTypeModel.DictType, dictLabel)
	if err != nil {
		return 0, err
	}
	return dictData.DictValue, nil
}
