package data_manage import ( "eta_gn/eta_api/global" "eta_gn/eta_api/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "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 } 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"` 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.* FROM edb_collect AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id WHERE 1=1 %s ORDER BY a.create_time DESC ) 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.*, a.edb_collect_classify_id AS collect_classify_id FROM edb_collect AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id WHERE 1=1 %s ORDER BY a.create_time DESC LIMIT ?,?`, condition) pars = append(pars, startSize, pageSize) err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error return } type CollectEdbInfoQuery struct { EdbInfo CollectClassifyId int `gorm:"column:collect_classify_id" description:"收藏分类ID"` } // 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"` CollectClassifyId int `description:"收藏分类ID"` } 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 item.CollectClassifyId = origin.CollectClassifyId return } // CollectEdbInfoListResp 收藏指标分页列表相应 type CollectEdbInfoListResp struct { Paging *paging.PagingItem List []*CollectEdbInfoItem }