package cygx

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"`
}

func GetSellerByCompanyId(companyId int) (item *AdminItem, err error) {
	o := orm.NewOrm()
	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.NewOrm()
	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.NewOrm()
	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.NewOrm()
	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.NewOrm()
	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
}

type CompanyProductDetial struct {
	IsSuspend int    `description:"1:暂停,0:启用"`
	Scale     string `description:"管理规模，空不填，1：:50亿以下，2：50~100亿，3：100亿以上。多个用, 隔开"`
}

// 获取对应销售以及销售的手机号
func GetCompanyProductDetail(companyId, productId int) (item *CompanyProductDetial, err error) {
	sql := ` SELECT * FROM company_product WHERE company_id = ? AND product_id = ?; `
	o := orm.NewOrm()
	err = o.Raw(sql, companyId, productId).QueryRow(&item)
	return
}

func GetCompanyDetailById(companyId int) (item *CompanyDetail, err error) {
	sql := ` SELECT a.company_id,a.company_name,b.status,b.seller_id,b.seller_name,c.mobile,p.product_id
            FROM company AS a
			INNER JOIN company_product AS b ON a.company_id=b.company_id
			INNER JOIN company_report_permission AS p ON p.company_id = a.company_id
			INNER JOIN chart_permission AS cp ON cp.chart_permission_id=p.chart_permission_id
			LEFT JOIN admin AS c ON b.seller_id=c.admin_id
			WHERE a.company_id=? AND cp.cygx_auth=1
			OR (a.company_id = ? AND  cp.permission_name = '策略' ) ORDER BY  b.product_id  DESC LIMIT 1` // 兼容永续状态
	o := orm.NewOrm()
	err = o.Raw(sql, companyId, companyId).QueryRow(&item)
	return
}
