package data_manage import ( "eta/eta_task/global" "eta/eta_task/utils" "fmt" "gorm.io/gorm" "strconv" "strings" "time" ) //func AddEdbDataGlBySql(sqlStr string) (err error) { // o := global.DbMap[utils.DbNameIndex] // err = o.Exec(sqlStr).Error // return //} type GlData struct { InputValue string `gorm:"column:DATA_VALUE" description:"日期"` DataTime string `gorm:"column:DATA_DATE" description:"值"` } func (m *GlData) AfterFind(db *gorm.DB) (err error) { m.DataTime = utils.GormDateStrToDateStr(m.DataTime) return } func GetGlDataByTradeCode(condition string, pars []interface{}) (item []*GlData, err error) { //sql := ` SELECT * FROM mb_index_main_data WHERE 1=1 AND DATA_VALUE is not null ` //o := global.DbMap[utils.DbNameIndex] //if condition != "" { // sql += condition //} //sql += ` ORDER BY PUBLISH_TIME DESC ` condition += " AND IS_DELETE=0 " sql1 := ` SELECT * FROM mb_index_main_data WHERE 1=1 AND DATA_VALUE is not null ` o := global.DbMap[utils.DbNameGL] if condition != "" { sql1 += condition } sql := `select * from (` + sql1 + ` having 1 order by PUBLISH_TIME DESC ) tmp group by DATA_DATE ORDER BY PUBLISH_TIME DESC ` err = o.Raw(sql, pars...).Find(&item).Error return } //func GetEdbDataGlByCodeAndDate(edbCode string, startDate string) (count int, err error) { // o := global.DbMap[utils.DbNameIndex] // sql := ` SELECT COUNT(1) AS count FROM edb_data_gl WHERE edb_code=? AND data_time=? ` // err = o.Raw(sql, edbCode, startDate).Scan(&count).Error // return //} //func ModifyEdbDataGl(edbInfoId int64, dataTime, value string) (err error) { // o := global.DbMap[utils.DbNameIndex] // sql := ` UPDATE edb_data_gl SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? ` // err = o.Exec(sql, value, edbInfoId, dataTime).Error // return //} // 刷新钢联指标数据 func RefreshEdbDataByGl(edbInfoId int, edbCode, startDate, endDate string) (err error) { o := global.DbMap[utils.DbNameIndex] tx := o.Begin() if tx.Error != nil { return tx.Error } defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() if err != nil { return } 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_DATE>=? " pars = append(pars, startDate) } if endDate != "" { condition += " AND DATA_DATE<=? " pars = append(pars, endDate) } condition += " AND IS_DELETE=0 " //获取已存在指标所有数据 existDataList := make([]*EdbDataBase, 0) dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_GL, utils.DATA_SUB_SOURCE_EDB) sql := `SELECT * FROM %s WHERE edb_info_id=? ` sql = fmt.Sprintf(sql, dataTableName) err = o.Raw(sql, edbInfoId).Find(&existDataList).Error if err != nil { return err } existDataMap := make(map[string]string) for _, v := range existDataList { existDataMap[v.DataTime] = v.Value } glDataList, err := GetGlDataByTradeCode(condition, pars) addSql := ` INSERT INTO edb_data_gl(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values ` var isAdd bool addExistMap := make(map[string]string) for _, v := range glDataList { item := v if _, ok := addExistMap[v.DataTime]; !ok { if existVal, existOk := existDataMap[v.DataTime]; !existOk { eDate := item.DataTime sValue := item.InputValue if sValue != "" { dataTime, err := time.Parse(utils.FormatDate, eDate) if err != nil { return err } timestamp := dataTime.UnixNano() / 1e6 timeStr := fmt.Sprintf("%d", timestamp) addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue) isAdd = true } } else { if existVal != item.InputValue { sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? ` sql = fmt.Sprintf(sql, dataTableName) err = tx.Exec(sql, item.InputValue, edbInfoId, v.DataTime).Error if err != nil { return err } } } } addExistMap[v.DataTime] = v.InputValue } if isAdd { addSql = strings.TrimRight(addSql, ",") addSql = utils.ReplaceDriverKeywords("", sql) err = tx.Exec(addSql, pars...).Error if err != nil { return err } } return }