package yb

import (
	"encoding/json"
	"github.com/rdlucklib/rdluck_tools/paging"
	"hongze/hz_crm_api/controllers"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/yb/request"
	"hongze/hz_crm_api/models/yb/response"
	"hongze/hz_crm_api/services/yb"
	"hongze/hz_crm_api/utils"
)

// 研报弹幕
type BulletChatController struct {
	controllers.BaseAuthController
}

// List
// @Title 弹幕列表
// @Description 弹幕列表
// @Param   Source		query	int		false	"来源：1-视频社区; 2-线上路演"
// @Param   Keywords	query	string	false	"关键词：弹幕内容/视频标题"
// @Success 200 {object} response.CommunityQuestionListResp
// @router /bullet_chat/list [get]
func (this *BulletChatController) List() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	source, _ := this.GetInt("Source", 0)
	keywords := this.GetString("Keywords", "")

	condition := ""
	pars := make([]interface{}, 0)
	if source > 0 {
		condition += ` AND source = ?`
		pars = append(pars, source)
	}
	if keywords != "" {
		k := "%" + keywords + "%"
		condition += ` AND (title LIKE ? OR content LIKE ?)`
		pars = append(pars, k, k)
	}

	var startSize int
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = paging.StartIndex(currentIndex, pageSize)

	total, list, err := yb.GetBulletChatList(condition, pars, startSize, pageSize)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取弹幕列表失败, ErrMsg:" + err.Error()
		return
	}

	page := paging.GetPaging(currentIndex, pageSize, total)
	resp := &response.BulletChatListResp{
		Paging: page,
		List:   list,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// Delete
// @Title 删除弹幕
// @Description 删除弹幕
// @Param	request  body  request.CommunityVideoOptionReq  true  "type json string"
// @Success 200 string "操作成功"
// @router /bullet_chat/delete [post]
func (this *BulletChatController) Delete() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req request.BulletChatDelReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.Id <= 0 {
		br.Msg = "参数有误"
		return
	}

	if e := yb.SoftDeleteBulletChat(sysUser.AdminId, req.Id); e != nil {
		br.Msg = "删除失败"
		br.ErrMsg = "删除弹幕失败, Err: " + e.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}
