package models import ( sql2 "database/sql" "eta/eta_index_lib/global" "eta/eta_index_lib/utils" "fmt" "gorm.io/gorm" "strconv" "strings" "time" ) // BaseFromRadishResearch 萝卜投研 type BaseFromRadishResearch struct{} type BaseFromRadishResearchData struct { BaseFromRadishResearchDataId int `gorm:"column:base_from_radish_research_data_id;primaryKey"` BaseFromRadishResearchIndexId int IndexCode string DataTime string Value string CreateTime time.Time ModifyTime time.Time DataTimestamp int64 } func (m *BaseFromRadishResearchData) TableName() string { return "base_from_radish_research_data" } func (e *BaseFromRadishResearchData) AfterFind(db *gorm.DB) (err error) { e.DataTime = utils.GormDateStrToDateStr(e.DataTime) return } func GetBaseFromRadishResearchDataByCondition(condition string, pars []interface{}) (list []*BaseFromRadishResearchData, err error) { sql := `SELECT * FROM base_from_radish_research_data WHERE 1=1 ` if condition != "" { sql += condition } err = global.DEFAULT_DB.Raw(sql, pars...).Find(&list).Error return } // Add 添加 func (obj BaseFromRadishResearch) Add(edbCode string) (err error) { var condition string var pars []interface{} if edbCode != "" { condition += " AND index_code = ? " pars = append(pars, edbCode) } dataList, err := GetBaseFromRadishResearchDataByCondition(condition, pars) if err != nil { return } var isAdd bool addSql := ` INSERT INTO edb_data_radish_research(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values ` addSql = utils.ReplaceDriverKeywords("", addSql) existMap := make(map[string]string) for _, sv := range dataList { eDate := sv.DataTime dataTime, err := time.Parse(utils.FormatDate, eDate) if err != nil { return err } timestamp := dataTime.UnixNano() / 1e6 timeStr := fmt.Sprintf("%d", timestamp) if _, ok := existMap[eDate]; !ok { addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Value) isAdd = true } existMap[eDate] = sv.Value } if isAdd { addSql = strings.TrimRight(addSql, ",") utils.FileLog.Info("addSql:" + addSql) err = global.DEFAULT_DB.Exec(addSql).Error if err != nil { return err } } return } // Refresh 刷新萝卜投研指标数据 func (obj BaseFromRadishResearch) Refresh(edbInfoId int, edbCode, startDate string) (err error) { source := obj.GetSource() edbInfoIdStr := strconv.Itoa(edbInfoId) var condition string var pars []interface{} if edbCode != "" { condition += " AND index_code=? " pars = append(pars, edbCode) } if startDate != "" { condition += " AND data_time>=? " pars = append(pars, startDate) } dataList, e := GetBaseFromRadishResearchDataByCondition(condition, pars) if e != nil { err = fmt.Errorf("GetBaseFromRadishResearchDataByCondition err: %v", e) return } // 真实数据的最大日期 , 插入规则配置的日期 var realDataMaxDate, edbDataInsertConfigDate time.Time var edbDataInsertConfig *EdbDataInsertConfig var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值 { edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId) if err != nil && !utils.IsErrNoRow(err) { return } if edbDataInsertConfig != nil { edbDataInsertConfigDate = edbDataInsertConfig.Date } } var existCondition string var existPars []interface{} existCondition += " AND edb_info_id=? " existPars = append(existPars, edbInfoId) if startDate != "" { existCondition += " AND data_time>=? " existPars = append(existPars, startDate) } existList, err := GetEdbDataByCondition(source, 0, existCondition, existPars) if err != nil { return err } existMap := make(map[string]*EdbInfoSearchData) for _, v := range existList { existMap[v.DataTime] = v } addSql := ` INSERT INTO edb_data_radish_research(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values ` addSql = utils.ReplaceDriverKeywords("", addSql) var isAdd bool for _, v := range dataList { item := v eDate := item.DataTime dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local) if err != nil { return err } if findItem, ok := existMap[v.DataTime]; !ok { sValue := item.Value timestamp := dataTime.UnixNano() / 1e6 timeStr := fmt.Sprintf("%d", timestamp) addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue) isAdd = true } else { if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Value { err = ModifyEdbDataById(source, 0, findItem.EdbDataId, item.Value) if err != nil { return err } } } // 下面代码主要目的是处理掉手动插入的数据判断 { if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) { realDataMaxDate = dataTime } if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) { isFindConfigDateRealData = true } } } // 处理手工数据补充的配置 HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, 0, existMap, isFindConfigDateRealData) if isAdd { addSql = strings.TrimRight(addSql, ",") err = global.DEFAULT_DB.Exec(addSql).Error if err != nil { fmt.Println("BaseFromRadishResearch add Err", err.Error()) return } } return } // GetSource 获取来源编码id func (obj BaseFromRadishResearch) GetSource() int { return utils.DATA_SOURCE_RADISH_RESEARCH } // GetSourceName 获取来源名称 func (obj BaseFromRadishResearch) GetSourceName() string { return utils.DATA_SOURCE_NAME_RADISH_RESEARCH } type BaseFromRadishResearchIndex struct { BaseFromRadishResearchIndexId int `gorm:"column:base_from_radish_research_index_id;primaryKey"` IndexCode string IndexName string Frequency string Unit string Source string `description:"数据来源"` ClassifyId int StartDate string EndDate string Sort int TerminalCode string FilePath string LatestValue float64 CreateTime time.Time ModifyTime time.Time } func (e *BaseFromRadishResearchIndex) AfterFind(db *gorm.DB) (err error) { e.StartDate = utils.GormDateStrToDateStr(e.StartDate) e.EndDate = utils.GormDateStrToDateStr(e.EndDate) return } func (m *BaseFromRadishResearchIndex) TableName() string { return "base_from_radish_research_index" } func (m *BaseFromRadishResearchIndex) Add() (lastId int64, err error) { err = global.DEFAULT_DB.Create(&m).Error if err != nil { return } lastId = int64(m.BaseFromRadishResearchIndexId) return } func (m *BaseFromRadishResearchIndex) Update(updateCols []string) (err error) { err = global.DEFAULT_DB.Model(&m).Select(updateCols).Updates(&m).Error return } func (m *BaseFromRadishResearchIndex) ModifyIndexMaxAndMinDate(indexCode string, item *EdbInfoMaxAndMinInfo) (err error) { sql := ` UPDATE base_from_radish_research_index SET start_date=?,end_date=?,latest_value=?,modify_time=NOW() WHERE index_code=? ` err = global.DEFAULT_DB.Exec(sql, item.MinDate, item.MaxDate, item.LatestValue, indexCode).Error return } func (m *BaseFromRadishResearchIndex) GetByIndexCode(indexCode string) (item *BaseFromRadishResearchIndex, err error) { sql := ` SELECT * FROM base_from_radish_research_index WHERE index_code = ? ` err = global.DEFAULT_DB.Raw(sql, indexCode).First(&item).Error return } func (m *BaseFromRadishResearchIndex) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromRadishResearchIndex, err error) { fields := strings.Join(fieldArr, ",") if len(fieldArr) == 0 { fields = `*` } order := `ORDER BY create_time DESC` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT %s FROM base_from_radish_research_index WHERE 1=1 %s %s`, fields, condition, order) err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error return } type BaseFromRadishResearchIndexList struct { RadishResearchIndexId int64 `gorm:"column:ccf_index_id;primaryKey"` IndexCode string IndexName string Frequency string Unit string ClassifyId int StartDate string EndDate string TerminalCode string CreateTime string ModifyTime string } func (m *BaseFromRadishResearchData) GetByIndexCode(indexCode string) (list []*BaseFromRadishResearchData, err error) { sql := ` SELECT * FROM base_from_radish_research_data WHERE index_code=? ` err = global.DEFAULT_DB.Raw(sql, indexCode).Find(&list).Error return } func (m *BaseFromRadishResearchData) AddMulti(item []*BaseFromRadishResearchData) (err error) { err = global.DEFAULT_DB.CreateInBatches(&item, utils.MultiAddNum).Error return } // HandleRadishResearchEdbData 萝卜投研的指标数据 type HandleRadishResearchEdbData struct { IndexName string `description:"指标名称"` IndexCode string `description:"指标编码"` ClassifyId int `description:"分类ID"` Unit string `description:"单位"` Source string `description:"数据来源"` Sort int `description:"排序"` Frequency string `description:"频度"` ExcelDataMap map[string]string `description:"指标数据"` } type HandleRadishResearchEdbDataReq struct { List []*HandleRadishResearchEdbData TerminalCode string `description:"终端编码"` FilePath string `description:"文件存储路径"` } type HandleRadishResearchTableData struct { ClassifyId int `description:"分类ID"` FromPage string `description:"表格来源"` TableDate time.Time `description:"表格日期"` TableContent string `description:"表格HTML"` } type HandleRadishResearchStockTableReq struct { Table *HandleRadishResearchTableData TerminalCode string `description:"编码"` } func (m *BaseFromRadishResearchData) GetMaxAndMinDateByIndexCode(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) { sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM base_from_radish_research_data WHERE index_code=? ` err = global.DEFAULT_DB.Raw(sql, indexCode).First(&item).Error sql = ` SELECT value AS latest_value FROM base_from_radish_research_data WHERE index_code=? ORDER BY data_time DESC LIMIT 1 ` var latestValueNull sql2.NullFloat64 err = global.DEFAULT_DB.Raw(sql, indexCode).Scan(&latestValueNull).Error if err == nil && latestValueNull.Valid { item.LatestValue = latestValueNull.Float64 } return } // BaseFromRadishResearchClassify 萝卜投研数据分类表 type BaseFromRadishResearchClassify struct { ClassifyId int `gorm:"column:classify_id;primaryKey"` ClassifyName string `description:"分类名称"` ParentId int `description:"父级id"` SysUserId int `description:"创建人id"` SysUserRealName string `description:"创建人姓名"` Level int `description:"层级"` Sort int `description:"排序字段,越小越靠前,默认值:10"` ModifyTime time.Time `description:"修改时间"` CreateTime time.Time `description:"创建时间"` } func (m *BaseFromRadishResearchClassify) Add() (lastId int64, err error) { err = global.DEFAULT_DB.Create(&m).Error if err != nil { return } lastId = int64(m.ClassifyId) return } func (m *BaseFromRadishResearchClassify) Update(updateCols []string) (err error) { err = global.DEFAULT_DB.Model(&m).Select(updateCols).Updates(&m).Error return } func (m *BaseFromRadishResearchClassify) GetByClassifyName(classifyName string) (item *BaseFromRadishResearchClassify, err error) { sql := ` SELECT * FROM base_from_ccf_classify WHERE classify_name=? ` err = global.DEFAULT_DB.Raw(sql, classifyName).First(&item).Error return } func (m *BaseFromRadishResearchClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromRadishResearchClassify, err error) { fields := strings.Join(fieldArr, ",") if len(fieldArr) == 0 { fields = `*` } order := `ORDER BY create_time DESC` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT %s FROM base_from_ccf_classify WHERE 1=1 %s %s`, fields, condition, order) err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error return } // BaseFromRadishResearchClassifyItem 萝卜投研数据分类信息 type BaseFromRadishResearchClassifyItem struct { ClassifyId int `description:"分类ID"` ClassifyName string `description:"分类名称"` ParentId int `description:"父级id"` Level int `description:"层级"` Sort int `description:"排序字段"` CreateTime string `description:"创建时间"` ModifyTime string `description:"修改时间"` Child []*BaseFromCCFClassifyItem `description:"子分类"` } func (m *BaseFromRadishResearchClassify) Format2Item(origin *BaseFromRadishResearchClassify) (item *BaseFromCCFClassifyItem) { if origin == nil { return } item = new(BaseFromCCFClassifyItem) item.ClassifyId = origin.ClassifyId item.ClassifyName = origin.ClassifyName item.ParentId = origin.ParentId item.Level = origin.Level item.Sort = origin.Sort item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime) item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime) return } // MultiUpdateBaseFromRadishResearchDataValue 批量更新萝卜投研指标数据 func MultiUpdateBaseFromRadishResearchDataValue(items []*BaseFromRadishResearchData) (err error) { if len(items) == 0 { return } to := global.DEFAULT_DB.Begin() defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() sql := `UPDATE base_from_radish_research_data SET value = ?, modify_time = NOW() WHERE index_code = ? AND data_time = ? LIMIT 1` for _, v := range items { if v.IndexCode == "" || v.DataTime == "" { continue } err = to.Exec(sql, v.Value, v.IndexCode, v.DataTime).Error if err != nil { return } } return }