package day_new

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

// 企业微信会话存档表
type WeworkMsg struct {
	Id         uint64    `orm:"column(id);pk;" description:"自增ID"`
	MsgId      string    `orm:"column(msg_id)" description:"企业微信消息id"`               //企业微信消息id，消息的唯一标识，企业可以使用此字段进行消息去重
	Action     string    `orm:"column(action)" description:"消息动作"`                   //消息动作，目前有send(发送消息)/recall(撤回消息)/switch(切换企业日志)三种类型。String类型
	From       string    `orm:"column(from)" description:"消息发送方id"`                  //消息发送方id。同一企业内容为userid，非相同企业为external_userid。消息如果是机器人发出，也为external_userid。String类型
	ToList     string    `orm:"column(to_list)" description:"消息接收方列表"`               //消息接收方列表，可能是多个，同一个企业内容为userid，非相同企业为external_userid。数组，内容为string类型
	RoomId     string    `orm:"column(room_id)" description:"群聊消息的群id"`              //群聊消息的群id。如果是单聊则为空。String类型
	MsgTime    int64     `orm:"column(msg_time)" description:"消息发送时间戳"`              //消息发送时间戳，utc时间，ms单位。
	MsgType    string    `orm:"column(msg_type)" description:"消息类型"`                 //消息类型文本消息为：text。
	Content    string    `orm:"column(content)" description:"消息内容"`                  //消息内容
	ContentEn  string    `orm:"column(content_en)" description:"翻译后的英文内容"`           //翻译后的英文内容
	ReportId   int64     `orm:"column(report_id)" description:"英文研报ID"`              //英文研报ID
	CreateTime time.Time `orm:"column(create_time)" description:"创建时间"`              //创建时间
	IsAdd      int8      `orm:"column(is_add)" description:"是否已加入到报告当中: 0未加入，1已加入 "` //是否已加入到报告当中: 0未加入，1已加入
	IsDelete   int8      `orm:"column(is_delete)" description:"是否被删除：0未删除，1已删除"`     //是否被删除：0未删除，1已删除
	ModifyTime time.Time `orm:"column(modify_time)" description:"修改时间"`              //修改时间
}

type WeworkMsgListItem struct {
	MsgId      string `description:"企业微信消息id"`
	Content    string `description:"消息内容"`
	ContentEn  string `description:"翻译后的英文内容"`
	CreateTime string `description:"创建时间"`
	IsAdd      int8   `description:"是否已加入到报告当中: 0未加入，1已加入 "`
	IsDelete   int8   `description:"是否被删除：0未删除，1已删除"`
	ModifyTime string `description:"修改时间"`
	MsgTime    string `description:"消息发送时间"`
	FromName   string `description:"消息发送者"`
}

// WeworkMsgListResp 分页列表响应体
type WeworkMsgListResp struct {
	List           []*WeworkMsgListItem
	Paging         *paging.PagingItem `description:"分页数据"`
	LastUpdateTime string             `description:"上次刷新时间"`
}

type DeleteWeworkMsgReq struct {
	MsgId      string `description:"企业微信消息id"`
}

// AddWeworkMsg 新增消息
func AddWeworkMsg(item *WeworkMsg) (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(&item)
	return
}

// AddWeworkMsgMulti 批量新增消息
func AddWeworkMsgMulti(list []*WeworkMsg) (err error) {
	o := orm.NewOrm()
	_, err = o.InsertMulti(len(list), list)
	return
}

// GetWeworkMsgList 获取企业微信群消息列表
func GetWeworkMsgList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*WeworkMsg, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM wework_msg WHERE 1 = 1 `
	sql += condition
	totalSQl := `SELECT COUNT(1) total FROM (` + sql + `) z`
	if err = o.Raw(totalSQl, pars).QueryRow(&total); err != nil {
		return
	}
	sql += ` ORDER BY msg_time DESC, id DESC`
	sql += ` LIMIT ?,?`
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
	return
}

// GetWeworkMsgByCondition 获取企业微信群消息列表
func GetWeworkMsgByCondition(condition string, pars []interface{}) (list []*WeworkMsg, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM wework_msg WHERE 1 = 1 `
	sql += condition
	sql += ` ORDER BY id asc`
	_, err = o.Raw(sql, pars).QueryRows(&list)
	return
}

// GetWeworkMsgByConditionLimit 获取企业微信群消息列表
func GetWeworkMsgByConditionLimit(condition string, pars []interface{}, limit int) (list []*WeworkMsg, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM wework_msg WHERE 1 = 1 `
	sql += condition
	sql += ` ORDER BY id asc LIMIT 0, ?`
	_, err = o.Raw(sql, pars, limit).QueryRows(&list)
	return
}

func MultiUpdateContentEn(multiSql string, updateSendIds string, pars []interface{}) (err error)  {
	o := orm.NewOrm()
	sql := `UPDATE wework_msg 
SET modify_time=NOW(), content_en =
CASE id `+multiSql+` END 
WHERE 
	id IN (`+updateSendIds+` ) `
	_, err = o.Raw(sql, pars).Exec()
	return
}

// DeleteWeworkMsgByMsgId 删除消息
func DeleteWeworkMsgByMsgId(msgId string) (err error)  {
	o := orm.NewOrm()
	sql := `UPDATE wework_msg 
SET modify_time=NOW(), is_delete = 1
WHERE 
	 msg_id = ?`
	_, err = o.Raw(sql, msgId).Exec()
	return
}

// GetWeworkMsgByMsgId 根据消息Id查询
func GetWeworkMsgByMsgId(msgId string) (item *WeworkMsg, err error)  {
	o := orm.NewOrm()
	sql := `select * from wework_msg where msg_id = ?`
	err = o.Raw(sql, msgId).QueryRow(&item)
	return
}

type AddEnglishReportReq struct {
	ClassifyIdFirst    int    `description:"一级分类id"`
	ClassifyNameFirst  string `description:"一级分类名称"`
	ClassifyIdSecond   int    `description:"二级分类id"`
	ClassifyNameSecond string `description:"二级分类名称"`
	Title              string `description:"标题"`
	Abstract           string `description:"摘要"`
	Author             string `description:"作者"`
	Frequency          string `description:"频度"`
}

// UpdateWeworkMsgIsAdd 更新消息状态为已加入报告
func UpdateWeworkMsgIsAdd(msgIds string, reportId int64) (err error)  {
	o := orm.NewOrm()
	sql := `UPDATE wework_msg 
SET modify_time=NOW(), is_add = 1, report_id= ?
WHERE 
	 msg_id in (`+msgIds+`) and is_delete = 0 and is_add = 0`
	_, err = o.Raw(sql, reportId).Exec()
	return
}