package services

import (
	"errors"
	"gin-vue-admin/common"
	"gin-vue-admin/repository"
)

func QueryCaseHistoryAndMoneyService(phone, t string) (interface{}, error) {
	// 1. 查询当前手机号是否有默认医院
	chainCode, _, err := repository.QueryChainCodeAndLocalIDByPhone(phone)
	if err != nil {
		return nil, err
	}

	if chainCode == "" {
		return nil, errors.New("请设置默认医院")
	}

	// 2. 拿到db
	safeMap := common.NewSafeMap()
	db, err := safeMap.GetOrStore(chainCode)
	if err != nil {
		return nil, err
	}

	return repository.QueryCaseHistoryAndMoney(db, t)
}

func QuerySpeciesAndAgeByKindService(phone, t string, _type int) (interface{}, error) {
	// 1. 查询当前手机号是否有默认医院
	chainCode, _, err := repository.QueryChainCodeAndLocalIDByPhone(phone)
	if err != nil {
		return nil, err
	}

	if chainCode == "" {
		return nil, errors.New("请设置默认医院")
	}

	// 2. 拿到db
	safeMap := common.NewSafeMap()
	db, err := safeMap.GetOrStore(chainCode)
	if err != nil {
		return nil, err
	}

	return repository.QuerySpeciesAndAgeByKindRepository(db, t, _type)
}

func DiseaseAndOperationRankService(phone, t string, _type int) (interface{}, error) {

	// 1. 查询当前手机号是否有默认医院
	chainCode, _, err := repository.QueryChainCodeAndLocalIDByPhone(phone)
	if err != nil {
		return nil, err
	}

	if chainCode == "" {
		return nil, errors.New("请设置默认医院")
	}

	// 2. 拿到db
	safeMap := common.NewSafeMap()
	db, err := safeMap.GetOrStore(chainCode)
	if err != nil {
		return nil, err
	}

	return repository.DiseaseAndOperationRankRepository(db, t, _type)
}
