package yb

import (
	"errors"
	"fmt"
	ybResponse "hongze/hongze_mobile_admin/models/response/yb"
	"hongze/hongze_mobile_admin/models/tables/wx_user"
	"hongze/hongze_mobile_admin/models/tables/yb_community_question_comment"
	"hongze/hongze_mobile_admin/utils"
	"strings"
	"time"
)

// GetCommunityQuestionCommentList 问答评论列表
func GetCommunityQuestionCommentList(condition string, pars []interface{}, startSize, pageSize int) (total int, commentList []*ybResponse.CommunityQuestionCommentItem, err error) {
	commentList = make([]*ybResponse.CommunityQuestionCommentItem, 0)
	total, list, e := yb_community_question_comment.GetCommunityQuestionCommentList(condition, pars, startSize, pageSize)
	if e != nil {
		err = errors.New("获取问题列表失败 Err:" + e.Error())
		return
	}
	if len(list) == 0 {
		return
	}
	// 查找用户
	userIdArr := make([]string, 0)
	for _, v := range list {
		userIdArr = append(userIdArr, fmt.Sprint(v.UserId))
	}

	//评论人信息
	wxUserCompanyInfoMap := make(map[int]*wx_user.UserCompanyInfoList, 0)
	if len(userIdArr) > 0 {
		userIds := strings.Join(userIdArr, ",")
		wxUserCompanyInfoList, tmpErr := wx_user.GetUserListByUserIds(userIds)
		if tmpErr != nil {
			err = tmpErr
			return
		}
		for _, v := range wxUserCompanyInfoList {
			wxUserCompanyInfoMap[v.UserId] = v
		}
	}

	for _, v := range list {
		item := &ybResponse.CommunityQuestionCommentItem{
			CommunityQuestionCommentId: v.CommunityQuestionCommentId,
			CommunityQuestionId:        v.CommunityQuestionId,
			UserId:                     v.UserId,
			Content:                    v.Content,
			IsTop:                      v.IsTop,
			IsHot:                      v.IsHot,
			HotTopTime:                 v.HotTopTime,
			IsShowName:                 v.IsShowName,
			SourceAgent:                v.SourceAgent,
			TopTime:                    v.TopTime,
			HotTime:                    v.HotTime,
			ModifyTime:                 v.ModifyTime,
			CreateTime:                 v.CreateTime,
			QuestionContent:            v.QuestionContent,
			CompanyId:                  0,
			UserName:                   "",
			CompanyName:                "",
			CompanyProductStatus:       "",
		}

		//评论人信息
		if tmpWxUserCompanyInfo, ok := wxUserCompanyInfoMap[int(v.UserId)]; ok {
			item.CompanyId = tmpWxUserCompanyInfo.CompanyId
			item.UserName = tmpWxUserCompanyInfo.RealName
			item.CompanyName = tmpWxUserCompanyInfo.CompanyName
			item.CompanyProductStatus = tmpWxUserCompanyInfo.CompanyProductStatus
		}

		commentList = append(commentList, item)
	}
	return
}

// SoftDeleteQuestionComment 删除问题评论
func SoftDeleteQuestionComment(questionCommentId int) (err error, errMsg string) {
	errMsg = `删除成功`
	questionCommentInfo, err := yb_community_question_comment.GetQuestionCommentById(questionCommentId)
	if err != nil {
		errMsg = "查找评论失败"
		if err.Error() == utils.ErrNoRow() {
			errMsg = "该评论不存在"
		}
		return
	}

	//标记删除
	questionCommentInfo.Enabled = 0
	questionCommentInfo.ModifyTime = time.Now()
	err = questionCommentInfo.Update([]string{"Enabled", "ModifyTime"})
	if err != nil {
		errMsg = "删除提问失败"
	}
	return
}

// SetHotQuestionComment 设置/取消 问题评论精选
func SetHotQuestionComment(questionCommentId int) (err error, errMsg string) {
	errMsg = `操作失败`
	questionCommentInfo, err := yb_community_question_comment.GetQuestionCommentById(questionCommentId)
	if err != nil {
		errMsg = "查找评论失败"
		if err.Error() == utils.ErrNoRow() {
			errMsg = "该评论不存在"
		}
		return
	}

	//var isHot int8
	isHot := int8(1)
	if questionCommentInfo.IsHot == 1 {
		isHot = 0
	}
	//标记删除
	questionCommentInfo.IsHot = isHot //是否设置精选(0-未设置,1-已设置)
	questionCommentInfo.HotTopTime = time.Now()
	questionCommentInfo.HotTime = time.Now()
	questionCommentInfo.ModifyTime = time.Now()
	err = questionCommentInfo.Update([]string{"IsHot", "HotTopTime", "HotTime", "ModifyTime"})
	if err != nil {
		errMsg = "操作失败"
	}
	return
}