package models

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

type CygxActivityUserSearchContent struct {
	Id                 int       `orm:"column(id);pk"`
	UserId             int       `description:"用户ID"`
	CreateTime         time.Time `description:"创建时间"`
	Mobile             string    `description:"手机号"`
	Email              string    `description:"邮箱"`
	CompanyId          int       `description:"公司id"`
	CompanyName        string    `description:"公司名称"`
	ModifyTime         time.Time `description:"更新时间"`
	ChartPermissionids string    `description:"行业id"`
	ActivityTypeids    string    `description:"活动类型id"`
	ActiveState        string    `description:"活动进行状态 未开始：1、进行中2、已结束3"`
	IsShowJurisdiction int       `description:"是否仅展示有权限的，1是，0否"`
}

//更新搜索信息
func AddUserSearchContent(item *CygxActivityUserSearchContent, ttlTime time.Duration) (lastId int64, err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		fmt.Println(err)
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	var count int
	sql := `SELECT COUNT(1) AS count FROM cygx_activity_user_search_content WHERE user_id=? `
	err = o.Raw(sql, item.UserId).QueryRow(&count)
	if err != nil {
		return
	}
	if count > 0 || ttlTime > 0 {
		sql := `UPDATE cygx_activity_user_search_content SET chart_permissionids = ?,activity_typeids = ?,active_state = ?,is_show_jurisdiction = ? ,modify_time=NOW() WHERE user_id=? `
		_, err = o.Raw(sql, item.ChartPermissionids, item.ActivityTypeids, item.ActiveState, item.IsShowJurisdiction, item.UserId).Exec()
		return
	} else {
		lastId, err = o.Insert(item)
	}
	return
}

//通过用户ID获取用户搜索条件详情
func GetUserSearchContentByUid(uid int) (item *CygxActivityUserSearchContent, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_activity_user_search_content WHERE user_id = ? `
	err = o.Raw(sql, uid).QueryRow(&item)
	return
}

type ActivityUserSearchContentList struct {
	IsShowJurisdiction   bool `description:"是否仅展示有权限的，1是，0否"`
	ListActivityStaus    []ActivityStaus
	ListChartPermission  []*ActivityChartPermission
	ListChartPermission2 []*ActivityChartPermission
	ListActivityType     []*ActivityType
}

type ActivityStaus struct {
	Id         int
	IsChoose   bool `description:"是否选择"`
	StatusName string
}

type Arrayse struct {
	IsChoose bool
}
