package cygx

import (
	"errors"
	"fmt"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/services/alarm_msg"
	"hongze/hz_crm_api/utils"
	"strconv"
	"strings"
	"time"
)

// 获取问答系列产业列表
func GetCygxAskserieVideoLabelListMap(askserieVideoIds []int) (mapResp map[int][]*cygx.IndustrialActivityGroupManagementRep) {
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
			go alarm_msg.SendAlarmMsg("获取产品内测的阅读数据，信息失败，GetCygxAskserieVideoLabelListMap Err："+err.Error(), 3)
		}
	}()
	lenArr := len(askserieVideoIds)
	if lenArr == 0 {
		return
	}
	industrialList, e := cygx.GetCygxIndustrialAskserieVideoGroupManagementList(askserieVideoIds)
	if e != nil && e.Error() != utils.ErrNoRow() {
		err = errors.New("获取详情失败 GetCygxIndustrialAskserieVideoGroupManagementList Err" + e.Error())
		return
	}
	mapResp = make(map[int][]*cygx.IndustrialActivityGroupManagementRep, 0)
	for _, v := range industrialList {
		item := new(cygx.IndustrialActivityGroupManagementRep)
		item.IndustryName = v.IndustryName
		item.IndustrialManagementId = v.IndustrialManagementId
		item.ChartPermissionId = v.ChartPermissionId
		mapResp[v.AskserieVideoId] = append(mapResp[v.AskserieVideoId], item)
	}
	return
}

// 获取问答系列产业列表
func GetCygxAskserieVideoLabelMap(askserieVideoIds []int) (mapResp map[int]string) {
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
			go alarm_msg.SendAlarmMsg("获取产品内测的阅读数据，信息失败，GetCygxAskserieVideoLabelListMap Err："+err.Error(), 3)
		}
	}()
	lenArr := len(askserieVideoIds)
	if lenArr == 0 {
		return
	}
	industrialList, e := cygx.GetCygxIndustrialAskserieVideoGroupManagementList(askserieVideoIds)
	if e != nil && e.Error() != utils.ErrNoRow() {
		err = errors.New("获取详情失败 GetCygxIndustrialAskserieVideoGroupManagementList Err" + e.Error())
		return
	}
	mapIndustryName := make(map[int][]string)
	mapResp = make(map[int]string, 0)
	for _, v := range industrialList {
		mapIndustryName[v.AskserieVideoId] = append(mapIndustryName[v.AskserieVideoId], v.IndustryName)
	}

	for k, v := range mapIndustryName {
		mapResp[k] = strings.Join(v, ",")
	}
	return
}

//func init() {
//	UpdateAskserieVideoResourceData(75)
//}

func init12() {
	var conditionupload string
	var pars []interface{}
	conditionupload = " AND  voice_name LIKE '%科技问答%' "
	listVoice, err := cygx.GetActivityVoiceList(conditionupload, pars)
	if err != nil && err.Error() != utils.ErrNoRow() {
		fmt.Println(err)
		return
	}

	for _, v := range listVoice {
		activityId := v.ActivityId
		var industrialManagementIds []string
		industrialList, err := cygx.GetIndustrialActivityGroupManagementList(activityId, 1)
		if err != nil && err.Error() != utils.ErrNoRow() {
			fmt.Println(v.ActivityId)
			fmt.Println(err)
			return
		}
		for _, vi := range industrialList {
			industrialManagementIds = append(industrialManagementIds, strconv.Itoa(vi.IndustrialManagementId))
		}

		activityInfo, err := cygx.GetAddActivityInfoById(activityId)
		if err != nil {
			fmt.Println(v.ActivityId)
			fmt.Println(err)
			return
		}
		if activityInfo != nil {

			item := new(cygx.CygxAskserieVideo)
			//item.AskserieVideoId = askserieVideoId
			item.VideoName = v.VoiceName
			item.VideoUrl = v.VoiceUrl
			item.VideoDuration = v.VoicePlaySeconds
			item.ChartPermissionId = activityInfo.ChartPermissionId
			item.ChartPermissionName = activityInfo.ChartPermissionName
			item.PublishStatus = 1
			item.BackgroundImg = v.BackgroundImg
			item.ShareImg = v.ShareImg
			//item.AdminId = sysUser.AdminId
			item.ModifyDate = time.Now()
			item.PublishDate = utils.StrTimeToTime(activityInfo.ActivityTime)
			item.CreateTime = time.Now()

			fmt.Println(item.VideoDuration, "VideoDuration")
			fmt.Println(item.ShareImg, "ShareImg")
			fmt.Println(item.PublishDate, "PublishDate")
			//fmt.Println(item, strings.Join(industrialManagementIds, ","))

			newId, err := cygx.AddCygxAskserieVideo(item, strings.Join(industrialManagementIds, ","))
			fmt.Println(newId)
			if err != nil {
				fmt.Println(err)
				return
			}
		}
	}
}
