package cygx

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

//用户交流反馈表

// 权益服务明细表
type CygxUserFeedback struct {
	UserFeedbackId int       `orm:"column(user_feedback_id);pk";comment:"主键id"`
	Content        string    `comment:"服务内容说明"`
	UserId         int       `comment:"用户ID"`
	Mobile         string    `comment:"手机号"`
	Email          string    `comment:"邮箱"`
	CompanyId      int       `comment:"公司ID"`
	CompanyName    string    `comment:"公司名称"`
	RealName       string    `comment:"用户实际名称"`
	AdminId        int       `comment:"操作人ID"`
	CreateTime     time.Time `comment:"创建时间"`
}

// 权益服务明细表
type CygxUserFeedbackResp struct {
	UserFeedbackId int    `comment:"主键id"`
	Content        string `comment:"服务内容说明"`
	CreateTime     string `comment:"创建时间"`
}

type AddCygxUserFeedbackReq struct {
	Content string `comment:"服务内容说明"`
	UserId  int    `comment:"用户ID"`
}

type CygxUserFeedbackIdReq struct {
	UserFeedbackId int `comment:"UserFeedbackId 主键ID"`
}

// 添加
func AddCygxUserFeedback(item *CygxUserFeedback) (newId int64, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	newId, err = o.Insert(item)
	return
}

// 删除数据
func DeleteCygxUserFeedback(userFeedbackId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` DELETE FROM cygx_user_feedback WHERE user_feedback_id = ?  `
	_, err = o.Raw(sql, userFeedbackId).Exec()
	return
}

// 列表
func GetCygxUserFeedbackListByUserId(userId int) (items []*CygxUserFeedbackResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_user_feedback WHERE  user_id = ? `
	sql += `  ORDER BY user_feedback_id DESC  LIMIT  1000 `
	_, err = o.Raw(sql, userId).QueryRows(&items)
	return
}

type CygxCygxUserFeedbackResp struct {
	List []*CygxUserFeedbackResp
}

func GetCygxUserFeedbackList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxUserFeedback, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT *
			FROM
			cygx_user_feedback
			WHERE 1 = 1 ` + condition
	sql += ` LIMIT ?,?  `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func GetCygxUserFeedbackListInit() (items []*CygxUserFeedback, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT *
			FROM
			cygx_user_feedback
			WHERE 1 = 1 `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 获取数量
func GetCygxUserFeedbackCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_user_feedback WHERE   1= 1  ` + condition
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 用户交流反馈
func GetCygxUserFeedback(condition string, startSize, pageSize int) (total int, items []*UserInteraction, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	var sql string
	sql += `SELECT
			r.content as key_word,
			r.user_id,
		    r.mobile,
			r.real_name,
			r.company_id,
			r.company_name,
			r.create_time
		FROM
			cygx_user_feedback AS r
		WHERE 1 = 1 ` + condition

	totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z `
	err = o.Raw(totalSql).QueryRow(&total)
	if err != nil {
		return
	}

	sql += ` ORDER BY create_time DESC `
	if startSize > 0 || pageSize > 0 {
		sql += ` LIMIT ` + strconv.Itoa(startSize) + "," + strconv.Itoa(pageSize)
	}
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 用户交流反馈
func GetCygxUserFeedbackWeekly(condition string, startSize, pageSize int) (total int, items []*UserInteraction, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	var sql string
	sql += `SELECT
			r.content as key_word,
			r.user_id,
		    r.mobile,
			r.real_name,
			r.company_id,
			r.company_name,
			r.create_time
		FROM
			cygx_user_feedback AS r
		   INNER JOIN %s.user_seller_relation AS us ON us.user_id = r.user_id   AND us.product_id = 2
		WHERE 1 = 1 ` + condition
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z `
	err = o.Raw(totalSql).QueryRow(&total)
	if err != nil {
		return
	}
	sql += ` ORDER BY create_time DESC `
	if startSize > 0 || pageSize > 0 {
		sql += ` LIMIT ` + strconv.Itoa(startSize) + "," + strconv.Itoa(pageSize)
	}
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 获取数量
func GetCygxUserFeedbackCountByUserModel(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_user_feedback as r
                  WHERE 1= 1  `
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 获取数量
func GetCygxUserFeedbackCountByUserModelWeekly(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM 	cygx_user_feedback AS r
			INNER JOIN %s.user_seller_relation AS us ON us.user_id = r.user_id   AND us.product_id = 2 
		WHERE 1 = 1  `
	databaseName := utils.GetWeeklyDatabase()
	sqlCount = fmt.Sprintf(sqlCount, databaseName)
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}
