package data_manage import ( sql2 "database/sql" "eta/eta_task/global" "eta/eta_task/utils" "github.com/rdlucklib/rdluck_tools/paging" "gorm.io/gorm" "time" ) type BaseFromIcpiIndex struct { BaseFromIcpiIndexId int `gorm:"column:base_from_icpi_index_id;primaryKey;autoIncrement"` BaseFromIcpiClassifyId int `description:"分类id"` IndexCode string `description:"指标编码"` IndexName string `description:"指标名称"` Frequency string `description:"频度"` StartDate time.Time `description:"开始日期"` EndDate time.Time `description:"结束日期"` CreateTime time.Time `description:"创建时间"` ModifyTime time.Time `description:"修改时间"` LatestValue float64 `description:"最新值"` } func GetBaseFromIcpiIndexMaxDate() (maxDate time.Time, err error) { o := global.DbMap[utils.DbNameIndex] sql := ` SELECT max(a.data_time) as max_date FROM base_from_icpi_data as a ` var timeNull sql2.NullTime err = o.Raw(sql).Scan(&timeNull).Error if err != nil { return } if timeNull.Valid { maxDate = timeNull.Time } return } type IcpiIndexResp struct { Ret int Msg string ErrMsg string ErrCode string Data []*BaseFromIcpiIndex } func GetBaseFromIcpiIndexAll(dateStr string) (list []*BaseFromIcpiIndex, err error) { o := global.DbMap[utils.DbNameIndex] sql := `SELECT * FROM base_from_icpi_index WHERE end_date>=?` err = o.Raw(sql, dateStr).Find(&list).Error return } func (obj *BaseFromIcpiIndex) AddBaseFromIcpiIndex(item *BaseFromIcpiIndex) (lastId int64, err error) { err = global.DbMap[utils.DbNameIndex].Create(item).Error return } func (obj *BaseFromIcpiIndex) InsertOrUpdateBaseFromIcpiIndex(item *BaseFromIcpiIndex) (lastId int64, err error) { saveItem := *item o := global.DbMap[utils.DbNameIndex] result := o.FirstOrCreate(item, "base_from_icpi_index_id = ?", item.BaseFromIcpiIndexId) if result.Error != nil { err = result.Error return } if result.RowsAffected == 0 { saveItem.BaseFromIcpiIndexId = 0 result = o.Model(item).Where("base_from_icpi_index_id = ?", item.BaseFromIcpiIndexId).Updates(saveItem) if result.Error != nil { err = result.Error return } } return int64(item.BaseFromIcpiIndexId), nil } type BaseFromIcpiClassify struct { BaseFromIcpiClassifyId int `gorm:"column:base_from_icpi_classify_id;primaryKey;autoIncrement"` ClassifyName string `description:"分类名称"` ClassifyNameEn string `description:"英文名称"` ParentId int `description:"上级id"` CreateTime string `description:"创建时间"` ModifyTime string `description:"修改时间"` } func (m *BaseFromIcpiClassify) AfterFind(db *gorm.DB) (err error) { m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime) m.ModifyTime = utils.GormDateStrToDateTimeStr(m.ModifyTime) return } type IcpiClassifyResp struct { Ret int Msg string ErrMsg string ErrCode string Data []*BaseFromIcpiClassify } func GetBaseFromIcpiClassifyAll() (list []*BaseFromIcpiClassify, err error) { o := global.DbMap[utils.DbNameIndex] sql := `SELECT * FROM base_from_icpi_classify ` err = o.Raw(sql).Find(&list).Error return } func AddBaseFromIcpiClassify(item *BaseFromIcpiClassify) (lastId int64, err error) { err = global.DbMap[utils.DbNameIndex].Create(item).Error return } func InsertOrUpdateBaseFromIcpiClassify(item *BaseFromIcpiClassify) (lastId int64, err error) { saveItem := *item o := global.DbMap[utils.DbNameIndex] result := o.FirstOrCreate(item, "base_from_icpi_classify_id = ?", item.BaseFromIcpiClassifyId) if result.Error != nil { err = result.Error return } if result.RowsAffected == 0 { saveItem.BaseFromIcpiClassifyId = 0 result = o.Model(item).Where("base_from_icpi_classify_id = ?", item.BaseFromIcpiClassifyId).Updates(saveItem) if result.Error != nil { err = result.Error return } } return int64(item.BaseFromIcpiClassifyId), nil } // GetBaseFromIcpiMaxDate 获取ICPI消费者指数最大数据 func GetBaseFromIcpiMaxDate() (maxDate time.Time, err error) { o := global.DbMap[utils.DbNameIndex] sql := ` SELECT max(a.create_time) as max_date FROM base_from_icpi_data as a ` var timeNull sql2.NullTime err = o.Raw(sql).Scan(&timeNull).Error if err != nil { return } if timeNull.Valid { maxDate = timeNull.Time } return } type BaseFromIcpiData struct { BaseFromIcpiDataId int `gorm:"column:base_from_icpi_data_id;primaryKey;autoIncrement"` BaseFromIcpiIndexId int `description:"指标id"` IndexCode string `description:"指标编码"` DataTime string `description:"日期"` Value string `description:"值"` CreateTime time.Time `description:"创建时间"` ModifyTime time.Time `description:"修改时间"` } // GetAllBaseFromIcpiDataList 获取ICPI消费者指数数据 func GetAllBaseFromIcpiDataList(startDate string) (list []*BaseFromIcpiData, err error) { o := global.DbMap[utils.DbNameIndex] sql := `SELECT * FROM base_from_icpi_data WHERE create_time>=? ORDER BY base_from_icpi_data_id ASC ` err = o.Raw(sql, startDate).Find(&list).Error return } type BaseFromIcpiDataResp struct { Ret int Msg string ErrMsg string ErrCode string Data BaseFromIcpiDataIndexAndDataResp } // BaseFromIcpiDataIndexAndDataResp 分页列表响应体 type BaseFromIcpiDataIndexAndDataResp struct { List []*BaseFromIcpiData Paging *paging.PagingItem `description:"分页数据"` } // MultiAddBaseFromIcpiDataIndex 批量添加数据 func MultiAddBaseFromIcpiDataIndex(items []*BaseFromIcpiData) (lastId int64, err error) { num := len(items) if num <= 0 { return } o := global.DbMap[utils.DbNameIndex] err = o.CreateInBatches(items, utils.MultiAddNum).Error return }