package company

import (
	"encoding/json"
	"errors"
	"eta_gn/eta_api/global"
	"eta_gn/eta_api/utils"
	"time"
)

const (
	ConfAreaCodeListKey = "area_code_list" // 手机号区号列表
	ConfEnAuthRoleKey   = "en_auth_role"   // 英文权限角色配置Key
)

type CrmConfig struct {
	ConfigId    int       `gorm:"primaryKey;column:config_id;type:int(11);not null"`
	ConfigCode  string    `gorm:"column:config_code;type:varchar(50);default:''"` // 配置编码
	ConfigValue string    `gorm:"column:config_value;type:text"`                  // 配置值
	Remark      string    `gorm:"column:remark;type:varchar(255);default:''"`     // 备注信息
	CreateTime  time.Time `gorm:"column:create_time;type:datetime"`               // 创建时间
}

func GetConfigDetailByCode(configCode string) (item CrmConfig, err error) {
	sql := ` SELECT config_value FROM crm_config WHERE config_code=? `
	err = global.DEFAULT_DmSQL.Raw(sql, configCode).First(&item).Error
	return
}

// ConfEnAuthRole 配置-英文权限角色
type ConfEnAuthRole struct {
	RoleCode string `description:"角色编码"`
	RoleName string `description:"角色名称"`
	SyncCrm  bool   `description:"是否同步CRM"`
}

// ConfigClassifyId
// @Description: 后台配置的报告id
type ConfigClassifyId struct {
	Debug   int `json:"debug"`
	Release int `json:"release"`
}

// GetReportClassifyIdByConfigKey
// @Description: 获取关联的报告id
// @author: Roc
// @datetime 2024-06-18 14:10:27
// @param configKey string
// @return classifyId int
// @return err error
func GetReportClassifyIdByConfigKey(configKey string) (classifyId int, err error) {
	// 别问为啥要从配置里拿=_=!
	conf, e := GetConfigDetailByCode(configKey)
	if e != nil {
		err = errors.New("获取配置的id失败, Err: " + e.Error())
		return
	}
	if conf.ConfigValue == "" {
		err = errors.New("ID配置有误")
		return
	}
	type TwoWeekIdConf struct {
		Debug   []int
		Release []int
	}
	classifyIdConf := new(ConfigClassifyId)
	if e = json.Unmarshal([]byte(conf.ConfigValue), &classifyIdConf); e != nil {
		err = errors.New("解析ID配置失败, Err: " + e.Error())
		return
	}
	if utils.RunMode == "debug" {
		classifyId = classifyIdConf.Debug
	} else {
		classifyId = classifyIdConf.Release
	}

	return
}