package models import ( "errors" "eta_gn/eta_index_lib/global" "eta_gn/eta_index_lib/utils" "fmt" "strings" "time" ) // BaseFromBusinessData // @Description: 外部指标(商家系统)原始数据表 type BaseFromBusinessData struct { BusinessDataId int `orm:"column(business_data_id);pk" json:"business_data_id"` BaseFromBusinessIndexId int `json:"base_from_business_index_id"` // 指标id IndexCode string `json:"index_code"` // 指标编码 DataTime time.Time `json:"data_time"` // 数据日期 Value float64 `json:"value"` // 数据值 CreateTime time.Time `json:"create_time"` // 创建时间 ModifyTime time.Time `json:"modify_time"` // 修改时间 } // TableName // @Description: 获取表名 // @author: Roc // @receiver m // @datetime 2024-04-26 13:41:36 // @return string func (m *BaseFromBusinessData) TableName() string { return "base_from_business_data" } // CollectionName // @Description: 获取集合名称 // @author: Roc // @receiver m // @datetime 2024-04-26 13:41:36 // @return string func (m *BaseFromBusinessData) CollectionName() string { return "base_from_business_data" } // DataBaseName // @Description: 获取数据库名称 // @author: Roc // @receiver m // @datetime 2024-04-26 13:41:33 // @return string func (m *BaseFromBusinessData) DataBaseName() string { return utils.MgoDataDbName } type WhereParams struct { Condition string Pars []interface{} Order string `description:"排序字段"` } // GetAllDataList // @Description: 根据条件获取所有数据 // @author: Roc // @receiver m // @datetime 2024-07-01 17:18:57 // @param condition []string // @param pars []interface{} // @param order string // @return result []*BaseFromBusinessData // @return err error func (m *BaseFromBusinessData) GetAllDataList(condition []string, pars []interface{}, order string) (result []*BaseFromBusinessData, err error) { sql := `SELECT * FROM base_from_business_data ` if len(condition) > 0 { sql += ` WHERE ` + strings.Join(condition, " AND ") } if order != `` { sql += ` ORDER BY ` + order } err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&result).Error return } // GetLimitDataList // @Description: 根据条件获取指定数量数据列表 // @author: Roc // @receiver m // @datetime 2024-07-01 17:19:16 // @param condition []string // @param pars []interface{} // @param order string // @param size int64 // @return result []*BaseFromBusinessData // @return err error func (m *BaseFromBusinessData) GetLimitDataList(condition []string, pars []interface{}, order string, size int64) (result []*BaseFromBusinessData, err error) { sql := `SELECT * FROM base_from_business_data ` if len(condition) > 0 { sql += ` WHERE ` + strings.Join(condition, " AND ") } if order != `` { sql += ` ORDER BY ` + order } sql += fmt.Sprintf(` LIMIT %d`, size) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&result).Error return } // GetPageDataList // @Description: 根据条件获取分页数据列表 // @author: Roc // @receiver m // @datetime 2024-07-01 17:19:42 // @param condition []string // @param pars []interface{} // @param order string // @param startSize int64 // @param size int64 // @return result []*BaseFromBusinessData // @return err error func (m *BaseFromBusinessData) GetPageDataList(condition []string, pars []interface{}, order string, startSize, size int64) (result []*BaseFromBusinessData, err error) { sql := `SELECT * FROM base_from_business_data ` if len(condition) > 0 { sql += ` WHERE ` + strings.Join(condition, " AND ") } if order != `` { sql += ` ORDER BY ` + order } sql += fmt.Sprintf(` LIMIT %d,%d`, startSize, size) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&result).Error return } // GetCountDataList // @Description: 根据条件获取数据列表总数 // @author: Roc // @receiver m // @datetime 2024-07-01 17:19:50 // @param condition []string // @param pars []interface{} // @return count int64 // @return err error func (m *BaseFromBusinessData) GetCountDataList(condition []string, pars []interface{}) (count int64, err error) { //o := orm.NewOrm() sql := `SELECT COUNT(1) as count FROM base_from_business_data ` if len(condition) > 0 { sql += ` WHERE ` + strings.Join(condition, " AND ") } //err = o.Raw(sql, pars).QueryRow(&count) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&count).Error return } // InsertDataByColl // @Description: 写入单条数据(外部传入集合) // @author: Roc // @receiver m // @datetime 2024-04-26 14:22:18 // @param addData interface{} // @return err error func (m *BaseFromBusinessData) InsertDataByColl(addData interface{}) (err error) { err = global.DEFAULT_DmSQL.Create(addData).Error return } // BatchInsertData // @Description: 批量写入数据 // @author: Roc // @receiver m // @datetime 2024-04-26 14:22:18 // @param bulk int 每次请求保存的数据量 // @param dataList []*BaseFromBusinessData // @return err error func (m *BaseFromBusinessData) BatchInsertData(bulk int, dataList []*BaseFromBusinessData) (err error) { err = global.DEFAULT_DmSQL.CreateInBatches(dataList, bulk).Error return } // UpdateData // @Description: 单条数据修改 // @author: Roc // @receiver m // @datetime 2024-04-26 15:01:51 // @param whereParams interface{} // @param updateParams interface{} // @return err error func (m *BaseFromBusinessData) UpdateData(updateCols []string) (err error) { err = global.DEFAULT_DmSQL.Model(m).Select(updateCols).Updates(m).Error if err != nil { fmt.Println("UpdateDataByColl:Err:" + err.Error()) return } return } // HandleData // @Description: 数据处理 // @author: Roc // @receiver m // @datetime 2024-07-01 17:35:54 // @param addDataList []*BaseFromBusinessData // @param updateDataList []*BaseFromBusinessData // @return err error func (m *BaseFromBusinessData) HandleData(addDataList, updateDataList []*BaseFromBusinessData) (err error) { to := global.DEFAULT_DmSQL.Begin() if err != nil { return } defer func() { if err != nil { fmt.Println("BaseFromBusinessData HandleData,Err:" + err.Error()) _ = to.Rollback() } else { _ = to.Commit() } }() // 插入数据 if len(addDataList) > 0 { err = to.CreateInBatches(addDataList, 500).Error if err != nil { return } } // 修改 if len(updateDataList) > 0 { for _, v := range updateDataList { err = to.Model(v).Select("Value", "ModifyTime").Updates(v).Error if err != nil { fmt.Println("BaseFromBusinessData HandleData Update:Err:" + err.Error()) return } } } return } // GetEdbInfoMaxAndMinInfo // @Description: 获取当前指标的最大最小值 // @author: Roc // @receiver m // @datetime 2024-04-30 17:15:39 // @param whereParams interface{} // @return result EdbInfoMaxAndMinInfo // @return err error func (m *BaseFromBusinessData) GetEdbInfoMaxAndMinInfo(indexCode string) (result *EdbInfoMaxAndMinInfo, err error) { sql := `` sql = ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM base_from_business_data WHERE index_code = ? ` err = global.DEFAULT_DmSQL.Raw(sql, indexCode).First(&result).Error if err != nil { return } var latestValue float64 sql = ` SELECT value AS latest_value FROM base_from_business_data WHERE index_code = ? ORDER BY data_time DESC LIMIT 1 ` err = global.DEFAULT_DmSQL.Raw(sql, indexCode).First(&latestValue).Error result.LatestValue = latestValue return } // DelDataByCond // @Description: 根据条件删除多条数据 // @author: Roc // @receiver m // @datetime 2024-07-01 17:46:56 // @param condition []string // @param pars []interface{} // @return err error func (m *BaseFromBusinessData) DelDataByCond(condition []string, pars []interface{}) (err error) { if len(condition) <= 0 { err = errors.New("条件不能为空") return } sql := `DELETE FROM base_from_business_data ` sql += ` WHERE ` + strings.Join(condition, " AND ") err = global.DEFAULT_DmSQL.Exec(sql, pars...).Error return }