package company

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

type SellerCompanyPermission struct {
	Id             int       `orm:"column(id);pk" description:"自增序号ID"`
	AdminId        int       `orm:"column(admin_id)" description:"系统用户id"`
	Enabled        int       `orm:"column(enabled)" description:"权限状态1:开放权限，0:禁用权限"`
	CreateTime     time.Time `orm:"column(create_time)" description:"创建时间"`
	ModifyTime     time.Time `orm:"column(modify_time)" description:"修改时间"`
	PermissionType int       `orm:"column(permission_type)" description:"权限类型（1:添加用户权限）"`
}

// ReceiveGroupSellersResp 销售列表（根据部门、分组来）
type ReceiveGroupSellersResp struct {
	List []ReceiveGroupSellers
}

type ReceiveGroupSellers struct {
	AdminId      string                   `description:"系统用户id"`
	RealName     string                   `description:"用户真实名称"`
	ChildrenList []ReceiveGroupSellers `description:"销售列表"`
}

type ReceiveSellerListResp struct {
	List   []*ReceiveSellerItemResp
	Paging *paging.PagingItem
}

type ReceiveSellerItemResp struct {
	AdminId         int    `description:"销售id"`
	Enabled         int    `description:"权限状态1:开放权限，0:禁用权限"`
	DepartmentGroup string `description:"所属分组"`
	RealName        string `description:"销售姓名"`
}

type ChangeReceivePermissionReq struct {
	AdminId         int    `description:"销售id"`
	Enabled         int    `description:"权限状态1:开放权限，0:禁用权限"`
}

// 新增权限记录
func AddSellerCompanyPermission(permission *SellerCompanyPermission) (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(permission)
	return
}

// GetSellerCompanyPermissionByAdminIdType 根据权限类型查询销售的权限详情
func GetSellerCompanyPermissionByAdminIdType(adminId, permissionType int) (item *SellerCompanyPermission, err error)  {
	o := orm.NewOrm()
	sql := ` select *  FROM seller_company_permission WHERE admin_id=? and permission_type=?`
	err = o.Raw(sql, adminId, permissionType).QueryRow(&item)
	return
}

// GetSellerCompanyPermissionByAdminIdsType 根据权限类型查询销售的权限详情
func GetSellerCompanyPermissionByAdminIdsType(adminIds string, permissionType int) (list []*SellerCompanyPermission, err error)  {
	o := orm.NewOrm()
	sql := ` select *  FROM seller_company_permission WHERE admin_id in (`+adminIds+`) and permission_type=?`
	_, err = o.Raw(sql, permissionType).QueryRows(&list)
	return
}

//UpdateSellerCompanyPermissionEnabledById 启用或禁用销售的某种类型的权限
func UpdateSellerCompanyPermissionEnabledById(id, enabled int) (err error) {
	o := orm.NewOrm()
	msql := " UPDATE seller_company_permission SET enabled = ?,modify_time=NOW() WHERE id = ? "
	_, err = o.Raw(msql, enabled, id).Exec()
	return
}