package services

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/utils"
	"time"
)

//10.5.1版本上线需要更新的数据

//func init1111() {
//	var condition string
//	var pars []interface{}
//	var err error
//	fmt.Print()
//
//	var topCond string
//	var topPars []interface{}
//	toplist, err := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
//	if err != nil {
//		return
//	}
//
//	mapIndustryName := make(map[int]string)
//	for _, v := range toplist {
//		mapIndustryName[v.IndustrialManagementId] = v.IndustryName
//	}
//	fmt.Println(mapIndustryName)
//	//处理上周纪要汇总
//	condition = ` AND  type = 1  `
//	list, err := models.GetCygxIndustryFllowListByCon(condition, pars, 0, 100000)
//	if err != nil {
//		fmt.Println(err)
//	}
//	var items []*models.CygxUserLabel
//	for _, user := range list {
//		item := new(models.CygxUserLabel)
//		item.UserId = user.UserId
//		item.CompanyId = user.CompanyId
//		item.RealName = user.RealName
//		item.Mobile = user.Mobile
//		item.Email = user.Email
//		item.Label = mapIndustryName[user.IndustrialManagementId]
//		item.SourceId = user.IndustrialManagementId
//		item.Source = 1
//		item.IsFollow = 1
//		item.CreateTime = time.Now()
//		item.ModifyTime = time.Now()
//		items = append(items, item)
//		if len(items)%5000 == 0 {
//			err = models.CygxUserLabelMulti(items)
//			fmt.Println(err)
//			items = make([]*models.CygxUserLabel, 0)
//		}
//	}
//	if len(items) > 0 {
//		err = models.CygxUserLabelMulti(items)
//		fmt.Println(err, "555")
//	}
//}

func init123() {
	var condition string
	var pars []interface{}
	var err error
	fmt.Print()

	var topCond string
	var topPars []interface{}
	toplist, err := models.GetCygxReportMappingCygxByCon(topCond, topPars)
	if err != nil {
		return
	}

	mapIndustryName := make(map[int]string)
	for _, v := range toplist {
		mapIndustryName[v.Id] = v.MatchTypeName
	}
	fmt.Println(mapIndustryName)

	condition = ` `
	list, err := models.GetCygxXzsChooseCategoryListByCon(condition, pars, 0, 100000)
	if err != nil {
		fmt.Println(err)
	}
	var items []*models.CygxUserLabel
	for _, user := range list {
		if user.IdCygx == 0 {
			continue
		}
		item := new(models.CygxUserLabel)
		item.UserId = user.UserId
		item.CompanyId = user.CompanyId
		item.RealName = user.RealName
		item.Mobile = user.Mobile
		item.Email = user.Email
		item.Label = mapIndustryName[user.IdCygx]
		item.SourceId = user.IdCygx
		item.Source = 2
		item.IsFollow = 1
		item.CreateTime = time.Now()
		item.ModifyTime = time.Now()
		fmt.Println(item.SourceId)
		items = append(items, item)
		if len(items)%5000 == 0 {
			err = models.CygxUserLabelMulti(items)
			fmt.Println(err)
			items = make([]*models.CygxUserLabel, 0)
		}
	}
	if len(items) > 0 {
		err = models.CygxUserLabelMulti(items)
		fmt.Println(err, "555")
	}
}

func init5333() {
	var condition string
	var pars []interface{}
	var err error
	fmt.Print()

	var topCond string
	var topPars []interface{}
	toplist, err := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
	if err != nil {
		return
	}

	mapIndustryName := make(map[int]string)
	for _, v := range toplist {
		mapIndustryName[v.IndustrialManagementId] = v.IndustryName
	}
	fmt.Println(mapIndustryName)
	condition = ` AND activity_time > '2022-11-25 00:00:00' AND publish_status = 1    `
	listAct, err := models.GetCygxActivityList(condition, pars, 0, 100000)
	if err != nil {
		fmt.Println(err)
	}
	var items []*models.CygxUserLabelActivity
	for _, v := range listAct {
		fmt.Println(v.ActivityTime)
		var condition1 string
		var pars1 []interface{}
		condition1 = ` AND activity_id = ?  `
		pars1 = append(pars1, v.ActivityId)
		total, e := models.GetCygxIndustrialActivityGroupManagementCount(condition1+"  AND  source = 1 ", pars1)
		if e != nil {
			fmt.Println(e)
			return
		}
		if total == 0 {
			continue
		}
		industrialList, e := models.GetCygxIndustrialActivityGroupManagementList(condition1+"  AND  source = 1 ", pars1)
		if e != nil {
			fmt.Println(e)
			return
		}

		pars = make([]interface{}, 0)
		condition = ` AND activity_id  = ? AND is_meeting = 1  `
		pars = append(pars, v.ActivityId)
		signUpList, err := models.GetSignupDetailBySignup(condition, pars)
		if err != nil && err.Error() != utils.ErrNoRow() {
			return
		}
		for _, vi := range industrialList {
			for _, user := range signUpList {
				item := new(models.CygxUserLabelActivity)
				item.UserId = user.UserId
				item.CompanyId = user.CompanyId
				item.RealName = user.RealName
				item.Mobile = user.Mobile
				item.Email = user.Email
				item.ActivityId = v.ActivityId
				item.IndustrialManagementId = vi.IndustrialManagementId
				item.Label = mapIndustryName[vi.IndustrialManagementId]
				item.CreateTime = utils.StrTimeToTime(v.ActivityTime)
				item.ModifyTime = time.Now()
				items = append(items, item)
				if len(items)%5000 == 0 {
					_, err = models.AddCygxUserLabelActivityList(items)
					fmt.Println(err)
					items = make([]*models.CygxUserLabelActivity, 0)
				}
			}
		}
	}
	fmt.Println(len(items))
	if len(items) > 0 {
		_, err = models.AddCygxUserLabelActivityList(items)
		fmt.Println(err, "555")
	}
}

func init5444() {
	var condition string
	var pars []interface{}
	var err error
	fmt.Print()

	var topCond string
	var topPars []interface{}
	toplist, err := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
	if err != nil {
		return
	}

	mapIndustryName := make(map[int]string)
	for _, v := range toplist {
		mapIndustryName[v.IndustrialManagementId] = v.IndustryName
	}
	fmt.Println(mapIndustryName)
	condition = ` AND is_meeting = 1  `
	listAct, err := models.GetCygxActivitySpecialmeetingDetailList(condition, pars)
	if err != nil {
		fmt.Println(err)
	}
	var items []*models.CygxUserLabelActivitySpecial
	for _, user := range listAct {
		var condition1 string
		var pars1 []interface{}
		condition1 = ` AND activity_id = ?  `
		pars1 = append(pars1, user.ActivityId)
		total, e := models.GetCygxIndustrialActivityGroupManagementCount(condition1+"  AND  source = 2 ", pars1)
		if e != nil {
			fmt.Println(e)
			return
		}
		if total == 0 {
			continue
		}
		industrialList, e := models.GetCygxIndustrialActivityGroupManagementList(condition1+"  AND  source = 2 ", pars1)
		if e != nil {
			fmt.Println(e)
			return
		}
		for _, vi := range industrialList {
			item := new(models.CygxUserLabelActivitySpecial)
			item.UserId = user.UserId
			item.CompanyId = user.CompanyId
			item.RealName = user.RealName
			item.Mobile = user.Mobile
			item.Email = user.Email
			item.ActivityId = user.ActivityId
			item.IndustrialManagementId = vi.IndustrialManagementId
			item.Label = mapIndustryName[vi.IndustrialManagementId]
			item.CreateTime = utils.StrTimeToTime(user.CreateTime)
			item.ModifyTime = time.Now()
			items = append(items, item)
			if len(items)%5000 == 0 {
				_, err = models.AddCygxUserLabelActivitySpecialList(items)
				fmt.Println(err)
				items = make([]*models.CygxUserLabelActivitySpecial, 0)
			}
		}
	}
	fmt.Println(len(items))
	if len(items) > 0 {
		_, err = models.AddCygxUserLabelActivitySpecialList(items)
		fmt.Println(err, "555")
	}
}

func init5555() {
	var condition string
	var pars []interface{}
	var err error
	fmt.Print()

	var topCond string
	var topPars []interface{}
	toplist, err := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
	if err != nil {
		return
	}

	mapIndustryName := make(map[int]string)
	for _, v := range toplist {
		mapIndustryName[v.IndustrialManagementId] = v.IndustryName
	}
	//fmt.Println(mapIndustryName)
	condition = ` AND   stop_time > 3  AND create_time > '2022-11-25 00:00:00' GROUP BY article_id  `
	//condition = `   AND   stop_time > 4   AND create_time > '2023-04-25 00:00:00' GROUP BY article_id  `
	listArtGroup, err := models.GetCygxArticleHistoryRecordNewpvList(condition, pars)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(len(listArtGroup))
	var source int

	itemsMap := make(map[int][]*models.CygxUserLabel)
	for _, v := range listArtGroup {

		fmt.Println(v.ArticleId)
		var items []*models.CygxUserLabel
		articleId := v.ArticleId
		var condition string
		var pars []interface{}
		condition = ` AND article_id = ? `
		pars = append(pars, v.ArticleId)
		industrialList, e := models.GetIndustrialArticleGroupManagementList(condition, pars)
		if e != nil && e.Error() != utils.ErrNoRow() {
			fmt.Println(e)
			continue
			//return
		}
		if len(industrialList) == 0 {
			//如果没有行产业归类就按照行业报告处理
			source = 2
			detailArticle, e := models.GetArticleDetailById(articleId)
			if e != nil {
				fmt.Println(e)
				continue
				//return
			}

			labelDetail, e := models.GetdetailByCategoryIdLabel(detailArticle.CategoryId)
			if e != nil {
				fmt.Println(e)
				continue
				//return
			}
			label := labelDetail.MatchTypeName
			industrialManagementId := labelDetail.Id
			item := new(models.CygxUserLabel)

			item.Label = label
			item.SourceId = industrialManagementId
			item.Source = source
			item.Weight = 1
			item.CreateTime = time.Now()
			item.ModifyTime = time.Now()
			items = append(items, item)
		} else {
			//如果有行产业归类就按照产业报告处理
			var topCond string
			var topPars []interface{}
			var industrialManagementIds []int

			for _, v := range industrialList {
				industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
			}
			idsLen := len(industrialManagementIds)
			if idsLen > 0 {
				topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)`
				topPars = append(topPars, industrialManagementIds)
			} else {
				continue
			}
			industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
			if e != nil {
				fmt.Println(e)
				continue
			}
			source = 1
			for _, v := range industrNamelist {
				label := v.IndustryName
				industrialManagementId := v.IndustrialManagementId

				item := new(models.CygxUserLabel)
				item.Label = label
				item.SourceId = industrialManagementId
				item.Source = source
				item.Weight = 1
				item.CreateTime = time.Now()
				item.ModifyTime = time.Now()
				items = append(items, item)

			}

			//for _, vi := range industrialList {
			//	for _, user := range signUpList {
			//		item := new(models.CygxUserLabelActivity)
			//		item.UserId = user.UserId
			//		item.CompanyId = user.CompanyId
			//		item.RealName = user.RealName
			//		item.Mobile = user.Mobile
			//		item.Email = user.Email
			//		item.ActivityId = v.ActivityId
			//		item.IndustrialManagementId = vi.IndustrialManagementId
			//		item.Label = mapIndustryName[vi.IndustrialManagementId]
			//		item.CreateTime = utils.StrTimeToTime(v.ActivityTime)
			//		item.ModifyTime = time.Now()
			//		items = append(items, item)
			//		if len(items)%50 == 0 {
			//			_, err = models.AddCygxUserLabelActivityList(items)
			//			fmt.Println(err)
			//			items = make([]*models.CygxUserLabelActivity, 0)
			//		}
			//	}
			//}
			itemsMap[articleId] = items
		}
	}
	condition = `   AND   stop_time > 3   AND create_time > '2022-11-25 00:00:00'   `
	//condition = `  AND   stop_time > 4    AND create_time > '2023-4-25 00:00:00'   `
	listArt, err := models.GetCygxArticleHistoryRecordNewpvList(condition, pars)
	if err != nil {
		fmt.Println(err)
	}
	var items []*models.CygxUserLabelArticle

	fmt.Println(len(listArt))

	for _, user := range listArt {
		if len(itemsMap[user.ArticleId]) > 0 {
			for k2, vlsit := range itemsMap {
				if k2 != user.ArticleId {
					continue
				}
				for _, vm := range vlsit {

					//if user.ArticleId == 8365 {
					//	fmt.Println("k", k, "_k2:", k2, "_k3:", k3)
					//	fmt.Println(vm.Label)
					//}
					item := new(models.CygxUserLabelArticle)
					item.UserId = user.UserId
					item.ArticleId = user.ArticleId
					item.CompanyId = user.CompanyId
					//item.RealName = user.R
					item.Mobile = user.Mobile
					item.Email = user.Email
					item.Label = vm.Label
					item.SourceId = vm.SourceId
					item.Source = vm.Source
					item.CreateTime = user.CreateTime
					item.ModifyTime = time.Now()
					items = append(items, item)
					if len(items)%5000 == 0 {
						_, err = models.AddCygxUserLabelArticleList(items)
						fmt.Println(err)
						items = make([]*models.CygxUserLabelArticle, 0)
					}
				}
			}
		}
	}
	//for k, vlsit := range itemsMap[8365] {
	//	fmt.Println(k)
	//	fmt.Println(vlsit)
	//	//for _, vm := range vlsit {
	//	//	fmt.Println(vm)
	//	//}
	//}
	fmt.Println(len(items))
	fmt.Println(len(itemsMap))
	fmt.Println((itemsMap))
	fmt.Println(len(items))
	if len(items) > 0 {
		_, err = models.AddCygxUserLabelArticleList(items)
		fmt.Println(err, "555")
	}
}

func init10_12() {
	var condition string
	var pars []interface{}
	condition = " AND active_state = 1 "
	listAct, err := models.GetCygxActivityList(condition, pars, 0, 100000)
	fmt.Println(err)
	for _, v := range listAct {
		resultTime := utils.StrTimeToTime(v.ActivityTime)                             //时间字符串格式转时间格式
		cancelDeadline := resultTime.Add(-time.Hour * 1).Format(utils.FormatDateTime) //默认一小时
		o := orm.NewOrm()
		sql := `UPDATE cygx_activity SET cancel_deadline = ? WHERE activity_id  = ? `
		_, err = o.Raw(sql, cancelDeadline, v.ActivityId).Exec()
		fmt.Println(err)
	}
}
