package repository

import (
	"strings"
)

////QueryChainCodeAndLocalIDByPhone 查询用户信息
//func QueryChainCodeAndLocalIDByPhone(phone string) (string, int, error) {
//
//	db, err := mysql.NewAccountConn()
//	if err != nil {
//		return "", 0, err
//	}
//	// 查询医院是否存在
//	var chainCode string
//	var employeeLocalID int
//	err = db.QueryRow(`select default_chain_code, employee_local_id from hos_database.employee_wx
//	where employee_phone = ?`, phone).Scan(&chainCode, &employeeLocalID)
//	switch {
//	case err == sql.ErrNoRows:
//		return "", 0, errors.New("没有这个用户")
//	case err != nil:
//		return "", 0, err
//	}
//
//	return chainCode, employeeLocalID, nil
//}
//
//// 根据医院code 获取云端id
//func GetHospitalIdByCode(hospitalCode string) (string, error) {
//
//	db, err := mysql.NewAccountConn()
//	if err != nil {
//		return "", err
//	}
//
//	var hospitalId string
//
//	err = db.QueryRow("select id from hospital_main where hospital_code = ?", hospitalCode).Scan(&hospitalId)
//	if err != nil {
//		return "", err
//	}
//
//	return hospitalId, nil
//}
//
//// QueryChainCodeByPhone 查询用户使用的是哪个医院的数据
//func QueryChainCodeByPhone(phone string) (chain string, err error) {
//
//	db, err := mysql.NewAccountConn()
//	if err != nil {
//		return "", err
//	}
//
//	// 查询医院是否存在
//	var chainCode string
//	err = db.QueryRow(`select default_chain_code from employee_wx
//	where employee_phone = ?`, phone).Scan(&chainCode)
//	if err != nil {
//		return "", err
//	}
//
//	if chainCode == "" {
//		return "", errors.New("查不到这个用户")
//	}
//
//	return chainCode, nil
//}
//
//// QueryChainCode 查询chain_code
//func QueryChainCode(phone string) (string, error) {
//
//	db, err := mysql.NewAccountConn()
//	if err != nil {
//		return "", err
//	}
//
//	var code string
//
//	err = db.QueryRow(`select c.chain_code from hospital_main as h
//	LEFT JOIN chain_main as c on h.chain_id  = c.id
//	 where c.delflag = 0 AND h.delflag = 0 AND h.create_phone =  ?`, phone).Scan(&code)
//
//	if err != nil {
//		return "", err
//	}
//
//	return code, nil
//}
//
//// QueryChainID 查询chain_ID
//func QueryChainID(phone string) (int, error) {
//
//	db, err := mysql.NewAccountConn()
//	if err != nil {
//		return 0, err
//	}
//
//	var id int
//
//	err = db.QueryRow(`select c.id from hospital_main as h
//	LEFT JOIN chain_main as c on h.chain_id  = c.id
//	 where c.delflag = 0 AND h.delflag = 0 AND h.create_phone =  ?`, phone).Scan(&id)
//
//	if err != nil {
//		return 0, err
//	}
//
//	return id, nil
//}
//
//// QueryHospitalCode  查询医院码
//func QueryHospitalCode(phone string) (string, error) {
//
//	db, err := mysql.NewAccountConn()
//	if err != nil {
//		return "", err
//	}
//
//	var code string
//
//	err = db.QueryRow(`select hospital_code from hospital_main where delflag = 0 and create_phone = ?`, phone).Scan(&code)
//
//	if err != nil {
//		return "", err
//	}
//
//	return code, nil
//}
//
//// CheckHospitalPhone 检查手机号码是否有效
//func CheckHospitalPhone(phone string) (bool, error) {
//
//	db, err := mysql.NewAccountConn()
//	if err != nil {
//		return false, err
//	}
//
//	var (
//		code           int
//		authorizeState int
//		storageMethod  int
//		chainID        int64
//	)
//	// 查询是否注册过
//	err = db.QueryRow(`select count(*) from hospital_main where create_phone = ?`, phone).Scan(&code)
//	if err != nil {
//		return false, err
//	}
//
//	//chain_id
//	err = db.QueryRow(`select chain_id from hospital_main where create_phone = ?`, phone).Scan(&chainID)
//	if err != nil {
//		return false, err
//	}
//
//	// 查询是否授权
//	err = db.QueryRow(`SELECT IFNULL(b.authorize_state ,0) FROM hospital_main AS a  left join hospital_operate AS b
//   ON a.id = b.hospital_main_id   WHERE a.create_phone = ? `, phone).Scan(&authorizeState)
//	if err != nil {
//		return false, err
//	}
//
//	// 查询是否授权
//	err = db.QueryRow(`SELECT store_type FROM chain_config where chain_main_id = ?   `, chainID).Scan(&storageMethod)
//	if err != nil {
//		return false, err
//	}
//
//	if authorizeState == 1 && code > 0 && storageMethod == 0 {
//		return true, nil
//	}
//
//	return false, err
//}

// PackingDatabaseName 包装数据库名称
func PackingDatabaseName(tableName, code string) string {
	return "dbc_" + strings.ToLower(code) + "." + tableName
}

// QueryHospitalID  查询医院码
//func QueryHospitalID(chainCode, hospitalCode string) (int, string, error) {
//
//	db, err := mysql.NewUserConn(chainCode)
//	if err != nil {
//		return 0, "", err
//	}
//
//	tableName := PackingDatabaseName("sys_hospital", chainCode)
//
//	querySQL := fmt.Sprintf(`select id,_name from %s
//	where delflag = 0 and hospital_code = ?`, tableName)
//
//	var (
//		id   int
//		name string
//	)
//
//	err = db.QueryRow(querySQL, hospitalCode).Scan(&id, &name)
//	if err != nil {
//		zap.L().Error("err", zap.Error(err))
//		return 0, "", err
//	}
//	return id, name, err
//}

//GetTheCostOfGoods 获取商品成本
//func GetTheCostOfGoods(chainCode, hospitalID string, db *sqlx.DB) ([]models.CommodityCost, error) {
//
//	commodityCosts := make([]models.CommodityCost, 0)
//
//	getTheCostOfGoodsSQL := fmt.Sprintf(`select  con_commodity_id,costmoney from g_stock
//	where delflag = 0  and sys_hospital_id = %d`, hospitalID)
//
//	rows, err := db.Query(getTheCostOfGoodsSQL)
//	if err != nil {
//		zap.L().Error("err", zap.Error(err))
//		return nil, err
//	}
//
//	for rows.Next() {
//		var t models.CommodityCost
//		err = rows.Scan(&t.CommodityID, t.CommodityCostPrice)
//		if err != nil {
//			zap.L().Error("err", zap.Error(err))
//			return nil, err
//		}
//		commodityCosts = append(commodityCosts, t)
//	}
//
//	return commodityCosts, err
//
//}