package models

import "github.com/beego/beego/v2/client/orm"

type AdminItem struct {
	AdminId        int    `description:"系统用户id"`
	RealName       string `description:"系统用户姓名"`
	Mobile         string `description:"手机号"`
	RoleType       int    `description:"角色类型:1需要录入指标,0:不需要"`
	RoleId         int    `description:"角色id"`
	RoleName       string `description:"角色名称"`
	RoleTypeCode   string `description:"角色编码"`
	DepartmentId   int    `description:"部门id"`
	DepartmentName string `json:"-" description:"部门名称"`
	GroupId        int    `description:"分组id"`
	GroupName      string `json:"-" description:"分组名称"`
	Authority      int    `description:"管理权限，0：无，1：部门负责人，2：小组负责人"`
	OpenId         string `description:"openId"`
	CompanyName    string `description:"客户名称"`
	UserId         int    `description:"联系人id"`
	ShareSellerId  int    `description:"共享销售ID"`
}

func GetSellerByCompanyId(companyId int) (item *AdminItem, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := ` SELECT b.*,c.open_id,c.user_id,a.company_name FROM company_product AS a
			INNER JOIN admin AS b ON a.seller_id=b.admin_id
			LEFT JOIN wx_user AS c ON b.mobile=c.mobile
			WHERE a.company_id=? AND role_type_code LIKE '%rai%'`
	err = o.Raw(sql, companyId).QueryRow(&item)
	return
}

func GetSellerByCompanyIdCheckFicc(companyId, productId int) (item *AdminItem, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := ` SELECT b.*,c.open_id,c.user_id,a.company_name FROM company_product AS a
			INNER JOIN admin AS b ON a.seller_id=b.admin_id
			LEFT JOIN wx_user AS c ON b.mobile=c.mobile
			WHERE a.company_id=? AND a.product_id=?`
	err = o.Raw(sql, companyId, productId).QueryRow(&item)
	return
}

func GetSellerByName(userName string) (item *AdminItem, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := ` SELECT * FROM admin WHERE real_name=? `
	err = o.Raw(sql, userName).QueryRow(&item)
	return
}

func GetSellerByAdminId(adminId int) (item *AdminItem, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := ` SELECT * FROM admin WHERE admin_id=?  `
	err = o.Raw(sql, adminId).QueryRow(&item)
	return
}

// 获取本组的销售ID
func GetSelleridWhichGroup(companyId, productId int) (adminId string, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := ` SELECT
			GROUP_CONCAT( DISTINCT admin_id SEPARATOR ',' ) AS adminId
			FROM
			admin
			WHERE
			group_id IN (
			SELECT
			a.group_id
			FROM
			company_product AS a
			INNER JOIN admin AS b ON a.seller_id = b.admin_id
			WHERE
			a.company_id = ?
			AND a.product_id = ?
)`
	err = o.Raw(sql, companyId, productId).QueryRow(&adminId)
	return
}

func GetSellerByAdminMobile(mobile string) (item *AdminItem, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := ` SELECT * FROM admin WHERE mobile=?  `
	err = o.Raw(sql, mobile).QueryRow(&item)
	return
}

// GetAdminListByGroupId 根据分组id获取系统用户列表
func GetAdminListByGroupId(groupId int) (items []*Admin, err error) {
	sql := `SELECT * FROM admin WHERE group_id=? and enabled=1 `
	o := orm.NewOrmUsingDB("weekly_report")
	_, err = o.Raw(sql, groupId).QueryRows(&items)
	return
}

// 获取权益销售信息
func GetRaiSellerByCompanyId(companyId int) (item *AdminItem, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := ` SELECT
   			a.share_seller_id,
     		b.admin_id,
			b.real_name,
			b.mobile 
		FROM
			company_product AS a
			INNER JOIN admin AS b ON a.seller_id = b.admin_id 
		WHERE
			a.product_id = 2 
			AND a.company_id = ?`
	err = o.Raw(sql, companyId).QueryRow(&item)
	return
}
