123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package data_manage
- import (
- "eta/eta_task/utils"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "strconv"
- "strings"
- "time"
- )
- func AddEdbDataGlBySql(sqlStr string) (err error) {
- o := orm.NewOrm()
- _, err = o.Raw(sqlStr).Exec()
- return
- }
- type GlData struct {
- InputValue string `orm:"column(DATA_VALUE)" description:"日期"`
- DataTime string `orm:"column(DATA_DATE)" description:"值"`
- }
- 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 := orm.NewOrm()
- //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 := orm.NewOrmUsingDB("gl")
- 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).QueryRows(&item)
- return
- }
- func GetEdbDataGlByCodeAndDate(edbCode string, startDate string) (count int, err error) {
- o := orm.NewOrm()
- sql := ` SELECT COUNT(1) AS count FROM edb_data_gl WHERE edb_code=? AND data_time=? `
- err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
- return
- }
- func ModifyEdbDataGl(edbInfoId int64, dataTime, value string) (err error) {
- o := orm.NewOrm()
- sql := ` UPDATE edb_data_gl SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
- _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
- return
- }
- // 刷新钢联指标数据
- func RefreshEdbDataByGl(edbInfoId int, edbCode, startDate, endDate string) (err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return err
- }
- 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).QueryRows(&existDataList)
- 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.Raw(sql, item.InputValue, edbInfoId, v.DataTime).Exec()
- if err != nil {
- return err
- }
- }
- }
- }
- addExistMap[v.DataTime] = v.InputValue
- }
- if isAdd {
- addSql = strings.TrimRight(addSql, ",")
- _, err = tx.Raw(addSql).Exec()
- if err != nil {
- return err
- }
- }
- return
- }
|