package roadshow

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

type CompanySearchView struct {
	CompanyId      int    `orm:"column(company_id);pk"`
	CompanyName    string `description:"客户名称"`
	CreditCode     string `description:"社会统一信用码"`
	CompanyCode    string `description:"客户编码"`
	EnglishCompany int    `description:"是否为英文客户"`
}

func CompanySearch(sellerId int, keyWord string) (list []*CompanySearchView, err error) {
	o := orm.NewOrm()
	sql := ` SELECT b.company_id,b.company_name FROM company AS a
			INNER JOIN company_product AS b ON a.company_id=b.company_id
			WHERE b.seller_id=?
			AND b.status IN('正式','试用')
            AND b.company_name LIKE ?
			`
	sql += ` GROUP BY b.company_id  `
	_, err = o.Raw(sql, sellerId, keyWord).QueryRows(&list)
	return
}

func CompanySearchV2(sellerId, groupId int, keyWord string) (list []*CompanySearchView, err error) {
	list = make([]*CompanySearchView, 0)

	// 中文客户
	companies := make([]*CompanySearchView, 0)
	o := orm.NewOrm()
	sql := `SELECT
				b.company_id,
				a.company_name,
				0 AS english_company
			FROM
				company AS a
			INNER JOIN company_product AS b ON a.company_id = b.company_id 
			WHERE
				b.status IN ('正式', '试用', '永续') AND (b.seller_id = ? OR b.share_seller_id = ?  OR b.group_id = ? OR b.share_group_id = ?  ) AND a.company_name LIKE ?
			GROUP BY
				b.company_id `
	_, err = o.Raw(sql, sellerId, sellerId, groupId, groupId, keyWord).QueryRows(&companies)
	if err != nil {
		return
	}
	list = append(list, companies...)

	// 英文客户
	enCompanies := make([]*CompanySearchView, 0)
	o2 := orm.NewOrmUsingDB("rddp")
	sql = `SELECT
				company_id,
				company_name,
				1 AS english_company
			FROM
				english_company
			WHERE
				is_deleted = 0 AND enabled > 0 AND company_name LIKE ?`
	_, err = o2.Raw(sql, keyWord).QueryRows(&enCompanies)
	if err != nil {
		return
	}
	list = append(list, enCompanies...)
	return
}

type CompanyDetailView struct {
	CompanyId        int    `orm:"column(company_id);pk"`
	CompanyName      string `description:"客户名称"`
	Status           string `description:"客户状态"`
	IndustryId       int    `description:"行业id"`
	IndustryName     string `description:"行业名称"`
	PermissionName   string `description:"开通品种"`
	ReportReadTotal  int    `description:"累计阅读次数"`
	EnglishCompany   int    `description:"是否为英文客户: 0-否; 1-是"`
	EnglishCountry   string `description:"英文客户-国家"`
	EnglishViewTotal int    `description:"英文客户-累计点击量"`
}
