123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- 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 BaseFromUsdaFasIndex struct {
- BaseFromUsdaFasIndexId int `gorm:"column:base_from_usda_fas_index_id;primaryKey;autoIncrement"`
- ClassifyId int
- IndexCode string
- IndexName string
- Frequency string
- Unit string
- Sort int
- StartDate string `description:"开始日期"`
- EndDate string `description:"结束日期"`
- EndValue float64
- Country string `description:"国家"`
- Commodity string `description:"属性"`
- CreateTime time.Time
- ModifyTime time.Time
- }
- func (m *BaseFromUsdaFasIndex) AfterFind(db *gorm.DB) (err error) {
- m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
- m.EndDate = utils.GormDateStrToDateStr(m.EndDate)
- return
- }
- // BaseFromUsdaFasClassify UsdaFas原始数据分类表
- type BaseFromUsdaFasClassify struct {
- ClassifyId int `gorm:"column:classify_id;primaryKey;autoIncrement"`
- ClassifyName string `description:"分类名称"`
- ParentId int `description:"父级id"`
- SysUserId int `description:"创建人id"`
- SysUserRealName string `description:"创建人姓名"`
- Level int `description:"层级"`
- Sort int `description:"排序字段,越小越靠前,默认值:10"`
- ModifyTime time.Time `description:"修改时间"`
- CreateTime time.Time `description:"创建时间"`
- }
- func GetBaseFromUsdaFasIndexMaxDate() (maxDate time.Time, err error) {
- o := global.DbMap[utils.DbNameIndex]
- sql := ` SELECT max(a.modify_time) as max_date FROM base_from_usda_fas_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 UsdaFasIndexResp struct {
- Ret int
- Msg string
- ErrMsg string
- ErrCode string
- Data []*BaseFromUsdaFasIndex
- }
- func GetBaseFromUsdaFasIndexAll(dateStr string) (list []*BaseFromUsdaFasIndex, err error) {
- o := global.DbMap[utils.DbNameIndex]
- sql := `SELECT * FROM base_from_usda_fas_index WHERE end_date>=?`
- err = o.Raw(sql, dateStr).Find(&list).Error
- return
- }
- func (obj *BaseFromUsdaFasIndex) AddBaseFromUsdaFasIndex(item *BaseFromUsdaFasIndex) (lastId int64, err error) {
- err = global.DbMap[utils.DbNameIndex].Create(item).Error
- if err != nil {
- return
- }
- lastId = int64(item.BaseFromUsdaFasIndexId)
- return
- }
- func (obj *BaseFromUsdaFasIndex) InsertOrUpdateBaseFromUsdaFasIndex(item *BaseFromUsdaFasIndex) (lastId int64, err error) {
- saveItem := *item
- o := global.DbMap[utils.DbNameIndex]
- //lastId, err = o.InsertOrUpdate(item)
- result := o.FirstOrCreate(item, "base_from_usda_fas_index_id = ?", item.BaseFromUsdaFasIndexId)
- if result.Error != nil {
- err = result.Error
- return
- }
- if result.RowsAffected == 0 {
- saveItem.BaseFromUsdaFasIndexId = 0
- result = o.Model(item).Where("base_from_usda_fas_index_id = ?", item.BaseFromUsdaFasIndexId).Updates(saveItem)
- if result.Error != nil {
- err = result.Error
- return
- }
- }
- return int64(item.BaseFromUsdaFasIndexId), nil
- }
- type UsdaFasClassifyResp struct {
- Ret int
- Msg string
- ErrMsg string
- ErrCode string
- Data []*BaseFromUsdaFasClassify
- }
- func GetBaseFromUsdaFasClassifyAll() (list []*BaseFromUsdaFasClassify, err error) {
- o := global.DbMap[utils.DbNameIndex]
- sql := `SELECT * FROM base_from_usda_fas_classify `
- err = o.Raw(sql).Find(&list).Error
- return
- }
- func AddBaseFromUsdaFasClassify(item *BaseFromUsdaFasClassify) (lastId int64, err error) {
- err = global.DbMap[utils.DbNameIndex].Create(item).Error
- if err != nil {
- return
- }
- lastId = int64(item.ClassifyId)
- return
- }
- func InsertOrUpdateBaseFromUsdaFasClassify(item *BaseFromUsdaFasClassify) (lastId int64, err error) {
- saveItem := *item
- o := global.DbMap[utils.DbNameIndex]
- //lastId, err = o.InsertOrUpdate(item)
- result := o.FirstOrCreate(item, "classify_id = ?", item.ClassifyId)
- if result.Error != nil {
- err = result.Error
- return
- }
- if result.RowsAffected == 0 {
- saveItem.ClassifyId = 0
- result = o.Model(item).Where("classify_id = ?", item.ClassifyId).Updates(saveItem)
- if result.Error != nil {
- err = result.Error
- return
- }
- }
- return int64(item.ClassifyId), nil
- }
- // GetBaseFromUsdaFasMaxDate 获取UsdaFas消费者指数最大数据
- func GetBaseFromUsdaFasMaxDate() (maxDate time.Time, err error) {
- o := global.DbMap[utils.DbNameIndex]
- sql := ` SELECT max(a.modify_time) as max_date FROM base_from_usda_fas_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 BaseFromUsdaFasData struct {
- BaseFromUsdaFasDataId int `gorm:"column:base_from_usda_fas_data_id;primaryKey;autoIncrement"`
- BaseFromUsdaFasIndexId int `description:"指标id"`
- IndexCode string `description:"指标编码"`
- DataTime string `description:"日期"`
- Value string `description:"值"`
- CreateTime time.Time `description:"创建时间"`
- ModifyTime time.Time `description:"修改时间"`
- }
- func (m *BaseFromUsdaFasData) AfterFind(db *gorm.DB) (err error) {
- m.DataTime = utils.GormDateStrToDateStr(m.DataTime)
- return
- }
- // GetAllBaseFromUsdaFasDataList 获取UsdaFas消费者指数数据
- func GetAllBaseFromUsdaFasDataList(startDate string) (list []*BaseFromUsdaFasData, err error) {
- o := global.DbMap[utils.DbNameIndex]
- sql := `SELECT * FROM base_from_usda_fas_data WHERE modify_time>=? ORDER BY base_from_usda_fas_data_id ASC `
- err = o.Raw(sql, startDate).Find(&list).Error
- return
- }
- type BaseFromUsdaFasDataResp struct {
- Ret int
- Msg string
- ErrMsg string
- ErrCode string
- Data BaseFromUsdaFasDataIndexAndDataResp
- }
- // BaseFromUsdaFasDataIndexAndDataResp 分页列表响应体
- type BaseFromUsdaFasDataIndexAndDataResp struct {
- List []*BaseFromUsdaFasData
- Paging *paging.PagingItem `description:"分页数据"`
- }
- // MultiAddBaseFromUsdaFasDataIndex 批量添加数据
- func MultiAddBaseFromUsdaFasDataIndex(items []*BaseFromUsdaFasData) (lastId int64, err error) {
- num := len(items)
- if num <= 0 {
- return
- }
- o := global.DbMap[utils.DbNameIndex]
- err = o.CreateInBatches(items, utils.MultiAddNum).Error
- return
- }
|