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 } 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 }