package cygx

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

type ActivityHelpAsk struct {
	AskId       int    `orm:"column(ask_id);主键ID"`
	ActivityId  int    `description:"活动ID"`
	UserId      int    `description:"用户ID"`
	CreateTime  string `description:"创建时间"`
	Mobile      string `description:"手机号"`
	CompanyName string `description:"公司名称"`
	RealName    string `description:"姓名"`
	Content     string `description:"内容"`
}

type ActivityHelpAskListResp struct {
	List []*ActivityHelpAsk
}

// 列表
func GetActivityHelpAskList(activityId int) (items []*ActivityHelpAsk, err error) {
	o := orm.NewOrm()
	sql := `SELECT k.*,u.real_name
			FROM cygx_activity_help_ask  as k
			INNER JOIN wx_user as u ON u.mobile = k.mobile
			WHERE activity_id = ? ORDER BY create_time DESC`
	_, err = o.Raw(sql, activityId).QueryRows(&items)
	return
}

// 获取活动带问的用户的openID
func GetActivityHelpAskOpenIdList(activityId int) (items []*OpenIdList, err error) {
	var sql string
	if utils.RunMode == "release" {
		sql = `SELECT DISTINCT cr.open_id,u.user_id
			FROM
			cygx_activity_help_ask AS m
			INNER JOIN user_record AS u ON u.bind_account = m.mobile 
			INNER JOIN cygx_user_record AS cr ON cr.union_id = u.union_id 
			WHERE m.activity_id = ? AND u.create_platform = 4 `
	} else {
		sql = `SELECT
			u.open_id,
			u.user_id 
		FROM
			cygx_activity_help_ask AS s
			INNER JOIN wx_user AS wx ON wx.user_id = s.user_id
			INNER JOIN user_record AS u ON u.bind_account = wx.mobile
			INNER JOIN company_product AS p ON p.company_id = wx.company_id 
		WHERE
			s.activity_id = ? 
			AND u.create_platform = 1 
			AND p.STATUS IN ( '正式', '试用', '永续' ) 
		GROUP BY
			u.open_id`
	}
	_, err = orm.NewOrm().Raw(sql, activityId).QueryRows(&items)
	return
}
