package yb

import (
	"encoding/json"
	"fmt"
	"github.com/rdlucklib/rdluck_tools/paging"
	"hongze/hz_crm_api/cache"
	"hongze/hz_crm_api/controllers"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/yb"
	"hongze/hz_crm_api/models/yb/request"
	ybResp "hongze/hz_crm_api/models/yb/response"
	ybService "hongze/hz_crm_api/services/yb"
	"hongze/hz_crm_api/utils"
	"strconv"
	"strings"
	"time"
)

type CommentController struct {
	controllers.BaseAuthController
}

// ReportList
// @Title 获取留言管理下的报告列表
// @Description 获取留言管理下的报告列表
// @Param   Keyword            query   string    false		"搜索关键字 string"
// @Param   SysIsRead          query   string    false		"管理员是否已读 0-未读，1-已读"
// @Param   ClassifyNameFirst  query   string    false      "报告一级分类名称"
// @Param   ReportId  query   int    false      "报告id"
// @Success 200 {object}  ybResp.CommentReportResp
// @router /comment/report/list [get]
func (this *CommentController) ReportList() {
	br := new(models.BaseResponse).Init()
	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
	}

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

	// 判断查询权限
	brMsg, brErrMsg := ybService.CheckCommentPermission(sysUser.AdminId)
	if brMsg != "" {
		br.Msg = brMsg
		br.ErrMsg = brErrMsg
		return
	}
	//构建查询条件查询有留言的 reportId
	condition := ""
	var pars []interface{}
	reqUnRead := this.GetString("UnRead", "")
	if reqUnRead != "" {
		if reqUnRead != "1" {
			br.Msg = "请输入正确的未读标识"
			return
		}
		condition += " AND c.sys_is_read = 0"
	}
	reqKeyword := this.GetString("Keyword")
	if reqKeyword != "" {
		if strings.Contains("匿名用户", reqKeyword) {
			condition += " AND c.is_show_name = 0"
		} else if strings.Contains(reqKeyword, "匿名用户") {
			reqKeywordNum := reqKeyword[12:]
			condition += " AND c.is_show_name = 0 AND (c.user_id+3333) = ?"
			pars = append(pars, reqKeywordNum)
		} else {
			reqKeyword = "%" + reqKeyword + "%"
			condition += " AND (u.nick_name LIKE ? OR c.content LIKE ?) AND c.is_show_name = 1"
			pars = append(pars, reqKeyword, reqKeyword)
		}
	}

	// 指定报告id
	reportId, _ := this.GetInt("ReportId")
	if reportId > 0 {
		condition += " AND c.report_id = ? "
		pars = append(pars, reportId)
	}

	commentList, err := yb.GetCommentReportIdsByCondition(condition, pars)
	if err != nil {
		br.Msg = "获取报告ID失败！"
		br.ErrMsg = "获取报告ID失败，Err:" + err.Error()
		return
	}

	var reportIdsAllStr string
	reportIdsAllMap := make(map[int][]int)
	//报告id列表，用作排序
	reportIdMap := make(map[int]int)
	reportIdList := make([]int, 0)
	for _, v := range commentList {
		if _, ok := reportIdMap[v.ReportId]; !ok {
			reportIdMap[v.ReportId] = v.ReportId
			reportIdList = append(reportIdList, v.ReportId)
		}

		chapters := make([]int, 0)
		var ok bool
		if chapters, ok = reportIdsAllMap[v.ReportId]; ok {
			if v.ReportChapterId > 0 {
				chapters = append(chapters, v.ReportChapterId)
				reportIdsAllMap[v.ReportId] = chapters
			}
		} else {
			reportIdsAllStr += "," + strconv.Itoa(v.ReportId)

		}
		if v.ReportChapterId > 0 {
			chapters = append(chapters, v.ReportChapterId)
			reportIdsAllMap[v.ReportId] = chapters
		}
	}

	//根据reportId查询报告列表
	reportCondition := ""
	var reportPars []interface{}

	if reportIdsAllStr == "" {
		//没有查到相关的报告ID
		page := paging.GetPaging(currentIndex, pageSize, 0)
		resp := ybResp.CommentReportResp{
			Paging: page,
		}
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = resp
		return
	}
	reportIdsAllStr = strings.Trim(reportIdsAllStr, ",")
	reportCondition += " and id in (" + reportIdsAllStr + ")"

	classifyIdFirst := this.GetString("ClassifyIdFirst", "")
	if classifyIdFirst != "" {
		reportCondition += " AND classify_id_first = ? "
		reportPars = append(reportPars, classifyIdFirst)
	}
	classifyIdSecond := this.GetString("ClassifyIdSecond", "")
	if classifyIdSecond != "" {
		reportCondition += " AND classify_id_second = ? "
		reportPars = append(reportPars, classifyIdSecond)
	}
	//reportIdList
	orderBy := ``
	if len(reportIdList) > 0 {
		orderByIdStr := ``
		for _, reportId := range reportIdList {
			orderByIdStr += fmt.Sprintf("'%d',", reportId)
		}
		orderByIdStr = strings.TrimSuffix(orderByIdStr, ",")
		orderBy = `ORDER BY FIELD(id,` + orderByIdStr + `)`
	}
	reportList, err := models.GetCommentReportByReportIdOrderBy(reportCondition, reportPars, startSize, pageSize, orderBy)
	if err != nil {
		br.Msg = "获取报告列表失败！"
		br.ErrMsg = "获取报告列表失败，Err:" + err.Error()
		return
	}
	//汇总列表中的reportId，reportChapterId
	var reportIdListStr string
	var reportChapterIdStr string
	for _, v := range reportList {
		reportIdListStr += "," + strconv.Itoa(v.Id)
		if chapters, ok := reportIdsAllMap[v.Id]; ok {
			for _, chapterId := range chapters {
				reportChapterIdStr += "," + strconv.Itoa(chapterId)
			}
		}
	}
	if reportIdListStr != "" {
		reportIdListStr = strings.Trim(reportIdListStr, ",")
	}
	if reportChapterIdStr != "" {
		reportChapterIdStr = strings.Trim(reportChapterIdStr, ",")
	}

	//查询留言总数和未读数
	reportTotalMap := make(map[int]int)
	reportChapterTotalMap := make(map[int]int)
	reportChapterUnreadTotalMap := make(map[int]int)
	reportUnreadTotalMap := make(map[int]int)
	var commentStatList []*yb.ReportCommentNum
	if reportIdListStr != "" || reportChapterIdStr != "" {
		commentStatList, err = yb.StatReportCommentNum(reportIdListStr, reportChapterIdStr, condition, pars)
		if err != nil {
			br.Msg = "获取报告留言统计数据失败！"
			br.ErrMsg = "获取报告留言统计数据失败，Err:" + err.Error()
			return
		}
		for _, stat := range commentStatList {
			reportTotalMap[stat.ReportId] += stat.Num
			reportChapterTotalMap[stat.ReportChapterId] += stat.Num
			if stat.SysIsRead == 0 {
				reportUnreadTotalMap[stat.ReportId] += stat.Num
				reportChapterUnreadTotalMap[stat.ReportChapterId] += stat.Num
			}
		}
	}

	//查询章节列表,
	reportChapterMap := make(map[int][]*ybResp.CommentReportChapterItem)
	//{
	//	var reportChapter []*models.ReportChapter
	//	if reportChapterIdStr != "" {
	//		reportChapter, err = models.GetChapterSimpleListByChapterIds(reportChapterIdStr)
	//	} else {
	//		if reportIdListStr != "" {
	//			reportChapter, err = models.GetChapterSimpleListByReportIds(reportIdListStr)
	//		}
	//	}
	//
	//	if err != nil {
	//		br.Msg = "获取报告章节列表失败！"
	//		br.ErrMsg = "获取报告列表失败，Err:" + err.Error()
	//		return
	//	}
	//	for _, chapter := range reportChapter {
	//		tmp := new(ybResp.CommentReportChapterItem)
	//		tmp.ReportChapterId = chapter.ReportChapterId
	//		tmp.Title = chapter.Title
	//		tmp.PublishTime = chapter.PublishTime.Format(utils.FormatDateTime)
	//		tmp.CreateTime = chapter.CreateTime
	//		tmp.TypeName = chapter.TypeName
	//		if num, ok := reportChapterTotalMap[chapter.ReportChapterId]; ok {
	//			tmp.CommentNum = num
	//		}
	//		if num, ok := reportChapterUnreadTotalMap[chapter.ReportChapterId]; ok {
	//			tmp.CommentUnreadNum = num
	//		}
	//		reportChapterMap[chapter.ReportId] = append(reportChapterMap[chapter.ReportId], tmp)
	//	}
	//}

	//fmt.Println(reportIdList)
	//fmt.Println(len(reportIdList))
	// 处理返回值
	var list []*ybResp.CommentReportItem
	for _, item := range reportList {
		tmp := new(ybResp.CommentReportItem)
		tmp.ReportId = item.Id
		tmp.CreateTime = item.CreateTime
		tmp.PublishTime = item.PublishTime.Format(utils.FormatDateTime)
		tmp.State = item.State
		if item.State != 2 {
			tmp.PublishTime = ""
		}
		tmp.Title = item.Title
		tmp.ClassifyNameFirst = item.ClassifyNameFirst
		tmp.ClassifyNameSecond = item.ClassifyNameSecond
		tmp.ClassifyIdFirst = item.ClassifyIdFirst
		tmp.ClassifyIdSecond = item.ClassifyIdSecond
		if chapters, ok := reportChapterMap[item.Id]; ok {
			tmp.ReportChapterList = chapters
		}
		if num, ok := reportTotalMap[item.Id]; ok {
			tmp.CommentNum = num
		}
		if num, ok := reportUnreadTotalMap[item.Id]; ok {
			tmp.CommentUnreadNum = num
		}
		list = append(list, tmp)
		//commentReportItemMap[item.Id] = tmp
	}

	// 获取列表
	total, err := models.GetCommentReportTotalByReportId(reportCondition, reportPars)
	if err != nil {
		br.Msg = "获取报告列表总数失败！"
		br.ErrMsg = "获取报告列表总数失败，Err:" + err.Error()
		return
	}

	page := paging.GetPaging(currentIndex, pageSize, int(total))
	resp := ybResp.CommentReportResp{
		List:   list,
		Paging: page,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
func (this *CommentController) ReportListBak() {
	br := new(models.BaseResponse).Init()
	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
	}

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

	// 判断查询权限
	brMsg, brErrMsg := ybService.CheckCommentPermission(sysUser.AdminId)
	if brMsg != "" {
		br.Msg = brMsg
		br.ErrMsg = brErrMsg
		return
	}
	//构建查询条件查询有留言的 reportId
	condition := ""
	var pars []interface{}
	reqUnRead := this.GetString("UnRead", "")
	if reqUnRead != "" {
		if reqUnRead != "1" {
			br.Msg = "请输入正确的未读标识"
			return
		}
		condition += " AND c.sys_is_read = 0"
	}
	reqKeyword := this.GetString("Keyword")
	if reqKeyword != "" {
		if strings.Contains("匿名用户", reqKeyword) {
			condition += " AND c.is_show_name = 0"
		} else if strings.Contains(reqKeyword, "匿名用户") {
			reqKeywordNum := reqKeyword[12:]
			condition += " AND c.is_show_name = 0 AND (c.user_id+3333) = ?"
			pars = append(pars, reqKeywordNum)
		} else {
			reqKeyword = "%" + reqKeyword + "%"
			condition += " AND (u.nick_name LIKE ? OR c.content LIKE ?) AND c.is_show_name = 1"
			pars = append(pars, reqKeyword, reqKeyword)
		}
	}

	// 指定报告id
	reportId, _ := this.GetInt("ReportId")
	if reportId > 0 {
		condition += " AND c.report_id = ? "
		pars = append(pars, reportId)
	}

	commentList, err := yb.GetCommentReportIdsByCondition(condition, pars)
	if err != nil {
		br.Msg = "获取报告ID失败！"
		br.ErrMsg = "获取报告ID失败，Err:" + err.Error()
		return
	}

	var reportIdsAllStr string
	reportIdsAllMap := make(map[int][]int)
	for _, v := range commentList {
		chapters := make([]int, 0)
		var ok bool
		if chapters, ok = reportIdsAllMap[v.ReportId]; ok {
			if v.ReportChapterId > 0 {
				chapters = append(chapters, v.ReportChapterId)
				reportIdsAllMap[v.ReportId] = chapters
			}
		} else {
			reportIdsAllStr += "," + strconv.Itoa(v.ReportId)

		}
		if v.ReportChapterId > 0 {
			chapters = append(chapters, v.ReportChapterId)
			reportIdsAllMap[v.ReportId] = chapters
		}
	}

	//根据reportId查询报告列表
	reportCondition := ""
	var reportPars []interface{}

	if reportIdsAllStr == "" {
		//没有查到相关的报告ID
		page := paging.GetPaging(currentIndex, pageSize, 0)
		resp := ybResp.CommentReportResp{
			Paging: page,
		}
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = resp
		return
	}
	reportIdsAllStr = strings.Trim(reportIdsAllStr, ",")
	reportCondition += " and id in (" + reportIdsAllStr + ")"

	classifyIdFirst := this.GetString("ClassifyIdFirst", "")
	if classifyIdFirst != "" {
		reportCondition += " AND classify_id_first = ?"
		reportPars = append(reportPars, classifyIdFirst)
	}
	classifyIdSecond := this.GetString("ClassifyIdSecond", "")
	if classifyIdSecond != "" {
		reportCondition += " AND classify_id_second = ?"
		reportPars = append(reportPars, classifyIdSecond)
	}
	reportList, err := models.GetCommentReportByReportId(reportCondition, reportPars, startSize, pageSize)
	if err != nil {
		br.Msg = "获取报告列表失败！"
		br.ErrMsg = "获取报告列表失败，Err:" + err.Error()
		return
	}
	//汇总列表中的reportId，reportChapterId
	var reportIdListStr string
	var reportChapterIdStr string
	for _, v := range reportList {
		reportIdListStr += "," + strconv.Itoa(v.Id)
		if chapters, ok := reportIdsAllMap[v.Id]; ok {
			for _, chapterId := range chapters {
				reportChapterIdStr += "," + strconv.Itoa(chapterId)
			}
		}
	}
	if reportIdListStr != "" {
		reportIdListStr = strings.Trim(reportIdListStr, ",")
	}
	if reportChapterIdStr != "" {
		reportChapterIdStr = strings.Trim(reportChapterIdStr, ",")
	}

	//查询留言总数和未读数
	reportTotalMap := make(map[int]int)
	reportChapterTotalMap := make(map[int]int)
	reportChapterUnreadTotalMap := make(map[int]int)
	reportUnreadTotalMap := make(map[int]int)
	var commentStatList []*yb.ReportCommentNum
	if reportIdListStr != "" || reportChapterIdStr != "" {
		commentStatList, err = yb.StatReportCommentNum(reportIdListStr, reportChapterIdStr, condition, pars)
		if err != nil {
			br.Msg = "获取报告留言统计数据失败！"
			br.ErrMsg = "获取报告留言统计数据失败，Err:" + err.Error()
			return
		}
		for _, stat := range commentStatList {
			reportTotalMap[stat.ReportId] += stat.Num
			reportChapterTotalMap[stat.ReportChapterId] += stat.Num
			if stat.SysIsRead == 0 {
				reportUnreadTotalMap[stat.ReportId] += stat.Num
				reportChapterUnreadTotalMap[stat.ReportChapterId] += stat.Num
			}
		}
	}

	//查询章节列表,
	reportChapterMap := make(map[int][]*ybResp.CommentReportChapterItem)
	//{
	//	var reportChapter []*models.ReportChapter
	//	if reportChapterIdStr != "" {
	//		reportChapter, err = models.GetChapterSimpleListByChapterIds(reportChapterIdStr)
	//	} else {
	//		if reportIdListStr != "" {
	//			reportChapter, err = models.GetChapterSimpleListByReportIds(reportIdListStr)
	//		}
	//	}
	//
	//	if err != nil {
	//		br.Msg = "获取报告章节列表失败！"
	//		br.ErrMsg = "获取报告列表失败，Err:" + err.Error()
	//		return
	//	}
	//	for _, chapter := range reportChapter {
	//		tmp := new(ybResp.CommentReportChapterItem)
	//		tmp.ReportChapterId = chapter.ReportChapterId
	//		tmp.Title = chapter.Title
	//		tmp.PublishTime = chapter.PublishTime.Format(utils.FormatDateTime)
	//		tmp.CreateTime = chapter.CreateTime
	//		tmp.TypeName = chapter.TypeName
	//		if num, ok := reportChapterTotalMap[chapter.ReportChapterId]; ok {
	//			tmp.CommentNum = num
	//		}
	//		if num, ok := reportChapterUnreadTotalMap[chapter.ReportChapterId]; ok {
	//			tmp.CommentUnreadNum = num
	//		}
	//		reportChapterMap[chapter.ReportId] = append(reportChapterMap[chapter.ReportId], tmp)
	//	}
	//}

	// 处理返回值
	var list []*ybResp.CommentReportItem
	for _, item := range reportList {
		tmp := new(ybResp.CommentReportItem)
		tmp.ReportId = item.Id
		tmp.CreateTime = item.CreateTime
		tmp.PublishTime = item.PublishTime.Format(utils.FormatDateTime)
		tmp.State = item.State
		if item.State != 2 {
			tmp.PublishTime = ""
		}
		tmp.Title = item.Title
		tmp.ClassifyNameFirst = item.ClassifyNameFirst
		tmp.ClassifyNameSecond = item.ClassifyNameSecond
		tmp.ClassifyIdFirst = item.ClassifyIdFirst
		tmp.ClassifyIdSecond = item.ClassifyIdSecond
		if chapters, ok := reportChapterMap[item.Id]; ok {
			tmp.ReportChapterList = chapters
		}
		if num, ok := reportTotalMap[item.Id]; ok {
			tmp.CommentNum = num
		}
		if num, ok := reportUnreadTotalMap[item.Id]; ok {
			tmp.CommentUnreadNum = num
		}
		list = append(list, tmp)
	}

	// 获取列表
	total, err := models.GetCommentReportTotalByReportId(reportCondition, reportPars)
	if err != nil {
		br.Msg = "获取报告列表总数失败！"
		br.ErrMsg = "获取报告列表总数失败，Err:" + err.Error()
		return
	}

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

// CommentList
// @Title 获取留言管理下的报告列表
// @Description 获取留言管理下的报告列表
// @Param   ReportId           query   string    true		"报告ID"
// @Param   ReportChapterId    query   string    true		"报告章节ID"
// @Success 200 {object}  ybResp.CommentListResp
// @router /comment/list [get]
func (this *CommentController) CommentList() {
	br := new(models.BaseResponse).Init()
	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
	}

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

	// 判断查询权限
	brMsg, brErrMsg := ybService.CheckCommentPermission(sysUser.AdminId)
	if brMsg != "" {
		br.Msg = brMsg
		br.ErrMsg = brErrMsg
		return
	}
	//根据搜索条件查询留言列表
	var (
		err             error
		hotTopType      int
		reportId        int
		reportChapterId int
	)
	condition := ""
	var pars []interface{}
	reqUnRead := this.GetString("UnRead", "")
	if reqUnRead != "" {
		if reqUnRead != "1" {
			br.Msg = "请输入正确的未读标识"
			return
		}
		condition += " AND c.sys_is_read = 0"
	}
	reqKeyword := this.GetString("Keyword")
	if reqKeyword != "" {
		if strings.Contains("匿名用户", reqKeyword) {
			condition += " AND c.is_show_name = 0"
		} else if strings.Contains(reqKeyword, "匿名用户") {
			reqKeywordNum := reqKeyword[12:]
			condition += " AND c.is_show_name = 0 AND (c.user_id+3333) = ?"
			pars = append(pars, reqKeywordNum)
		} else {
			reqKeyword = "%" + reqKeyword + "%"
			condition += " AND (u.nick_name LIKE ? OR c.content LIKE ?) AND c.is_show_name = 1"
			pars = append(pars, reqKeyword, reqKeyword)
		}
	}

	reqReportId := this.GetString("ReportId", "")
	if reqReportId == "" {
		br.Msg = "请输入报告ID"
		br.ErrMsg = "请输入报告ID"
		return
	}
	reportId, err = strconv.Atoi(reqReportId)
	if err != nil {
		br.Msg = "请输入正确的报告ID"
		br.ErrMsg = "请输入正确的报告ID：" + err.Error()
		return
	}
	condition += " AND c.report_id=?"
	pars = append(pars, reportId)
	reqReportChapterId := this.GetString("ReportChapterId", "")
	if reqReportChapterId != "" {
		reportChapterId, err = strconv.Atoi(reqReportChapterId)
		if err != nil {
			br.Msg = "请输入正确的章节ID"
			br.ErrMsg = "请输入正确的章节ID：" + err.Error()
			return
		}
		condition += " AND c.report_chapter_id=?"
		pars = append(pars, reportChapterId)
	}
	//空为全部，1精选，2置顶
	reqHotTopType := this.GetString("HotTopType", "")
	orderStr := ""
	numCondition := condition
	if reqHotTopType != "" {
		hotTopType, err = strconv.Atoi(reqHotTopType)
		if err != nil {
			br.Msg = "请输入正确的置顶标识"
			br.ErrMsg = "请输入正确的置顶标识：" + err.Error()
			return
		}
		if hotTopType == 2 {
			condition += " and c.is_top = 1"
			orderStr = "  ORDER BY  c.top_time DESC , c.comment_id DESC"
		} else if hotTopType == 1 {
			condition += " and c.is_hot = 1"
			orderStr = "  ORDER BY  c.hot_time DESC , c.comment_id DESC"
		} else {
			br.Msg = "请输入正确的置顶标识"
			return
		}
	}
	commentList, err := yb.GetCommentListByByCondition(condition, pars, orderStr, startSize, pageSize)
	if err != nil {
		br.Msg = "获取留言列表失败！"
		br.ErrMsg = "获取留言列表失败，Err:" + err.Error()
		return
	}

	var commentIds string
	var userIds string
	for _, v := range commentList {
		commentIds += "," + strconv.Itoa(int(v.CommentId))
		userIds += "," + strconv.Itoa(int(v.UserId))
	}
	// 查询所有的回复列表
	replyListMap := make(map[uint64][]*ybResp.ReplyItem)
	if len(commentIds) > 0 {
		commentIds = "(" + strings.Trim(commentIds, ",") + ")"
		replyList, tErr := yb.GetReplyListByReplyCommentIds(reportId, reportChapterId, commentIds)
		if tErr != nil {
			br.Msg = "获取回复列表失败"
			br.ErrMsg = "获取回复列表失败，Err:" + tErr.Error()
			return
		}
		for _, v := range replyList {
			t := new(ybResp.ReplyItem)
			t.CommentId = v.CommentId
			t.Content = v.Content
			t.AdminId = v.AdminId
			t.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
			t.AdminName = "弘则研究"
			t.AdminImgUrl = utils.DEFAULT_HONGZE_SYS_LOGO
			t.ReplyCommentId = v.ReplyCommentId
			replyListMap[v.ReplyCommentId] = append(replyListMap[v.ReplyCommentId], t)
		}
	}

	// 查询留言相关的用户
	var userOthers []*models.WxUser
	if len(userIds) > 0 {
		userOthers, err = models.GetWxUserListByUserIds(strings.Trim(userIds, ","))
		if err != nil {
			br.Msg = "查询留言用户出错"
			br.ErrMsg = "查询留言用户出错，Err:" + err.Error()
			return
		}
	}
	usersMap := make(map[uint64]*models.WxUser)
	for _, v := range userOthers {
		usersMap[uint64(v.UserId)] = v
	}
	var list []*ybResp.CommentItemResp
	for _, v := range commentList {
		tmp := new(ybResp.CommentItemResp)
		tmp.CommentId = v.CommentId
		tmp.UserId = v.UserId
		tmp.NickName = "匿名用户" + strconv.Itoa(int(3333+v.UserId))
		tmp.HeadImgUrl = utils.DEFAULT_HONGZE_USER_LOGO
		if info, ok := usersMap[v.UserId]; ok && v.IsShowName == 1 {
			tmp.NickName = info.NickName
			tmp.HeadImgUrl = info.Headimgurl
		}
		tmp.Content = v.Content
		tmp.IsHot = v.IsHot
		tmp.IsTop = v.IsTop
		tmp.HotTopTime = v.HotTopTime
		tmp.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
		tmp.Enabled = v.Enabled
		tmp.SourceAgent = v.SourceAgent
		if existList, ok := replyListMap[v.CommentId]; ok {
			tmp.ReplyList = existList
		}
		list = append(list, tmp)
	}

	// 获取列表分页总数
	total, err := yb.GetCommentListTotalByByCondition(condition, pars)
	if err != nil {
		br.Msg = "获取留言列表总数失败！"
		br.ErrMsg = "获取留言列表总数失败，Err:" + err.Error()
		return
	}

	//查询留言总数和精选总数，置顶总数
	var statNum ybResp.CommentStatNum
	if hotTopType == 0 {
		statNum.AllNum = total
	} else {
		statNum.AllNum, err = yb.GetCommentListTotalByByCondition(numCondition, pars)
		if err != nil {
			br.Msg = "获取留言列表总数失败！"
			br.ErrMsg = "获取留言列表总数失败，Err:" + err.Error()
			return
		}
	}
	if hotTopType == 2 {
		statNum.TopNum = total
	} else {
		topCondition := numCondition + " and c.is_top = 1"
		statNum.TopNum, err = yb.GetCommentListTotalByByCondition(topCondition, pars)
		if err != nil {
			br.Msg = "获取置顶留言列表总数失败！"
			br.ErrMsg = "获取置顶留言列表总数失败，Err:" + err.Error()
			return
		}
	}

	if hotTopType == 1 {
		statNum.HotNum = total
	} else {
		hotCondition := numCondition + " and c.is_hot = 1"
		statNum.HotNum, err = yb.GetCommentListTotalByByCondition(hotCondition, pars)
		if err != nil {
			br.Msg = "获取精选留言列表总数失败！"
			br.ErrMsg = "获取精选留言列表总数失败，Err:" + err.Error()
			return
		}
	}

	// 设置留言已读
	updateComments, err := yb.GetCommentIdsByCondition(condition, pars)
	if err != nil {
		br.Msg = "获取留言列表失败！"
		br.ErrMsg = "获取留言列表失败，Err:" + err.Error()
		return
	}
	updateIds := ""
	for _, v := range updateComments {
		updateIds += "," + strconv.Itoa(int(v.CommentId))
	}
	if updateIds != "" {
		updateIds = strings.Trim(updateIds, ",")
		err = yb.UpdateCommentSysIsReadByCommentIds(sysUser.AdminId, updateIds)
		if err != nil {
			br.Msg = "设置留言已读失败！"
			br.ErrMsg = "设置留言已读失败，Err:" + err.Error()
			return
		}
	}

	page := paging.GetPaging(currentIndex, pageSize, int(total))
	resp := ybResp.CommentListResp{
		List:    list,
		Paging:  page,
		StatNum: statNum,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
	return
}

// AddReply
// @Title 新增回复
// @Description 新增回复
// @Param	request	body request.AddReplyCommentReq true "type json string"
// @Success Ret=200 操作成功
// @router /comment/reply/add [post]
func (this *CommentController) AddReply() {
	br := new(models.BaseResponse).Init()
	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.AddReplyCommentReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.CommentId <= 0 {
		br.Msg = "请输入正确的留言ID！"
		br.ErrMsg = "请输入正确的留言ID！"
		return
	}

	if req.Content == "" {
		br.Msg = "请输入回复的内容！"
		br.ErrMsg = "请输入回复的内容！"
		return
	}
	brMsg, brErrMsg := ybService.CheckCommentPermission(sysUser.AdminId)
	if brMsg != "" {
		br.Msg = brMsg
		br.ErrMsg = brErrMsg
		return
	}
	//查询评论状态，如果已被用户删除则不允许新增回复
	commentInfo, err := yb.GetCommentUserByCommentId(req.CommentId)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "留言不存在"
			br.ErrMsg = "留言不存在"
			return
		}
		br.Msg = "查询留言失败！"
		br.ErrMsg = "查询留言失败，Err:" + err.Error()
		return
	}

	if commentInfo.Enabled == 0 {
		br.Msg = "留言已被删除，不可回复"
		br.ErrMsg = "留言已被删除，不可回复"
		return
	}
	if commentInfo.Type != 1 {
		br.Msg = "只允许对留言回复"
		br.ErrMsg = "只允许对留言回复"
		return
	}
	replyNum, err := yb.GetReplyTotalByCommentId(req.CommentId)
	if err != nil {
		br.Msg = "查询回复次数失败 ！"
		br.ErrMsg = "查询回复次数失败，Err:" + err.Error()
		return
	}
	ConfigReplyMax, err := cache.GetYbConfigIntValueByCode("reply_max_num")
	if err != nil {
		br.Msg = "查询配置信息失败 ！"
		br.ErrMsg = "查询配置信息失败，Err:" + err.Error()
		return
	}
	if int(replyNum) >= ConfigReplyMax {
		br.Msg = "超过回复条数限制"
		br.ErrMsg = "超过回复条数限制"
		return
	}
	//新增回复
	now := time.Now()
	reply := &yb.Comment{
		AdminId:            uint64(sysUser.AdminId),
		Content:            req.Content,
		ReportId:           commentInfo.ReportId,
		ReportChapterId:    commentInfo.ReportChapterId,
		ReplyCommentId:     commentInfo.CommentId,
		Type:               2,
		Enabled:            1,
		OldReportId:        commentInfo.OldReportId,
		OldReportChapterId: commentInfo.OldReportChapterId,
		CreateTime:         now,
		ModifyTime:         now,
	}
	replyId, err := yb.AddComment(reply)
	if err != nil {
		br.Msg = "新增回复失败！"
		br.ErrMsg = "新增回失败，Err:" + err.Error()
		return
	}

	// 新增消息通知
	sendMessage := &yb.Message{
		UserId:             commentInfo.UserId,
		Type:               1, //消息类型：1-留言回复通知，2-精选留言通知
		Enabled:            1,
		CommentId:          uint64(replyId),
		ContentFirst:       "您的留言：" + commentInfo.Content,
		ContentSecond:      "作者回复：" + req.Content,
		ReportId:           commentInfo.ReportId,
		ReportChapterId:    commentInfo.ReportChapterId,
		OldReportId:        commentInfo.OldReportId,
		OldReportChapterId: commentInfo.OldReportChapterId,
		CreateTime:         time.Now(),
		ModifyTime:         time.Now(),
	}
	err = yb.AddMessage(sendMessage)
	if err != nil {
		br.Msg = "新增消息失败！"
		br.ErrMsg = "新增消息失败，Err:" + err.Error()
		return
	}
	//回复列表
	var replyList []*ybResp.ReplyItem
	replyAll, tErr := yb.GetReplyByReplyCommentId(commentInfo.CommentId)
	if tErr != nil {
		br.Msg = "获取回复列表失败"
		br.ErrMsg = "获取回复列表失败，Err:" + tErr.Error()
		return
	}
	for _, v := range replyAll {
		t := new(ybResp.ReplyItem)
		t.CommentId = v.CommentId
		t.Content = v.Content
		t.AdminId = v.AdminId
		t.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
		t.AdminName = "弘则研究"
		t.AdminImgUrl = utils.DEFAULT_HONGZE_SYS_LOGO
		t.ReplyCommentId = v.ReplyCommentId
		replyList = append(replyList, t)
	}
	//评论信息
	tmp := new(ybResp.CommentItemResp)
	tmp.CommentId = commentInfo.CommentId
	tmp.UserId = commentInfo.UserId
	tmp.NickName = "匿名用户" + strconv.Itoa(int(3333+commentInfo.UserId))
	tmp.HeadImgUrl = utils.DEFAULT_HONGZE_USER_LOGO
	if commentInfo.IsShowName == 1 {
		tmp.NickName = commentInfo.NickName
		tmp.HeadImgUrl = commentInfo.Headimgurl
	}
	tmp.Content = commentInfo.Content
	tmp.IsHot = commentInfo.IsHot
	tmp.IsTop = commentInfo.IsTop
	tmp.HotTopTime = commentInfo.HotTopTime
	tmp.CreateTime = commentInfo.CreateTime.Format(utils.FormatDateTime)
	tmp.Enabled = commentInfo.Enabled
	tmp.SourceAgent = commentInfo.SourceAgent
	tmp.ReplyList = replyList

	data := tmp
	br.Ret = 200
	br.Success = true
	br.Msg = "回复成功"
	br.Data = data
	return
}

// DeleteComment
// @Title 删除留言
// @Description 删除留言
// @Param	request	body request.CommentReq true "type json string"
// @Success Ret=200 操作成功
// @router /comment/del [post]
func (this *CommentController) DeleteComment() {
	br := new(models.BaseResponse).Init()
	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.CommentReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.CommentId <= 0 {
		br.Msg = "请输入正确的留言ID！"
		br.ErrMsg = "请输入正确的留言ID！"
		return
	}
	brMsg, brErrMsg := ybService.CheckCommentPermission(sysUser.AdminId)
	if brMsg != "" {
		br.Msg = brMsg
		br.ErrMsg = brErrMsg
		return
	}
	//查询评论状态，
	commentInfo, err := yb.GetCommentByCommentId(req.CommentId)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "留言不存在或者已删除"
			br.ErrMsg = "留言不存在或者已删除"
			return
		}
		br.Msg = "查询留言失败！"
		br.ErrMsg = "查询留言失败，Err:" + err.Error()
		return
	}
	if commentInfo.Type != 1 {
		br.Msg = "只允许删除留言"
		br.ErrMsg = "只允许删除留言"
		return
	}
	err = yb.DeleteComment(req.CommentId)
	if err != nil {
		br.Msg = "删除留言操作失败！"
		br.ErrMsg = "删除留言操作失败，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	return
}

// DeleteReply
// @Title 删除回复
// @Description 删除回复
// @Param	request	body request.CommentReq true "type json string"
// @Success Ret=200 操作成功
// @router /comment/reply/del [post]
func (this *CommentController) DeleteReply() {
	br := new(models.BaseResponse).Init()
	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.CommentReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.CommentId <= 0 {
		br.Msg = "请输入正确的回复ID！"
		br.ErrMsg = "请输入正确的回复ID！"
		return
	}
	brMsg, brErrMsg := ybService.CheckCommentPermission(sysUser.AdminId)
	if brMsg != "" {
		br.Msg = brMsg
		br.ErrMsg = brErrMsg
		return
	}
	//查询评论状态，
	commentInfo, err := yb.GetCommentByCommentId(req.CommentId)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "留言不存在或者已删除"
			br.ErrMsg = "留言不存在或者已删除"
			return
		}
		br.Msg = "查询留言失败！"
		br.ErrMsg = "查询留言失败，Err:" + err.Error()
		return
	}
	if commentInfo.Type != 2 {
		br.Msg = "只允许删除回复"
		br.ErrMsg = "只允许删除回复"
		return
	}
	err = yb.DeleteCommentReply(req.CommentId)
	if err != nil {
		br.Msg = "删除回复操作失败！"
		br.ErrMsg = "删除回复操作失败，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	return
}

// SetHot
// @Title 设置留言精选/取消留言精选
// @Description 设置留言精选
// @Param	request	body request.CommentReq true "type json string"
// @Success Ret=200 操作成功
// @router /comment/hot [post]
func (this *CommentController) SetHot() {
	br := new(models.BaseResponse).Init()
	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.CommentReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.CommentId <= 0 {
		br.Msg = "请输入正确的留言ID！"
		br.ErrMsg = "请输入正确的留言ID！"
		return
	}
	brMsg, brErrMsg := ybService.CheckCommentPermission(sysUser.AdminId)
	if brMsg != "" {
		br.Msg = brMsg
		br.ErrMsg = brErrMsg
		return
	}
	//查询评论状态，
	commentInfo, err := yb.GetCommentByCommentId(req.CommentId)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "留言不存在"
			br.ErrMsg = "留言不存在"
			return
		}
		br.Msg = "查询留言失败！"
		br.ErrMsg = "查询留言失败，Err:" + err.Error()
		return
	}

	if commentInfo.Enabled == 0 {
		br.Msg = "留言已被删除，不可回复"
		br.ErrMsg = "留言已被删除，不可回复"
		return
	}
	if commentInfo.Type != 1 {
		br.Msg = "只允许对留言操作"
		br.ErrMsg = "只允许对留言操作"
		return
	}
	now := time.Now()
	if commentInfo.IsHot == 0 {
		//精选操作
		var notifyMsgFlag bool
		tmpTime := commentInfo.HotTime.Format(utils.FormatDateTime)
		if tmpTime == "0001-01-01 00:00:00" { //首次精选才会发送通知
			notifyMsgFlag = true
		}
		commentInfo.HotTopTime = now
		commentInfo.ModifyTime = now
		commentInfo.HotTime = now
		commentInfo.IsHot = 1
		err = commentInfo.Update([]string{"is_hot", "hot_time", "hot_top_time", "modify_time"})
		if err != nil {
			br.Msg = "留言精选操作失败！"
			br.ErrMsg = "留言精选操作失败，Err:" + err.Error()
			return
		}

		// 新增消息通知
		if notifyMsgFlag {
			sendMessage := &yb.Message{
				UserId:             commentInfo.UserId,
				Type:               2, //消息类型：1-留言回复通知，2-精选留言通知
				Enabled:            1,
				CommentId:          commentInfo.CommentId,
				ContentFirst:       "您的留言：" + commentInfo.Content,
				ContentSecond:      "入选为精选留言",
				ReportId:           commentInfo.ReportId,
				ReportChapterId:    commentInfo.ReportChapterId,
				OldReportId:        commentInfo.OldReportId,
				OldReportChapterId: commentInfo.OldReportChapterId,
				CreateTime:         time.Now(),
				ModifyTime:         time.Now(),
			}
			err = yb.AddMessage(sendMessage)
			if err != nil {
				br.Msg = "新增消息失败！"
				br.ErrMsg = "新增消息失败，Err:" + err.Error()
				return
			}
		}
	} else {
		//取消精选操作,同时取消置顶
		commentInfo.IsHot = 0
		commentInfo.IsTop = 0
		commentInfo.ModifyTime = now
		err = commentInfo.Update([]string{"is_hot", "modify_time", "is_top"})
		if err != nil {
			br.Msg = "取消精选操作失败！"
			br.ErrMsg = "取消精选操作失败，Err:" + err.Error()
			return
		}
	}
	data := ybResp.CommentHotResp{
		CommentId: int64(commentInfo.CommentId),
		IsHot:     commentInfo.IsHot,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	br.Data = data
	return
}

// SetTop
// @Title 设置留言置顶
// @Description 设置留言置顶
// @Param	request	body request.CommentReq true "type json string"
// @Success Ret=200 操作成功
// @router /comment/top [post]
func (this *CommentController) SetTop() {
	br := new(models.BaseResponse).Init()
	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.CommentReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.CommentId <= 0 {
		br.Msg = "请输入正确的留言ID！"
		br.ErrMsg = "请输入正确的留言ID！"
		return
	}
	brMsg, brErrMsg := ybService.CheckCommentPermission(sysUser.AdminId)
	if brMsg != "" {
		br.Msg = brMsg
		br.ErrMsg = brErrMsg
		return
	}
	//查询评论状态
	commentInfo, err := yb.GetCommentByCommentId(req.CommentId)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "留言不存在"
			br.ErrMsg = "留言不存在"
			return
		}
		br.Msg = "查询留言失败！"
		br.ErrMsg = "查询留言失败，Err:" + err.Error()
		return
	}

	if commentInfo.Enabled == 0 {
		br.Msg = "留言已被删除，不可回复"
		br.ErrMsg = "留言已被删除，不可回复"
		return
	}
	if commentInfo.Type != 1 {
		br.Msg = "只允许对留言操作"
		br.ErrMsg = "只允许对留言操作"
		return
	}
	now := time.Now()
	if commentInfo.IsTop == 0 {
		//置顶操作
		var notifyMsgFlag bool
		updateStr := []string{"is_top", "hot_top_time", "top_time", "modify_time"}
		if commentInfo.IsHot == 0 {
			tmpTime := commentInfo.HotTime.Format(utils.FormatDateTime)
			if tmpTime == "0001-01-01 00:00:00" {
				notifyMsgFlag = true
			}
			commentInfo.IsHot = 1
			commentInfo.HotTime = now
			updateStr = append(updateStr, "is_hot")
			updateStr = append(updateStr, "hot_time")
		}
		commentInfo.IsTop = 1
		commentInfo.HotTopTime = now
		commentInfo.TopTime = now
		commentInfo.ModifyTime = now
		err = commentInfo.Update(updateStr)
		if err != nil {
			br.Msg = "留言置顶操作失败！"
			br.ErrMsg = "留言置顶操作失败，Err:" + err.Error()
			return
		}

		//发送精选通知
		if notifyMsgFlag {
			sendMessage := &yb.Message{
				UserId:          commentInfo.UserId,
				Type:            2, //消息类型：1-留言回复通知，2-精选留言通知
				Enabled:         1,
				CommentId:       commentInfo.CommentId,
				ContentFirst:    "您的留言：" + commentInfo.Content,
				ContentSecond:   "入选为精选留言",
				ReportId:        commentInfo.ReportId,
				ReportChapterId: commentInfo.ReportChapterId,
				CreateTime:      time.Now(),
				ModifyTime:      time.Now(),
			}
			err = yb.AddMessage(sendMessage)
			if err != nil {
				br.Msg = "新增消息失败！"
				br.ErrMsg = "新增消息失败，Err:" + err.Error()
				return
			}
		}
	} else {
		//取消置顶操作
		commentInfo.IsTop = 0
		commentInfo.ModifyTime = now
		err = commentInfo.Update([]string{"is_top", "modify_time"})
		if err != nil {
			br.Msg = "取消置顶操作失败！"
			br.ErrMsg = "取消置顶操作失败，Err:" + err.Error()
			return
		}
	}
	data := ybResp.CommentTopResp{
		CommentId: int64(commentInfo.CommentId),
		IsTop:     commentInfo.IsTop,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	br.Data = data
	return
}

// AllCommentList
// @Title 获取所有未处理的留言列表
// @Description 获取所有未处理的留言列表
// @Success 200 {object}  ybResp.CommentListResp
// @router /comment/all_list [get]
func (this *CommentController) AllCommentList() {
	br := new(models.BaseResponse).Init()
	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
	}

	currentIndex := 1
	pageSize := 10000
	var startSize int
	startSize = paging.StartIndex(currentIndex, pageSize)

	// 判断查询权限
	brMsg, _ := ybService.CheckCommentPermission(sysUser.AdminId)
	if brMsg != "" {
		br.Ret = 200
		br.Success = true
		br.Data = ybResp.AllCommentResp{
			List:   make([]ybResp.CommentItemResp, 0),
			IsShow: false,
		}
		return
	}

	condition := ""
	var pars []interface{}
	condition += " AND c.sys_is_read = ? AND type = 1 "
	pars = append(pars, 0)
	orderStr := " ORDER BY  c.create_time DESC , c.comment_id DESC"

	commentList, err := yb.GetCommentListByByCondition(condition, pars, orderStr, startSize, pageSize)
	if err != nil {
		br.Msg = "获取留言列表失败！"
		br.ErrMsg = "获取留言列表失败，Err:" + err.Error()
		return
	}

	var userIds string
	for _, v := range commentList {
		userIds += "," + strconv.Itoa(int(v.UserId))
	}

	// 查询留言相关的用户
	var userOthers []*models.WxUser
	if len(userIds) > 0 {
		userOthers, err = models.GetWxUserListByUserIds(strings.Trim(userIds, ","))
		if err != nil {
			br.Msg = "查询留言用户出错"
			br.ErrMsg = "查询留言用户出错，Err:" + err.Error()
			return
		}
	}
	usersMap := make(map[uint64]*models.WxUser)
	for _, v := range userOthers {
		usersMap[uint64(v.UserId)] = v
	}
	var list []ybResp.CommentItemResp

	for _, v := range commentList {
		tmp := ybResp.CommentItemResp{
			CommentId:   v.CommentId,
			ReportId:    v.ReportId,
			UserId:      v.UserId,
			Content:     v.Content,
			IsTop:       v.IsTop,
			IsHot:       v.IsHot,
			HotTopTime:  v.HotTopTime,
			SourceAgent: v.SourceAgent,
			NickName:    "匿名用户" + strconv.Itoa(int(3333+v.UserId)),
			HeadImgUrl:  utils.DEFAULT_HONGZE_USER_LOGO,
			CreateTime:  v.CreateTime.Format(utils.FormatDateTime),
			Enabled:     v.Enabled,
			ReplyList:   make([]*ybResp.ReplyItem, 0),
		}
		if info, ok := usersMap[v.UserId]; ok && v.IsShowName == 1 {
			tmp.NickName = info.NickName
			tmp.HeadImgUrl = info.Headimgurl
		}
		list = append(list, tmp)
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = ybResp.AllCommentResp{
		List:   list,
		IsShow: true,
	}
	return
}

// ProductCommentList
// @Title 获取评论合集列表
// @Description 获取评论合集列表
// @Param   CommunityId       query   string    true		"评论ID"
// @Param   ProductType       query   string    false		"板块类型"
// @Param   KeyWord       query   string    false		"留言关键词"
// @Param   PageSize          query   string    false      "页码"
// @param   CurrentIndex      query   string    false      "每页展示的记录数"
// @Success 200 {object}  ybResp.LikeListResp
// @router /product/comment/list [get]
func (this *CommentController) ProductCommentList() {
	br := new(models.BaseResponse).Init()
	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
	}

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

	productType := this.GetString("ProductType", "")
	keyWord := this.GetString("KeyWord", "")
	createDate := this.GetString("CreateDate", "")

	list, total, err, errMsg := ybService.GetProductComments(sysUser.AdminId, productType, keyWord, createDate, startSize, pageSize)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = err.Error()
		return
	}
	page := paging.GetPaging(currentIndex, pageSize, int(total))
	resp := ybResp.ProductCommentResp{
		List:   list,
		Paging: page,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
	return
}

// OtherProductList
// @Title 获取其他分产品的筛选列表
// @Description 获取其他分产品的筛选列表
// @Success 200 {object} ybResp.OtherProductTypeListResp
// @router /comment/product [get]
func (this *CommentController) OtherProductList() {
	br := new(models.BaseResponse).Init()
	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
	}

	resp := make([]ybResp.OtherProductTypeListResp, 0)
	reportItem := ybResp.OtherProductTypeListResp{
		ProductType: 1,
		ProductName: "研报",
	}
	questionItem := ybResp.OtherProductTypeListResp{
		ProductType: 2,
		ProductName: "问答社区",
	}
	videoItem := ybResp.OtherProductTypeListResp{
		ProductType: 3,
		ProductName: "视频社区",
	}
	roadVideoItem := ybResp.OtherProductTypeListResp{
		ProductType: 4,
		ProductName: "线上路演",
	}
	resp = append(resp, reportItem, questionItem, videoItem, roadVideoItem)
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// DeleteProductComment
// @Title 删除板块留言
// @Description 删除留言
// @Param	request	body request.CommentProductDelReq true "type json string"
// @Success Ret=200 操作成功
// @router /product/comment/del [post]
func (this *CommentController) DeleteProductComment() {
	br := new(models.BaseResponse).Init()
	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.CommentProductDelReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.CommentId <= 0 {
		br.Msg = "请输入正确的留言ID！"
		br.ErrMsg = "请输入正确的留言ID！"
		return
	}
	err, errMsg := ybService.DelProductComments(req)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "删除成功"
	return
}
