package like

import (
	"errors"
	"fmt"
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/models/request/like"
	"hongze/hongze_yb/models/response"
	"hongze/hongze_yb/models/tables/yb_like"
	"hongze/hongze_yb/services"
	"hongze/hongze_yb/services/user"
	"time"
)

// 设置点赞
func SetLike(user user.UserInfo, req like.ReqLike) (resp response.RespLike, err error) {
	var errMsg string
	defer func() {
		if err != nil {
			global.LOG.Critical(fmt.Sprintf("SetLike: userId=%d, err:%s， errMsg:%s", user.UserID, err.Error(), errMsg))
		}
	}()
	reportId := req.ReportId
	reportChapterId := req.ReportChapterId

	if req.SourceAgent == 0 {
		err = errors.New("请输入点赞来源")
		return
	}
	if req.ReportChapterId <=0 && req.ReportId <=0 && req.OldReportId <=0 && req.OldReportChapterId <=0{
		err = errors.New("请输入报告ID")
		return
	}
	if (req.ReportChapterId > 0 && req.ReportId <=0 ) || (req.OldReportChapterId > 0 && req.OldReportId <= 0){
		err = errors.New("请输入报告ID")
		return
	}
	// 判断是否有点赞权限
	err, errMsg = services.CheckSimpleCompanyProduct(user)
	if err != nil {
		return
	}
	//处理老报告, 转化成新报告ID
	if req.ReportChapterId <=0 && req.ReportId <=0 && (req.OldReportId >0 || req.OldReportChapterId > 0) {
		reportId, reportChapterId, err, errMsg = services.GetReportIdReportChapterIdByOldReportId(uint64(req.OldReportId), uint64(req.OldReportChapterId))
		if err != nil {
			return
		}
	} else {
		// 判断报告ID是否正确
		err, errMsg = services.CheckReportExistByReportIdReportChapterId(reportId, reportChapterId)
		if err != nil {
			return
		}
	}

	//查询用户对研报的点赞状态
	var likeItem *yb_like.YbLike
	if reportId > 0 {
		likeItem, err = yb_like.GetLikeByUserIdAndReportId(user.UserID, reportId, reportChapterId)
	} else {
		likeItem, err = yb_like.GetLikeByUserIdAndOldReportId(user.UserID, req.OldReportId, req.OldReportChapterId)
	}
	if err != nil {
		errMsg = err.Error()
		err = errors.New("查询用户点赞记录出错")
		return
	}
	//如果未点赞则新增点赞记录
	now :=  time.Now()
	if likeItem.LikeId <= 0 {
		likeItem = &yb_like.YbLike{
			ReportId:          reportId,
			ReportChapterId:   reportChapterId,
			UserId:            user.UserID,
			Enabled:         1,
			CreateTime:     now,
			ModifyTime:     now,
			SourceAgent:    req.SourceAgent,
			OldReportId:    req.OldReportId,
			OldReportChapterId: req.OldReportChapterId,
		}
		err = likeItem.Create()
		if err != nil {
			errMsg = err.Error()
			err = errors.New("新增点赞记录出错")
			return
		}
	} else {
		//如果已点赞则更新成取消点赞
		if likeItem.Enabled == 1 {
			updates := []string{"enabled", "modify_time"}
			likeItem.Enabled = 0
			likeItem.ModifyTime = now
			err = likeItem.Update(updates)
			if err != nil {
				errMsg = err.Error()
				err = errors.New("更新点赞记录出错")
				return
			}
		} else {
			//如果取消点赞则更新成已点赞
			updates := []string{"enabled", "modify_time"}
			likeItem.Enabled = 1
			likeItem.ModifyTime = now
			err = likeItem.Update(updates)
			if err != nil {
				errMsg = err.Error()
				err = errors.New("更新点赞记录出错")
				return
			}
		}
	}
	//查询总的点赞数
	var likeNum int64
	if reportId > 0 {
		likeNum, err = yb_like.GetLikeNumByReportId(reportId, reportChapterId)
	}else{
		likeNum, err = yb_like.GetLikeNumByOldReportId(req.OldReportId, req.OldReportChapterId)
	}

	if err != nil {
		errMsg = err.Error()
		err = errors.New("查询点赞数出错")
		return
	}
	resp.LikeEnabled = likeItem.Enabled
	resp.LikeNum = likeNum
	return
}
