package cygx

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

type RaiServeTypeResp struct {
	ServeTypeId   int    `description:"服务类型id"`
	ServeTypeName string `description:"服务类型名称"`
}

type RaiServeTypeListResp struct {
	List []*RaiServeTypeResp
}

// 服务类型列表
func GetRaiServeTypeRespList(condition string) (items []*RaiServeTypeResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_rai_serve_type WHERE  1= 1 `
	if condition != "" {
		sql += condition
	}
	sql += `  ORDER BY sort DESC  LIMIT  100 `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type RaiServeTagResp struct {
	TagType int    `description:"标签类型"`
	TagId   int    `description:"标签ID"`
	TagName string `description:"标签名称"`
	Md5Key  string `description:"加密key,前端找参数当唯一索引值使用"`
}

type ChekChartPermissionNameResp struct {
	ChartPermissionName string `description:"权限名称"`
	Belong              bool   `description:"权限名称"`
	List                []*RaiServeTagResp
}

type RaiServeTagListResp struct {
	List []*RaiServeTagResp
}

type RaiServeCoverageRateResp struct {
	//List []string
	ThisWeekAmount  string `comment:"本周互动量"`
	LastWeekAmount  string `comment:"上周互动量"`
	TwoWeekAmount   string `comment:"上上周互动量"`
	ThreeWeekAmount string `comment:"上三周互动量"`
}

// 服务类型列表
func GetRaiServeSearchTagRespList(keywords string) (items []*RaiServeTagResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
			1 AS tag_type,
			i.industrial_management_id AS tag_id,
			i.industry_name AS tag_name,
			i.create_time 
		FROM
			cygx_industrial_management AS i 
		WHERE
			1 = 1 
			AND i.chart_permission_id IN ( 19, 20, 21, 22 ) 
			AND  (i.industry_name LIKE '%` + keywords + `%' ) UNION ALL
		SELECT
			2 AS tag_type,
			s.industrial_subject_id AS tag_id,
			s.subject_name AS tag_name,
			s.create_time 
		FROM
			cygx_industrial_subject AS s
			INNER JOIN cygx_industrial_management AS i ON i.industrial_management_id = s.industrial_management_id 
		WHERE
			1 = 1 
			AND i.chart_permission_id IN ( 19, 20, 21, 22 ) 
			AND ( s.subject_name LIKE  '%` + keywords + `%') 
		ORDER BY
			create_time ASC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func GetRaiServeSearchTagRespListBycharId(keywords, charIds, conditionindustrial, conditionsubject string) (items []*RaiServeTagResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
			1 AS tag_type,
			i.industrial_management_id AS tag_id,
			i.industry_name AS tag_name,
			i.create_time 
		FROM
			cygx_industrial_management AS i 
		WHERE
			1 = 1 
			AND i.chart_permission_id IN (` + charIds + ` ) 
		AND  (i.industry_name LIKE '%` + keywords + `%' ` + conditionindustrial + ` )  UNION ALL
		SELECT
			2 AS tag_type,
			s.industrial_subject_id AS tag_id,
			s.subject_name AS tag_name,
			s.create_time 
		FROM
			cygx_industrial_subject AS s
			INNER JOIN cygx_industrial_management AS i ON i.industrial_management_id = s.industrial_management_id 
		WHERE
			1 = 1 
			AND i.chart_permission_id IN (` + charIds + ` ) 
			AND ( s.subject_name LIKE  '%` + keywords + `%' ` + conditionsubject + ` ) 
		ORDER BY
			create_time ASC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 权益服务明细表
type CygxRaiServeBillResp struct {
	Content             string  `comment:"服务内容说明"`
	ServeTypeName       string  `comment:"服务类型"`
	Mobile              string  `comment:"手机号"`
	Email               string  `comment:"邮箱"`
	RealName            string  `comment:"用户实际名称"`
	ServeCount          float64 `comment:"服务量小计"`
	Tag                 string  `comment:"标签，多个用 , 隔开"`
	IsKp                int     `comment:"是否是KP,1:是、0：否"`
	SourceId            int     `comment:"来源ID"`
	Source              string  `comment:"来源 "`
	ViewTime            string  `comment:"浏览时间"`
	ChartPermissionId   int     `description:"行业id"`
	ChartPermissionName string  `description:"行业名称"`
}

type CygxRaiServeBillListResp struct {
	Paging *paging.PagingItem `description:"分页数据"`
	List   []*CygxRaiServeBillResp
}

func GetCygxRaiServeBillCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_rai_serve_bill as art WHERE 1= 1  `
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 列表
func GetCygxRaiServeBillRespList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxRaiServeBillResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_rai_serve_bill as art WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY bill_id DESC   LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

// 权益服务明细表
type CygxRaiServeBill struct {
	BillId           int       `orm:"column(bill_id);pk" description:"服务明细主键ID"`
	Content          string    `comment:"服务内容说明"`
	ServeTypeId      int       `comment:"服务类型ID"`
	ServeTypeName    string    `comment:"服务类型"`
	UserId           int       `comment:"用户ID"`
	Mobile           string    `comment:"手机号"`
	Email            string    `comment:"邮箱"`
	CompanyId        int       `comment:"公司ID"`
	CompanyName      string    `comment:"公司名称"`
	RealName         string    `comment:"用户实际名称"`
	RegisterPlatform int       `comment:"来源 1小程序，2：网页"`
	ServeCount       float64   `comment:"服务量小计"`
	IsKp             int       `comment:"是否是KP,1:是、0：否"`
	SourceId         int       `comment:"来源ID"`
	Source           string    `comment:"来源 "`
	WeekStartDate    string    `comment:"周一开始日期"`
	WeekEndDate      string    `comment:"周日结束日期"`
	CreateTime       time.Time `comment:"创建时间"`
	ViewTime         string    `comment:"浏览时间"`
}

// 列表
func GetCygxRaiServeBillListAll(condition string, pars []interface{}) (items []*CygxRaiServeBill, err error) {
	if condition == "" {
		return
	}
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_rai_serve_bill   WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}
