package company

import (
	"github.com/beego/beego/v2/client/orm"
	"hongze/hz_crm_api/utils"
)

type CompanySellers struct {
	AdminId   int    `description:"系统用户id"`
	AdminName string `description:"系统用户名称"`
	RealName  string `description:"用户真实名称"`
}

func GetCompanySeller(roleTypeCode string) (items []*CompanySellers, err error) {
	o := orm.NewOrm()
	sql := ` SELECT a.admin_id,a.admin_name,real_name FROM admin AS a
			INNER JOIN sys_role AS b ON a.role_id=b.role_id
			WHERE enabled=1 AND b.role_type_code IN(` + roleTypeCode + `) ORDER BY created_time DESC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type CompanySellerResp struct {
	List []*CompanySellers
}

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

type DepartmentGroupSellers struct {
	AdminId string `description:"系统用户id"`
	//AdminName string `description:"系统用户名称"`
	RealName     string                   `description:"用户真实名称"`
	ChildrenList []DepartmentGroupSellers `description:"销售列表"`
	RoleTypeCode string                   `description:"角色编码"`
	Authority    int                      `description:"管理权限，0：无，1：部门负责人，2：小组负责人,或者ficc销售主管，4:ficc销售组长"`
}

func GetCompanyCheckSeller(condition string, pars []interface{}) (items []*CompanySellers, err error) {
	o := orm.NewOrm()
	sql := ` SELECT a.admin_id,a.admin_name,real_name FROM admin AS a
			INNER JOIN sys_role AS b ON a.role_id=b.role_id
			WHERE enabled=1 AND b.role_type_code IN(` + utils.ROLE_TYPE_SELLERS + `)  `
	if condition != "" {
		sql += condition
	}
	sql += `ORDER BY created_time DESC`
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// 根据企业id获取对应的销售信息
func GetCompanySellerByCompanyId(companyId int) (items []*CompanySellers, err error) {
	o := orm.NewOrm()
	sql := ` SELECT a.admin_id,a.admin_name,real_name FROM admin AS a
			INNER JOIN sys_role AS b ON a.role_id=b.role_id
			INNER JOIN company_product AS c ON a.admin_id=c.seller_id
			WHERE enabled=1 AND c.company_id=? ORDER BY created_time DESC `
	_, err = o.Raw(sql, companyId).QueryRows(&items)
	return
}

// GetCompanySellerByRoleCodes
func GetCompanySellerByRoleCodes(roleTypeCodes []string) (items []*CompanySellers, err error) {
	itemsLen := len(roleTypeCodes)
	if itemsLen == 0 {
		return
	}
	o := orm.NewOrm()
	sql := `SELECT
				a.admin_id,
				a.admin_name,
				real_name
			FROM
				admin AS a
			INNER JOIN sys_role AS b ON a.role_id = b.role_id
			WHERE
				enabled = 1 AND b.role_type_code IN (` + utils.GetOrmInReplace(itemsLen) + `)
			ORDER BY
				created_time DESC `
	_, err = o.Raw(sql, roleTypeCodes).QueryRows(&items)
	return
}
