123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- 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("初始化系统用户权限完成")
- }
|