123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- package data_manage
- import (
- "eta_gn/eta_api/global"
- "eta_gn/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "strconv"
- "strings"
- "time"
- )
- // EdbCollect 指标收藏
- type EdbCollect struct {
- EdbCollectId int `gorm:"primaryKey;autoIncrement;column:edb_collect_id;type:int(10) unsigned;not null"`
- EdbCollectClassifyId int `gorm:"index:idx_classify_id;column:edb_collect_classify_id;type:int(10) unsigned;not null;default:0"` // 指标收藏分类ID
- EdbInfoId int `gorm:"column:edb_info_id;type:int(10) unsigned;not null;default:0"` // 指标ID
- EdbCode string `gorm:"column:edb_code;type:varchar(255);not null;default:''"` // 指标编码
- SysUserId int `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0"` // 创建人ID
- SysRealName string `gorm:"column:sys_real_name;type:int(10) unsigned;not null;default:0"` // 创建人姓名
- Sort int `gorm:"column:sort;type:int(10);default:0"` // 排序
- CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间
- ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 更新时间
- }
- func (m *EdbCollect) TableName() string {
- return "edb_collect"
- }
- type EdbCollectCols struct {
- PrimaryId string
- EdbCollectClassifyId string
- EdbInfoId string
- EdbCode string
- SysUserId string
- SysRealName string
- Sort string
- CreateTime string
- ModifyTime string
- }
- func (m *EdbCollect) Cols() EdbCollectCols {
- return EdbCollectCols{
- PrimaryId: "edb_collect_id",
- EdbCollectClassifyId: "edb_collect_classify_id",
- EdbInfoId: "edb_info_id",
- EdbCode: "edb_code",
- SysUserId: "sys_user_id",
- SysRealName: "sys_real_name",
- Sort: "sort",
- CreateTime: "create_time",
- ModifyTime: "modify_time",
- }
- }
- func (m *EdbCollect) Create() (err error) {
- err = global.DmSQL["data"].Create(m).Error
- return
- }
- func (m *EdbCollect) CreateMulti(items []*EdbCollect) (err error) {
- if len(items) == 0 {
- return
- }
- err = global.DmSQL["data"].CreateInBatches(items, utils.MultiAddNum).Error
- return
- }
- func (m *EdbCollect) Update(cols []string) (err error) {
- err = global.DmSQL["data"].Select(cols).Updates(m).Error
- return
- }
- func (m *EdbCollect) Remove() (err error) {
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
- err = global.DmSQL["data"].Exec(sql, m.EdbCollectId).Error
- return
- }
- func (m *EdbCollect) MultiRemove(ids []int) (err error) {
- if len(ids) == 0 {
- return
- }
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
- err = global.DmSQL["data"].Exec(sql, ids).Error
- return
- }
- func (m *EdbCollect) RemoveByCondition(condition string, pars []interface{}) (err error) {
- if condition == "" {
- return
- }
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
- err = global.DmSQL["data"].Exec(sql, pars...).Error
- return
- }
- // RemoveAndCreateMulti
- // @Description: 清除原有配置并新增收藏
- // @receiver m
- // @param delCondition
- // @param delPars
- // @param items
- // @return err
- func (m *EdbCollect) RemoveAndCreateMulti(delCondition string, delPars []interface{}, items []*EdbCollect) (err error) {
- to := global.DmSQL["data"].Begin()
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- // 先删除
- {
- if delCondition == "" {
- return
- }
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), delCondition)
- err = to.Exec(sql, delPars...).Error
- if err != nil {
- return
- }
- }
- if len(items) == 0 {
- return
- }
- err = to.CreateInBatches(items, utils.MultiAddNum).Error
- return
- }
- func (m *EdbCollect) GetItemById(id int) (item *EdbCollect, err error) {
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
- err = global.DmSQL["data"].Raw(sql, id).First(&item).Error
- return
- }
- func (m *EdbCollect) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *EdbCollect, err error) {
- order := ``
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
- err = global.DmSQL["data"].Raw(sql, pars...).First(&item).Error
- return
- }
- func (m *EdbCollect) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
- sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
- err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
- return
- }
- func (m *EdbCollect) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbCollect, err error) {
- fields := strings.Join(fieldArr, ",")
- if len(fieldArr) == 0 {
- fields = `*`
- }
- order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
- err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
- return
- }
- func (m *EdbCollect) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EdbCollect, err error) {
- fields := strings.Join(fieldArr, ",")
- if len(fieldArr) == 0 {
- fields = `*`
- }
- order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
- pars = append(pars, startSize, pageSize)
- err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
- return
- }
- // GetCollectEdbInfoByClassifyId 获取分类下收藏的指标信息
- func GetCollectEdbInfoByClassifyId(classifyId int) (items []*EdbInfo, err error) {
- sql := `SELECT b.* FROM edb_collect AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id WHERE a.edb_collect_classify_id = ? ORDER BY a.sort ASC`
- err = global.DmSQL["data"].Raw(sql, classifyId).Find(&items).Error
- return
- }
- // EdbCollectReq 加入/取消收藏
- type EdbCollectReq struct {
- ClassifyId int `description:"分类ID"`
- ClassifyIdList []int `description:"分类ID列表"`
- EdbInfoId int `description:"指标ID"`
- }
- // GetCollectEdbInfoCount 获取收藏的指标信息总数
- func GetCollectEdbInfoCount(condition string, pars []interface{}) (total int64, err error) {
- sql := fmt.Sprintf(`SELECT COUNT(1) AS ct FROM (
- SELECT b.edb_info_id FROM edb_collect AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
- WHERE 1=1 %s
- GROUP BY b.edb_info_id
- ) AS sub`, condition)
- err = global.DmSQL["data"].Raw(sql, pars...).Scan(&total).Error
- return
- }
- // GetCollectEdbInfoPageList 获取收藏的指标信息列表-分页
- func GetCollectEdbInfoPageList(condition string, pars []interface{}, startSize, pageSize int) (list []*CollectEdbInfoQuery, err error) {
- sql := fmt.Sprintf(`SELECT b."edb_info_id",
- WM_CONCAT(DISTINCT a."edb_collect_classify_id") AS "collect_classify_id",
- MAX(a.create_time) as collect_time,
- MAX(b."edb_code") AS "edb_code",
- MAX(b."edb_name") "edb_name",
- MAX(b."edb_info_type") "edb_info_type",
- MAX(b."edb_type") "edb_type",
- MAX(b."source") "source",
- MAX(b."source_name") "source_name",
- MAX(b."frequency") "frequency",
- MAX(b."unit") "unit",
- MAX(b."classify_id") "classify_id",
- MAX(b."create_time") "create_time",
- MAX(b."unique_code") "unique_code",
- MAX(b."chart_image") "chart_image",
- MAX(b."modify_time") "modify_time",
- MAX(a."sort") AS "sort"
- FROM edb_collect AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
- WHERE 1=1 %s
- GROUP BY b.edb_info_id
- ORDER BY collect_time DESC LIMIT ?,?`, condition)
- pars = append(pars, startSize, pageSize)
- err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
- return
- }
- // GetUserAllCollectEdbInfoIdList
- // @Description: 获取用户所有收藏的指标id列表
- // @author: Roc
- // @datetime 2024-11-28 15:27:52
- // @param userId int
- // @return list []int
- // @return err error
- func GetUserAllCollectEdbInfoIdList(userId int) (list []int, err error) {
- sql := `SELECT edb_info_id FROM edb_collect WHERE 1=1 AND sys_user_id = ? GROUP BY edb_info_id`
- err = global.DmSQL["data"].Raw(sql, userId).Scan(&list).Error
- return
- }
- type CollectEdbInfoQuery struct {
- EdbInfo
- CollectClassifyIdStr string `gorm:"column:collect_classify_id" description:"收藏分类ID"`
- CollectTime time.Time `gorm:"column:collect_time" description:"收藏时间"`
- }
- // CollectEdbInfoItem 收藏列表指标信息
- type CollectEdbInfoItem struct {
- EdbInfoId int `description:"指标ID"`
- EdbInfoType int `description:"指标类型:0-普通指标; 1-预测指标"`
- EdbType int `description:"指标类型:1-基础指标; 2-计算指标"`
- Source int `description:"来源ID"`
- SourceName string `description:"来源名称"`
- EdbCode string `description:"指标编码"`
- EdbName string `description:"指标名称"`
- Frequency string `description:"频率"`
- Unit string `description:"单位"`
- UniqueCode string `description:"唯一编码"`
- ChartImage string `description:"图表图片"`
- ClassifyId int `description:"指标分类ID"`
- CollectClassifyIdList []int `description:"收藏分类ID列表"`
- CollectTime string `description:"收藏时间"`
- CreateTime string `description:"创建时间"`
- Sort int `description:"排序"`
- }
- func FormatEdbInfo2CollectItem(origin *CollectEdbInfoQuery) (item *CollectEdbInfoItem) {
- item = new(CollectEdbInfoItem)
- item.EdbInfoId = origin.EdbInfoId
- item.EdbInfoType = origin.EdbInfoType
- item.EdbType = origin.EdbType
- item.Source = origin.Source
- item.SourceName = origin.SourceName
- item.EdbCode = origin.EdbCode
- item.EdbName = origin.EdbName
- item.Frequency = origin.Frequency
- item.Unit = origin.Unit
- item.UniqueCode = origin.UniqueCode
- item.ChartImage = origin.ChartImage
- item.ClassifyId = origin.ClassifyId
- collectClassifyIdList := make([]int, 0)
- if origin.CollectClassifyIdStr != `` {
- collectClassifyIdStrList := strings.Split(origin.CollectClassifyIdStr, ",")
- for _, v := range collectClassifyIdStrList {
- collectClassifyId, tmpErr := strconv.Atoi(v)
- if tmpErr == nil {
- collectClassifyIdList = append(collectClassifyIdList, collectClassifyId)
- }
- }
- }
- item.CollectClassifyIdList = collectClassifyIdList
- item.CollectTime = origin.CollectTime.Format(utils.FormatDateTime)
- item.CreateTime = origin.CreateTime.Format(utils.FormatDateTime)
- item.Sort = origin.Sort
- return
- }
- // CollectEdbInfoListResp 收藏指标分页列表相应
- type CollectEdbInfoListResp struct {
- Paging *paging.PagingItem
- List []*CollectEdbInfoItem
- }
- // EdbCollectMoveReq 移动收藏
- type EdbCollectMoveReq struct {
- EdbInfoId int `description:"收藏的指标ID(分类下指标ID唯一)"`
- PrevEdbInfoId int `description:"前一个收藏的指标ID"`
- NextEdbInfoId int `description:"后一个收藏的指标ID"`
- ClassifyId int `description:"当前分类ID"`
- }
- func GetEdbCollectSort(adminId, classifyId, sort int) (item *EdbCollect, err error) {
- sql := ` SELECT * FROM edb_collect WHERE 1=1 AND sys_user_id = ? AND edb_collect_classify_id = ? `
- if sort == 1 {
- sql += ` ORDER BY sort DESC, edb_collect_id ASC LIMIT 1 `
- } else {
- sql += ` ORDER BY sort ASC, edb_collect_id DESC LIMIT 1 `
- }
- err = global.DmSQL["data"].Raw(sql, adminId, classifyId).First(&item).Error
- return
- }
- func GetEdbCollectByEdbInfoId(adminId, edbInfoId, classifyId int) (item *EdbCollect, err error) {
- sql := `SELECT * FROM edb_collect WHERE sys_user_id = ? AND edb_info_id = ? AND edb_collect_classify_id=? `
- err = global.DmSQL["data"].Raw(sql, adminId, edbInfoId, classifyId).First(&item).Error
- return
- }
- func UpdateEdbCollectSortByClassifyId(classifyId, nowSort int, prevMyChartClassifyMappingId int, updateSort string) (err error) {
- sql := ` update edb_collect set sort = ` + updateSort + ` WHERE edb_collect_classify_id = ? `
- if prevMyChartClassifyMappingId > 0 {
- sql += ` AND ( sort > ? or ( edb_collect_id < ? and sort=? )) `
- }
- err = global.DmSQL["data"].Exec(sql, classifyId, nowSort, prevMyChartClassifyMappingId, nowSort).Error
- return
- }
- func UpdateEdbCollectMove(sort, adminId, edbInfoId, myChartClassifyId int) (err error) {
- sql := ` UPDATE edb_collect SET sort = ?,modify_time=NOW() WHERE sys_user_id=? AND edb_info_id=? AND edb_collect_classify_id=? `
- err = global.DmSQL["data"].Exec(sql, sort, adminId, edbInfoId, myChartClassifyId).Error
- return
- }
- // EdbCollectItem 指标收藏信息
- type EdbCollectItem struct {
- EdbCollectId int `gorm:"primaryKey;autoIncrement;column:edb_collect_id;type:int(10) unsigned;not null"`
- EdbCollectClassifyId int `gorm:"index:idx_classify_id;column:edb_collect_classify_id;type:int(10) unsigned;not null;default:0"` // 指标收藏分类ID
- EdbInfoId int `gorm:"column:edb_info_id;type:int(10) unsigned;not null;default:0"` // 指标ID
- EdbCode string `gorm:"column:edb_code;type:varchar(255);not null;default:''"` // 指标编码
- SysUserId int `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0"` // 创建人ID
- SysRealName string `gorm:"column:sys_real_name;type:int(10) unsigned;not null;default:0"` // 创建人姓名
- ClassifyName string `gorm:"column:classify_name;type:varchar(255);not null;default:''"` // 分类名称
- Sort int `gorm:"column:sort;type:int(10);default:0"` // 排序
- CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间
- ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 更新时间
- }
- func (m *EdbCollect) GetItemsByUserIdAndEdbInfoIdList(userId int, edbInfoIdList []int) (items []*EdbCollectItem, err error) {
- if len(edbInfoIdList) <= 0 {
- return
- }
- sql := `SELECT a.*,b.classify_name FROM edb_collect AS a
- JOIN edb_collect_classify b on a.edb_collect_classify_id = b.edb_collect_classify_id
- WHERE a.sys_user_id = ? AND a.edb_info_id in (?) ORDER BY a.edb_collect_id DESC `
- err = global.DmSQL["data"].Raw(sql, userId, edbInfoIdList).Find(&items).Error
- return
- }
|