package services

import (
	"context"
	"errors"
	"fmt"
	"github.com/beego/beego/v2/task"
	"hongze/hongze_task/models"
	"hongze/hongze_task/services/company"
	"hongze/hongze_task/services/company_contract"
	"hongze/hongze_task/services/data"
	"hongze/hongze_task/services/roadshow"
	"hongze/hongze_task/utils"
	"strconv"
	"strings"
	"sync"
	"time"
)

func Task() {
	fmt.Println("task start")
	//如果是生产环境,才需要走这些任务
	if utils.RunMode == "release" {
		releaseTask()
	}
	//每日定时合同处理
	handleCompanyContract := task.NewTask("handleCompanyContract", "0 1 2 * * *", company_contract.HandleCompanyContract)
	task.AddTask("每日定时合同处理", handleCompanyContract)

	//正式->试用
	companyTryOut := task.NewTask("companyTryOut", "0 5 2 * * *", CompanyTryOut)
	task.AddTask("正式->试用", companyTryOut)

	//试用->冻结
	companyFreeze := task.NewTask("companyFreeze", "0 10 2 * * *", CompanyFreeze)
	task.AddTask("试用->冻结", companyFreeze)

	//冻结->流失
	companyLoss := task.NewTask("companyLoss", "0 20 2 * * *", CompanyLoss)
	task.AddTask("冻结->流失", companyLoss)

	//用户产品权限正式-->试用
	companyReportPermissionTryOut := task.NewTask("companyReportPermissionTryOut", "0 30 2 * * *", CompanyReportPermissionTryOut)
	task.AddTask("用户产品权限正式-->试用", companyReportPermissionTryOut)

	//用户产品权限试用-->关闭
	companyReportPermissionClose := task.NewTask("companyReportPermissionClose", "0 35 2 * * *", CompanyReportPermissionClose)
	task.AddTask("用户产品权限试用-->关闭", companyReportPermissionClose)

	// 存量客户数据统计
	stackCompanyStatistic := task.NewTask("stackCompanyStatistic", "0 35 2 * * *", StackCompanyStatistic)
	task.AddTask("存量客户数据统计", stackCompanyStatistic)

	// 定时往同花顺推送报告
	sendWaitReport := task.NewTask("sendWaitReport", "0 */1 * * * * ", SendWaitReport)
	task.AddTask("定时往同花顺推送报告", sendWaitReport)

	// 研报电话会提醒
	ybTelRemind := task.NewTask("YbTelRemind", "0 */1 * * * * ", YbTelRemind)
	task.AddTask("研报电话会提醒", ybTelRemind)

	// 研报沙龙提醒
	ybSalonRemind := task.NewTask("ybSalonRemind", "0 */1 * * * * ", YbSalonRemind)
	task.AddTask("研报沙龙提醒", ybSalonRemind)

	// 定时新增手工指标数据提醒
	addEdbTask := task.NewTask("sendWaitReport", "1 0 2 * * * ", AddEdbTask)
	task.AddTask("定时新增手工指标数据提醒", addEdbTask)
	//每次服务启动都需要执行一次的
	_ = AddEdbTask(nil)

	//每日用户阅读数据统计
	statisticsUserView := task.NewTask("statisticsUserView", "0 5 2 * * *", StatisticsUserView)
	task.AddTask("每日用户阅读数据统计", statisticsUserView)

	//路演-活动状态修改
	modifyRsCalendarStatus := task.NewTask("modifyRsCalendarStatus", "0 */1 * * * * ", roadshow.ModifyRsCalendarResearcherStatus)
	task.AddTask("modifyRsCalendarStatus", modifyRsCalendarStatus)

	addReportRecord := task.NewTask("addReportRecord", "0 0 */1 * * *", roadshow.AddReportRecord)
	task.AddTask("addReportRecord", addReportRecord)

	//ficc存量客户数据统计
	addCompanyReportRecord := task.NewTask("addCompanyReportRecord", "0 0 */1 * * *", company.AddCompanyReportRecord)
	task.AddTask("addCompanyReportRecord", addCompanyReportRecord)

	//rai存量客户数据统计
	addRaiCompanyReportRecord := task.NewTask("addRaiCompanyReportRecord", "0 0 */1 * * *", company.AddRaiCompanyReportRecord)
	task.AddTask("addRaiCompanyReportRecord", addRaiCompanyReportRecord)

	//CRM 6.3 客户列表路演次数统计
	roadShowTotal := task.NewTask("roadShowTotal", "0 */30 * * * *", roadshow.RoadShow)
	task.AddTask("roadShowTotal", roadShowTotal)

	//每半个小时用户阅读数据统计
	statisticsUserView1Hour := task.NewTask("statisticsUserView1Hour", "0 */30 * * * *", StatisticsUserView1Hour)
	task.AddTask("每半个小时用户阅读数据统计", statisticsUserView1Hour)

	//路演公开会议合并
	setPublicMeetingUnionCode := task.NewTask("setPublicMeetingUnionCode", "0 */10 * * * *", roadshow.SetPublicMeetingUnionCode)
	task.AddTask("setPublicMeetingUnionCode", setPublicMeetingUnionCode)

	//同步进门会议参会人员数据
	syncComeinMeeting := task.NewTask("SyncComeinMeeting", "0 1 */1 * * * ", SyncComeinMeeting)
	task.AddTask("SyncComeinMeeting", syncComeinMeeting)

	//同步user_record中的关注状态
	syncSubStatus := task.NewTask("syncSubStatus", "0 0 2 * * *", SyncSubStatus)
	task.AddTask("syncSubStatus", syncSubStatus)

	//删除日志 report_save_log,ppt_v2_save_log,保留一个月的
	deleteLog := task.NewTask("syncSubStatus", "0 0 2 1 * *", DeleteLog)
	task.AddTask("deleteLog", deleteLog)

	// 定时发布研报语音播报
	publishVoiceBroadcast := task.NewTask("publishVoiceBroadcast", "0 */1 * * * *", PublishVoiceBroadcast)
	task.AddTask("定时发布研报语音播报", publishVoiceBroadcast)

	task.StartTask()

	fmt.Println("task end")
}

//func Task() {
//	fmt.Println("start")
//	cont := new(context.Context)
//	roadshow.SetPublicMeetingUnionCode(*cont)
//	fmt.Println("end")
//}

//生产环境需要走的任务
func releaseTask() {
	//隆众调研指标获取
	getLzSurveyProduct := task.NewTask("getLzSurveyProduct", "0 5 08-19/1 * * * ", GetLzSurveyProduct)
	task.AddTask("getLzSurveyProduct", getLzSurveyProduct)
	//隆众调研指标数据获取
	getLzSurveyProductData := task.NewTask("getLzSurveyProductData", "0 10 08-20/1 * * * ", GetLzSurveyProductData)
	task.AddTask("getLzSurveyProductData", getLzSurveyProductData)

	//发送邮件
	sendEmail := task.NewTask("sendEmail", "0 0 12 * * 0 ", SendEmail)
	task.AddTask("sendEmail", sendEmail)

	// 正式/试用 用户到期提醒
	companyRemind := task.NewTask("companyRemind", "0 30 08 * * *", CompanyRemind)
	task.AddTask("companyRemind", companyRemind)

	//潜在客户
	freeViewerDetail := task.NewTask("freeViewerDetail", "0 0 9 * * 1 ", FreeViewerDetail)
	task.AddTask("潜在客户", freeViewerDetail)

	//上周增量客户列表
	incrementCompany := task.NewTask("incrementCompany", "0 0 9 * * 1 ", IncrementCompany)
	task.AddTask("上周增量客户列表", incrementCompany)

	//刷新指标数据
	refreshData := task.NewTask("refreshData", "0 1 0,19 * * *", RefreshData)
	task.AddTask("refreshData", refreshData)

	//刷新交易所指标数据
	refreshTradeData := task.NewTask("refreshData", "0 1 4 * * *", RefreshTradeData)
	task.AddTask("refreshTradeData", refreshTradeData)

	//刷新欧洲天然气指标数据
	refreshEicData := task.NewTask("refreshData", "0 1 3,7 * * *", RefreshEicData)
	task.AddTask("refreshEicData", refreshEicData)

	//刷新指标基础数据
	refreshBaseData := task.NewTask("refreshBaseData", "0 */30 * * * * ", RefreshBaseData)
	task.AddTask("refreshBaseData", refreshBaseData)

	//同步弘则数据库中来自,钢联,隆众,有色,人工等基础数据--每隔五分钟,同步一次最新数据
	syncBaseData := task.NewTask("syncBaseData", "0 */5 * * * * ", SyncBaseData)
	task.AddTask("syncBaseData", syncBaseData)

	syncBaseDataExt := task.NewTask("syncBaseDataExt", "0 */30 * * * * ", SyncBaseDataExt)
	task.AddTask("syncBaseDataExt", syncBaseDataExt)

	// 定时往同花顺同步微信群的截止日期(凌晨4点)
	syncThsWxGroupEveryDay := task.NewTask("syncThsWxGroupEveryDay", "0 1 4 * * * ", SyncWxGroupEveryDay)
	task.AddTask("定时往同花顺同步微信群的截止日期", syncThsWxGroupEveryDay)

	//检测数据服务器
	checkDataServer := task.NewTask("checkDataServer", "0 */2 * * * * ", checkDataServer)
	task.AddTask("checkDataServer", checkDataServer)

	//初始化指标更新状态
	resetEdbInfoIsUpdate := task.NewTask("resetEdbInfoIsUpdate", "0 0 0 * * *", data.ResetEdbInfoIsUpdate)
	task.AddTask("resetEdbInfoIsUpdate", resetEdbInfoIsUpdate)

	// 定时检测同花顺客群推送消息状态
	checkThsReportList := task.NewTask("checkThsReportList", "0 */2 * * * * ", CheckThsReportList)
	task.AddTask("checkThsReportList", checkThsReportList)
}

func TaskTest() {
	fmt.Println("The task is start")
	//companyReportPermissionClose := task.NewTask("companyTryOut", "0 5 0 * * *", CompanyReportPermissionClose)
	//companyReportPermissionClose := task.NewTask("companyReportPermissionClose", "0/30 * * * * *", CompanyReportPermissionClose)
	//task.AddTask("用户产品权限试用-->关闭", companyReportPermissionClose)
	//publishVoiceBroadcast := task.NewTask("publishVoiceBroadcast", "0 */1 * * * *", PublishVoiceBroadcast)
	//task.AddTask("定时发布研报语音播报", publishVoiceBroadcast)

	task.StartTask()
	fmt.Println("The task is end")
}

func SendEmail(cont context.Context) (err error) {
	//报告历史访问次数
	go ReportViewTimes()
	//报告访问详情
	go ReportViewDetail()
	//用户权限统计
	go HongzeUsers()
	return
}

func OneMinute(cont context.Context) (err error) {
	//日度
	//FrequencyByDay()
	//周度
	FrequencyByWeek()
	//月度
	FrequencyByMonth()
	return
}

func RefreshData(cont context.Context) (err error) {
	wg := sync.WaitGroup{}
	wg.Add(11)
	//hour := time.Now().Hour()
	//if hour != 0 {
	//}
	go data.RefreshDataFromWind(&wg)
	//同花顺
	go data.RefreshDataFromThs(&wg)
	//彭博
	go data.RefreshDataFromPb(&wg)
	//彭博财务
	go data.RefreshDataFromPbFinance(&wg)
	//手工数据
	go data.RefreshDataFromManual(&wg)
	//隆众数据
	go data.RefreshDataFromLz(&wg)
	//有色
	go data.RefreshDataFromYs(&wg)
	//钢联
	go data.RefreshDataFromGl(&wg)
	//路透
	go data.RefreshDataFromLt(&wg)
	//煤炭
	go data.RefreshDataFromCoal(&wg)
	//谷歌出行数据
	go data.RefreshDataFromGoogleTravel(&wg)

	wg.Wait()
	////计算指标
	data.RefreshDataFromCalculateAll()
	time.Sleep(5 * time.Second)
	data.RefreshNotice()
	fmt.Println("Refresh End")
	return
}

// RefreshTradeData 刷新交易所数据
func RefreshTradeData(cont context.Context) (err error) {
	wg := sync.WaitGroup{}
	wg.Add(5)

	//郑商所
	go data.RefreshDataFromZz(&wg)
	//上期所
	go data.RefreshDataFromSh(&wg)
	//上期能源
	go data.RefreshDataFromShfe(&wg)
	//中金所
	go data.RefreshDataFromCffex(&wg)
	//大商所
	go data.RefreshDataFromDl(&wg)
	wg.Wait()
	//计算指标
	data.RefreshDataFromCalculateAll()

	return
}

// RefreshEicData 刷新欧洲天然气数据
func RefreshEicData(cont context.Context) (err error) {
	wg := sync.WaitGroup{}
	wg.Add(1)

	//欧洲天然气
	go data.RefreshDataFromEic(&wg)
	wg.Wait()
	//计算指标
	data.RefreshDataFromCalculateAll()

	return
}

//刷新基础数据
func RefreshBaseData(cont context.Context) (err error) {
	now := time.Now()
	if now.Hour() == 0 || now.Hour() == 19 {
		return nil
	}
	//同步有色基础指标数据
	go data.SyncSmmIndexDataBase()
	return
}

//刷新基础数据
func SyncBaseData(cont context.Context) (err error) {
	now := time.Now()
	if now.Hour() == 0 || now.Hour() == 19 {
		return nil
	}
	//同步钢联基础数据
	go data.SyncGlDataBase()
	return
}

//刷新基础数据
func SyncBaseDataExt(cont context.Context) (err error) {
	now := time.Now()
	if now.Hour() == 0 || now.Hour() == 19 {
		return nil
	}
	//同步隆众基础数据
	go data.SyncLzDataBase()
	//同步手工数据
	go data.SyncManualDataBase()
	//同步有色基础数据
	go data.SyncSmmDataBase()
	//刷新图表中,指标的最新日期
	go data.SetChartEdbEndDate()
	return
}

// EdbTaskNameMap 手工指标定时任务名称map集合
var EdbTaskNameMap map[string]map[string]bool

// EdbTaskNameChannel 手工指标定时任务名称channel
var EdbTaskNameChannel chan string

// EdbTaskStopChannel 手工指标定时任务停止channel
var EdbTaskStopChannel chan string

// EdbTaskRunNum 手工指标定时任务开始次数
var EdbTaskRunNum int

// AddEdbTask 新增手工指标数据录入提醒
func AddEdbTask(cont context.Context) (err error) {
	//失败列表
	failList := make([]string, 0)

	defer func() {
		if len(failList) > 0 {
			fmt.Println("提醒失败:")
			for _, v := range failList {
				fmt.Println(v)
			}

		}
	}()
	list, err := models.GetEdbInfoByFrequencyNotDay()
	if err != nil {
		fmt.Println("查询获取频度非日度 且 提醒时间不为空 的指标数据失败,Err:", err.Error())
	}
	//如果还没有初始化map,那么先初始
	if EdbTaskNameMap == nil {
		EdbTaskNameMap = make(map[string]map[string]bool)
	}
	tmpEdbTaskNameMap := make(map[string]bool)

	// 今天的日期字符串(格式:2021-10-25)
	todayStr := time.Now().Format(utils.FormatDate)

	//当前周的周一与周日
	nowWeekFirstDay := utils.GetNowWeekMonday()
	nowWeekLastDay := utils.GetNowWeekLastDay()

	//当前月的一号与最后一天
	nowMonthFirstDay := utils.GetNowMonthFirstDay()
	nowMonthLastDay := utils.GetNowMonthLastDay()

	//当前季度的第一天与最后一天
	nowQuarterFirstDay := utils.GetNowQuarterFirstDay()
	nowQuarterLastDay := utils.GetNowQuarterLastDay()

	//当前半年的第一天与最后一天
	nowHalfYearFirstDay := utils.GetNowHalfYearFirstDay()
	nowHalfYearLastDay := utils.GetNowHalfYearLastDay()

	// 当前年的第一天与最后一天
	nowYearFirstDay := utils.GetNowYearFirstDay()
	nowYearLastDay := utils.GetNowYearLastDay()

	debugNoticeUserId := 0 //测试环境,需要发送消息的用户
	//测试环境也不发了
	//if utils.RunMode == "debug" {
	//	tmpWxUser, tmpErr := models.GetWxUserByMobile("17634786714")
	//	if tmpErr == nil && tmpWxUser != nil {
	//		//debugNoticeUserId = 44078 //测试环境的话,发送邮箱给颜鹏
	//		debugNoticeUserId = int(tmpWxUser.UserId) //测试环境的话,发送邮箱给嘉豪
	//	}
	//}

	//task.globalTaskManager.adminTaskList
	for _, edb := range list {
		if edb.UserId <= 0 {
			continue //没有配置user_id的话,那么不需要提醒
		}
		tmpEdb := edb            //指标信息
		isNotice := false        //是否需要提醒
		noticeTime := "12:00:00" //提醒时间

		var dataDtTime time.Time
		edbData, tmpErr := models.GetLastEdbdataInfo(tmpEdb.TradeCode)
		if tmpErr != nil {
			if tmpErr.Error() != utils.ErrNoRow() {
				failList = append(failList, fmt.Sprint(tmpEdb.TradeCode, "失败,Err:", tmpErr.Error()))
				continue
			}
		}

		//如果确实是有数据的
		if edbData != nil {
			tmpDataDtTime, _ := time.ParseInLocation(utils.FormatDate, edbData.Dt, time.Now().Location())
			dataDtTime = tmpDataDtTime
		}

		switch tmpEdb.Frequency {
		case "周度":
			modifyDate := nowWeekLastDay //下次更新日期
			if tmpEdb.NoticeTime != "" {
				addDay := 7
				noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
				if len(noticeArr) >= 2 {
					noticeTime = noticeArr[1]
				}
				noticeWeek := noticeArr[0]
				switch noticeWeek {
				case "周一":
					addDay = 1
				case "周二":
					addDay = 2
				case "周三":
					addDay = 3
				case "周四":
					addDay = 4
				case "周五":
					addDay = 5
				case "周六":
					addDay = 6
				case "周日":
					addDay = 7
				}
				modifyDate = modifyDate.AddDate(0, 0, addDay-7)
			}

			//如果正好是提醒日,同时本周没有过记录,那么需要提醒
			if todayStr == modifyDate.Format(utils.FormatDate) && !nowWeekFirstDay.Before(dataDtTime) {
				isNotice = true
			}
		case "月度":
			addDay := 0
			modifyDate := nowMonthLastDay //下次更新日期
			if tmpEdb.NoticeTime != "" {
				strArr := strings.Split(tmpEdb.NoticeTime, "日")
				if len(strArr) >= 2 {
					noticeTime = strArr[1]
				}
				tmpAddDay, tmpErr := strconv.Atoi(strArr[0])
				if tmpErr != nil {
					continue
				}
				addDay = tmpAddDay - 1
				modifyDate = nowMonthFirstDay.AddDate(0, 0, addDay)
			}

			//如果正好是提醒日,同时本月没有过记录,那么需要提醒
			if todayStr == modifyDate.Format(utils.FormatDate) && !nowMonthFirstDay.Before(dataDtTime) {
				isNotice = true
			}
		case "季度":
			//提醒时间
			if tmpEdb.NoticeTime != "" {
				noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
				if len(noticeArr) >= 2 {
					noticeTime = noticeArr[1]
				}
			}
			//每季度更新数据时间
			//如果正好是提醒日(每季度最后一天),同时本季度没有过记录,那么需要提醒
			if todayStr == nowQuarterLastDay.Format(utils.FormatDate) && !nowQuarterFirstDay.Before(dataDtTime) {
				isNotice = true
			}
		case "半年度":
			//提醒时间
			if tmpEdb.NoticeTime != "" {
				noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
				if len(noticeArr) >= 2 {
					noticeTime = noticeArr[1]
				}
			}
			//每半年度更新数据时间
			//如果正好是提醒日(每半年度最后一天),同时本半年度没有过记录,那么需要提醒
			if todayStr == nowHalfYearLastDay.Format(utils.FormatDate) && !nowHalfYearFirstDay.Before(dataDtTime) {
				isNotice = true
			}
		case "年度":
			//提醒时间
			if tmpEdb.NoticeTime != "" {
				noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
				if len(noticeArr) >= 2 {
					noticeTime = noticeArr[1]
				}
			}
			//每年度更新数据时间
			//如果正好是提醒日(每年度最后一天),同时半年度没有过记录,那么需要提醒
			if todayStr == nowYearLastDay.Format(utils.FormatDate) && !nowYearFirstDay.Before(dataDtTime) {
				isNotice = true
			}
		}

		if isNotice {
			taskName := "edb_task_" + todayStr + ":" + fmt.Sprint(tmpEdb.TradeCode)
			//fmt.Println(taskName, ";", tmpEdb.SecName)

			//定时任务
			tmpTaskFunc := func(ctx context.Context) (funcErr error) {
				//方法执行结束后,移除定时任务
				defer func() {
					EdbTaskNameChannel <- taskName
				}()
				// 匿名方法内判断是否发送提醒,因为可能时间到的时候,发现
				funcIsNotice := false
				// 再次获取指标数据详情
				edbData, tmpErr := models.GetLastEdbdataInfo(tmpEdb.TradeCode)
				if tmpErr != nil {
					if tmpErr.Error() != utils.ErrNoRow() {
						funcErr = tmpErr
						return
					}
				}
				if utils.RunMode == "debug" && debugNoticeUserId > 0 {
					tmpEdb.UserId = debugNoticeUserId //测试环境的话,发送邮箱给嘉豪
				}

				//数据过期时间
				var funcDataDtTime time.Time
				//如果确实是有数据的
				if edbData != nil {
					tmpDataDtTime, _ := time.ParseInLocation(utils.FormatDate, edbData.Dt, time.Now().Location())
					funcDataDtTime = tmpDataDtTime
				}

				//提示频度文案
				notifyFrequency := "每日"

				switch tmpEdb.Frequency {
				case "周度":
					notifyFrequency = "每周"
					modifyDate := nowWeekLastDay //下次更新日期
					if tmpEdb.NoticeTime != "" {
						addDay := 7
						noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
						if len(noticeArr) >= 2 {
							noticeTime = noticeArr[1]
						}
						noticeWeek := noticeArr[0]
						switch noticeWeek {
						case "周一":
							addDay = 1
						case "周二":
							addDay = 2
						case "周三":
							addDay = 3
						case "周四":
							addDay = 4
						case "周五":
							addDay = 5
						case "周六":
							addDay = 6
						case "周日":
							addDay = 7
						}
						modifyDate = modifyDate.AddDate(0, 0, addDay-7)
					}

					//如果正好是提醒日,同时本周没有过记录,那么需要提醒
					if todayStr == modifyDate.Format(utils.FormatDate) && !nowWeekFirstDay.Before(funcDataDtTime) {
						funcIsNotice = true
					}
				case "月度":
					notifyFrequency = "每月"
					addDay := 0
					modifyDate := nowMonthLastDay //下次更新日期
					if tmpEdb.NoticeTime != "" {
						strArr := strings.Split(tmpEdb.NoticeTime, "日")
						if len(strArr) >= 2 {
							noticeTime = strArr[1]
						}
						tmpAddDay, tmpErr := strconv.Atoi(strArr[0])
						if tmpErr != nil {
							funcErr = tmpErr
						}
						addDay = tmpAddDay - 1
						modifyDate = nowMonthFirstDay.AddDate(0, 0, addDay)
					}

					//如果正好是提醒日,同时本月没有过记录,那么需要提醒
					if todayStr == modifyDate.Format(utils.FormatDate) && !nowMonthFirstDay.Before(funcDataDtTime) {
						funcIsNotice = true
					}
				case "季度":
					notifyFrequency = "每季度"
					//提醒时间
					if tmpEdb.NoticeTime != "" {
						noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
						if len(noticeArr) >= 2 {
							noticeTime = noticeArr[1]
						}
					}
					//每季度更新数据时间
					//如果正好是提醒日(每季度最后一天),同时本季度没有过记录,那么需要提醒
					if todayStr == nowQuarterLastDay.Format(utils.FormatDate) && !nowQuarterFirstDay.Before(funcDataDtTime) {
						funcIsNotice = true
					}
				case "半年度":
					notifyFrequency = "每半年度"
					//提醒时间
					if tmpEdb.NoticeTime != "" {
						noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
						if len(noticeArr) >= 2 {
							noticeTime = noticeArr[1]
						}
					}
					//每半年度更新数据时间
					//如果正好是提醒日(每半年度最后一天),同时本半年度没有过记录,那么需要提醒
					if todayStr == nowHalfYearLastDay.Format(utils.FormatDate) && !nowHalfYearFirstDay.Before(funcDataDtTime) {
						funcIsNotice = true
					}
				case "年度":
					notifyFrequency = "每年"
					//提醒时间
					if tmpEdb.NoticeTime != "" {
						noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
						if len(noticeArr) >= 2 {
							noticeTime = noticeArr[1]
						}
					}
					//每年度更新数据时间
					//如果正好是提醒日(每年度最后一天),同时半年度没有过记录,那么需要提醒
					if todayStr == nowYearLastDay.Format(utils.FormatDate) && !nowYearFirstDay.Before(funcDataDtTime) {
						funcIsNotice = true
					}
				}

				//fmt.Println(tmpEdb.TradeCode, " funcIsNotice:", funcIsNotice)
				//如果还是要提醒
				if funcIsNotice {
					//用户微信openid列表数据
					openIdList := make([]*models.OpenIdList, 0)

					//获取用户信息
					isAdmin := true
					admin, err := models.GetAdminByAdminId(tmpEdb.UserId)
					if err != nil {
						if err.Error() == utils.ErrNoRow() {
							isAdmin = false
						} else {
							return err
						}
					}
					if admin == nil {
						isAdmin = false
					}
					if isAdmin {
						if admin.Mobile == "" {

						} else {
							wxUser, err := models.GetWxUserByMobile(admin.Mobile)
							if err != nil {
								return err
							}
							if wxUser == nil {
								funcErr = errors.New("用户信息不存在:mobile:" + admin.Mobile)
								return err
							}
							tmpOpenidList, err := models.GetUserOpenidListByUserId(int(wxUser.UserId))
							if err != nil {
								return err
							}
							openIdList = tmpOpenidList
						}
					} else {
						tmpOpenidList, err := models.GetUserOpenidListByUserId(tmpEdb.UserId)
						if err != nil {
							return err
						}
						openIdList = tmpOpenidList
					}
					//发送消息
					if len(openIdList) <= 0 {
						funcErr = errors.New("openId 列表为空" + strconv.Itoa(tmpEdb.UserId))
						return
					}

					first := "数据录入提醒"
					keyword1 := tmpEdb.SecName
					keyword2 := notifyFrequency + " " + tmpEdb.NoticeTime
					remark := tmpEdb.SecName + "该更新了"

					err = SendWxMsgWithFrequency(first, keyword1, keyword2, remark, openIdList)
					if err != nil {
						return err
					}
					//发送成功,记录发送日志
					{
						sendRecord := new(models.EdbinfoSendMsgRecord)
						sendRecord.UserId = tmpEdb.UserId
						sendRecord.TradeCode = tmpEdb.TradeCode
						sendRecord.CreateTime = time.Now()
						err = models.AddEdbinfoSendMsgRecord(sendRecord)
						if err != nil {
							return err
						}
					}
				}
				return
			}

			//添加定时任务(没有设置通知时间就不进行定时任务通知了)
			spec := ``
			if noticeTime != "" {
				noticeArr := strings.Split(noticeTime, ":")
				if len(noticeArr) == 3 {
					//spec = ` */20 * * * * * `
					spec = fmt.Sprintf(` %s %s %s * * * `, noticeArr[2], noticeArr[1], noticeArr[0])

					//定时任务开始的时间
					tmpTask := task.NewTask(taskName, spec, tmpTaskFunc)

					task.AddTask(taskName, tmpTask)
					tmpEdbTaskNameMap[taskName] = true
				}
			}

		}
	}

	//将当天的手工指标加入到手工指标池去
	EdbTaskNameMap[todayStr] = tmpEdbTaskNameMap
	//开启协程,用来清除定时任务
	go deleteTask()

	//如果当前定时任务执行次数大于0次,那么需要往手工指标定时任务停止channel写入数据,用来关闭昨天没有执行的的定时任务
	if EdbTaskRunNum > 0 {
		//清除昨天的数据
		EdbTaskStopChannel <- time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
	}
	//手工指标定时任务开始次数累加
	EdbTaskRunNum++
	return
	//fmt.Println(task.NewMapSorter())
}

// deleteTask 清除已通知的任务
func deleteTask() {
	for {
		select {
		case taskName := <-EdbTaskNameChannel:
			task.DeleteTask(taskName)
			delete(EdbTaskNameMap, taskName)

		case dayStr := <-EdbTaskStopChannel: //收到停止信号,先清除掉那一天的定时任务,
			for taskName := range EdbTaskNameMap[dayStr] {
				task.DeleteTask(taskName)
				delete(EdbTaskNameMap, taskName)
			}
			break
		}
	}
}

//检测数据服务
func checkDataServer(cont context.Context) (err error) {
	//检测wind新服务器
	go data.CheckWindDataInterface(cont)
	//检测同花顺数据服务器
	//go data.CheckThsDataInterface(cont)
	//检测路透数据服务器
	go data.CheckLtDataInterface(cont)
	//检测彭博
	go data.CheckPbDataInterface(cont)
	return
}