package models

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

type UserTrialApply struct {
	Id          uint      `orm:"column(id);pk;auto"`
	CompanyName string    `orm:"size(255)";description:"企业名称"`
	RealName    string    `orm:"size(64)";description:"联系人姓名"`
	Phone       string    `orm:"size(20)";description:"手机号联系方式"`
	Email       string    `orm:"size(64)";description:"邮箱"`
	Industry    string    `orm:"size(20)";description:"所属行业:'公募基金','私募基金','券商资管','保险','海外','生产商','贸易商','下游用户','其他金融机构'";default:"公募基金"`
	ProductInfo string    `orm:"size(255)";description:"申请使用的产品,多值,用,隔开"`
	Message     string    `orm:"size(255)";description:"留言信息"`
	SourceIp    string    `orm:"size(20)";description:"来源ip"`
	SourceType  string    `orm:"size(20)";description:"来源,枚举值:'中文官网','英文官网'";default:"中文官网"`
	Status      string    `description:"状态,枚举值:'待处理','已处理'";orm:"size(16)";default:"待处理"`
	OpUserId    uint      `description:"操作用户id"`
	OpUserName  string    `orm:"size(64)";description:"操作用户名称"`
	CreateTime  time.Time `description:"申请时间"`
	ModifyTime  time.Time `description:"修改时间"`
	MarkGroup   string    `description:"标记分组"`
}

type UserTrialApplyItem struct {
	Id            uint      `orm:"column(id);pk;auto"`
	CompanyName   string    `orm:"size(255)";description:"企业名称"`
	RealName      string    `orm:"size(64)";description:"联系人姓名"`
	Phone         string    `orm:"size(20)";description:"手机号联系方式"`
	Email         string    `orm:"size(64)";description:"邮箱"`
	Industry      string    `orm:"size(20)";description:"所属行业:'公募基金','私募基金','券商资管','保险','海外','生产商','贸易商','下游用户','其他金融机构'";default:"公募基金"`
	ProductInfo   string    `orm:"size(255)";description:"申请使用的产品,多值,用,隔开"`
	Message       string    `orm:"size(255)";description:"留言信息"`
	SourceIp      string    `orm:"size(20)";description:"来源ip"`
	SourceType    string    `orm:"size(20)";description:"来源,枚举值:'中文官网','英文官网'";default:"中文官网"`
	Status        string    `description:"状态,枚举值:'待处理','已处理'";orm:"size(16)";default:"待处理"`
	OpUserId      uint      `description:"操作用户id"`
	OpUserName    string    `orm:"size(64)";description:"操作用户名称"`
	CreateTime    time.Time `description:"申请时间"`
	CreateTimeStr string    `description:"申请时间字符串"`
	ModifyTime    time.Time `description:"修改时间"`
	ModifyTimeStr string    `description:"申请时间字符串"`
	MarkGroup     string    `description:"标记分组"`
}

// 列表数据
type UserTrialApplyListResp struct {
	List   []*UserTrialApplyItem
	Paging *paging.PagingItem `description:"分页数据"`
}

// 确认申请
type UserTrialApplyConfirmReq struct {
	Id int `description:"Id"`
}

// 获取获取官网申请列表页数据的数据数
func GetOfficialApplyUserListListCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT 
			COUNT(id ) AS COUNT
			FROM user_trial_apply `

	if condition != "" {
		sql += " WHERE " + condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

// 获取官网申请列表页数据
func GetOfficialApplyUserListList(condition string, pars []interface{}, startSize, pageSize int) (items []*UserTrialApplyItem, err error) {
	o := orm.NewOrm()
	sql := `SELECT * from user_trial_apply `
	if condition != "" {
		sql += " WHERE " + condition
	}
	sql += `ORDER BY  id DESC LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

// GetOfficialApplyUserListListExport 导出官网申请试用列表
func GetOfficialApplyUserListListExport(condition string, pars []interface{}) (items []*UserTrialApplyItem, err error) {
	o := orm.NewOrm()
	sql := `SELECT * from user_trial_apply `
	if condition != "" {
		sql += " WHERE " + condition
	}
	sql += `ORDER BY  id DESC`
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// 获取申请记录信息
func GetOfficialApplyUserById(id int) (item *UserTrialApply, err error) {
	o := orm.NewOrm()
	err = o.Raw(`SELECT * from user_trial_apply where id = ?`, id).QueryRow(&item)
	return
}

// 确认申请记录
func ConfirmOfficialApplyUser(id int, opUserId int, opUserName string) (err error) {
	sql := ` UPDATE user_trial_apply
			SET
			  status = ?,
			  op_user_id = ?,
			  op_user_name = ?,
			  modify_time = ?
			WHERE id = ? `
	_, err = orm.NewOrm().Raw(sql, "已处理", opUserId, opUserName, time.Now(), id).Exec()
	return
}

type UserTrialApplyMarkGroupReq struct {
	Id        int    `description:"申请记录ID"`
	GroupName string `description:"分组名"`
}

// 标记分组
func OfficialApplyUserMarkGroup(id int, opUserId int, opUserName,groupName string) (err error) {
	sql := ` UPDATE user_trial_apply
			SET
			  status = ?,
			  op_user_id = ?,
			  op_user_name = ?,
			  mark_group = ?,
			  modify_time = ?
			WHERE id = ? `
	_, err = orm.NewOrm().Raw(sql, "已处理", opUserId, opUserName, groupName, time.Now(), id).Exec()
	return
}