package data_manage import ( "fmt" "hongze/hongze_task/utils" "github.com/rdlucklib/rdluck_tools/orm" "strconv" "strings" "time" ) func AddEdbDataGlBySql(sqlStr string) (err error) { o := orm.NewOrm() o.Using("data") _, 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 ` o := orm.NewOrm() o.Using("gl") if condition != "" { sql += condition } sql += ` 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() o.Using("data") 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() o.Using("data") 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() o.Using("data") o.Begin() defer func() { if err != nil { o.Rollback() } else { o.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) } //获取已存在指标所有数据 existDataList := make([]*EdbDataBase, 0) dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_GL) 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 = o.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 = o.Raw(addSql).Exec() if err != nil { return err } } return }