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 ResearchSummaryController struct {
	controllers.BaseAuthController
}

// @Title 本周研究汇总内容的保存与发布
// @Description 本周研究汇总内容的保存与发布接口
// @Param	request	body cygx.AddCygxResearchSummary true "type json string"
// @Success 200 操作成功
// @router /researchSummary/PreserveAndPublish [post]
func (this *ResearchSummaryController) 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.AddCygxResearchSummary
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	//`description:"类型'SDBG：深度报告片篇,’CYDYJY：产业调研纪要’,’SJDP：事件点评,’BZCHJH：本周晨会精华’,’SSGS：上市公司调研纪要篇’"`
	content := req.Content
	sortSdbg := content.SortSdbg
	var sortSdbgStr string
	var msgErr string
	if sortSdbg != "" {
		match, _ := regexp.MatchString(`^[A-Za-z]+$`, sortSdbg)
		if !match || len(sortSdbg) > 1 {
			br.Msg = "深度报告篇排序有误"
			return
		}
		content.SortSdbg = strings.ToUpper(content.SortSdbg)
		sortSdbgStr += content.SortSdbg
	}

	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
	}
	sortSjdp := content.SortSjdp
	if sortSjdp != "" {
		match, _ := regexp.MatchString(`^[A-Za-z]+$`, sortSjdp)
		if !match || len(sortSjdp) > 1 {
			br.Msg = "事件点评排序有误"
			return
		}
		content.SortSjdp = strings.ToUpper(content.SortSjdp)
		if strings.Contains(sortSdbgStr, content.SortSjdp) {
			br.Msg = "事件点评排序有误"
			return
		}
		sortSdbgStr += content.SortSjdp
	}
	sortBzchjh := content.SortBzchjh
	if sortBzchjh != "" {
		match, _ := regexp.MatchString(`^[A-Za-z]+$`, sortBzchjh)
		if !match || len(sortBzchjh) > 1 {
			br.Msg = "本周晨会精华排序有误"
			return
		}
		content.SortBzchjh = strings.ToUpper(content.SortBzchjh)
		if strings.Contains(sortSdbgStr, content.SortBzchjh) {
			br.Msg = "本周晨会精华排序有误"
			return
		}
	}

	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
		}
	}
	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.CygxResearchSummaryLog

	//合并深度报告片篇
	if len(content.ListSdbg) > 0 {
		items, msgErr, err = cygxService.AppendItemsLog(items, content.ListSdbg, "SDBG", strings.ToUpper(content.SortSdbg))
		if msgErr != "" {
			br.Msg = msgErr
			return
		}
		if err != nil {
			br.Msg = "操作失败"
			br.ErrMsg = "获取失败，Err:" + err.Error()
			return
		}
	}

	//产业调研纪要
	if len(content.ListCydyjy) > 0 {
		items, msgErr, err = cygxService.AppendItemsLog(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.AppendItemsLog(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.ListSjdp) > 0 {
		for _, v := range content.ListSjdp {
			item := new(cygx.CygxResearchSummaryLog)
			item.Type = "SJDP"
			item.Sort = strings.ToUpper(content.SortSjdp)
			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)
		}
	}
	//本周晨会精华
	if len(content.ListBzchjh) > 0 {
		items, msgErr, err = cygxService.AppendItemsLog(items, content.ListBzchjh, "BZCHJH", strings.ToUpper(content.SortBzchjh))
		if msgErr != "" {
			br.Msg = msgErr
			return
		}
		if err != nil {
			br.Msg = "操作失败"
			br.ErrMsg = "获取失败，Err:" + err.Error()
			return
		}
	}

	//研选
	if len(content.ListYanx) > 0 {
		items, msgErr, err = cygxService.AppendItemsLog(items, content.ListYanx, "YANX", strings.ToUpper(content.SortYanx))
		if msgErr != "" {
			br.Msg = msgErr
			return
		}
		if err != nil {
			br.Msg = "操作失败"
			br.ErrMsg = "获取失败，Err:" + err.Error()
			return
		}
	}
	condition := ` AND have_publish = 1 `
	var pars []interface{}
	total, err := cygx.GetCygxResearchSummary(condition, pars)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	itemCrs := new(cygx.CygxResearchSummary)
	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.AddCygxResearchSummaryOrm(itemCrs, items)
		if err != nil {
			br.Msg = "操作失败"
			br.ErrMsg = "发布失败,Err:" + err.Error()
			return
		}
		req.ArticleId = int(articleId)
	} else {
		detail, err := cygx.GetCygxResearchSummaryInfoById(req.ArticleId)
		if err != nil {
			br.Msg = "内容不存在"
			br.ErrMsg = "操作失败,Err:" + err.Error()
			return
		}
		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.UpdateCygxResearchSummaryOrm(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, 2)
	//}
	//生成音频文件
	articleId := req.ArticleId
	var contentVideo string
	contentVideo = "摘要" + req.Abstract
	listFirst, err := cygx.GetResearchSummarylogListFirst(articleId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取子类信息失败，Err:" + err.Error()
		return
	}
	listArticleType, err := cygx.GetCygxArticleTypeList()
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取文章类型数据失败，Err:" + err.Error()
		return
	}
	for k, v := range listFirst {
		if v.Type == "SDBG" {
			contentVideo += "深度报告篇"
		} else if v.Type == "BZCHJH" {
			contentVideo += "本周晨会精华"
		} else if v.Type == "CYDYJY" {
			contentVideo += "产业调研纪要篇"
		} else if v.Type == "SSGS" {
			contentVideo += "上市公司调研纪要篇"
		} else if v.Type == "YANX" {
			contentVideo += utils.CHART_PERMISSION_NAME_MF_YANXUAN
		} else {
			contentVideo += "事件点评"
		}
		listSecond, err := cygx.GetResearchSummarylogSonListSecond(articleId, v.Type)
		if err != nil && err.Error() != utils.ErrNoRow() {
			br.Msg = "获取信息失败"
			br.ErrMsg = "获取信息失败,Err:" + err.Error()
			return
		}
		for k2, v2 := range listSecond {
			if v.Type == "YANX" {
				for _, vType := range listArticleType {
					if v2.ChartPermissionId == vType.YanxPermissionId {
						contentVideo += vType.YanxPermissionName
					}
				}
			} else {
				contentVideo += v2.PermissionName
			}

			//if v2.ChartPermissionId == utils.YanxSummaryPermissionId {
			//	contentVideo += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "纪要"
			//} else if v2.ChartPermissionId == utils.YanxViewpointPermissionId {
			//	contentVideo += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "观点"
			//} else {
			//	contentVideo += v2.PermissionName
			//}
			listThird, err := cygx.GetResearchSummarylogSonListThird(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_research_summary")
	go cygxService.UpdateResearchSummaryResourceData(articleId) //写入首页最新  cygx_resource_data 表
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	br.IsAddLog = true
}

// @Title 删除本周研究汇总内容
// @Description 删除本周研究汇总内容接口
// @Param   request	body cygx.ResearchSummaryId true "type json string"
// @Success Ret=200
// @router /researchSummary/delete [post]
func (this *ResearchSummaryController) 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.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.GetCygxResearchSummaryInfoById(articleId)
	if err != nil {
		br.Msg = "内容不存在"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	if detail.HavePublish != 0 {
		br.Msg = "删除失败"
		br.ErrMsg = "已经存在期数无法删除"
		return
	}
	err = cygx.DeleteCygxResearchSummary(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.DetailCygxResearchSummaryRep
// @router /researchSummary/detail [get]
func (this *ResearchSummaryController) 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.GetCygxResearchSummaryInfoById(articleId)
	if err != nil {
		br.Msg = "内容不存在"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	resp := new(cygx.DetailCygxResearchSummaryRep)
	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 items []*cygx.CygxRSlChartPermissionIco
	//`description:"类型'SDBG：深度报告片篇,’CYDYJY：产业调研纪要’,’SJDP：事件点评,’BZCHJH：本周晨会精华’,’SSGS：本周晨会精华’"`
	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
	}
	condition = ` AND chart_permission_id IN (19,20,21,22,23) `
	listMore, err := cygx.GetChartPermissionIcoDetail(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	listSdbg, err := cygx.GetCygxResearchSummaryLogRepList(articleId, "SDBG")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listSdbg {
		listSon, err := cygx.GetCygxResearchSummaryLogRepListSon(articleId, v.ChartPermissionId, "SDBG")
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取数据失败，Err:" + err.Error()
			return
		}
		for k2, v2 := range listMore {
			if v2.ChartPermissionId == v.ChartPermissionId {
				listMore[k2].List = listSon
				listMore[k2].ChartPermissionSort = listSon[0].ChartPermissionSort
			}
		}
		resp.SortSdbg = v.Sort
	}
	resp.ListSdbg = listMore

	//产业调研纪要
	//items = make([]*cygx.CygxRSlChartPermissionIco, 0)
	listCydyjy, err := cygx.GetCygxResearchSummaryLogRepList(articleId, "CYDYJY")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listCydyjy {
		listSon, err := cygx.GetCygxResearchSummaryLogRepListSon(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

	//上市公司
	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
	}
	listSsgs, err := cygx.GetCygxResearchSummaryLogRepList(articleId, "SSGS")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listSsgs {
		listSon, err := cygx.GetCygxResearchSummaryLogRepListSon(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

	//研选

	var listYanxItem []*cygx.CygxRSlChartPermissionIco

	listType, err := cygx.GetCygxArticleTypeList()
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listType {
		item := new(cygx.CygxRSlChartPermissionIco)
		item.ChartPermissionId = v.YanxPermissionId
		item.ChartPermissionName = v.YanxPermissionName
		item.IcoLink = v.IcoLink
		item.List = make([]*cygx.AddCygxResearchSummaryLog, 0)
		listYanxItem = append(listYanxItem, item)
	}
	itemList := listYanxItem
	condition = ` AND chart_permission_id IN (19,20,21,22,31) `
	listLess, err = cygx.GetChartPermissionIcoDetail(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	listYanx, err := cygx.GetCygxResearchSummaryLogRepList(articleId, "YANX")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listYanx {
		listSon, err := cygx.GetCygxResearchSummaryLogRepListSon(articleId, v.ChartPermissionId, "YANX")
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取数据失败，Err:" + err.Error()
			return
		}
		for k2, v2 := range itemList {
			if v2.ChartPermissionId == v.ChartPermissionId {
				itemList[k2].List = listSon
				itemList[k2].ChartPermissionSort = listSon[0].ChartPermissionSort
			}
		}
		resp.SortYanx = v.Sort
	}
	resp.ListYanx = itemList

	//事件点评
	listSjdp, err := cygx.GetCygxResearchSummarySjdpList(articleId, "SJDP")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	var itemsSjdp []*cygx.AddCygxResearchSummaryLog
	for _, v := range listSjdp {
		item := new(cygx.AddCygxResearchSummaryLog)
		item.Body = v.Body
		item.ReportLink = v.ReportLink
		itemsSjdp = append(itemsSjdp, item)
		resp.SortSjdp = v.Sort
	}
	resp.ListSjdp = itemsSjdp

	//items = make([]*cygx.CygxRSlChartPermissionIco, 0)
	condition = ` AND chart_permission_id IN (19,20,21,22,23) `
	listMore, err = cygx.GetChartPermissionIcoDetail(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	listBzchjh, err := cygx.GetCygxResearchSummaryLogRepList(articleId, "BZCHJH")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listBzchjh {
		listSon, err := cygx.GetCygxResearchSummaryLogRepListSon(articleId, v.ChartPermissionId, "BZCHJH")
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取数据失败，Err:" + err.Error()
			return
		}
		for k2, v2 := range listMore {
			if v2.ChartPermissionId == v.ChartPermissionId {
				listMore[k2].List = listSon
				listMore[k2].ChartPermissionSort = listSon[0].ChartPermissionSort
			}
		}
		resp.SortBzchjh = v.Sort
	}
	resp.ListBzchjh = listMore
	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.CygxResearchSummaryListRep
// @router /researchSummary/list [get]
func (this *ResearchSummaryController) 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.GetCygxResearchSummary(condition, pars)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	list, err := cygx.GetResearchSummaryList(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.CygxResearchSummaryListRep)
	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 /researchSummary/PublishAndCancel [post]
func (this *ResearchSummaryController) 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.GetCygxResearchSummaryInfoById(articleId)
	if err != nil {
		br.Msg = "内容不存在"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	item := new(cygx.CygxResearchSummaryRep)
	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, 2)
	} 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.GetCygxResearchSummary(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.ResearchSummaryPublishAndCancel(item)
	if err != nil {
		br.Msg = "操作失败"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	br.IsAddLog = true
}

// @Title  本周研究汇总初始模板
// @Description  本周研究汇总初始模板接口
// @Success Ret=200 {object} cygx.DetailCygxResearchSummaryRep
// @router /researchSummary/detailTemplate [get]
func (this *ResearchSummaryController) DetailTemplate() {
	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
	}
	resp := new(cygx.DetailCygxResearchSummaryTmpRep)
	//`description:"类型'SDBG：深度报告片篇,’CYDYJY：产业调研纪要’,’SJDP：事件点评,’BZCHJH：本周晨会精华’，'YANX:研选'"`
	var condition string
	condition = ` AND chart_permission_id IN (19,20,21,22,23) `
	listMore, err := cygx.GetChartPermissionIcoNew(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	condition = ` AND chart_permission_id IN (19,20,21,22) `
	listLess, err := cygx.GetChartPermissionIcoNew(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	for k := range listMore {
		listMore[k].List = make([]string, 0)
	}
	for k := range listLess {
		listLess[k].List = make([]string, 0)
	}

	listType, err := cygx.GetCygxArticleTypeList()
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	var listYanx []*cygx.CygxRSlChartPermissionIcoTmp
	for _, v := range listType {
		item := new(cygx.CygxRSlChartPermissionIcoTmp)
		item.ChartPermissionId = v.YanxPermissionId
		item.ChartPermissionName = v.YanxPermissionName
		item.IcoLink = v.IcoLink
		item.List = make([]string, 0)
		listYanx = append(listYanx, item)
	}
	//listYanx := []*cygx.CygxRSlChartPermissionIcoTmp{{ChartPermissionId: utils.YanxSummaryPermissionId, ChartPermissionName: "研选纪要", ChartPermissionSort: "", IcoLink: "https://hongze.oss-cn-shanghai.aliyuncs.com/static/images/202111/20211104/QbTGTNhD9MxYp24cJ7V5WpCN0oNl.png", List: make([]string, 0)}, {ChartPermissionId: utils.YanxViewpointPermissionId, ChartPermissionName: "研选观点", ChartPermissionSort: "", IcoLink: "https://hongze.oss-cn-shanghai.aliyuncs.com/static/images/202111/20211104/QbTGTNhD9MxYp24cJ7V5WpCN0oNl.png", List: make([]string, 0)}}
	resp.ListBzchjh = listMore
	resp.ListSdbg = listMore
	resp.ListCydyjy = listLess
	resp.ListSsgs = listLess
	resp.ListYanx = listYanx
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// 批量修改已生成的音频文件
func init111() {
	var pars []interface{}
	list, err := cygx.GetResearchSummaryList("", pars, 0, 1000)
	if err != nil {
		fmt.Println(err)
		return
	}
	for k, v := range list {
		fmt.Println(k)
		//生成音频文件
		articleId := v.ArticleId
		var contentVideo string
		contentVideo = "摘要" + v.Abstract
		listFirst, err := cygx.GetResearchSummarylogListFirst(articleId)
		if err != nil {
			fmt.Println(err)
			return
		}
		listArticleType, err := cygx.GetCygxArticleTypeList()
		if err != nil {
			fmt.Println(err)
			return
		}
		for k, v := range listFirst {
			if v.Type == "SDBG" {
				contentVideo += "深度报告"
			} else if v.Type == "BZCHJH" {
				contentVideo += "本周晨会精华"
			} else if v.Type == "CYDYJY" {
				contentVideo += "产业调研纪要"
			} else if v.Type == "SSGS" {
				contentVideo += "上市公司调研纪要"
			} else if v.Type == "YANX" {
				contentVideo += utils.CHART_PERMISSION_NAME_MF_YANXUAN
			} else {
				contentVideo += "事件点评"
			}
			listSecond, err := cygx.GetResearchSummarylogSonListSecond(articleId, v.Type)
			if err != nil && err.Error() != utils.ErrNoRow() {
				fmt.Println(err)
				return
			}
			for k2, v2 := range listSecond {
				if v.Type == "YANX" {
					for _, vType := range listArticleType {
						if v2.ChartPermissionId == vType.YanxPermissionId {
							contentVideo += vType.YanxPermissionName
						}
					}
				} else {
					contentVideo += v2.PermissionName
				}

				//if v2.ChartPermissionId == utils.YanxSummaryPermissionId {
				//	contentVideo += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "纪要"
				//} else if v2.ChartPermissionId == utils.YanxViewpointPermissionId {
				//	contentVideo += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "观点"
				//} else {
				//	contentVideo += v2.PermissionName
				//}
				listThird, err := cygx.GetResearchSummarylogSonListThird(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_research_summary")
	}
}

// @Title  本周研究汇总邮件发送
// @Description  本周研究汇总邮件发送接口
// @Param   ArticleId   query   int  true       "报告Id"
// @router /researchSummary/sendMail [get]
func (this *ResearchSummaryController) ResearchSummarySendMail() {
	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.GetCygxResearchSummaryInfoById(articleId)
	if err != nil {
		br.Msg = "内容不存在"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	resp := new(cygx.DetailCygxResearchSummaryRep)
	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 items []*cygx.CygxRSlChartPermissionIco
	//`description:"类型'SDBG：深度报告片篇,’CYDYJY：产业调研纪要’,’SJDP：事件点评,’BZCHJH：本周晨会精华’,’SSGS：本周晨会精华’"`
	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
	}
	condition = ` AND chart_permission_id IN (19,20,21,22,23) `
	listMore, err := cygx.GetChartPermissionIcoDetail(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	listSdbg, err := cygx.GetCygxResearchSummaryLogRepList(articleId, "SDBG")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listSdbg {
		listSon, err := cygx.GetCygxResearchSummaryLogRepListSon(articleId, v.ChartPermissionId, "SDBG")
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取数据失败，Err:" + err.Error()
			return
		}
		for k2, v2 := range listMore {
			if v2.ChartPermissionId == v.ChartPermissionId {
				listMore[k2].List = listSon
				listMore[k2].ChartPermissionSort = listSon[0].ChartPermissionSort
			}
		}
		resp.SortSdbg = v.Sort
	}
	resp.ListSdbg = listMore

	//产业调研纪要
	//items = make([]*cygx.CygxRSlChartPermissionIco, 0)
	listCydyjy, err := cygx.GetCygxResearchSummaryLogRepList(articleId, "CYDYJY")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listCydyjy {
		listSon, err := cygx.GetCygxResearchSummaryLogRepListSon(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

	//上市公司
	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
	}
	listSsgs, err := cygx.GetCygxResearchSummaryLogRepList(articleId, "SSGS")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listSsgs {
		listSon, err := cygx.GetCygxResearchSummaryLogRepListSon(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

	//研选

	var listYanxItem []*cygx.CygxRSlChartPermissionIco

	listType, err := cygx.GetCygxArticleTypeList()
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listType {
		item := new(cygx.CygxRSlChartPermissionIco)
		item.ChartPermissionId = v.YanxPermissionId
		item.ChartPermissionName = v.YanxPermissionName
		item.IcoLink = v.IcoLink
		item.List = make([]*cygx.AddCygxResearchSummaryLog, 0)
		listYanxItem = append(listYanxItem, item)
	}
	itemList := listYanxItem
	condition = ` AND chart_permission_id IN (19,20,21,22,31) `
	listLess, err = cygx.GetChartPermissionIcoDetail(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	listYanx, err := cygx.GetCygxResearchSummaryLogRepList(articleId, "YANX")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listYanx {
		listSon, err := cygx.GetCygxResearchSummaryLogRepListSon(articleId, v.ChartPermissionId, "YANX")
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取数据失败，Err:" + err.Error()
			return
		}
		for k2, v2 := range itemList {
			if v2.ChartPermissionId == v.ChartPermissionId {
				itemList[k2].List = listSon
				itemList[k2].ChartPermissionSort = listSon[0].ChartPermissionSort
			}
		}
		resp.SortYanx = v.Sort
	}
	resp.ListYanx = itemList

	//事件点评
	listSjdp, err := cygx.GetCygxResearchSummarySjdpList(articleId, "SJDP")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	var itemsSjdp []*cygx.AddCygxResearchSummaryLog
	for _, v := range listSjdp {
		item := new(cygx.AddCygxResearchSummaryLog)
		item.Body = v.Body
		item.ReportLink = v.ReportLink
		itemsSjdp = append(itemsSjdp, item)
		resp.SortSjdp = v.Sort
	}
	resp.ListSjdp = itemsSjdp

	//items = make([]*cygx.CygxRSlChartPermissionIco, 0)
	condition = ` AND chart_permission_id IN (19,20,21,22,23) `
	listMore, err = cygx.GetChartPermissionIcoDetail(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	listBzchjh, err := cygx.GetCygxResearchSummaryLogRepList(articleId, "BZCHJH")
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range listBzchjh {
		listSon, err := cygx.GetCygxResearchSummaryLogRepListSon(articleId, v.ChartPermissionId, "BZCHJH")
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取数据失败，Err:" + err.Error()
			return
		}
		for k2, v2 := range listMore {
			if v2.ChartPermissionId == v.ChartPermissionId {
				listMore[k2].List = listSon
				listMore[k2].ChartPermissionSort = listSon[0].ChartPermissionSort
			}
		}
		resp.SortBzchjh = v.Sort
	}
	resp.ListBzchjh = listMore
	var list []string
	//添加测试环境的过滤
	if utils.RunMode == "release" {
		list, err = cygx.GetAllOverseasRaiEmail()
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取邮箱列表失败，Err:" + err.Error()
			return
		}
	}

	go cygxService.SendResearchSummaryEmail(resp, list)
	br.Success = true
	br.Msg = "发送成功"
	br.Ret = 200
	//br.Data = resp
}

// @Title   本周研究汇总的可见范围修改
// @Description   本周研究汇总的可见范围修改接口
// @Param	request	body cygx.ResearchSummaryId true "type json string"
// @Success 200 操作成功
// @router /researchSummary/visibleRange [post]
func (this *ResearchSummaryController) 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.GetCygxResearchSummaryInfoById(articleId)
	if err != nil {
		br.Msg = "内容不存在"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	item := new(cygx.CygxResearchSummaryRep)
	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, 2)
		}
		//go cygxService.UpdateResourceData(articleId, "researchsummary", "add", time.Now().Format(utils.FormatDateTime))
	} else {
		item.AdminId = detail.AdminId
		item.AdminName = detail.AdminName
		item.VisibleRange = 0
		//go cygxService.UpdateResourceData(articleId, "researchsummary", "delete", time.Now().Format(utils.FormatDateTime))
	}
	err = cygx.ResearchSummaryVisibleRange(item)
	if err != nil {
		br.Msg = "操作失败"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	go cygxService.UpdateResearchSummaryResourceData(articleId) //写入首页最新  cygx_resource_data 表
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	br.IsAddLog = true
}
