package cygx

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type UpdateUserYanxuanPermissionReq struct {
	UserId int `description:"用户ID"`
	Type   int `description:"类型 0关闭 、1开通"`
}

type CygxUserYanxuanPermission struct {
	UserYanxuanPermissionId int64     `orm:"column(user_yanxuan_permission_id);pk"`
	UserId                  int       `description:"用户ID"`
	CompanyId               int       `description:"权限开始日期"`
	StartDate               string    `description:"权限开始日期"`
	EndDate                 string    `description:"权限结束日期"`
	Status                  string    `description:"'试用','未开通'"`
	Mobile                  string    `description:"手机号"`
	Email                   string    `description:"邮箱"`
	RealName                string    `description:"用户实际名称"`
	CompanyName             string    `description:"公司名称"`
	AdminId                 int       `description:"后台添加人员ID"`
	AdminRealName           string    `description:"后台添加人员姓名"`
	CreatedTime             time.Time `description:"创建时间"`
	LastUpdatedTime         time.Time `description:"修改时间根据时间戳自动更新"`
	ModifyTime              time.Time `description:"修改时间"`
}

type CygxUserYanxuanPermissionLog struct {
	UserYanxuanPermissionId int64     `orm:"column(user_yanxuan_permission_log_id);pk"`
	UserId                  int       `description:"用户ID"`
	CompanyId               int       `description:"权限开始日期"`
	StartDate               string    `description:"权限开始日期"`
	EndDate                 string    `description:"权限结束日期"`
	Status                  string    `description:"'试用','未开通'"`
	Mobile                  string    `description:"手机号"`
	Email                   string    `description:"邮箱"`
	RealName                string    `description:"用户实际名称"`
	CompanyName             string    `description:"公司名称"`
	AdminId                 int       `description:"后台添加人员ID"`
	AdminRealName           string    `description:"后台添加人员姓名"`
	CreatedTime             time.Time `description:"创建时间"`
	LastUpdatedTime         time.Time `description:"修改时间根据时间戳自动更新"`
	ModifyTime              time.Time `description:"修改时间"`
}

// 添加
func AddCygxUserYanxuanPermission(item *CygxUserYanxuanPermission) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	lastId, err = o.Insert(item)
	return
}

// 添加日志记录
func AddCygxUserYanxuanPermissionLog(item *CygxUserYanxuanPermissionLog) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	lastId, err = o.Insert(item)
	return
}

// 根据用户ID获取是否开通过研选权限
func GetCygxUserYanxuanPermissionCountByUserId(userId int) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT COUNT(1) FROM cygx_user_yanxuan_permission WHERE user_id = ?  `
	err = o.Raw(sql, userId).QueryRow(&count)
	return
}

// 将研选权限状态改为未开通
func UpdateCygxUserYanxuanPermissionClose(userId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` UPDATE cygx_user_yanxuan_permission
			SET
			  status ='未开通',
			  modify_time = ?
			WHERE user_id = ? `
	_, err = o.Raw(sql, time.Now(), userId).Exec()
	return
}

// 将研选权限状态改为未开通
func UpdateCygxUserYanxuanPermissionOpen(item *CygxUserYanxuanPermission) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	updateParams := make(map[string]interface{})
	updateParams["CompanyId"] = item.CompanyId
	updateParams["StartDate"] = item.StartDate
	updateParams["EndDate"] = item.EndDate
	updateParams["Status"] = item.Status
	updateParams["Mobile"] = item.Mobile
	updateParams["Email"] = item.Email
	updateParams["RealName"] = item.RealName
	updateParams["CompanyName"] = item.CompanyName
	updateParams["AdminId"] = item.AdminId
	updateParams["AdminRealName"] = item.AdminRealName
	ptrStructOrTableName := "cygx_user_yanxuan_permission"
	whereParam := map[string]interface{}{"user_id": item.UserId}
	qs := o.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	return
}

// 列表
func GetCygxUserYanxuanPermissionList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxUserYanxuanPermission, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_user_yanxuan_permission WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,?  `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}
