package models import ( "eta/eta_index_lib/utils" "fmt" "strconv" "strings" "time" "github.com/beego/beego/v2/client/orm" ) func GetBaseFromSciHqDataByCondition(condition string, pars []interface{}) (list []*BaseFromSciHqData, err error) { o := orm.NewOrm() sql := `SELECT * FROM base_from_sci_hq_data WHERE 1=1 ` if condition != "" { sql += condition } _, err = o.Raw(sql, pars).QueryRows(&list) return } func GetBaseFromSciHqDataByIndexCode(indexCode string) (list []*BaseFromSciHqData, err error) { o := orm.NewOrm() sql := `SELECT * FROM base_from_sci_hq_data WHERE 1=1 AND index_code=?` _, err = o.Raw(sql, indexCode).QueryRows(&list) return } // AddEdbDataFromSciHq 新增卓创红期指标数据 func AddEdbDataFromSciHq(edbCode string) (err error) { o := orm.NewOrm() var condition string var pars []interface{} if edbCode != "" { condition += " AND index_code=? " pars = append(pars, edbCode) } sciBaseDataAll, err := GetBaseFromSciHqDataByCondition(condition, pars) if err != nil && err.Error() != utils.ErrNoRow() { return } var isAdd bool addSql := ` INSERT INTO edb_data_sci_hq(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values ` existMap := make(map[string]string) for _, sv := range sciBaseDataAll { eDate := sv.DataTime dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local) 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 = o.Raw(addSql).Exec() if err != nil { return err } } return } // RefreshEdbDataFromSciHq 刷新卓创红期指标数据 func RefreshEdbDataFromSciHq(edbInfoId int, edbCode, startDate string) (err error) { source := utils.DATA_SOURCE_SCI_HQ subSource := utils.DATA_SUB_SOURCE_EDB o := orm.NewOrm() 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) } sciDataList, err := GetBaseFromSciDataByCondition(condition, pars) if err != nil { return } // 真实数据的最大日期 , 插入规则配置的日期 var realDataMaxDate, edbDataInsertConfigDate time.Time var edbDataInsertConfig *EdbDataInsertConfig var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值 { edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId) if err != nil && err.Error() != utils.ErrNoRow() { 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, subSource, 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_sci_hq(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values ` var isAdd bool for _, v := range sciDataList { 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, subSource, 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, subSource, existMap, isFindConfigDateRealData) if isAdd { addSql = strings.TrimRight(addSql, ",") _, err = o.Raw(addSql).Exec() if err != nil { fmt.Println("RefreshEdbDataFromSci add Err", err.Error()) return } } return } // HandleSciHqExcelDataReq 卓创红期的excel数据 type HandleSciHqExcelDataReq struct { IndexName string ExcelIndexCode string Frequency string Unit string TerminalCode string FilePath string Data map[string]string } type BaseFromSciHqIndex struct { BaseFromSciHqIndexId int `orm:"column(base_from_sci_hq_index_id);pk"` //序号 IndexCode string `description:"指标编码"` IndexName string `description:"指标名称"` ClassifyId int `description:"分类id"` Frequency string `description:"频率"` Unit string `description:"单位"` StartDate string `description:"开始日期"` EndDate string `description:"结束日期"` Sort int `description:"排序"` TerminalCode string `description:"终端编码"` FilePath string `description:"文件路径"` CreateTime time.Time `description:"创建时间"` ModifyTime time.Time `description:"更新时间"` } func GetAllBaseFromSciHqIndex() (items []*BaseFromSciHqIndex, err error) { o := orm.NewOrm() sql := `SELECT * FROM base_from_sci_hq_index` _, err = o.Raw(sql).QueryRows(&items) return } func BatchAddBaseFromSciHqIndex(list []*BaseFromSciHqIndex) (err error) { o := orm.NewOrm() _, err = o.InsertMulti(len(list), list) return } type BaseFromSciHqData struct { SciHqDataId int `orm:"column(sci_hq_data_id);pk"` //序号 BaseFromSciHqIndexId int `orm:"column(base_from_sci_hq_index_id)"` //指标id IndexCode string `description:"指标编码"` DataTime string `description:"数据时间"` Value string `description:"数据值"` CreateTime time.Time `description:"创建时间"` ModifyTime time.Time `description:"更新时间"` DataTimestamp int64 `description:"数据时间戳"` } func (r *BaseFromSciHqData) Update(cols []string) (err error) { o := orm.NewOrm() _, err = o.Update(r, cols...) return } func BatchAddBaseFromSciHqData(list []*BaseFromSciHqData) (err error) { o := orm.NewOrm() _, err = o.InsertMulti(500, list) return } func GetSciHqIndexInfoMaxAndMinInfo(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) { o := orm.NewOrm() 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_sci_hq_data WHERE index_code=? ` err = o.Raw(sql, indexCode).QueryRow(&item) if err != nil { return } // 获取最新值 var latest_value float64 var latestDate string sql = ` SELECT value AS latest_value, data_time AS latest_date FROM base_from_sci_hq_data WHERE index_code=? ORDER BY data_time DESC LIMIT 1 ` err = o.Raw(sql, indexCode).QueryRow(&latest_value, &latestDate) if err != nil { return } item.LatestValue = latest_value item.LatestDate = latestDate return } func ModifySciHqIndexMaxAndMinInfo(indexCode string, item *EdbInfoMaxAndMinInfo) (err error) { o := orm.NewOrm() sql := ` UPDATE base_from_sci_hq_index SET start_date=?,end_date=?,latest_value=?,latest_date=?,modify_time=NOW() WHERE index_code=? ` _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.LatestValue, item.LatestDate, indexCode).Exec() return }