package company_user

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

// 联系人图表分类权限
type ChartClassifyPermission struct {
	Id         int       `orm:"column(id);pk"`
	CompanyId  int       `description:"客户ID"`
	UserId     int       `description:"用户ID"`
	StartTime  time.Time `description:"起始时间"`
	EndTime    time.Time `description:"终止时间"`
	Enabled    int       `description:"状态 0-关闭 1-开启"`
	CreateTime time.Time `description:"创建时间"`
	UpdateTime time.Time `description:"更新时间"`
}

func (chartClassifyPermission *ChartClassifyPermission) TableName() string {
	return "company_user_chart_classify_permission"
}

// Update 更新分类权限
func (chartClassifyPermission *ChartClassifyPermission) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(chartClassifyPermission, cols...)
	return
}

// AddChartClassifyPermission 新增分类权限
func AddChartClassifyPermission(item *ChartClassifyPermission) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

// GetChartClassifyPermissionByUserId 获取联系人分类权限
func GetChartClassifyPermissionByUserId(userId int) (item *ChartClassifyPermission, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM company_user_chart_classify_permission WHERE user_id = ? `
	err = o.Raw(sql, userId).QueryRow(&item)
	return
}

// GetChartClassifyPermissionListByUserIds 通过联系人Id集合获取分类权限列表
func GetChartClassifyPermissionListByUserIds(userIds string) (list []*ChartClassifyPermission, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM company_user_chart_classify_permission WHERE user_id IN (` + userIds + `) `
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// ClearCompanyChartPermission 清除客户下所有联系人图表权限
func ClearCompanyChartPermission(companyId int) (err error) {
	o := orm.NewOrm()
	sql := ` DELETE FROM company_user_chart_classify_permission WHERE company_id = ? `
	_, err = o.Raw(sql, companyId).Exec()
	return
}

// GetCompanyChartPermissionByCompanyId 获取客户下所有联系人图表权限
func GetCompanyChartPermissionByCompanyId(companyId int) (items []*ChartClassifyPermission, err error) {
	o := orm.NewOrm()
	sql := ` select * FROM company_user_chart_classify_permission WHERE company_id = ? `
	_, err = o.Raw(sql, companyId).QueryRows(&items)
	return
}

// ClearUserChartPermission 清除联系人图表权限
func ClearUserChartPermission(userId int) (err error) {
	o := orm.NewOrm()
	sql := ` DELETE FROM company_user_chart_classify_permission WHERE user_id = ? LIMIT 1 `
	_, err = o.Raw(sql, userId).Exec()
	return
}

// CompanyUserChartPermissionOperationRecord 图表权限设置记录
type CompanyUserChartPermissionOperationRecord struct {
	Id         int       `orm:"column(id);pk"`
	Type       int       `description:"操作类型 1-新增 2-编辑"`
	CompanyId  int       `description:"当前所属客户ID"`
	UserId     int       `description:"用户ID"`
	AdminId    int       `description:"操作人ID"`
	StartTime  time.Time `description:"设置的起始时间"`
	EndTime    time.Time `description:"设置的终止时间"`
	Enabled    int       `description:"设置的开启关闭状态"`
	CreateTime time.Time `description:"操作时间"`
}

// AddChartPermissionOperationRecord 新增图表权限设置记录
func AddChartPermissionOperationRecord(item *CompanyUserChartPermissionOperationRecord) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}
