package services

import (
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/utils"
	"strconv"
	"strings"
	"time"
)

// GetBannerUrlBody 处理banner的连接并做跳转处理
func GetBannerUrlBody(url string) (itemResp *models.BannerUrlResp) {
	//2：文章详情  https://web.hzinsights.com/material/info/8436  小程序路径：/pageMy/reportDetail/reportDetail?id=
	//3：活动详情  https://web.hzinsights.com/activity/detail/2701  小程序路径：/activityPages/activityDetail/activityDetail?id=
	//4：产业详情  https://web.hzinsights.com/indepth/info/20/79  小程序路径：/reportPages/IndustryReport/IndustryReport?id=
	//5：关于我们  https://clpttest.hzinsights.com/about
	//6：产品内测 https://clpttest.hzinsights.com/internal/article/29  小程序路径：/reportPages/internalDetials/internalDetials?id=
	//7：本周研究汇总 https://clpttest.hzinsights.com/summary/2/152  小程序路径：/reportPages/reportSecretDetail/reportSecretDetail?type=2&id=
	//8：上周研究汇总 https://clpttest.hzinsights.com/summary/3/112 小程序路径：/reportPages/reportSecretDetail/reportSecretDetail?type=3&id=
	//9：专项调研活动 https://clpttest.hzinsights.com/activity/info/70  小程序路径：/activityPages/specialDetail/specialDetail?id=
	//10： 重点公司 https://clpttest.hzinsights.com/recent/67  小程序路径：/reportPages/keyCompany/keyCompany
	//11： 主题详情 https://clpttest.hzinsights.com/community/theme/117  小程序路径：/reportPages/researchTheme/researchTheme?id=
	//12： 作者详情 https://clpttest.hzinsights.com/community/author/78 小程序路径：/reportPages/authorPages/authorPages?id=

	mapPath := make(map[int]string)
	mapPath[2] = utils.WX_MSG_PATH_ARTICLE_DETAIL          //文章详情
	mapPath[3] = utils.WX_MSG_PATH_ACTIVITY_DETAIL         //活动详情
	mapPath[4] = utils.WX_MSG_PATH_INDUSTRY_DETAIL         //产业详情
	mapPath[5] = utils.WX_MSG_PATH_ABOUT_US                //关于我们视频
	mapPath[6] = utils.WX_MSG_PATH_PRODUCTINTERIOR_DETAIL  //产品内测
	mapPath[7] = utils.WX_MSG_PATH_THIS_WEEK_DETAIL        //本周研究汇总
	mapPath[8] = utils.WX_MSG_PATH_LAST_WEEK_DETAIL        //上周研究汇总
	mapPath[9] = utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL //专项调研活动
	mapPath[10] = utils.WX_MSG_PATH_KEY_COMPANY_DETAIL     //重点公司
	mapPath[11] = utils.WX_MSG_PATH_RESEARCHTHEME_DETAIL   //主题详情
	mapPath[12] = utils.WX_MSG_PATH_AUTHOR_DETAIL          //作者详情

	material := "material/info"             //2：文章详情
	activity := "activity/detail"           //3：活动详情
	activityVideo := "activity/video"       //3：活动视频页，调活动详情
	indepth := "indepth/info"               //4：产业详情
	indepthVideo := "indepth/video"         //4：产业视频页,调详情
	about := "/about"                       //5：关于我们
	internal := "internal/article"          //6：产品内测
	thisWeek := "summary/2/"                //7：本周研究汇总
	lastWeek := "summary/3/"                //8：上周研究汇总
	activitySpecial := "activity/info"      //9：专项调研活动
	recent := "/recent/"                    //10：重点公司
	communityTheme := "/community/theme/"   //11：主题详情
	communityAuthor := "/community/author/" //12：作者详情
	item := new(models.BannerUrlResp)
	item.Body = url
	urlSlice := strings.Split(url, "/")
	lenurlSlice := len(urlSlice)
	sourceId, _ := strconv.Atoi(urlSlice[lenurlSlice-1])

	if strings.Contains(url, material) {
		item.Type = 2
	} else if strings.Contains(url, activity) {
		item.Type = 3
	} else if strings.Contains(url, activityVideo) {
		activityVideo, err := models.GetCygxActivityVideoById(sourceId)
		if err != nil {
			go utils.SendAlarmMsg("banner链接解析失败"+err.Error()+url, 2)
		}
		if activityVideo != nil {
			sourceId = activityVideo.ActivityId
			item.Type = 3
		}
	} else if strings.Contains(url, indepth) {
		if lenurlSlice >= 2 {
			chartPermissionId, _ := strconv.Atoi(urlSlice[lenurlSlice-2])
			item.ChartPermissionId = chartPermissionId
		}
		item.Type = 4
	} else if strings.Contains(url, indepthVideo) {
		if lenurlSlice >= 2 {
			sourceId, _ = strconv.Atoi(urlSlice[lenurlSlice-2])
		}
		item.Type = 4
	} else if strings.Contains(url, about) {
		item.Type = 5
		sourceId = 1 // 强制设置为正数前端跳转使用
	} else if strings.Contains(url, internal) {
		item.Type = 6
	} else if strings.Contains(url, thisWeek) {
		item.Type = 7
	} else if strings.Contains(url, lastWeek) {
		item.Type = 8
	} else if strings.Contains(url, activitySpecial) {
		item.Type = 9
	} else if strings.Contains(url, recent) {
		item.Type = 10
	} else if strings.Contains(url, communityTheme) {
		item.Type = 11
	} else if strings.Contains(url, communityAuthor) {
		item.Type = 12
	} else {
		item.Type = 1
	}
	if mapPath[item.Type] != "" {
		item.Path = "/" + mapPath[item.Type]
	}
	item.SourceId = sourceId
	itemResp = item
	return
}

func AddCygxBannerHistory(user *models.WxUserItem, bannerId int) (err error) {
	if user.UserId == 0 {
		return
	}
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg("banner点击信息记录失败"+err.Error()+"bannerId"+strconv.Itoa(bannerId)+"userId："+strconv.Itoa(user.UserId), 2)
		}
	}()
	historyRecord := new(models.CygxBannerHistory)
	historyRecord.UserId = user.UserId
	historyRecord.BannerId = bannerId
	historyRecord.CreateTime = time.Now()
	historyRecord.Mobile = user.Mobile
	historyRecord.Email = user.Email
	historyRecord.CompanyId = user.CompanyId
	historyRecord.CompanyName = user.CompanyName
	historyRecord.RegisterPlatform = utils.REGISTER_PLATFORM
	sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	historyRecord.RealName = user.RealName
	if sellerItem != nil {
		historyRecord.SellerName = sellerItem.RealName
	}
	_, err = models.AddCygxBannerHistory(historyRecord)
	return
}
