package data_manage import ( sql2 "database/sql" "eta_gn/eta_api/global" "eta_gn/eta_api/utils" "fmt" "time" ) // EdbInfoShare // @Description: 指标分享表 type EdbInfoShare struct { EdbInfoShareId int `gorm:"primaryKey" ` EdbInfoId int `description:"指标id"` SysUserId int `description:"sys_user_id"` ShareType int8 `description:"分享类型,1:仅查看;2:可编辑"` CreateTime time.Time `description:"创建时间"` } // TableName // @Description: 表名 // @author: Roc // @receiver m // @datetime 2024-11-27 14:04:14 // @return string func (m EdbInfoShare) TableName() string { return `edb_info_share` } // GetListByEdbInfoId // @Description: 根据指标id获取分享用户列表 // @author: Roc // @receiver m // @datetime 2024-11-27 15:39:17 // @param edbInfoId int // @return items []*EdbInfoShare // @return err error func (m EdbInfoShare) GetListByEdbInfoId(edbInfoId int) (items []*EdbInfoShare, err error) { sql := ` SELECT * FROM edb_info_share WHERE edb_info_id = ? ORDER BY edb_info_share_id ASC ` err = global.DmSQL["data"].Raw(sql, edbInfoId).Find(&items).Error return } // SaveEdbInfoShare // @Description: 保存配置 // @author: Roc // @receiver m // @datetime 2024-11-27 15:54:07 // @param edbInfoIdList []int // @param userIdList []int // @param shareType int8 // @return err error func (m EdbInfoShare) SaveEdbInfoShare(edbInfoIdList, userIdList []int, shareType int8) (err error) { // TODO 保存之前,得先做关联校验 addList := make([]*EdbInfoShare, 0) for _, edbInfoId := range edbInfoIdList { for _, userId := range userIdList { addList = append(addList, &EdbInfoShare{ EdbInfoShareId: 0, EdbInfoId: edbInfoId, SysUserId: userId, ShareType: shareType, CreateTime: time.Now(), }) } } tx := global.DmSQL["data"].Begin() defer func() { if err != nil { _ = tx.Rollback() return } _ = tx.Commit() }() // 先删除原来的配置,新增最新的配置 sql := fmt.Sprintf("DELETE FROM %s WHERE edb_info_id IN (?) AND share_type = ? ", m.TableName()) err = tx.Exec(sql, edbInfoIdList, shareType).Error if err != nil { return } // 批量添加新的配置 if len(addList) > 0 { err = tx.CreateInBatches(addList, utils.MultiAddNum).Error if err != nil { return } } return } // ShareEdbInfoQuery // @Description: 共享指标 type ShareEdbInfoQuery struct { EdbInfo ShareType string `gorm:"column:share_type" description:"共享类型"` ShareTime time.Time `gorm:"column:share_time" description:"设置共享时间"` } // GetAllSharedEdbInfoListByFromUserId // @Description: 获取所有我共享出去的指标列表 // @author: Roc // @receiver m // @datetime 2024-12-03 09:56:41 // @param sysUserId int // @return list []*ShareEdbInfoQuery // @return err error func (m EdbInfoShare) GetAllSharedEdbInfoListByFromUserId(sysUserId int) (list []*ShareEdbInfoQuery, err error) { sql := `SELECT b."edb_info_id", MAX(a.share_type) AS share_type, MAX(a.create_time) as share_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(b."start_date") "start_date", MAX(b."sort") AS "sort" FROM edb_info_share AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id WHERE 1=1 AND b.sys_user_id = ? GROUP BY b.edb_info_id ORDER BY share_time DESC` err = global.DmSQL["data"].Raw(sql, sysUserId).Scan(&list).Error return } // GetAllSharedEdbInfoListByReceivedUserId // @Description: 获取所有我收到的共享的指标列表 // @author: Roc // @receiver m // @datetime 2024-12-03 09:56:41 // @param sysUserId int // @return list []*ShareEdbInfoQuery // @return err error func (m EdbInfoShare) GetAllSharedEdbInfoListByReceivedUserId(sysUserId int) (list []*ShareEdbInfoQuery, err error) { sql := `SELECT b."edb_info_id", MAX(a.share_type) AS share_type, MAX(a.create_time) as share_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."latest_date") "latest_date", MAX(b."latest_value") "latest_value", MAX(b."sys_user_id") "sys_user_id", MAX(b."sys_user_real_name") "sys_user_real_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(b."start_date") "start_date", MAX(b."sort") AS "sort" FROM edb_info_share AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id WHERE 1=1 AND a.sys_user_id = ? AND b.sys_user_id != ? GROUP BY b.edb_info_id ORDER BY share_time DESC` err = global.DmSQL["data"].Raw(sql, sysUserId, sysUserId).Scan(&list).Error return } // GetAllEditSharedEdbInfoIdListByReceivedUserId // @Description: 获取所有我收到的共享的、可以编辑的指标id列表 // @author: Roc // @receiver m // @datetime 2024-12-03 09:56:41 // @param sysUserId int // @return list []*ShareEdbInfoQuery // @return err error func (m EdbInfoShare) GetAllEditSharedEdbInfoIdListByReceivedUserId(sysUserId int, edbInfoIdList []int) (list []int, err error) { sql := `SELECT a."edb_info_id" FROM edb_info_share AS a WHERE 1=1 AND a.sys_user_id = ? AND share_type = ? ` pars := []interface{}{sysUserId, utils.ShareTypeEdit} if len(edbInfoIdList) > 0 { sql += ` AND a.edb_info_id in (?) ` pars = append(pars, edbInfoIdList) } sql += ` GROUP BY a.edb_info_id ` err = global.DmSQL["data"].Raw(sql, sysUserId, utils.ShareTypeEdit).Scan(&list).Error return } // GetShareEdbInfoListPageList // @Description: 获取分享的指标列表(分页) // @author: Roc // @receiver m // @datetime 2024-12-03 14:42:12 // @param condition string // @param pars []interface{} // @param startSize int // @param pageSize int // @return total int32 // @return list []*EdbInfoList // @return err error func (m EdbInfoShare) GetShareEdbInfoListPageList(condition string, pars []interface{}, startSize, pageSize int) (total int32, list []*EdbInfoList, err error) { baseSql := `SELECT b."edb_info_id", MAX(a.share_type) AS share_type, MAX(a.create_time) as share_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."end_date") "end_date", MAX(b."end_value") "end_value", MAX(b."latest_date") "latest_date", MAX(b."latest_value") "latest_value", MAX(b."sys_user_id") "sys_user_id", MAX(b."sys_user_real_name") "sys_user_real_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(b."start_date") "start_date", MAX(b."sort") AS "sort" FROM edb_info_share AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id WHERE 1=1 ` baseSql += condition baseSql += ` GROUP BY b.edb_info_id ` totalSql := `SELECT COUNT(1) AS total FROM (` + baseSql + `) AS d` var totalNull sql2.NullInt32 err = global.DmSQL["data"].Raw(totalSql, pars...).Scan(&totalNull).Error if err != nil { return } total = totalNull.Int32 sql := baseSql + ` ORDER BY share_time DESC LIMIT ?,? ` pars = append(pars, startSize, pageSize) err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error if err != nil { return } for _, v := range list { v.ConvertToResp() } return } // UserShareEdbClassifyItem // @Description: 用户分享的指标分类列表 type UserShareEdbClassifyItem struct { UserId int `description:"用户id"` UserName string `description:"用户名称"` ClassifyId int `description:"分类id"` EdbInfoId int `description:"指标id"` ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"` ClassifyName string ClassifyNameEn string ParentId int RootId int `description:"顶级id"` Level int `description:"层级"` Sort int `description:"排序字段,越小越靠前,默认值:10"` UniqueCode string `description:"唯一编码"` Source int `description:"来源id"` SourceName string `description:"来源名称"` SysUserId int `description:"创建人id"` SysUserRealName string `description:"创建人姓名"` StartDate string EdbCode string EdbType int `description:"指标类型:1:基础指标,2:计算指标"` Children []*UserShareEdbClassifyItem `gorm:"-"` Button EdbClassifyItemsButton `gorm:"-" description:"操作权限"` IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"` HaveOperaAuth bool `description:"是否有数据权限"` ClassifyIdPath string `description:"分类的完整路径,格式为:父级ID,当前ID"` } type UserShareEdbClassifyItemList []*UserShareEdbClassifyItem func (m UserShareEdbClassifyItemList) Len() int { return len(m) } func (m UserShareEdbClassifyItemList) Less(i, j int) bool { return m[i].Sort < m[j].Sort } func (m UserShareEdbClassifyItemList) Swap(i, j int) { m[i], m[j] = m[j], m[i] }