package data_manage import ( "eta_gn/eta_task/global" "eta_gn/eta_task/utils" "fmt" "strconv" "strings" "time" ) func AddEdbDataManualBySql(sqlStr string) (err error) { //o := orm.NewOrm() //_, err = o.Raw(sqlStr).Exec() err = global.DEFAULT_DmSQL.Exec(sqlStr).Error return } func GetEdbDataManualMaxOrMinDate(edbCode string) (min_date, max_date string, err error) { //o := orm.NewOrm() sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM edb_data_manual WHERE edb_code=? ` //err = o.Raw(sql, edbCode).QueryRow(&min_date, &max_date) err = global.DEFAULT_DmSQL.Raw(sql, edbCode).First(&min_date, &max_date).Error return } type ManualEdbdata struct { TradeCode string `gorm:"column:TRADE_CODE;primaryKey"` // `orm:"column(TRADE_CODE);pk" description:"指标编码"` Dt string `orm:"column(DT)" description:"日期"` Close float64 `orm:"column(CLOSE)" description:"值"` ModifyTime time.Time `orm:"column(modify_time)" description:"修改时间"` } func GetEdbdataManualByTradeCode(condition string, pars []interface{}) (item []*ManualEdbdata, err error) { sql := ` SELECT * FROM edbdata WHERE 1=1 ` //o := orm.NewOrm() if condition != "" { sql += condition } sql += ` ORDER BY DT DESC ` //_, err = o.Raw(sql, pars).QueryRows(&item) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&item).Error return } func GetEdbDataManualByCodeAndDate(edbCode string, startDate string) (count int, err error) { //o := orm.NewOrm() sql := ` SELECT COUNT(1) AS count FROM edb_data_manual WHERE edb_code=? AND data_time=? ` //err = o.Raw(sql, edbCode, startDate).QueryRow(&count) err = global.DEFAULT_DmSQL.Raw(sql, edbCode, startDate).Scan(&count).Error return } func ModifyEdbDataManual(edbInfoId int64, dataTime, value string) (err error) { // o := orm.NewOrm() sql := ` UPDATE edb_data_manual SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? ` //_, err = o.Raw(sql, value, edbInfoId, dataTime).Exec() err = global.DEFAULT_DmSQL.Exec(sql, value, edbInfoId, dataTime).Error return } // 刷新手工指标数据 func RefreshManual(item *EdbInfoList) (err error) { //edbInfoId int, edbCode, startDate, endDate string //o := orm.NewOrm() //tx, err := o.Begin() tx := global.DEFAULT_DmSQL.Begin() if err != nil { return err } defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() edbInfoId := item.EdbInfoId edbCode := item.EdbCode if err != nil { return } edbInfoIdStr := strconv.Itoa(edbInfoId) //计算数据 var condition string var pars []interface{} if edbCode != "" { condition += " AND TRADE_CODE=? " pars = append(pars, edbCode) } //获取已存在的手工数据 manualDataList, err := GetEdbdataManualByTradeCode(condition, pars) var existCondition string var existPars []interface{} if edbCode != "" { existCondition += " AND edb_code=? " existPars = append(existPars, edbCode) } //获取ETA指标库已存在的所有数据 existDataList, err := GetEdbDataListAll(existCondition, existPars, utils.DATA_SOURCE_MANUAL, utils.DATA_SUB_SOURCE_EDB, 0) if err != nil { return err } existMap := make(map[string]*EdbInfoSearchData) for _, v := range existDataList { existMap[v.DataTime] = v } addSql := ` INSERT INTO edb_data_manual(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values ` var isAdd bool manualMap := make(map[string]*ManualEdbdata) for _, v := range manualDataList { item := v eDate := item.Dt sValue := utils.SubFloatToString(item.Close, 4) if findItem, ok := existMap[v.Dt]; !ok { nowStr := time.Now().Format(utils.FormatDateTime) dataTime, err := time.Parse(utils.FormatDate, eDate) if err != nil { return err } timestamp := dataTime.UnixNano() / 1e6 timeStr := fmt.Sprintf("%d", timestamp) addSql += "(" addSql += edbInfoIdStr + "," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + sValue + "," + "'" + nowStr + "'" + "," + "'" + nowStr + "'" + "," + "1" + "," + "'" + timeStr + "'" addSql += ")," isAdd = true } else { if item.Close != findItem.Value { err = ModifyEdbDataManual(int64(edbInfoId), eDate, sValue) if err != nil { return err } } } manualMap[v.Dt] = v } for _, v := range existDataList { if _, ok := manualMap[v.DataTime]; !ok { go DeleteEdbDataByIdAndSource(v.EdbDataId, utils.DATA_SOURCE_MANUAL, utils.DATA_SUB_SOURCE_EDB) } } if isAdd { addSql = strings.TrimRight(addSql, ",") //_, err = tx.Raw(addSql).Exec() err = tx.Exec(addSql).Error if err != nil { return err } } maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(utils.DATA_SOURCE_MANUAL, utils.DATA_SUB_SOURCE_EDB, edbCode) if err != nil { if err.Error() == utils.ErrNoRow() { err = nil } else { return err } } else { if maxAndMinItem != nil { err = ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem) if err != nil { return err } } } return }