123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- package data_manage
- import (
- "database/sql"
- "eta/eta_api/global"
- "eta/eta_api/utils"
- "fmt"
- "time"
- )
- type BaseFromBaiinfo struct {
- BaseFromBaiinfoId int `orm:"column(base_from_baiinfo_id);pk"`
- Aid int
- Name string
- Interface string
- ApiUpdate int
- ApiUpdateType string
- ApiType int
- ApiTypeAll string
- Type1 string `orm:"column(type_1)"`
- Type2 string `orm:"column(type_2)"`
- Type3 string `orm:"column(type_3)"`
- ApiStartTime string
- ApiUpdateTime string
- StartTime string
- FinishTime string
- AuthModule string
- AuthLang string
- ApiLabel string
- Enable string
- EditPerson string
- EditTime string
- AuthDur int
- AuthDurType string
- }
- func AddBaseFromBaiinfo(item *BaseFromBaiinfo) (lastId int64, err error) {
- err = global.DbMap[utils.DbNameIndex].Create(item).Error
- return
- }
- func GetBaseFromBaiinfoList() (list []*BaseFromBaiinfo, err error) {
- sql := `SELECT * FROM base_from_baiinfo `
- err = global.DbMap[utils.DbNameIndex].Raw(sql).Find(&list).Error
- return
- }
- func GetBaseFromBaiinfoByBaiinfoCode(baiinfoCode string) (item *BaseFromBaiinfo, err error) {
- sql := `SELECT * FROM base_from_baiinfo WHERE interface=?`
- err = global.DbMap[utils.DbNameIndex].Raw(sql, baiinfoCode).First(&item).Error
- return
- }
- type BaseFromBaiinfoIndex struct {
- BaseFromBaiinfoIndexId int `orm:"column(base_from_baiinfo_index_id);pk"`
- ClassifyId int
- Interface string
- IndexCode string
- IndexName string
- Frequency string
- Unit string
- Sort int
- CreateTime time.Time
- ModifyTime time.Time
- }
- func AddBaseFromBaiinfoIndex(item *BaseFromBaiinfoIndex) (lastId int64, err error) {
- err = global.DbMap[utils.DbNameIndex].Create(item).Error
- lastId = int64(item.BaseFromBaiinfoIndexId)
- return
- }
- func GetBaseFromBaiinfoIndex() (list []*BaseFromBaiinfoIndex, err error) {
- sql := `SELECT * FROM base_from_baiinfo_index `
- err = global.DbMap[utils.DbNameIndex].Raw(sql).Find(&list).Error
- return
- }
- type BaseFromBaiinfoData struct {
- BaiinfoDataId int `orm:"column(baiinfo_data_id);pk"`
- BaseFromBaiinfoIndexId int
- IndexCode string
- DataTime string
- Value string
- CreateTime time.Time
- ModifyTime time.Time
- DataTimestamp int64
- }
- func AddBaseFromBaiinfoData(item *BaseFromBaiinfoData) (lastId int64, err error) {
- err = global.DbMap[utils.DbNameIndex].Create(item).Error
- return
- }
- func GetBaseFromBaiinfoDataAll() (list []*BaseFromBaiinfoData, err error) {
- sql := `SELECT * FROM base_from_baiinfo_data `
- err = global.DbMap[utils.DbNameIndex].Raw(sql).Find(&list).Error
- return
- }
- func ModifyBaseFromBaiinfoData(baiinfoDataId int, value string) (err error) {
- sql := `UPDATE base_from_baiinfo_data SET value=?,modify_time=NOW() WHERE baiinfo_data_id=? `
- err = global.DbMap[utils.DbNameIndex].Exec(sql, value, baiinfoDataId).Error
- return
- }
- func GetBaseFromBaiinfoIndexByBaiinfoCode(baiinfoCode string) (list []*BaseFromBaiinfoIndex, err error) {
- sql := ` SELECT * FROM base_from_baiinfo_index WHERE interface=? `
- err = global.DbMap[utils.DbNameIndex].Raw(sql, baiinfoCode).Find(&list).Error
- return
- }
- //func GetBaseFromBaiinfoMaxOrMinDate(indexCode string) (min_date, max_date string, err error) {
- // sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM base_from_baiinfo_data WHERE index_code=? `
- // err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).Scan(&min_date, &max_date).Error
- // return
- //}
- func ModifyBaseFromBaiinfoMinDateAndMaxDate(baseFromBaiinfoIndexId int, minDate, maxDate string) (err error) {
- sql := ` UPDATE base_from_baiinfo_index SET start_date=?,end_date=?,modify_time=NOW() WHERE base_from_baiinfo_index_id=? `
- err = global.DbMap[utils.DbNameIndex].Exec(sql, minDate, maxDate, baseFromBaiinfoIndexId).Error
- return
- }
- func GetBaseFromBaiinfoDataAllByIndexCode(indexCode string) (list []*BaseFromBaiinfoData, err error) {
- sql := `SELECT * FROM base_from_baiinfo_data WHERE index_code=? `
- err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).Find(&list).Error
- return
- }
- func GetBaseFromBaiinfoIndexByIndexCode(indexCode string) (list *BaseFromBaiinfoIndex, err error) {
- sql := ` SELECT * FROM base_from_baiinfo_index WHERE index_code=? `
- err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).First(&list).Error
- return
- }
- // GetBaseFromBaiinfoIndexCountByClassifyIds 获取分类下指标的个数
- func GetBaseFromBaiinfoIndexCountByClassifyIds(classifyIds []int) (count int, err error) {
- num := len(classifyIds)
- if num <= 0 {
- return
- }
- sqlStr := `SELECT COUNT(1) AS count FROM base_from_baiinfo_index WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
- var totalNull sql.NullInt64
- err = global.DbMap[utils.DbNameIndex].Raw(sqlStr, classifyIds).Scan(&totalNull).Error
- if !totalNull.Valid {
- count = 0
- } else {
- count = int(totalNull.Int64)
- }
- return
- }
- // GetBaseFromBaiinfoIndexByClassifyId 获取根据分类查询指标
- func GetBaseFromBaiinfoIndexByClassifyId(classifyId int) (list []*BaseFromBaiinfoIndex, err error) {
- sql := `SELECT * FROM base_from_baiinfo_index WHERE classify_id =? ORDER BY sort ASC, base_from_baiinfo_index_id ASC `
- err = global.DbMap[utils.DbNameIndex].Raw(sql, classifyId).Find(&list).Error
- return
- }
- // GetBaiinfoIndexAll 用于分类展示
- func GetBaiinfoIndexAll() (items []*BaseFromBaiinfoClassifyItems, err error) {
- sql := ` SELECT base_from_baiinfo_index_id, classify_id, index_name as classify_name, index_code as base_from_baiinfo_index_code
- FROM base_from_baiinfo_index ORDER BY sort ASC, base_from_baiinfo_index_id ASC `
- err = global.DbMap[utils.DbNameIndex].Raw(sql).Find(&items).Error
- return
- }
- // GetBaseFromBaiinfoIndexByIndexId 根据指标id获取指标信息
- func GetBaseFromBaiinfoIndexByIndexId(indexId int) (item *BaseFromBaiinfoIndex, err error) {
- sql := `SELECT * FROM base_from_baiinfo_index WHERE base_from_baiinfo_index_id=? `
- err = global.DbMap[utils.DbNameIndex].Raw(sql, indexId).First(&item).Error
- return
- }
- // GetBaseFromBaiinfoIndexMaxSortByClassifyId 根据指标id获取指标信息
- func GetBaseFromBaiinfoIndexMaxSortByClassifyId(classifyId int) (sort int, err error) {
- sqlStr := `SELECT max(sort) FROM base_from_baiinfo_index WHERE classify_id=? `
- var totalNull sql.NullInt64
- err = global.DbMap[utils.DbNameIndex].Raw(sqlStr, classifyId).Scan(&totalNull).Error
- if !totalNull.Valid {
- sort = 0
- } else {
- sort = int(totalNull.Int64)
- }
- return
- }
- // GetBaseFromBaiinfoIndexMinSortByClassifyId 获取最小不等于0的排序
- func GetBaseFromBaiinfoIndexMinSortByClassifyId(classifyId int) (sort int, err error) {
- sql := `SELECT min(sort) FROM base_from_baiinfo_index WHERE classify_id=? and sort <> 0 `
- err = global.DbMap[utils.DbNameIndex].Raw(sql, classifyId).Scan(&sort).Error
- return
- }
- type BaseFromBaiinfoIndexType struct {
- Type2 string `orm:"column(type_2)"`
- Type3 string `orm:"column(type_3)"`
- }
- // GetBaseFromBaiinfoIndexType 获取历史指标信息类型
- func GetBaseFromBaiinfoIndexType() (list []BaseFromBaiinfoIndexType, err error) {
- sql := `SELECT type_2, type_3 from base_from_baiinfo_index where type_2 <> "" GROUP BY type_2, type_3`
- err = global.DbMap[utils.DbNameIndex].Raw(sql).Find(&list).Error
- return
- }
- // MoveBaseFromBaiinfoIndex 移动指标分类
- func MoveBaseFromBaiinfoIndex(chartInfoId, classifyId int) (err error) {
- sql := ` UPDATE base_from_baiinfo_index
- SET
- classify_id = ?, modify_time=NOW()
- WHERE base_from_baiinfo_index_id = ?`
- err = global.DbMap[utils.DbNameIndex].Exec(sql, classifyId, chartInfoId).Error
- return
- }
- // UpdateBaseFromBaiinfoIndexByClassifyId 根据指标id更新排序
- func UpdateBaseFromBaiinfoIndexByClassifyId(classifyId, nowSort, prevIndexInfoId int, updateSort string) (err error) {
- sql := ` update base_from_baiinfo_index set sort = ` + updateSort + ` WHERE classify_id=? AND `
- if prevIndexInfoId > 0 {
- sql += ` ( sort > ? or (base_from_baiinfo_index_id > ` + fmt.Sprint(prevIndexInfoId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
- }
- err = global.DbMap[utils.DbNameIndex].Exec(sql, classifyId, nowSort).Error
- return
- }
- // MoveUpBaiinfoIndexBySort 往上移动
- func MoveUpBaiinfoIndexBySort(classifyId, nextSort, currentSort int) (err error) {
- sql := `update base_from_baiinfo_index set sort = sort + 1 where classify_id=? and sort >= ? and sort< ?`
- err = global.DbMap[utils.DbNameIndex].Exec(sql, classifyId, nextSort, currentSort).Error
- return
- }
- // MoveDownBaiinfoIndexBySort 往下移动
- func MoveDownBaiinfoIndexBySort(classifyId, prevSort, currentSort int) (err error) {
- sql := `update base_from_baiinfo_index set sort = sort - 1 where classify_id=? and sort <= ? and sort> ? `
- err = global.DbMap[utils.DbNameIndex].Exec(sql, classifyId, prevSort, currentSort).Error
- return
- }
- // GetFirstBaseFromBaiinfoIndexByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
- func GetFirstBaseFromBaiinfoIndexByClassifyId(classifyId int) (item *BaseFromBaiinfoIndex, err error) {
- sql := ` SELECT * FROM base_from_baiinfo_index WHERE classify_id=? order by sort asc,base_from_baiinfo_index_id asc limit 1`
- err = global.DbMap[utils.DbNameIndex].Raw(sql, classifyId).First(&item).Error
- return
- }
- // Update 更新Baiinfo指标基础信息
- func (item *BaseFromBaiinfoIndex) Update(cols []string) (err error) {
- err = global.DbMap[utils.DbNameIndex].Select(cols).Updates(item).Error
- return
- }
- // DeleteBaseFromBaiinfoIndexById 根据指标id删除对应的指标、指标数据
- func DeleteBaseFromBaiinfoIndexById(baiinfoIndexId int) (err error) {
- tx := global.DbMap[utils.DbNameIndex].Begin()
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- //删除指标
- sql := `DELETE FROM base_from_baiinfo_index WHERE base_from_baiinfo_index_id = ? `
- err = tx.Exec(sql, baiinfoIndexId).Error
- if err != nil {
- return
- }
- //删除指标数据
- sql = `DELETE FROM base_from_baiinfo_data WHERE base_from_baiinfo_index_id = ? `
- err = tx.Exec(sql, baiinfoIndexId).Error
- if err != nil {
- return
- }
- return
- }
- // EditBaiinfoIndexInfoResp 新增指标的返回
- type EditBaiinfoIndexInfoResp struct {
- BaseFromBaiinfoIndexId int `description:"指标ID"`
- IndexCode string `description:"指标code"`
- }
- type BaiinfoSingleDataResp struct {
- BaseFromBaiinfoIndexId int
- ClassifyId int
- Interface string
- Name string
- IndexCode string
- IndexName string
- Frequency string
- Unit string
- ApiStartTime string
- ApiUpdateTime string
- StartTime string
- FinishTime string
- CreateTime string
- ModifyTime string
- Data []*BaiinfoIndexData
- }
|