package services

import (
	"errors"
	"fmt"
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/utils"
	"strconv"
	"time"
)

// 记录用户活动音频播放记录
func AddAskserieVideoHistoryRecord(user *models.WxUserItem, askserieVideoId, playSeconds int) {
	if playSeconds == 0 {
		return
	}
	key := utils.CYGX_VIDEO_HISTORY_KEY + utils.CYGX_OBJ_ASKSERIEVIDEO + strconv.Itoa(askserieVideoId) + "_" + strconv.Itoa(user.UserId)

	var err error
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg(fmt.Sprint("记录用户活动音频播放记录失败 AddAskserieVideoHistoryRecord Err:", err.Error(), "askserieVideoId：", askserieVideoId, "UserId：", user.UserId), 2)
		}
	}()
	item := new(models.CygxAskserieVideoHistoryRecord)
	item.UserId = user.UserId
	item.RealName = user.RealName
	item.AskserieVideoId = askserieVideoId
	item.VideoDuration = playSeconds
	item.Mobile = user.Mobile
	item.Email = user.Email
	item.CompanyId = user.CompanyId
	item.CompanyName = user.CompanyName
	item.CreateTime = time.Now().Add(-time.Duration(playSeconds) * time.Second) //减去观看时长当做，阅读时间
	item.ModifyTime = time.Now()
	item.RegisterPlatform = utils.REGISTER_PLATFORM
	sellerItem, e := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
	if e != nil && e.Error() != utils.ErrNoRow() {
		err = errors.New("GetSellerByCompanyIdCheckFicc, Err: " + e.Error())
		return
	}
	if sellerItem != nil {
		item.SellerName = sellerItem.RealName
	}
	fmt.Println(utils.Rc.IsExist(key))
	if !utils.Rc.IsExist(key) {
		fmt.Println("添加")
		//添加记录
		e = models.AddCygxAskserieVideoHistoryRecord(item)
		if e != nil {
			err = errors.New("AddCygxAskserieVideoHistoryRecord, Err: " + e.Error())
			return
		}
		//播放记录加1
		e = models.UpdateAskserieVideoCounts(askserieVideoId)
		if e != nil {
			err = errors.New("UpdateActivityVideoCounts, Err: " + e.Error())
			return
		}
		utils.Rc.Put(key, 1, 10*time.Minute)
	} else {
		fmt.Println("更新")
		//更新停留时长
		lastItem, e := models.GetLastCygxAskserieVideoHistoryRecord(askserieVideoId, user.UserId)
		if e != nil {
			err = errors.New("GetLastCygxActivityVideoHistory, Err: " + e.Error())
			return
		}
		//更新播放时间
		e = models.UpdateLastCygxAskserieVideoHistoryRecord(strconv.Itoa(playSeconds), lastItem.Id)
		if e != nil {
			err = errors.New("UpdateLastCygxAskserieVideoHistoryRecord, Err: " + e.Error())
			return
		}
	}

	//if playSeconds != 0 {
	//	//lastItem, e := models.GetLastCygxAskserieVideoHistoryRecord(askserieVideoId, user.UserId)
	//	//if e != nil {
	//	//	err = errors.New("GetLastCygxActivityVideoHistory, Err: " + e.Error())
	//	//	return
	//	//}
	//	////更新播放时间
	//	//e = models.UpdateLastCygxAskserieVideoHistoryRecord(strconv.Itoa(playSeconds), lastItem.Id)
	//	//if e != nil {
	//	//	err = errors.New("UpdateLastCygxAskserieVideoHistoryRecord, Err: " + e.Error())
	//	//	return
	//	//}
	//
	//} else {
	//	//添加记录
	//	e = models.AddCygxAskserieVideoHistoryRecord(item)
	//	if e != nil {
	//		err = errors.New("AddCygxAskserieVideoHistoryRecord, Err: " + e.Error())
	//		return
	//	}
	//	//播放记录加1
	//	e = models.UpdateAskserieVideoCounts(askserieVideoId)
	//	if e != nil {
	//		err = errors.New("UpdateActivityVideoCounts, Err: " + e.Error())
	//		return
	//	}
	//}

	return
}

// 系列问答视频留言记录
func AddCygxAskserieVideoCollection(user *models.WxUserItem, askserieVideoId int, content string) {
	var err error
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg(fmt.Sprint("记录用户活动音频播放记录失败 AddAskserieVideoHistoryRecord Err:", err.Error(), "askserieVideoId：", askserieVideoId, "UserId：", user.UserId), 2)
		}
	}()
	item := new(models.CygxAskserieVideoCollection)
	item.UserId = user.UserId
	item.RealName = user.RealName
	item.AskserieVideoId = askserieVideoId
	item.Content = content
	item.Mobile = user.Mobile
	item.Email = user.Email
	item.CompanyId = user.CompanyId
	item.CompanyName = user.CompanyName
	item.CreateTime = time.Now()
	item.ModifyTime = time.Now()
	item.RegisterPlatform = utils.REGISTER_PLATFORM
	sellerItem, e := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
	if e != nil && e.Error() != utils.ErrNoRow() {
		err = errors.New("GetSellerByCompanyIdCheckFicc, Err: " + e.Error())
		return
	}
	if sellerItem != nil {
		item.SellerName = sellerItem.RealName
	}
	//添加记录
	e = models.AddCygxAskserieVideoCollection(item)
	if e != nil {
		err = errors.New("AddCygxAskserieVideoCollection, Err: " + e.Error())
		return
	}
	//播放记录加1
	e = models.UpdateAskserieVideoCommentNum(askserieVideoId)
	if e != nil {
		err = errors.New("UpdateAskserieVideoCommentNum, Err: " + e.Error())
		return
	}
	return
}
