123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- package data_manage
- import (
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "hongze/hongze_task/utils"
- "strconv"
- "strings"
- "time"
- )
- func AddEdbDataManualBySql(sqlStr string) (err error) {
- o := orm.NewOrmUsingDB("data")
- _, err = o.Raw(sqlStr).Exec()
- return
- }
- func GetEdbDataManualMaxOrMinDate(edbCode string) (min_date, max_date string, err error) {
- o := orm.NewOrmUsingDB("data")
- 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)
- return
- }
- type ManualEdbdata struct {
- TradeCode string `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.NewOrmUsingDB("data")
- if condition != "" {
- sql += condition
- }
- sql += ` ORDER BY DT DESC `
- _, err = o.Raw(sql, pars).QueryRows(&item)
- return
- }
- func GetEdbDataManualByCodeAndDate(edbCode string, startDate string) (count int, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT COUNT(1) AS count FROM edb_data_manual WHERE edb_code=? AND data_time=? `
- err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
- return
- }
- func ModifyEdbDataManual(edbInfoId int64, dataTime, value string) (err error) {
- o := orm.NewOrmUsingDB("data")
- 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()
- return
- }
- //刷新手工指标数据
- func RefreshManual(item *EdbInfoList) (err error) {
- //edbInfoId int, edbCode, startDate, endDate string
- o := orm.NewOrmUsingDB("data")
- tx,err:=o.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, 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)
- }
- }
- if isAdd {
- addSql = strings.TrimRight(addSql, ",")
- _, err = tx.Raw(addSql).Exec()
- if err != nil {
- return err
- }
- }
- maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(utils.DATA_SOURCE_MANUAL, 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
- }
|