package mysql

import (
	"crm-api/pkg/lfshook"
)

/* *****************************************************************************************
// GetExtensionRangeInfoByName
// 根据名字获取号码取值范围
func GetExtensionRangeInfoByName(name string) map[string]string {
	data, err := DBOrmInstance.SQL("select exten_start,exten_end from t_extension_range where name = ?", name).QueryString()
	if err != nil {
		lfshook.NewLogger().Error(err)
		return nil
	} else if data == nil {
		return nil
	}
	return data[0]
}
// ***************************************************************************************** */

// GetDialPlanByUserID 通过用户 ID 获取主控话机的拨号规则名
func GetDialPlanByUserID(id int64) (plan string) {
	data, err := DBOrmInstance.SQL("select name from t_dial_plan where id = (select dial_plan_id from t_extension_digital where exten = (select user_exten from t_user where id = ?))", id).QueryString()
	if err != nil {
		lfshook.NewLogger().Errorf("middleware GetExtraInfo %+v", err)
		return "default"
	}
	lfshook.NewLogger().Infof("GetDialPlanByUserID %+v", data)
	if len(data) == 0 {
		return "default"
	}
	return data[0]["name"]
}

// GetDialPlanByExtension 通过分机号 获取主控话机的拨号规则名
func GetDialPlanByExtension(extension string) (plan string) {
	data, err := DBOrmInstance.SQL("select name from t_dial_plan where id = (select dial_plan_id from t_extension_digital where exten = ?)", extension).QueryString()
	if err != nil {
		lfshook.NewLogger().Errorf("middleware GetExtraInfo %+v", err)
		return "default"
	}
	lfshook.NewLogger().Infof("GetDialPlanByUserID %+v", data)
	if len(data) == 0 {
		return "default"
	}
	return data[0]["name"]
}

/* *****************************************************************************************
// GetExtensionByUserID 通过用户 ID 获取主控话机的分机号
func GetExtensionByUserID(id int64) (extension string) {
	user := commonModel.User{
		ID: id,
	}
	_, err := DBOrmInstance.Get(&user)
	if err != nil {
		lfshook.NewLogger().Error(err)
	}
	return user.UserExtension
}

// GetUserInfoByID 通过 ID 获取用户信息
func GetUserInfoByID(id int64) commonModel.User {
	user := commonModel.User{}
	_, err := DBOrmInstance.Where("id = ?", id).Get(&user)
	if err != nil {
		lfshook.NewLogger().Error(err)
	}
	if user.ID == 0 {
		user.Role = "extension"
	}
	return user
}

// GetExtensionNameByNumber 通过号码获取名称
func GetExtensionNameByNumber(number string) (name string) {
	data := adminModel.TabExtensionDigital{
		Exten: number,
	}
	ok, err := DBOrmInstance.Get(&data)
	if err != nil {
		lfshook.NewLogger().Error(err)
		return number
	}
	if ok {
		return data.Username
	}
	return number
}

func GetAllAnalogExtension() (data []*adminModel.TabExtensionAnalog, err error) {
	err = DBOrmInstance.Find(&data)
	if err != nil {
		lfshook.NewLogger().Error(err)
		return data, err
	}
	return data, nil
}

func GetAllDigitalExtension() (data []*adminModel.TabExtensionDigital, err error) {
	err = DBOrmInstance.Find(&data)
	if err != nil {
		lfshook.NewLogger().Error(err)
		return data, err
	}
	return data, nil
}
// ***************************************************************************************** */
