// @Author gmy 2024/9/21 14:47:00
package models

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

/**
CREATE TABLE `user_collect_classify` (
  `user_collect_classify_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `classify_id` int(9) unsigned NOT NULL DEFAULT '0' COMMENT '分类id',
  `sys_user_id` int(9) unsigned DEFAULT '0' COMMENT '收藏人用户id',
  `create_time` datetime DEFAULT NULL COMMENT '收藏时间',
  PRIMARY KEY (`user_collect_classify_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户收藏的分类id';
*/

type UserCollectClassify struct {
	UserCollectClassifyId int    `orm:"column(user_collect_classify_id);pk" description:"用户收藏的分类id"`
	ClassifyId            int    `orm:"column(classify_id)" description:"分类id"`
	SysUserId             int    `orm:"column(sys_user_id)" description:"收藏人用户id"`
	CreateTime            string `orm:"column(create_time)" description:"收藏时间"`
}

// GetUserCollectClassifyList 查询用户收藏的分类列表
func GetUserCollectClassifyList(sysUserId, classifyId int) (list []*UserCollectClassify, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM user_collect_classify WHERE 1=1`
	var params []interface{}

	// 处理 sysUserId 参数
	if sysUserId > 0 {
		sql += ` AND sys_user_id=?`
		params = append(params, sysUserId)
	}

	// 处理 classifyId 参数
	if classifyId > 0 {
		sql += ` AND classify_id=?`
		params = append(params, classifyId)
	}

	_, err = o.Raw(sql, params...).QueryRows(&list)

	return
}

// InsertUserCollectClassify 新增用户收藏的分类
func InsertUserCollectClassify(item UserCollectClassify) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `INSERT INTO user_collect_classify(classify_id, sys_user_id, create_time) VALUES(?, ?, ?)`
	_, err = o.Raw(sql, item.ClassifyId, item.SysUserId, item.CreateTime).Exec()
	return
}

// DeleteUserCollectClassify 删除分类
func DeleteUserCollectClassify(sysUserId, classifyId int) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `DELETE FROM user_collect_classify WHERE sys_user_id=? AND classify_id=?`
	_, err = o.Raw(sql, sysUserId, classifyId).Exec()
	return
}
