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 = " "
	listVoice, err := cygx.GetActivityVoiceListNew(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.ActivityId = v.ActivityId
			//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, ","))
			//continue
			newId, err := cygx.AddCygxAskserieVideo(item, strings.Join(industrialManagementIds, ","))
			fmt.Println(newId)
			if err != nil {
				fmt.Println(err)
				return
			}
		}
	}
}

// 修改分享图片脚本
func initpy() {

	//python3 main.py
	var condition string
	var pars []interface{}
	listVoice, err := cygx.GetActivityVoiceList(condition, pars)
	if err != nil && err.Error() != utils.ErrNoRow() {
		fmt.Println(err)
		return
	}

	for _, v := range listVoice {
		shareImg, _ := MakeCygxMp3HtmlImg(v.VoicePlaySeconds, 0) //生成分享图片
		v.ShareImg = shareImg
		err = cygx.UpdateCygxActivityVoice(v)
		if err != nil {
			err = errors.New("UpdateCygxActivityVoice" + err.Error())
			return
		}
		fmt.Println(err)
	}

	listVideo, err := cygx.GetCygxActivityVideoList(condition, pars)
	if err != nil && err.Error() != utils.ErrNoRow() {
		fmt.Println(err)
		return
	}

	for _, v := range listVideo {
		shareImg, _ := MakeCygxMp4HtmlImg(v.VideoDuration, 0) //生成分享图片
		v.ShareImg = shareImg
		err = cygx.UpdateCygxActivityVideo(v)
		if err != nil {
			err = errors.New("UpdateCygxActivityVideo" + err.Error())
			return
		}
		fmt.Println(err)
	}

	listVideoRoadshow, err := cygx.GetCygxMicroRoadshowVideoList(condition, pars, 0, 1000)
	if err != nil && err.Error() != utils.ErrNoRow() {
		fmt.Println(err)
		return
	}

	for _, v := range listVideoRoadshow {
		shareImg, _ := MakeCygxMp4HtmlImg(v.VideoDuration, 0) //生成分享图片
		v.ShareImgUrl = shareImg
		err = cygx.UpdateCygxMicroRoadshowVideo(v)
		if err != nil {
			err = errors.New("UpdateCygxMicroRoadshowVideo" + err.Error())
			return
		}
		fmt.Println(err)
	}

	listAskserVoice, err := cygx.GetCygxAskserieVideoList(condition, pars, 0, 1000)
	if err != nil && err.Error() != utils.ErrNoRow() {
		fmt.Println(err)
		return
	}

	for _, v := range listAskserVoice {
		shareImg, _ := MakeCygxMp3HtmlImg(v.VideoDuration, 0) //生成分享图片
		v.ShareImg = shareImg
		err = cygx.EditCygxAskserieVideoShareImg(shareImg, v.AskserieVideoId)
		if err != nil {
			err = errors.New("EditCygxAskserieVideoShareImg" + err.Error())
			return
		}
		fmt.Println(err)
	}

	return
}

// 去掉后缀名脚本
func initFileName() {
	var condition string
	var pars []interface{}
	listVoice, err := cygx.GetActivityVoiceList(condition, pars)
	if err != nil && err.Error() != utils.ErrNoRow() {
		fmt.Println(err)
		return
	}

	for _, v := range listVoice {
		v.VoiceName = utils.RemoveFileSuffixName(v.VoiceName) //去掉后缀名称
		err = cygx.UpdateCygxActivityVoice(v)
		if err != nil {
			err = errors.New("UpdateCygxActivityVoice" + err.Error())
			return
		}
		fmt.Println(err)
	}

	listVideo, err := cygx.GetCygxActivityVideoList(condition, pars)
	if err != nil && err.Error() != utils.ErrNoRow() {
		fmt.Println(err)
		return
	}

	for _, v := range listVideo {
		v.VideoName = utils.RemoveFileSuffixName(v.VideoName) //去掉后缀名称
		err = cygx.UpdateCygxActivityVideo(v)
		if err != nil {
			err = errors.New("UpdateCygxActivityVideo" + err.Error())
			return
		}
		fmt.Println(err)
	}

	listVideoRoadshow, err := cygx.GetCygxMicroRoadshowVideoList(condition, pars, 0, 1000)
	if err != nil && err.Error() != utils.ErrNoRow() {
		fmt.Println(err)
		return
	}

	for _, v := range listVideoRoadshow {
		v.VideoName = utils.RemoveFileSuffixName(v.VideoName) //去掉后缀名称
		err = cygx.UpdateCygxMicroRoadshowVideo(v)
		if err != nil {
			err = errors.New("UpdateCygxMicroRoadshowVideo" + err.Error())
			return
		}
		fmt.Println(err)
	}

	listAskserVoice, err := cygx.GetCygxAskserieVideoList(condition, pars, 0, 1000)
	if err != nil && err.Error() != utils.ErrNoRow() {
		fmt.Println(err)
		return
	}

	for _, v := range listAskserVoice {
		v.VideoName = utils.RemoveFileSuffixName(v.VideoName) //去掉后缀名称
		err = cygx.EditCygxAskserieVideoVideoName(v.VideoName, v.AskserieVideoId)
		if err != nil {
			err = errors.New("EditCygxAskserieVideoShareImg" + err.Error())
			return
		}
		fmt.Println(err)
	}

	return
}
