123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638 |
- package data
- import (
- "encoding/json"
- "errors"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/data_manage/edb_refresh"
- "eta/eta_api/models/data_manage/edb_refresh/request"
- "eta/eta_api/utils"
- "fmt"
- "strings"
- "time"
- )
- // 所有刷新配置key
- var allDefaultEdbRefreshConfigKey = `edb_refresh_config:default:all:`
- // GetAllDefaultEdbRefreshConfigListBySourceList
- // @Description: 获取默认的所有刷新配置列表
- // @author: Roc
- // @datetime 2024-01-10 15:03:36
- // @param source int
- // @param subSource int
- // @return list []*edb_refresh.EdbRefreshDefaultConfig
- // @return err error
- func GetAllDefaultEdbRefreshConfigListBySourceList(source, subSource int) (list []*edb_refresh.EdbRefreshDefaultConfig, err error) {
- key := getAllDefaultEdbRefreshConfigKey(source, subSource)
- if utils.Re == nil {
- if utils.Re == nil && utils.Rc.IsExist(key) {
- if data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
- err = json.Unmarshal(data, &list)
- return
- }
- }
- }
- list, err = edb_refresh.GetAllListBySourceList(source, subSource)
- if err != nil {
- return
- }
- // 将数据加入缓存
- if utils.Re == nil {
- data, _ := json.Marshal(list)
- utils.Rc.Put(key, data, 2*time.Hour)
- }
- return
- }
- // SaveEdbRefreshDefaultConfig
- // @Description: 设置默认的指标刷新配置接口
- // @author: Roc
- // @datetime 2024-01-10 15:11:19
- // @param source int
- // @param subSource int
- // @param frequency string
- // @param list []request.RefreshConfigReq
- // @return err error
- // @return errMsg string
- // @return isSendEmail bool
- func SaveEdbRefreshDefaultConfig(source, subSource int, frequency string, list []request.RefreshConfigReq) (err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- errMsg = `保存失败`
- if source <= 0 {
- errMsg = "来源不能为空"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // 非有色的来源,频度不能为空
- if source != utils.DATA_SOURCE_YS && frequency == `` {
- errMsg = "频度不能为空"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- lenConf := len(list)
- if lenConf == 0 {
- errMsg = "至少需要一个刷新配置"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if lenConf > 5 {
- errMsg = "刷新时间设置最多不超过5个"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- tmpArr := []string{"每周", "每旬", "每月", "每季", "每半年", "每年"}
- // 配置的map,避免同一种类型配置同一个时间
- configMap := make(map[string]string)
- for _, v := range list {
- if utils.InArrayByStr(tmpArr, v.RefreshFrequency) && v.RefreshFrequencyDay < 0 {
- errMsg = "请选择具体日期"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if v.RefreshTime == "" {
- errMsg = "请选择具体时间"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // 配置的map,避免同一种类型配置同一个时间
- key := fmt.Sprint(v.RefreshFrequency, "_", v.RefreshFrequencyDay, "_", v.RefreshTime)
- if _, ok := configMap[key]; ok {
- errMsg = "刷新频率和日期不能重复"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- configMap[key] = key
- }
- addList := make([]*edb_refresh.EdbRefreshDefaultConfig, 0)
- for _, v := range list {
- addList = append(addList, &edb_refresh.EdbRefreshDefaultConfig{
- Source: source,
- SubSource: subSource,
- Frequency: frequency,
- RefreshFrequency: v.RefreshFrequency,
- RefreshFrequencyDay: v.RefreshFrequencyDay,
- RefreshAllData: v.RefreshAllData,
- RefreshTime: v.RefreshTime,
- RefreshDataNum: v.RefreshDataNum,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- })
- }
- // 保存
- err = edb_refresh.SaveEdbRefreshDefaultConfig(source, subSource, frequency, addList)
- // 清除缓存
- {
- key := getAllDefaultEdbRefreshConfigKey(source, subSource)
- if utils.Re == nil {
- _ = utils.Rc.Delete(key)
- }
- }
- return
- }
- // SaveEdbRefreshConfig
- // @Description: 设置指标刷新配置接口
- // @author: Roc
- // @datetime 2024-01-10 15:41:45
- // @param source int
- // @param subSource int
- // @param classifyId string
- // @param terminalCode string
- // @param frequency string
- // @param keyword string
- // @param status string
- // @param sysUserIdStr string
- // @param isSelectAll bool
- // @param list []request.RefreshConfigReq
- // @param edbSelectIdList []int
- // @param sysUserId int
- // @param sysUserRealName string
- // @return err error
- // @return errMsg string
- // @return isSendEmail bool
- func SaveEdbRefreshConfig(source, subSource int, classifyId, terminalCode, frequency, keyword, status, sysUserIdStr string, isSelectAll bool, list []request.RefreshConfigReq, edbSelectIdList []int, sysUserId int, sysUserRealName string) (err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- errMsg = `保存失败`
- if source <= 0 {
- errMsg = "来源不能为空"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- lenConf := len(list)
- if lenConf == 0 {
- errMsg = "至少需要一个刷新配置"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if lenConf > 5 {
- errMsg = "刷新时间设置最多不超过5个"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- tmpArr := []string{"每周", "每旬", "每月", "每季", "每半年", "每年"}
- // 配置的map,避免同一种类型配置同一个时间
- configMap := make(map[string]string)
- for _, v := range list {
- if utils.InArrayByStr(tmpArr, v.RefreshFrequency) && v.RefreshFrequencyDay < 0 {
- errMsg = "请选择具体日期"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if v.RefreshTime == "" {
- errMsg = "请选择具体时间"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // 配置的map,避免同一种类型配置同一个时间
- key := fmt.Sprint(v.RefreshFrequency, "_", v.RefreshFrequencyDay, "_", v.RefreshTime)
- if _, ok := configMap[key]; ok {
- errMsg = "刷新频率和日期不能重复"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- configMap[key] = key
- }
- edbIdList := make([]int, 0)
- // 指标id列表
- if isSelectAll {
- // 如果是列表全选
- _, edbList, tmpErr := GetList(source, subSource, classifyId, terminalCode, sysUserIdStr, frequency, keyword, status, 0, 100000, "", "")
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- err = tmpErr
- return
- }
- // 不配置的指标id
- notIdMap := make(map[int]int, 0)
- for _, v := range edbSelectIdList {
- notIdMap[v] = v
- }
- for _, v := range edbList {
- _, ok := notIdMap[v.EdbInfoId]
- // 在不配置的指标id列表内的话,那就过滤
- if ok {
- continue
- }
- // 加入到待配置的指标列表id
- edbIdList = append(edbIdList, v.EdbInfoId)
- }
- } else {
- edbIdList = edbSelectIdList
- }
- if len(edbIdList) <= 0 {
- errMsg = "指标不能为空"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // 待添加的配置关系数据
- addMappingList := make([]*edb_refresh.EdbRefreshMapping, 0)
- // 待添加的日期配置项
- addConfigList := make([]*edb_refresh.EdbRefreshConfig, 0)
- for _, v := range list {
- item, tmpErr := edb_refresh.GetEdbRefreshConfigListByCondition(v.RefreshFrequency, v.RefreshTime, v.RefreshFrequencyDay, v.RefreshAllData, v.RefreshDataNum)
- if tmpErr != nil {
- if tmpErr.Error() != utils.ErrNoRow() {
- err = tmpErr
- return
- }
- addConfigList = append(addConfigList, &edb_refresh.EdbRefreshConfig{
- RefreshFrequency: v.RefreshFrequency,
- RefreshFrequencyDay: v.RefreshFrequencyDay,
- RefreshTime: v.RefreshTime,
- RefreshAllData: v.RefreshAllData,
- RefreshDataNum: v.RefreshDataNum,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- })
- continue
- }
- for _, edbId := range edbIdList {
- addMappingList = append(addMappingList, &edb_refresh.EdbRefreshMapping{
- EdbRefreshMappingId: 0,
- Source: source,
- SubSource: subSource,
- EdbInfoId: edbId,
- EdbRefreshConfigId: item.EdbRefreshConfigId,
- SysUserId: sysUserId,
- SysUserRealName: sysUserRealName,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- })
- }
- }
- // 保存
- err = edb_refresh.SaveEdbRefreshConfig(source, subSource, sysUserId, sysUserRealName, addConfigList, addMappingList, edbIdList)
- return
- }
- // HandleRefreshTime
- // @Description: 处理刷新时间的显示
- // @author: Roc
- // @datetime 2024-01-10 17:00:03
- // @param source int
- // @param subSource int
- // @param list []*data_manage.BaseRefreshEdbInfo
- // @return newList []*data_manage.BaseRefreshEdbInfo
- // @return err error
- // @return errMsg string
- // @return isSendEmail bool
- func HandleRefreshTime(source, subSource int, list []*data_manage.BaseRefreshEdbInfo) (newList []*data_manage.BaseRefreshEdbInfo, err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- errMsg = "获取失败"
- newList = list
- // 默认刷新配置
- defaultEdbRefreshConfigMap := make(map[string]string)
- {
- // 获取默认配置
- allDefaultEdbRefreshConfigList, tmpErr := GetAllDefaultEdbRefreshConfigListBySourceList(source, subSource)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- defaultEdbRefreshConfigListMap := make(map[string][]string)
- for _, v := range allDefaultEdbRefreshConfigList {
- tmp, ok := defaultEdbRefreshConfigListMap[v.Frequency]
- if !ok {
- tmp = make([]string, 0)
- }
- defaultEdbRefreshConfigListMap[v.Frequency] = append(tmp, GetRefreshStr(v.RefreshFrequency, v.RefreshFrequencyDay, v.RefreshTime))
- }
- for k, v := range defaultEdbRefreshConfigListMap {
- defaultEdbRefreshConfigMap[k] = strings.Join(v, ",")
- }
- }
- edbInfoIdList := make([]int, 0)
- for _, v := range newList {
- edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
- }
- // 指标的刷新时间配置
- edbRefreshConfigMap := make(map[int]string)
- {
- // 获取指标的单独配置
- configList, tmpErr := edb_refresh.GetEdbRefreshConfigAndEdbListBySourceAndeEdbInfoId(source, subSource, edbInfoIdList)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- edbRefreshConfigListMap := make(map[int][]string)
- for _, v := range configList {
- tmp, ok := edbRefreshConfigListMap[v.EdbInfoId]
- if !ok {
- tmp = make([]string, 0)
- }
- edbRefreshConfigListMap[v.EdbInfoId] = append(tmp, GetRefreshStr(v.RefreshFrequency, v.RefreshFrequencyDay, v.RefreshTime))
- }
- for k, v := range edbRefreshConfigListMap {
- edbRefreshConfigMap[k] = strings.Join(v, ",")
- }
- }
- // 处理刷新时间
- for _, v := range newList {
- refreshTime, ok := edbRefreshConfigMap[v.EdbInfoId]
- if ok {
- v.RefreshTime = refreshTime
- continue
- }
- if source == utils.DATA_SOURCE_YS {
- v.RefreshTime = defaultEdbRefreshConfigMap[``]
- } else {
- v.RefreshTime = defaultEdbRefreshConfigMap[v.Frequency]
- }
- }
- return
- }
- // GetList
- // @Description: 获取指标列表
- // @author: Roc
- // @datetime 2024-01-08 15:14:16
- // @param source int
- // @param subSource int
- // @param classifyId string
- // @param terminalCode string
- // @param sysUserId string
- // @param frequency string
- // @param keyword string
- // @param startSize int
- // @param pageSize int
- // @return total int
- // @return list []*data_manage.BaseRefreshEdbInfo
- // @return err error
- func GetList(source, subSource int, classifyId, terminalCode, sysUserId, frequency, keyword, status string, startSize, pageSize int, sortParam, sortType string) (total int, list []*data_manage.BaseRefreshEdbInfo, err error) {
- var pars []interface{}
- var condition string
- list = make([]*data_manage.BaseRefreshEdbInfo, 0)
- isStop := -1
- if status == `暂停` {
- isStop = 1
- } else if status == "启用" {
- isStop = 0
- }
- switch source {
- case utils.DATA_SOURCE_MYSTEEL_CHEMICAL: // 钢联
- if classifyId != `` {
- classifyIdSlice := strings.Split(classifyId, ",")
- condition += ` AND base_from_mysteel_chemical_classify_id IN (` + utils.GetOrmInReplace(len(classifyIdSlice)) + `)`
- pars = append(pars, classifyIdSlice)
- }
- if terminalCode != `` {
- condition += " AND terminal_code = ? "
- pars = append(pars, terminalCode)
- }
- if sysUserId != `` {
- sysUserIdSlice := strings.Split(sysUserId, ",")
- condition += ` AND sys_user_id IN (` + utils.GetOrmInReplace(len(sysUserIdSlice)) + `)`
- pars = append(pars, sysUserIdSlice)
- }
- if frequency != `` {
- frequencySlice := strings.Split(frequency, ",")
- condition += ` AND frequency IN (` + utils.GetOrmInReplace(len(frequencySlice)) + `)`
- pars = append(pars, frequencySlice)
- }
- if keyword != `` {
- keywordSlice := strings.Split(keyword, " ")
- if len(keywordSlice) > 0 {
- tmpConditionSlice := make([]string, 0)
- tmpConditionSlice = append(tmpConditionSlice, ` index_name like ? or index_code like ? `)
- pars = utils.GetLikeKeywordPars(pars, keyword, 2)
- for _, v := range keywordSlice {
- if v == ` ` || v == `` {
- continue
- }
- tmpConditionSlice = append(tmpConditionSlice, ` index_name like ? or index_code like ? `)
- pars = utils.GetLikeKeywordPars(pars, v, 2)
- }
- condition += ` AND (` + strings.Join(tmpConditionSlice, " or ") + `)`
- } else {
- condition += ` index_name like ? or index_code like ? `
- pars = utils.GetLikeKeywordPars(pars, keyword, 2)
- }
- }
- if isStop >= 0 {
- condition += " AND is_stop = ? "
- pars = append(pars, isStop)
- }
- sortStr := ``
- if sortParam != `` {
- sortStr = fmt.Sprintf("%s %s,base_from_mysteel_chemical_index_id desc ", sortParam, sortType)
- }
- total, list, err = data_manage.GetMysteelChemicalBaseInfoList(condition, pars, sortStr, startSize, pageSize)
- case utils.DATA_SOURCE_YS: // 有色
- if classifyId != `` {
- classifyIdSlice := strings.Split(classifyId, ",")
- condition += ` AND classify_id IN (` + utils.GetOrmInReplace(len(classifyIdSlice)) + `)`
- pars = append(pars, classifyIdSlice)
- }
- if terminalCode != `` {
- condition += " AND terminal_code = ? "
- pars = append(pars, terminalCode)
- }
- if frequency != `` {
- frequencySlice := strings.Split(frequency, ",")
- condition += ` AND frequency IN (` + utils.GetOrmInReplace(len(frequencySlice)) + `)`
- pars = append(pars, frequencySlice)
- }
- if keyword != `` {
- keywordSlice := strings.Split(keyword, " ")
- if len(keywordSlice) > 0 {
- tmpConditionSlice := make([]string, 0)
- tmpConditionSlice = append(tmpConditionSlice, ` index_name like ? or index_code like ? `)
- pars = utils.GetLikeKeywordPars(pars, keyword, 2)
- for _, v := range keywordSlice {
- if v == ` ` || v == `` {
- continue
- }
- tmpConditionSlice = append(tmpConditionSlice, ` index_name like ? or index_code like ? `)
- pars = utils.GetLikeKeywordPars(pars, v, 2)
- }
- condition += ` AND (` + strings.Join(tmpConditionSlice, " or ") + `)`
- } else {
- condition += ` index_name like ? or index_code like ? `
- pars = utils.GetLikeKeywordPars(pars, keyword, 2)
- }
- }
- if isStop >= 0 {
- condition += " AND is_stop = ? "
- pars = append(pars, isStop)
- }
- sortStr := ``
- if sortParam != `` {
- sortStr = fmt.Sprintf("%s %s,base_from_smm_index_id desc ", sortParam, sortType)
- }
- total, list, err = data_manage.GetSmmBaseInfoList(condition, pars, sortStr, startSize, pageSize)
- default:
- condition += ` AND source = ? AND sub_source = ? `
- pars = append(pars, source, subSource)
- if isStop >= 0 {
- condition += " AND no_update = ? "
- pars = append(pars, isStop)
- }
- if classifyId != `` {
- classifyIdSlice := strings.Split(classifyId, ",")
- condition += ` AND classify_id IN (` + utils.GetOrmInReplace(len(classifyIdSlice)) + `)`
- pars = append(pars, classifyIdSlice)
- }
- if terminalCode != `` {
- condition += " AND terminal_code = ? "
- pars = append(pars, terminalCode)
- }
- if sysUserId != `` {
- sysUserIdSlice := strings.Split(sysUserId, ",")
- condition += ` AND sys_user_id IN (` + utils.GetOrmInReplace(len(sysUserIdSlice)) + `)`
- pars = append(pars, sysUserIdSlice)
- }
- if frequency != `` {
- frequencySlice := strings.Split(frequency, ",")
- condition += ` AND frequency IN (` + utils.GetOrmInReplace(len(frequencySlice)) + `)`
- pars = append(pars, frequencySlice)
- }
- if keyword != `` {
- keywordSlice := strings.Split(keyword, " ")
- if len(keywordSlice) > 0 {
- tmpConditionSlice := make([]string, 0)
- tmpConditionSlice = append(tmpConditionSlice, ` edb_name like ? or edb_code like ? `)
- pars = utils.GetLikeKeywordPars(pars, keyword, 2)
- for _, v := range keywordSlice {
- if v == ` ` || v == `` {
- continue
- }
- tmpConditionSlice = append(tmpConditionSlice, ` edb_name like ? or edb_code like ? `)
- pars = utils.GetLikeKeywordPars(pars, v, 2)
- }
- condition += ` AND (` + strings.Join(tmpConditionSlice, " or ") + `)`
- } else {
- condition += ` edb_name like ? or edb_code like ? `
- pars = utils.GetLikeKeywordPars(pars, keyword, 2)
- }
- }
- sortStr := ``
- if sortParam != `` {
- sortStr = fmt.Sprintf("%s %s,edb_info_id desc ", sortParam, sortType)
- }
- total, list, err = data_manage.GetEdbBaseInfoList(condition, pars, sortStr, startSize, pageSize)
- }
- return
- }
- // getAllDefaultEdbRefreshConfigKey
- // @Description: 获取默认的所有刷新配置key
- // @author: Roc
- // @datetime 2024-01-10 15:02:49
- // @param source int
- // @param subSource int
- // @return string
- func getAllDefaultEdbRefreshConfigKey(source, subSource int) string {
- return allDefaultEdbRefreshConfigKey + fmt.Sprintf("%d_%d", source, subSource)
- }
- // GetRefreshStr
- // @Description: 获取刷新配置的中文字符串
- // @author: Roc
- // @datetime 2024-01-10 16:05:10
- // @param refreshFrequency string
- // @param refreshFrequencyDay int
- // @param refreshTime string
- // @return string
- func GetRefreshStr(refreshFrequency string, refreshFrequencyDay int, refreshTime string) string {
- refreshDayStr := ``
- //if refreshFrequency != "每自然日" && refreshFrequency != "每交易日"
- switch refreshFrequency {
- case "每自然日", "每交易日":
- case "每周":
- switch refreshFrequencyDay {
- case 0:
- refreshDayStr = "日"
- case 1:
- refreshDayStr = "一"
- case 2:
- refreshDayStr = "二"
- case 3:
- refreshDayStr = "三"
- case 4:
- refreshDayStr = "四"
- case 5:
- refreshDayStr = "五"
- case 6:
- refreshDayStr = "六"
- case 7:
- refreshDayStr = "日"
- }
- default:
- if refreshFrequencyDay > 0 {
- refreshDayStr = fmt.Sprintf("第%d天", refreshFrequencyDay)
- } else {
- refreshDayStr = `最后一天`
- }
- }
- return refreshFrequency + refreshDayStr + " " + refreshTime
- }
|