package services import ( "eta/eta_mini_crm/models" "eta/eta_mini_crm/services/elastic" "eta/eta_mini_crm/utils" "fmt" "time" ) func InitTask() { fmt.Println("start task") // 创建Es索引 CreateIndex() // 初始化研报推送状态 InitEtaReportPushStatus() // 初始化pdf报告推送状态 InitPdfReportPushStatus() // 初始化系统用户权限 InitSystemUserPermission() fmt.Println("end task!") } func CreateIndex() { var mappingJson = `{ "mappings": { "properties": { "ReportPdfId": {"type": "integer"}, "PdfUrl": {"type": "text"}, "PdfName": {"type": "text"}, "Title": {"type": "text"}, "Author": {"type": "text"}, "Abstract": {"type": "text"}, "ClassifyIdFirst": {"type": "integer"}, "ClassifyNameFirst": {"type": "text"}, "ClassifyIdSecond": {"type": "integer"}, "ClassifyNameSecond": {"type": "text"}, "Stage": {"type": "integer"}, "PublishTime": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "ModifyTime": {"type": "date"}, "Pv": {"type": "integer"}, "Uv": {"type": "integer"}, "SysUserId": {"type": "integer"}, "SysRealName": {"type": "text"}, "State": {"type": "integer"} } } }` err := elastic.EsCreateIndex(utils.MINI_REPORT_INDEX_NAME, mappingJson) if err != nil { fmt.Println(err) } } func InitEtaReportPushStatus() { for { maxId, err := models.GetMaxSyncIdReportPush(1) fmt.Println("同步研报开始, maxId:", maxId) if err != nil { fmt.Println("同步研报失败, Err:", err) } reportList, err := models.GetBatchReport(maxId, 100) if err != nil { fmt.Println("同步研报失败, Err:", err) } var reportIds []int for _, v := range reportList { reportIds = append(reportIds, v.Id) } reportPushList, err := models.GetReportPushStatusByReportIds(utils.ReportTypeEta, reportIds) if err != nil { return } reportPushMap := make(map[int]struct{}) for _, v := range reportPushList { reportPushMap[v.ReportId] = struct{}{} } var insertReportPushList []*models.ReportPushStatus for _, v := range reportList { if _, ok := reportPushMap[v.Id]; !ok { insertReportPushList = append(insertReportPushList, &models.ReportPushStatus{ ReportId: v.Id, State: 0, Title: v.Title, Abstract: v.Abstract, Stage: v.Stage, ClassifyIdFirst: v.ClassifyIdFirst, ClassifyNameFirst: v.ClassifyNameFirst, ClassifyIdSecond: v.ClassifyIdSecond, ClassifyNameSecond: v.ClassifyNameSecond, ClassifyIdThird: v.ClassifyIdThird, ClassifyNameThird: v.ClassifyNameThird, Author: v.Author, ReportType: utils.ReportTypeEta, PublishTime: v.PublishTime, CreateTime: time.Now(), ModifyTime: time.Now(), }) } } err = models.BatchAddReportPushStatus(insertReportPushList) if err != nil { return } if len(reportList) != 100 { fmt.Println("同步研报完成") return } } } // 初始化pdf报告的推送状态 func InitPdfReportPushStatus() { for { maxId, err := models.GetMaxSyncIdReportPush(2) fmt.Println("同步pdf研报开始, maxId:", maxId) if err != nil { fmt.Println("同步研pdf报失败, Err:", err) } reportList, err := models.GetBatchPdfReport(maxId, 100) if err != nil { fmt.Println("同步pdf研报失败, Err:", err) } var reportIds []int for _, v := range reportList { reportIds = append(reportIds, v.ReportPdfId) } reportPushList, err := models.GetReportPushStatusByReportIds(utils.ReportTypePdf, reportIds) if err != nil { return } reportPushMap := make(map[int]struct{}) for _, v := range reportPushList { reportPushMap[v.ReportId] = struct{}{} } var insertReportPushList []*models.ReportPushStatus for _, v := range reportList { if _, ok := reportPushMap[v.ReportPdfId]; !ok { insertReportPushList = append(insertReportPushList, &models.ReportPushStatus{ ReportId: v.ReportPdfId, State: 0, Title: v.Title, Abstract: v.Abstract, Stage: v.Stage, ClassifyIdFirst: v.ClassifyIdFirst, ClassifyNameFirst: v.ClassifyNameFirst, ClassifyIdSecond: v.ClassifyIdSecond, ClassifyNameSecond: v.ClassifyNameSecond, ClassifyIdThird: v.ClassifyIdThird, ClassifyNameThird: v.ClassifyNameThird, Author: v.Author, ReportType: utils.ReportTypePdf, PublishTime: v.PublishTime, CreateTime: time.Now(), ModifyTime: time.Now(), }) } } err = models.BatchAddReportPushStatus(insertReportPushList) if err != nil { return } if len(reportList) != 100 { fmt.Println("同步pdf研报完成") return } } } func InitSystemUserPermission() { menuList, err := models.GetSysMenuItemsByCondition("", []interface{}{}, []string{"sys_menu_id"}, "") if err != nil { fmt.Println("获取系统菜单失败, Err:", err) utils.FileLog.Info("获取系统菜单失败, Err:", err) return } roleMenuList, err := models.GetSysRoleMenuByRoleId(1) if err != nil { if err.Error() == utils.ErrNoRow() { fmt.Println("系统角色不存在, 请先创建系统角色") utils.FileLog.Info("系统角色不存在, 请先创建系统角色") } fmt.Println("获取系统角色菜单失败, Err:", err) utils.FileLog.Info("获取系统角色菜单失败, Err:", err) return } menuMap := make(map[int]struct{}) for _, v := range menuList { menuMap[v.SysMenuId] = struct{}{} } for _, v := range roleMenuList { delete(menuMap, v.SysMenuId) } insertMenu := make([]*models.SysRoleMenuMapping, 0) for k, _ := range menuMap { t := new(models.SysRoleMenuMapping) t.SysMenuId = k t.SysRoleId = 1 t.Type = 0 insertMenu = append(insertMenu, t) } if len(insertMenu) > 0 { err = models.BatchAddSysRoleMenu(insertMenu) if err != nil { fmt.Println("初始化系统用户权限失败, Err:", err) utils.FileLog.Info("初始化系统用户权限失败, Err:", err) return } } fmt.Println("初始化系统用户权限完成") }