package cygx

import (
	"encoding/json"
	"fmt"
	"github.com/rdlucklib/rdluck_tools/paging"
	"hongze/hz_crm_api/controllers"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/services"
	cygxService "hongze/hz_crm_api/services/cygx"
	"hongze/hz_crm_api/utils"
	"regexp"
	"strconv"
	"strings"
	"time"
)

// 上周纪要汇总
type MinutesSummaryController struct {
	controllers.BaseAuthController
}

// @Title 上周纪要汇总内容的保存与发布
// @Description 上周纪要汇总内容的保存与发布接口
// @Param	request	body cygx.AddCygxMinutesSummary true "type json string"
// @Success 200 操作成功
// @router /minutesSummary/PreserveAndPublish [post]
func (this *MinutesSummaryController) PreserveAndPublish() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	AdminUser := this.SysUser
	if AdminUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
		return
	}
	var req cygx.AddCygxMinutesSummary
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	var sortSdbgStr, msgErr string
	content := req.Content
	sortYanx := content.SortYanx
	if sortYanx != "" {
		match, _ := regexp.MatchString(`^[A-Za-z]+$`, sortYanx)
		if !match || len(sortYanx) > 1 {
			br.Msg = "研选排序有误"
			return
		}
		content.SortYanx = strings.ToUpper(content.SortYanx)
		if strings.Contains(sortSdbgStr, content.SortYanx) {
			br.Msg = "研选排序有误"
			return
		}
	}

	sortCydyjy := content.SortCydyjy
	if sortCydyjy != "" {
		match, _ := regexp.MatchString(`^[A-Za-z]+$`, sortCydyjy)
		if !match || len(sortCydyjy) > 1 {
			br.Msg = "产业调研纪要排序有误"
			return
		}
		content.SortCydyjy = strings.ToUpper(content.SortCydyjy)
		if strings.Contains(sortSdbgStr, content.SortCydyjy) {
			br.Msg = "产业调研纪要排序有误"
			return
		}
		sortSdbgStr += content.SortCydyjy
	}
	sortSsgs := content.SortSsgs
	if sortSsgs != "" {
		match, _ := regexp.MatchString(`^[A-Za-z]+$`, sortSsgs)
		if !match || len(sortSsgs) > 1 {
			br.Msg = "上市公司调研纪要篇排序有误"
			return
		}
		content.SortSsgs = strings.ToUpper(content.SortSsgs)
		if strings.Contains(sortSdbgStr, content.SortSsgs) {
			br.Msg = "上市公司调研纪要篇序有误"
			return
		}
	}
	var items []*cygx.CygxMinutesSummaryLog
	mapSort := make(map[string]int)
	if len(req.List) > 0 {
		for _, v := range req.List {
			for _, v2 := range v.List {
				item := new(cygx.CygxMinutesSummaryLog)
				chartPermissionSort := v.ChartPermissionSort
				match, _ := regexp.MatchString(`^[A-Za-z]+$`, chartPermissionSort)
				if !match || len(chartPermissionSort) > 1 {
					br.Msg = "排序有误"
					return
				}
				item.ChartPermissionSort = strings.ToUpper(chartPermissionSort)
				if mapSort[v.ChartPermissionSort] != 0 && mapSort[v.ChartPermissionSort] != v.ChartPermissionId {
					br.Msg = "排序有误,排序重复"
					return
				}
				mapSort[v.ChartPermissionSort] = v.ChartPermissionId
				item.ChartPermissionId = v.ChartPermissionId
				item.CreateTime = time.Now()
				item.Body = cygxService.GetRichtext(v2.Body)
				item.ReportLink = v2.ReportLink
				if item.ReportLink != "" {
					articleIdLink, err := cygxService.GetReportLinkToArticleid(item.ReportLink)
					if err != nil {
						br.Msg = "操作失败"
						br.ErrMsg = "解析报告链接ID失败，Err:" + err.Error()
						return
					}
					item.LinkArticleId = articleIdLink
				}
				items = append(items, item)
			}
		}
	}

	//产业调研纪要
	if len(content.ListCydyjy) > 0 {
		items, msgErr, err = cygxService.AppendMinutesItemsLog(items, content.ListCydyjy, "CYDYJY", strings.ToUpper(content.SortCydyjy))
		if msgErr != "" {
			br.Msg = msgErr
			return
		}
		if err != nil {
			br.Msg = "操作失败"
			br.ErrMsg = "获取失败，Err:" + err.Error()
			return
		}
	}
	//上市公司调研纪要篇
	if len(content.ListSsgs) > 0 {
		items, msgErr, err = cygxService.AppendMinutesItemsLog(items, content.ListSsgs, "SSGS", strings.ToUpper(content.SortSsgs))
		if msgErr != "" {
			br.Msg = msgErr
			return
		}
		if err != nil {
			br.Msg = "操作失败"
			br.ErrMsg = "获取失败，Err:" + err.Error()
			return
		}
	}
	//研选
	if len(content.ListYanx) > 0 {
		for _, v := range content.ListYanx {
			item := new(cygx.CygxMinutesSummaryLog)
			item.Type = "YANX"
			item.Sort = strings.ToUpper(content.SortYanx)
			item.CreateTime = time.Now()
			item.Body = cygxService.GetRichtext(v.Body)
			item.ReportLink = v.ReportLink
			if item.ReportLink != "" {
				articleIdLink, err := cygxService.GetReportLinkToArticleid(item.ReportLink)
				if err != nil {
					br.Msg = "操作失败"
					br.ErrMsg = "解析报告链接ID失败，Err:" + err.Error()
					return
				}
				item.LinkArticleId = articleIdLink
			}
			items = append(items, item)
		}
	}
	condition := ` AND have_publish = 1 `
	var pars []interface{}
	total, err := cygx.GetCygxMinutesSummary(condition, pars)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	itemCrs := new(cygx.CygxMinutesSummary)
	itemCrs.AdminId = AdminUser.AdminId
	itemCrs.AdminName = AdminUser.RealName
	itemCrs.Title = req.Title
	itemCrs.Department = req.Department
	itemCrs.Abstract = req.Abstract
	itemCrs.PublishStatus = req.DoType
	if req.DoType == 1 {
		itemCrs.HavePublish = 1
		itemCrs.Periods = strconv.Itoa(total + 1)
	}
	itemCrs.PublishDate = utils.StrTimeToTime(req.PublishDate + " 00:00:00")
	itemCrs.CreateTime = time.Now()
	itemCrs.LastUpdatedTime = time.Now()
	if req.ArticleId == 0 {
		articleId, err := cygx.AddCygxMinutesSummaryOrm(itemCrs, items)
		if err != nil {
			br.Msg = "操作失败"
			br.ErrMsg = "发布失败,Err:" + err.Error()
			return
		}
		req.ArticleId = int(articleId)
	} else {
		detail, err := cygx.GetCygxMinutesSummaryInfoById(req.ArticleId)
		if err != nil {
			br.Msg = "内容不存在"
			br.ErrMsg = "操作失败,Err:" + err.Error()
			return
		}
		//如果点发布PublishStatus = 1 如果点保存，PublishStatus则为之前的状态 发布人还是之前的发布人
		if itemCrs.PublishStatus == 0 {
			itemCrs.PublishStatus = detail.PublishStatus
			itemCrs.AdminId = detail.AdminId
			itemCrs.AdminName = detail.AdminName
		}
		if detail.HavePublish == 1 {
			itemCrs.Periods = detail.Periods
			itemCrs.HavePublish = detail.HavePublish
		}
		itemCrs.ArticleId = req.ArticleId
		_, err = cygx.UpdateCygxMinutesSummaryOrm(itemCrs, items)
		if err != nil {
			br.Msg = "操作失败"
			br.ErrMsg = "发布失败,Err:" + err.Error()
			return
		}
	}
	//if req.DoType == 1 {
	//	cygxService.DoThisWeekLastWeekWxTemplateMsg(itemCrs.Title, itemCrs.PublishDate.Format(utils.FormatDateTime), itemCrs.Abstract, req.ArticleId, 3)
	//}

	//生成音频文件
	articleId := req.ArticleId
	var contentVideo string
	contentVideo = "摘要" + req.Abstract
	listFirst, err := cygx.GetMinutesSummarylogListAll(articleId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取子类信息失败，Err:" + err.Error()
		return
	}
	for k, v := range listFirst {
		if v.Type == "CYDYJY" {
			contentVideo += "产业调研纪要"
		} else if v.Type == "SSGS" {
			contentVideo += "上市公司调研纪要篇"
		} else if v.Type == "YANX" {
			contentVideo += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "纪要"
		}
		listSecond, err := cygx.GetMinutesSummarylogSonListSecond(articleId, v.Type)
		if err != nil && err.Error() != utils.ErrNoRow() {
			br.Msg = "获取信息失败"
			br.ErrMsg = "获取信息失败,Err:" + err.Error()
			return
		}
		for k2, v2 := range listSecond {
			contentVideo += v2.PermissionName
			listThird, err := cygx.GetMinutesSummarylogListThird(articleId, v2.ChartPermissionId, v.Type)
			if err != nil && err.Error() != utils.ErrNoRow() {
				br.Msg = "获取信息失败"
				br.ErrMsg = "获取信息失败,Err:" + err.Error()
				return
			}
			if len(listThird) > 0 {
				listSecond[k2].List = listThird
				for _, v3 := range listThird {
					contentVideo += v3.Body
				}
			}
		}
		if len(listSecond) > 0 {
			listFirst[k].List = listSecond
		}
	}
	go services.CreateVideoWhithContent(articleId, req.Title, contentVideo, "cygx_minutes_summary")
	go cygxService.UpdateMinutesSummaryResourceData(articleId) //写入首页最新  cygx_resource_data 表
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	br.IsAddLog = true
}

// @Title 删除上周纪要汇总内容
// @Description 删除上周纪要汇总内容接口
// @Param   request	body cygx.MinutesSummaryId true "type json string"
// @Success Ret=200
// @router /minutesSummary/delete [post]
func (this *MinutesSummaryController) Delete() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	AdminUser := this.SysUser
	if AdminUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req cygx.MinutesSummaryId
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	articleId := req.ArticleId
	detail, err := cygx.GetCygxMinutesSummaryInfoById(articleId)
	if err != nil {
		br.Msg = "内容不存在"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	if detail.HavePublish != 0 {
		br.Msg = "删除失败"
		br.ErrMsg = "已经存在期数无法删除"
		return
	}

	err = cygx.DeleteCygxMinutesSummary(articleId)
	if err != nil {
		br.Msg = "删除信息失败"
		br.ErrMsg = "删除信息失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "已删除"
	br.IsAddLog = true
}

// @Title  上周纪要汇总内容详情
// @Description  上周纪要汇总内容详情接口
// @Param   ArticleId   query   int  true       "报告Id"
// @Success Ret=200 {object} cygx.DetailCygxMinutesSummaryRep
// @router /minutesSummary/detail [get]
func (this *MinutesSummaryController) Detail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	AdminUser := this.SysUser
	if AdminUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
		return
	}
	articleId, _ := this.GetInt("ArticleId")
	detail, err := cygx.GetCygxMinutesSummaryInfoById(articleId)
	if err != nil {
		br.Msg = "内容不存在"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	resp := new(cygx.DetailCygxMinutesSummaryRep)
	resp.ArticleId = detail.ArticleId
	resp.Title = detail.Title
	resp.Department = detail.Department
	resp.Abstract = detail.Abstract
	resp.PublishStatus = detail.PublishStatus
	resp.PublishDate = utils.StrTimeToTime(detail.PublishDate).Format("2006-01-02")
	var condition string
	condition = ` AND chart_permission_id IN (19,20,21,22) `
	listLess, err := cygx.GetChartPermissionIcoDetail(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	listCydyjy, err := cygx.GetCygxMinutesSummaryLogRepList(articleId, "CYDYJY")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listCydyjy {
		listSon, err := cygx.GetCygxMinutesSummaryLogRepListSon(articleId, v.ChartPermissionId, "CYDYJY")
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取数据失败，Err:" + err.Error()
			return
		}
		for k2, v2 := range listLess {
			if v2.ChartPermissionId == v.ChartPermissionId {
				listLess[k2].List = listSon
				listLess[k2].ChartPermissionSort = listSon[0].ChartPermissionSort
			}
		}
		resp.SortCydyjy = v.Sort
	}
	resp.ListCydyjy = listLess

	//产业调研纪要
	listLess, err = cygx.GetChartPermissionIcoDetail(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	listSsgs, err := cygx.GetCygxMinutesSummaryLogRepList(articleId, "SSGS")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listSsgs {
		listSon, err := cygx.GetCygxMinutesSummaryLogRepListSon(articleId, v.ChartPermissionId, "SSGS")
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取数据失败，Err:" + err.Error()
			return
		}
		for k2, v2 := range listLess {
			if v2.ChartPermissionId == v.ChartPermissionId {
				listLess[k2].List = listSon
				listLess[k2].ChartPermissionSort = listSon[0].ChartPermissionSort
			}
		}
		resp.SortSsgs = v.Sort
	}
	resp.ListSsgs = listLess

	//事件点评
	listYanx, err := cygx.GetCygxMinutesSummaryYanxList(articleId, "YANX")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	var itemsYanx []*cygx.AddCygxMinutesSummaryLog
	for _, v := range listYanx {
		item := new(cygx.AddCygxMinutesSummaryLog)
		item.Body = v.Body
		item.ReportLink = v.ReportLink
		itemsYanx = append(itemsYanx, item)
		resp.SortYanx = v.Sort
	}
	resp.ListYanx = itemsYanx
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 上周纪要汇总列表
// @Description 获取上周纪要汇总列表接口
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码，从1开始"
// @Param   PublishStatus   query   int  false       "发布状态 ，1未发布，1已发布，传2查询所有"
// @Param   StartDate   query   string  false       "开始时间 ，列如2021-03-06 "
// @Param   EndDate   query   string  false       "结束时间，列如2021-03-06 "
// @Success 200 {object} cygx.CygxMinutesSummaryListRep
// @router /minutesSummary/list [get]
func (this *MinutesSummaryController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	AdminUser := this.SysUser
	if AdminUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		return
	}
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	publishStatus, _ := this.GetInt("PublishStatus")
	startDate := this.GetString("StartDate")
	endDate := this.GetString("EndDate")
	var startSize int
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = utils.StartIndex(currentIndex, pageSize)
	var condition string
	var pars []interface{}
	if startDate != "" {
		condition += ` AND rs.publish_date >= ` + "'" + startDate + " 00:00:00'"
	}
	if endDate != "" {
		condition += ` AND rs.publish_date <= ` + "'" + endDate + " 23:59:59'"
	}
	if publishStatus == 0 || publishStatus == 1 {
		condition += ` AND rs.publish_status = ` + strconv.Itoa(publishStatus)
	}
	total, err := cygx.GetCygxMinutesSummary(condition, pars)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	list, err := cygx.GetMinutesSummaryList(condition, pars, startSize, pageSize)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	for k, v := range list {
		if v.Periods != "" && v.Periods != "0" {
			list[k].Periods = "第" + v.Periods + "期"
		} else {
			list[k].Periods = ""
		}
		list[k].PublishDate = utils.StrTimeToTime(v.PublishDate).Format("2006-01-02")
	}
	page := paging.GetPaging(currentIndex, pageSize, total)
	resp := new(cygx.CygxMinutesSummaryListRep)
	resp.List = list
	resp.Paging = page
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title   上周纪要汇总的发布与取消发布
// @Description   上周纪要汇总的发布与取消发布接口
// @Param	request	body cygx.ResearchSummaryId true "type json string"
// @Success 200 操作成功
// @router /minutesSummary/PublishAndCancel [post]
func (this *MinutesSummaryController) PublishAndCancel() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	AdminUser := this.SysUser
	if AdminUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
		return
	}
	var req cygx.ResearchSummaryId
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	articleId := req.ArticleId
	detail, err := cygx.GetCygxMinutesSummaryInfoById(articleId)
	if err != nil {
		br.Msg = "内容不存在"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	item := new(cygx.CygxMinutesSummaryRep)
	item.ArticleId = articleId
	if detail.PublishStatus == 0 {
		item.PublishStatus = 1
		item.AdminId = AdminUser.AdminId
		item.AdminName = AdminUser.RealName
		//cygxService.DoThisWeekLastWeekWxTemplateMsg(detail.Title, detail.PublishDate, detail.Abstract, req.ArticleId, 3)
	} else {
		item.AdminId = detail.AdminId
		item.AdminName = detail.AdminName
		item.PublishStatus = 0
	}
	if detail.HavePublish == 0 {
		condition := ` AND have_publish = 1 `
		var pars []interface{}
		total, err := cygx.GetCygxMinutesSummary(condition, pars)
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取失败，Err:" + err.Error()
			return
		}
		item.Periods = strconv.Itoa(total + 1)
	} else {
		item.Periods = detail.Periods
	}
	err = cygx.MinutesSummaryPublishAndCancel(item)
	if err != nil {
		br.Msg = "操作失败"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	br.IsAddLog = true
}

// 批量修改已生成的音频文件
func init123() {
	var pars []interface{}
	list, err := cygx.GetMinutesSummaryList("", pars, 0, 1000)
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, v := range list {

		//生成音频文件
		articleId := v.ArticleId
		var contentVideo string
		contentVideo = "摘要" + v.Abstract
		listFirst, err := cygx.GetMinutesSummarylogListAll(articleId)
		if err != nil {
			fmt.Println(err)
			return
		}
		for k, v := range listFirst {
			if v.Type == "CYDYJY" {
				contentVideo += "产业调研纪要"
			} else if v.Type == "SSGS" {
				contentVideo += "上市公司调研纪要篇"
			} else if v.Type == "YANX" {
				contentVideo += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "纪要"
			}
			listSecond, err := cygx.GetMinutesSummarylogSonListSecond(articleId, v.Type)
			if err != nil && err.Error() != utils.ErrNoRow() {
				fmt.Println(err)
				return
			}
			for k2, v2 := range listSecond {
				contentVideo += v2.PermissionName
				listThird, err := cygx.GetMinutesSummarylogListThird(articleId, v2.ChartPermissionId, v.Type)
				if err != nil && err.Error() != utils.ErrNoRow() {
					fmt.Println(err)
					return
				}
				if len(listThird) > 0 {
					listSecond[k2].List = listThird
					for _, v3 := range listThird {
						contentVideo += v3.Body
					}
				}
			}
			if len(listSecond) > 0 {
				listFirst[k].List = listSecond
			}
		}
		go services.CreateVideoWhithContent(articleId, v.Title, contentVideo, "cygx_minutes_summary")
	}

}

// @Title  上周研究汇总邮件发送
// @Description  上周研究汇总邮件发送接口
// @Param   ArticleId   query   int  true       "报告Id"
// @router /minutesSummary/sendMail [get]
func (this *MinutesSummaryController) MinutesSummarySendMail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	AdminUser := this.SysUser
	if AdminUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
		return
	}

	articleId, _ := this.GetInt("ArticleId")
	detail, err := cygx.GetCygxMinutesSummaryInfoById(articleId)
	if err != nil {
		br.Msg = "内容不存在"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	resp := new(cygx.DetailCygxMinutesSummaryRep)
	resp.ArticleId = detail.ArticleId
	resp.Title = detail.Title
	resp.Department = detail.Department
	resp.Abstract = detail.Abstract
	resp.PublishStatus = detail.PublishStatus
	resp.PublishDate = utils.StrTimeToTime(detail.PublishDate).Format("2006-01-02")
	var condition string
	condition = ` AND chart_permission_id IN (19,20,21,22) `
	listLess, err := cygx.GetChartPermissionIcoDetail(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	listCydyjy, err := cygx.GetCygxMinutesSummaryLogRepList(articleId, "CYDYJY")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listCydyjy {
		listSon, err := cygx.GetCygxMinutesSummaryLogRepListSon(articleId, v.ChartPermissionId, "CYDYJY")
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取数据失败，Err:" + err.Error()
			return
		}
		for k2, v2 := range listLess {
			if v2.ChartPermissionId == v.ChartPermissionId {
				listLess[k2].List = listSon
				listLess[k2].ChartPermissionSort = listSon[0].ChartPermissionSort
			}
		}
		resp.SortCydyjy = v.Sort
	}
	resp.ListCydyjy = listLess

	//产业调研纪要
	listLess, err = cygx.GetChartPermissionIcoDetail(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	listSsgs, err := cygx.GetCygxMinutesSummaryLogRepList(articleId, "SSGS")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listSsgs {
		listSon, err := cygx.GetCygxMinutesSummaryLogRepListSon(articleId, v.ChartPermissionId, "SSGS")
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取数据失败，Err:" + err.Error()
			return
		}
		for k2, v2 := range listLess {
			if v2.ChartPermissionId == v.ChartPermissionId {
				listLess[k2].List = listSon
				listLess[k2].ChartPermissionSort = listSon[0].ChartPermissionSort
			}
		}
		resp.SortSsgs = v.Sort
	}
	resp.ListSsgs = listLess

	//事件点评
	listYanx, err := cygx.GetCygxMinutesSummaryYanxList(articleId, "YANX")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	var itemsYanx []*cygx.AddCygxMinutesSummaryLog
	for _, v := range listYanx {
		item := new(cygx.AddCygxMinutesSummaryLog)
		item.Body = v.Body
		item.ReportLink = v.ReportLink
		itemsYanx = append(itemsYanx, item)
		resp.SortYanx = v.Sort
	}
	resp.ListYanx = itemsYanx
	var list []string
	//添加测试环境的过滤
	if utils.RunMode == "release" {
		list, err = cygx.GetAllOverseasRaiEmail()
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取邮箱列表失败，Err:" + err.Error()
			return
		}
	}
	go cygxService.SendMinutesSummaryEmail(resp, list)
	br.Success = true
	br.Msg = "发送成功"
	br.Ret = 200
}

// @Title   上周纪要汇总的可见范围修改
// @Description   上周纪要汇总的可见范围修改接口
// @Param	request	body cygx.ResearchSummaryId true "type json string"
// @Success 200 操作成功
// @router /minutesSummary/visibleRange [post]
func (this *MinutesSummaryController) VisibleRange() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	AdminUser := this.SysUser
	if AdminUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
		return
	}
	var req cygx.ResearchSummaryId
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	articleId := req.ArticleId
	detail, err := cygx.GetCygxMinutesSummaryInfoById(articleId)
	if err != nil {
		br.Msg = "内容不存在"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	item := new(cygx.CygxMinutesSummaryRep)
	item.ArticleId = articleId
	if detail.VisibleRange == 0 {
		item.VisibleRange = 1
		item.AdminId = AdminUser.AdminId
		item.AdminName = AdminUser.RealName
		if detail.IsSendWxMsg == 0 {
			cygxService.DoThisWeekLastWeekWxTemplateMsg(detail.Title, detail.PublishDate, detail.Abstract, req.ArticleId, 3)
		}
		//go cygxService.UpdateResourceData(articleId, "minutessummary", "add", time.Now().Format(utils.FormatDateTime))
	} else {
		item.AdminId = detail.AdminId
		item.AdminName = detail.AdminName
		item.VisibleRange = 0
		//go cygxService.UpdateResourceData(articleId, "minutessummary", "delete", time.Now().Format(utils.FormatDateTime))
	}
	err = cygx.MinutesSummaryVisibleRange(item)
	if err != nil {
		br.Msg = "操作失败"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	go cygxService.UpdateMinutesSummaryResourceData(articleId) //写入首页最新  cygx_resource_data 表
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	br.IsAddLog = true
}
