package yb

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

type CommunityQuestionController struct {
	controllers.BaseAuthController
}

// QuestionList
// @Title 获取问答列表
// @Description 获取问答列表
// @Param   ReplyStatus	query	int		false	"提问状态 1-待分配 2-待回答 3-已回答"
// @Param   ReplierIds	query	string	false	"回复人ID,多个用英文逗号分割"
// @Param   SortParam   query   string  false     "排序字段参数，用来排序的字段， 枚举值：'ClickNum':点击量"
// @Param   SortType   query   string  true       "如何排序，是正序还是倒序，枚举值：`asc 正序`,`desc 倒叙`"
// @Success 200 {object} response.CommunityQuestionListResp
// @router /community/question/list [get]
func (this *CommunityQuestionController) QuestionList() {
	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
	}
	// 权限校验
	msg, err := yb.CheckCommunityQuestionPermission(sysUser.AdminId)
	if err != nil {
		br.Msg = msg
		br.ErrMsg = err.Error()
		return
	}

	condition := ""
	pars := make([]interface{}, 0)
	replyStatus, _ := this.GetInt("ReplyStatus", 0)
	if replyStatus > 0 {
		condition += ` AND q.reply_status = ?`
		pars = append(pars, replyStatus)
	}
	replierIds := this.GetString("ReplierIds", "")
	if replierIds != "" {
		condition += ` AND q.replier_admin_id IN (` + replierIds + `)`
	}
	// 排序相关
	sortParam := this.GetString("SortParam")
	sortType := this.GetString("SortType")
	orderStr := ""
	if sortType != "" && strings.ToLower(sortType) != "desc" && strings.ToLower(sortType) != "asc" {
		br.Msg = "请输入正确的排序类型"
		return
	}
	if sortParam == "ClickNum" {
		orderStr = "  ORDER BY l.click_num"
		if sortType != "" {
			orderStr += " " + sortType
		} else {
			orderStr += " DESC"
		}

		orderStr += ", q.create_time DESC"
	}
	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, resp, err := yb.GetQuestionList(condition, pars, orderStr, startSize, pageSize)
	if err != nil {
		br.Msg = "获取问答列表失败"
		br.ErrMsg = "QuestionList ErrMsg:" + err.Error()
		return
	}

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

// QuestionDistribute
// @Title 分配回复人
// @Description 获取问答列表
// @Param   ReplyStatus	query	int		false	"提问状态 1-待分配 2-待回答 3-已回答"
// @Param   ReplierIds	query	string	false	"回复人ID,多个用英文逗号分割"
// @Success 200 string "操作成功"
// @router /community/question/distribute [post]
func (this *CommunityQuestionController) QuestionDistribute() {
	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
	}
	// 权限校验
	msg, err := yb.CheckCommunityQuestionPermission(sysUser.AdminId)
	if err != nil {
		br.Msg = msg
		br.ErrMsg = err.Error()
		return
	}

	var req request.QuestionDistributeReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	errMsg, err := yb.DistributeQuestion(req.QuestionId, req.AdminId, req.ResearchGroupFirstId, req.ResearchGroupSecondId, sysUser.AdminId)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = "DistributeQuestion ErrMsg:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = errMsg
}

// QuestionDelete
// @Title 删除提问
// @Description 删除提问
// @Param   QuestionId	query	int	false	"问题ID"
// @Success 200 string "操作成功"
// @router /community/question/delete [post]
func (this *CommunityQuestionController) QuestionDelete() {
	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
	}
	// 权限校验
	msg, err := yb.CheckCommunityQuestionPermission(sysUser.AdminId)
	if err != nil {
		br.Msg = msg
		br.ErrMsg = err.Error()
		return
	}

	var req request.QuestionDeleteReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if err := yb.SoftDeleteQuestion(req.QuestionId); err != nil {
		br.Msg = "删除提问失败"
		br.ErrMsg = "DeleteQuestion ErrMsg:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}

// SendTemplateMsg
// @Title 推送模板消息给回复人
// @Description 推送模板消息给回复人
// @Param   QuestionId	query	int	false	"问题ID"
// @Success 200 string "操作成功"
// @router /community/question/send_template_msg [post]
func (this *CommunityQuestionController) SendDistributeTemplateMsg() {
	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
	}
	// 权限校验
	msg, err := yb.CheckCommunityQuestionPermission(sysUser.AdminId)
	if err != nil {
		br.Msg = msg
		br.ErrMsg = err.Error()
		return
	}

	var req request.QuestionMsgSendReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "SendDistributeTemplateMsg 参数解析异常:" + err.Error()
		return
	}
	errMsg, err := yb.SendMsgToReplier(req.QuestionId)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = "SendDistributeTemplateMsg ErrMsg:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}

// QuestionEdit
// @Title 编辑问题
// @Description 编辑问题
// @Param   QuestionId		query	int		true	"问题ID"
// @Param   QuestionContent	query	string	true	"问题内容"
// @Success 200 string "操作成功"
// @router /community/question/edit [post]
func (this *CommunityQuestionController) QuestionEdit() {
	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
	}
	// 权限校验
	msg, err := yb.CheckCommunityQuestionPermission(sysUser.AdminId)
	if err != nil {
		br.Msg = msg
		br.ErrMsg = err.Error()
		return
	}

	var req request.QuestionEditReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.QuestionId <= 0 {
		br.Msg = "问题ID有误"
		return
	}
	if req.QuestionContent == "" {
		br.Msg = "内容不可为空"
		return
	}
	contentRune := []rune(req.QuestionContent)
	if len(contentRune) > 28 {
		br.Msg = "问题字数不可超过28个字符"
		return
	}
	errMsg, err := yb.EditQuestion(req.QuestionId, req.QuestionContent)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = "QuestionEdit ErrMsg:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}

// QuestionClickLogList
// @Title 获取点击量列表
// @Description 获取点击量列表
// @Param   QuestionId	query	int	false	"问题ID"
// @Success 200 {object} response.QuestionClickLogListResp
// @router /community/question/click/logs [get]
func (this *CommunityQuestionController) QuestionClickLogList() {
	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
	}
	questionId, _ := this.GetInt("QuestionId")
	if questionId <= 0 {
		br.Msg = "请输入正确的提问ID"
		return
	}

	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, resp, err := yb.GetQuestionClickLogList(questionId, startSize, pageSize)
	if err != nil {
		br.Msg = "获取问答列表失败"
		br.ErrMsg = "QuestionList ErrMsg:" + err.Error()
		return
	}

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

// UserInfo
// @Title 获取提问者基本信息
// @Description 获取提问者基本信息
// @Param   UserId	query	int	false	"用户ID"
// @Success 200 {object} response.CommunityQuestionListResp
// @router /community/question/user [get]
func (this *CommunityQuestionController) UserInfo() {
	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
	}
	questionId, _ := this.GetInt("QuestionId")
	if questionId <= 0 {
		br.Msg = "请输入正确的提问ID"
		return
	}

	resp, err := yb.GetQuestionCompanyUser(questionId)
	if err != nil {
		br.Msg = "用户不存在或者查询出错"
		br.ErrMsg = "UserInfo ErrMsg:" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
	return
}

// LikeTeaseListWithQuestion
// @Title 获取已被点赞/吐槽的问答列表
// @Description 获取已被点赞/吐槽的问答列表
// @Param   SortParam         query   string    false		"排序关键词：Num"
// @Param   SortType          query   string    false		"排序顺序：asc、desc"
// @Param   DataType          query   string    false		"类型：like、tease"
// @Param   PageSize          query   string    false      "每页展示的记录数"
// @param   CurrentIndex      query   string    false      "页码"
// @param   Source      query   int    false      "来源：1-问答社区; 2-视频社区"
// @Success 200 {object}  ybResp.LikeReportListResp
// @router /community/question/like_tease_question_list [get]
func (this *CommunityQuestionController) LikeTeaseListWithQuestion() {
	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
	}
	// 来源(默认问答社区)
	source, _ := this.GetInt("Source")
	if source <= 0 {
		source = 1
	}

	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 := yb.CheckCommentPermission(sysUser.AdminId)
	if brMsg != "" {
		br.Msg = brMsg
		br.ErrMsg = brErrMsg
		return
	}

	// 数据类型，默认是点赞列表
	reqDataType := this.GetString("DataType")
	if reqDataType == `` {
		reqDataType = "like"
	}

	var opType int8 //类型. 1-点赞 2-吐槽
	switch reqDataType {
	case "like":
		opType = 1
	case "tease":
		opType = 2
	default:
		br.Msg = "错误的数据类型：" + reqDataType
		br.ErrMsg = "错误的数据类型：" + reqDataType
		return
	}

	//排序
	orderStr := ""
	reqSortParam := this.GetString("SortParam", "")
	switch reqSortParam {
	case "Num":
		orderStr = ` num `
	default:
		orderStr = ` community_question_id ` // 默认问答倒序
	}
	reqSortType := this.GetString("SortType")
	if reqSortType == `` {
		reqSortType = "desc"
	}
	orderStr = orderStr + " " + reqSortType

	total := 0
	list := make([]*ybModels.StatCommunityQuestionLikeTeaseItem, 0)
	// 问答社区
	if source == 1 {
		qaTotal, e := ybModels.GetYbCommunityQuestionLikeTeaseTotal(opType)
		if e != nil {
			br.Msg = "获取数据汇总数失败！"
			br.ErrMsg = "获取数据汇总数失败，Err:" + e.Error()
			return
		}
		total = int(qaTotal)

		qaList, e := ybModels.GetYbCommunityQuestionLikeTeaseQuestion(opType, orderStr, startSize, pageSize)
		if e != nil {
			br.Msg = "获取问答数据列表失败！"
			br.ErrMsg = "获取问答数据列表失败，Err:" + e.Error()
			return
		}
		list = qaList
	}
	// 视频社区
	if source == 2 {
		vdTotal, e := ybModels.GetYbCommunityVideoLikeTeaseTotal(opType)
		if e != nil {
			br.Msg = "获取视频数据汇总数失败！"
			br.ErrMsg = "获取视频数据汇总数失败，Err:" + e.Error()
			return
		}
		total = int(vdTotal)

		vdList, e := ybModels.GetYbCommunityVideoLikeTeaseList(opType, orderStr, startSize, pageSize)
		if e != nil {
			br.Msg = "获取视频数据列表失败！"
			br.ErrMsg = "获取视频数据列表失败，Err:" + e.Error()
			return
		}
		// 标签分类及名称
		tagList, e := models.GetVarietyTagList()
		if e != nil {
			br.Msg = "获取视频标签列表失败"
			br.ErrMsg = "获取视频标签列表失败, Err:" + e.Error()
			return
		}
		tagMap := make(map[int]*models.VarietyTag, 0)
		for i := range tagList {
			tagMap[tagList[i].VarietyTagId] = tagList[i]
		}
		for i := range vdList {
			t := tagMap[vdList[i].VarietyTagId]
			if t != nil {
				vdList[i].VarietyClassifyTag = fmt.Sprintf("%s-%s", t.VarietyClassifyName, t.TagName)
			}
		}
		list = vdList
	}
	// 线上路演
	if source == 3 {
		rvTotal, e := ybModels.GetYbRoadVideoLikeTeaseTotal(opType)
		if e != nil {
			br.Msg = "获取线上路演数据汇总数失败！"
			br.ErrMsg = "获取线上路演数据汇总数失败，Err:" + e.Error()
			return
		}
		total = int(rvTotal)

		rvList, e := ybModels.GetYbRoadVideoLikeTeaseList(opType, orderStr, startSize, pageSize)
		if e != nil {
			br.Msg = "获取线上路演点赞吐槽列表失败！"
			br.ErrMsg = "获取线上路演点赞吐槽列表失败，Err:" + e.Error()
			return
		}

		// 线上路演品种
		cpList, e := models.GetChartPermissionList()
		if e != nil {
			br.Msg = "获取路演视频品种失败！"
			br.ErrMsg = "获取路演视频品种失败，Err:" + e.Error()
			return
		}
		cpMap := make(map[int]string, 0)
		for i := range cpList {
			cpMap[cpList[i].ChartPermissionId] = cpList[i].PermissionName
		}
		for i := range rvList {
			if rvList[i].VarietyClassifyTag == "" {
				continue
			}
			ids := utils.JoinStr2IntArr(strings.ReplaceAll(rvList[i].VarietyClassifyTag, `'`, ``), ",")
			if len(ids) == 0 {
				continue
			}
			cpArr := make([]string, 0)
			for s := range ids {
				if cpMap[ids[s]] != "" {
					cpArr = append(cpArr, cpMap[ids[s]])
				}
			}
			rvList[i].VarietyClassifyTag = strings.Join(cpArr, ",")
		}

		list = rvList
	}

	page := paging.GetPaging(currentIndex, pageSize, int(total))
	data := &ybResp.LikeTeaseListWithQuestionResp{
		List:   list,
		Paging: page,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	br.Data = data
	return
}

// LikeTeaseList
// @Title 获取某个问答的点赞/吐槽列表
// @Description 获取某个问答的点赞/吐槽列表
// @Param   CommunityQuestionId           query   string    true		"问答ID"
// @Param   DataType          query   string    false		"类型：like、tease"
// @Param   PageSize          query   string    false      "页码"
// @param   CurrentIndex      query   string    false      "每页展示的记录数"
// @param   Source      query   int    false      "来源：1-问答社区; 2-视频社区"
// @Success 200 {object}  ybResp.LikeListResp
// @router /community/question/like_tease_list [get]
func (this *CommunityQuestionController) LikeTeaseList() {
	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
	}
	// 来源(默认问答社区)
	source, _ := this.GetInt("Source")
	if source <= 0 {
		source = 1
	}

	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 := yb.CheckCommentPermission(sysUser.AdminId)
	if brMsg != "" {
		br.Msg = brMsg
		br.ErrMsg = brErrMsg
		return
	}

	//根据搜索条件查询留言列表
	var (
		err                 error
		communityQuestionId int
	)
	reqCommunityQuestionId := this.GetString("CommunityQuestionId", "")
	if reqCommunityQuestionId == "" {
		br.Msg = "请输入问答ID"
		br.ErrMsg = "请输入问答ID"
		return
	}
	communityQuestionId, err = strconv.Atoi(reqCommunityQuestionId)
	if err != nil {
		br.Msg = "请输入问答ID"
		br.ErrMsg = "请输入问答ID：" + err.Error()
		return
	}
	// 数据类型，默认是点赞列表
	reqDataType := this.GetString("DataType")
	if reqDataType == `` {
		reqDataType = "like"
	}

	var opType int //类型. 1-点赞 2-吐槽
	switch reqDataType {
	case "like":
		opType = 1
	case "tease":
		opType = 2
	default:
		br.Msg = "错误的数据类型：" + reqDataType
		br.ErrMsg = "错误的数据类型：" + reqDataType
		return
	}

	// 获取数据列表
	total, list, err := ybModels.GetYbCommunityQuestionLikeTeaseQuestionListById(communityQuestionId, opType, source, startSize, pageSize)
	if err != nil {
		br.Msg = "获取点赞列表失败！"
		br.ErrMsg = "获取点赞列表失败，Err:" + err.Error()
		return
	}
	var userIds string
	companyIdList := make([]int, 0)

	for _, v := range list {
		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)

	companyIdMap := make(map[int]int)
	for _, v := range userOthers {
		usersMap[uint64(v.UserId)] = v

		if _, ok := companyIdMap[v.CompanyId]; !ok { //避免company_id重复append
			companyIdList = append(companyIdList, v.CompanyId)
			companyIdMap[v.CompanyId] = v.CompanyId
		}
	}

	// 查询点赞/吐槽相关用户的公司
	companyProductMap := make(map[int]*company.CompanyProduct)
	{
		if len(companyIdList) > 0 {
			var companyProductOthers []*company.CompanyProduct
			companyProductOthers, err = company.GetCompanyProductsByCompanyIdsAndProductId(companyIdList, 1)
			if err != nil {
				br.Msg = "查询客户出错"
				br.ErrMsg = "查询客户出错，Err:" + err.Error()
				return
			}
			for _, v := range companyProductOthers {
				companyProductMap[v.CompanyId] = v
			}
		}
	}
	for _, v := range list {
		if info, ok := usersMap[v.UserId]; ok {
			v.NickName = info.NickName
			//v.RealName = utils.DealRealName(info.RealName)
			v.RealName = info.RealName

			//客户信息
			if companyProductInfo, ok := companyProductMap[info.CompanyId]; ok {
				v.CompanyId = companyProductInfo.CompanyId
				v.CompanyName = companyProductInfo.CompanyName
				v.CompanyFiccStatus = companyProductInfo.Status
			}
		}
	}

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

// CommentList
// @Title 获取社区问答评论列表
// @Description 获取社区问答评论列表
// @Param   CommunityQuestionId           query   string    true		"问答ID"
// @Param   HotStatus          query   string    false		"精选状态，全部：-1；未精选：0；精选：1"
// @Param   PageSize          query   string    false      "页码"
// @param   CurrentIndex      query   string    false      "每页展示的记录数"
// @Success 200 {object}  ybResp.LikeListResp
// @router /community/question/comment/list [get]
func (this *CommunityQuestionController) 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 := yb.CheckCommentPermission(sysUser.AdminId)
	//if brMsg != "" {
	//	br.Msg = brMsg
	//	br.ErrMsg = brErrMsg
	//	return
	//}

	condition := ""
	pars := make([]interface{}, 0)

	//精选状态
	hotStatus := this.GetString("HotStatus", "") //精选状态，全部：-1；未精选：0；精选：1
	switch hotStatus {
	case "0":
		condition += ` AND is_hot = ? `
		pars = append(pars, 0)
		//is = 1
	case "1":
		condition += ` AND is_hot = ? `
		pars = append(pars, 1)
	}

	// 获取数据列表
	total, list, err := ybModels.GetCommunityQuestionCommentList(condition, pars, startSize, pageSize)
	if err != nil {
		br.Msg = "获取点赞列表失败！"
		br.ErrMsg = "获取点赞列表失败，Err:" + err.Error()
		return
	}
	var userIds string
	companyIdList := make([]int, 0)

	for _, v := range list {
		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)

	companyIdMap := make(map[int]int)
	for _, v := range userOthers {
		usersMap[uint64(v.UserId)] = v

		if _, ok := companyIdMap[v.CompanyId]; !ok { //避免company_id重复append
			companyIdList = append(companyIdList, v.CompanyId)
			companyIdMap[v.CompanyId] = v.CompanyId
		}
	}

	// 查询点赞/吐槽相关用户的公司
	companyProductMap := make(map[int]*company.CompanyProduct)
	{
		if len(companyIdList) > 0 {
			var companyProductOthers []*company.CompanyProduct
			companyProductOthers, err = company.GetCompanyProductsByCompanyIdsAndProductId(companyIdList, 1)
			if err != nil {
				br.Msg = "查询客户出错"
				br.ErrMsg = "查询客户出错，Err:" + err.Error()
				return
			}
			for _, v := range companyProductOthers {
				companyProductMap[v.CompanyId] = v
			}
		}
	}
	for _, v := range list {
		if info, ok := usersMap[v.UserId]; ok {
			v.NickName = info.NickName
			//v.RealName = utils.DealRealName(info.RealName)
			v.RealName = info.RealName

			//客户信息
			if companyProductInfo, ok := companyProductMap[info.CompanyId]; ok {
				v.CompanyId = companyProductInfo.CompanyId
				v.CompanyName = companyProductInfo.CompanyName
				v.CompanyFiccStatus = companyProductInfo.Status
			}
		}
	}

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

// CommentDelete
// @Title 删除评论
// @Description 删除评论
// @Param   QuestionId	query	int	false	"问题ID"
// @Success 200 string "操作成功"
// @router /community/question/comment/delete [post]
func (this *CommunityQuestionController) CommentDelete() {
	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
	}

	// 判断查询权限
	//brMsg, brErrMsg := yb.CheckCommentPermission(sysUser.AdminId)
	//if brMsg != "" {
	//	br.Msg = brMsg
	//	br.ErrMsg = brErrMsg
	//	return
	//}

	var req request.QuestionCommentDeleteReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if err, errMsg := yb.SoftDeleteQuestionComment(req.CommunityQuestionCommentId); err != nil {
		br.Msg = errMsg
		br.ErrMsg = "DeleteQuestion，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "删除成功"
}

// CommentHot
// @Title 设置/取消 问题评论精选
// @Description 设置/取消 问题评论精选
// @Success 200 string "操作成功"
// @router /community/question/comment/hot [post]
func (this *CommunityQuestionController) CommentHot() {
	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
	}

	// 判断查询权限
	//brMsg, brErrMsg := yb.CheckCommentPermission(sysUser.AdminId)
	//if brMsg != "" {
	//	br.Msg = brMsg
	//	br.ErrMsg = brErrMsg
	//	return
	//}

	var req request.QuestionCommentHotReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if err, errMsg := yb.SetHotQuestionComment(req.CommunityQuestionCommentId); err != nil {
		br.Msg = errMsg
		br.ErrMsg = "CommentHot，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}

// QuestionDetail
// @Title 问答详情
// @Description 问答详情
// @Param   QuestionId  query  int  false  "问答ID"
// @Success 200 {object} response.CommunityQuestionListResp
// @router /community/question/detail [get]
func (this *CommunityQuestionController) QuestionDetail() {
	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
	}
	questionId, _ := this.GetInt("QuestionId", 0)
	if questionId <= 0 {
		br.Msg = "参数有误"
		return
	}

	// 权限校验
	msg, err := yb.CheckCommunityQuestionPermission(sysUser.AdminId)
	if err != nil {
		br.Msg = msg
		br.ErrMsg = err.Error()
		return
	}
	// 获取问答
	item, e := ybModels.GetQuestionById(questionId)
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取问答信息失败, Err: " + e.Error()
		return
	}
	// 获取问答流程
	processes, e := ybModels.GetCommunityQuestionProcessItemList(questionId)
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取问答流程失败, Err: " + e.Error()
		return
	}
	resp := new(ybModels.CommunityQuestionDetail)
	resp.CommunityQuestionId = item.CommunityQuestionId
	resp.Mobile = item.Mobile
	resp.RealName = item.RealName
	resp.QuestionContent = item.QuestionContent
	resp.ResearchGroupFirstName = item.ResearchGroupFirstName
	resp.ResearchGroupSecondName = item.ResearchGroupSecondName
	resp.VarietyTagName = item.VarietyTagName
	resp.ReplyStatus = item.ReplyStatus
	resp.MsgSendStatus = item.MsgSendStatus
	resp.ReplyTime = item.ReplyTime.Format(utils.FormatDateTime)
	resp.CreateTime = item.CreateTime.Format(utils.FormatDateTime)
	resp.StopReason = item.StopReason
	resp.ProcessList = processes

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// QuestionTransfer
// @Title 转移并通知
// @Description 转移并通知
// @Param	request  body  request.QuestionTransferReq  true  "type json string"
// @Success 200 string "操作成功"
// @router /community/question/transfer [post]
func (this *CommunityQuestionController) QuestionTransfer() {
	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
	}
	// 权限校验
	msg, err := yb.CheckCommunityQuestionPermission(sysUser.AdminId)
	if err != nil {
		br.Msg = msg
		br.ErrMsg = err.Error()
		return
	}

	var req request.QuestionTransferReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	errMsg, err := yb.TransferQuestion(req.QuestionId, req.AdminId, req.ResearchGroupFirstId, req.ResearchGroupSecondId, sysUser.AdminId, sysUser.RealName)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = "DistributeQuestion ErrMsg:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}
