123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- package data_manage
- import (
- sql2 "database/sql"
- "eta_gn/eta_api/global"
- "eta_gn/eta_api/utils"
- "fmt"
- "time"
- )
- // ChartInfoShare
- // @Description: 指标分享表
- type ChartInfoShare struct {
- ChartInfoShareId int `gorm:"primaryKey" `
- ChartInfoId 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 ChartInfoShare) TableName() string {
- return `chart_info_share`
- }
- // GetListByChartInfoId
- // @Description: 根据指标id获取分享用户列表
- // @author: Roc
- // @receiver m
- // @datetime 2024-11-27 15:39:17
- // @param chartInfoId int
- // @return items []*ChartInfoShare
- // @return err error
- func (m ChartInfoShare) GetListByChartInfoId(chartInfoId int) (items []*ChartInfoShare, err error) {
- sql := ` SELECT * FROM chart_info_share WHERE chart_info_id = ? ORDER BY chart_info_share_id ASC `
- err = global.DmSQL["data"].Raw(sql, chartInfoId).Find(&items).Error
- return
- }
- // SaveChartInfoShare
- // @Description: 保存配置
- // @author: Roc
- // @receiver m
- // @datetime 2024-11-27 15:54:07
- // @param chartInfoIdList []int
- // @param userIdList []int
- // @param shareType int8
- // @return err error
- func (m ChartInfoShare) SaveChartInfoShare(chartInfoIdList, userIdList []int, shareType int8) (err error) {
- // TODO 保存之前,得先做关联校验
- addList := make([]*ChartInfoShare, 0)
- for _, chartInfoId := range chartInfoIdList {
- for _, userId := range userIdList {
- addList = append(addList, &ChartInfoShare{
- ChartInfoShareId: 0,
- ChartInfoId: chartInfoId,
- 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 chart_info_id IN (?) AND share_type = ? ", m.TableName())
- err = tx.Exec(sql, chartInfoIdList, shareType).Error
- if err != nil {
- return
- }
- // 批量添加新的配置
- if len(addList) > 0 {
- err = tx.CreateInBatches(addList, utils.MultiAddNum).Error
- if err != nil {
- return
- }
- }
- return
- }
- // ShareChartInfoQuery
- // @Description: 共享指标
- type ShareChartInfoQuery struct {
- ChartInfo
- ShareType string `gorm:"column:share_type" description:"共享类型"`
- ShareTime time.Time `gorm:"column:share_time" description:"设置共享时间"`
- }
- // GetAllSharedChartInfoListByFromUserId
- // @Description: 获取所有我共享出去的指标列表
- // @author: Roc
- // @receiver m
- // @datetime 2024-12-03 09:56:41
- // @param sysUserId int
- // @return list []*ShareChartInfoQuery
- // @return err error
- func (m ChartInfoShare) GetAllSharedChartInfoListByFromUserId(sysUserId int) (list []*ShareChartInfoQuery, err error) {
- sql := `SELECT b."chart_info_id",
- MAX(a.share_type) AS share_type,
- MAX(a.create_time) as share_time,
- MAX(b."chart_name") "chart_name",
- MAX(b."source") "source",
- MAX(b."chart_type") "chart_type",
- MAX(b."chart_classify_id") "chart_classify_id",
- MAX(b."chart_public_classify_id") "chart_public_classify_id",
- MAX(b."unique_code") "unique_code",
- MAX(b."chart_image") "chart_image",
- MAX(b."sys_user_id") "sys_user_id",
- MAX(b."sys_user_real_name") "sys_user_real_name",
- MAX(b."create_time") "create_time",
- MAX(b."modify_time") "modify_time",
- MAX(b."sort") AS "sort"
- FROM chart_info_share AS a
- JOIN chart_info AS b ON a.chart_info_id = b.chart_info_id
- WHERE 1=1 AND b.sys_user_id = ?
- GROUP BY b.chart_info_id
- ORDER BY share_time DESC`
- err = global.DmSQL["data"].Raw(sql, sysUserId).Scan(&list).Error
- return
- }
- // GetAllSharedChartInfoListByReceivedUserId
- // @Description: 获取所有我收到的共享的指标列表
- // @author: Roc
- // @receiver m
- // @datetime 2024-12-03 09:56:41
- // @param sysUserId int
- // @return list []*ShareChartInfoQuery
- // @return err error
- func (m ChartInfoShare) GetAllSharedChartInfoListByReceivedUserId(sysUserId int) (list []*ShareChartInfoQuery, err error) {
- sql := `SELECT b."chart_info_id",
- MAX(a.share_type) AS share_type,
- MAX(a.create_time) as share_time,
- MAX(b."chart_name") "chart_name",
- MAX(b."source") "source",
- MAX(b."chart_type") "chart_type",
- MAX(b."chart_classify_id") "chart_classify_id",
- MAX(b."chart_public_classify_id") "chart_public_classify_id",
- MAX(b."unique_code") "unique_code",
- MAX(b."chart_image") "chart_image",
- MAX(b."sys_user_id") "sys_user_id",
- MAX(b."sys_user_real_name") "sys_user_real_name",
- MAX(b."create_time") "create_time",
- MAX(b."modify_time") "modify_time",
- MAX(b."sort") AS "sort"
- FROM chart_info_share AS a
- JOIN chart_info AS b ON a.chart_info_id = b.chart_info_id
- WHERE 1=1 AND a.sys_user_id = ? AND b.sys_user_id != ?
- GROUP BY b.chart_info_id
- ORDER BY share_time DESC`
- err = global.DmSQL["data"].Raw(sql, sysUserId, sysUserId).Scan(&list).Error
- return
- }
- // GetAllEditSharedChartInfoIdListByReceivedUserId
- // @Description: 获取所有我收到的共享的、可以编辑的图表id列表
- // @author: Roc
- // @receiver m
- // @datetime 2024-12-03 09:56:41
- // @param sysUserId int
- // @return list []*ShareChartInfoQuery
- // @return err error
- func (m ChartInfoShare) GetAllEditSharedChartInfoIdListByReceivedUserId(sysUserId int, chartInfoIdList []int) (list []int, err error) {
- sql := `SELECT a."chart_info_id" FROM chart_info_share AS a
- WHERE 1=1 AND a.sys_user_id = ? AND share_type = ? `
- pars := []interface{}{sysUserId, utils.ShareTypeEdit}
- if len(chartInfoIdList) > 0 {
- sql += ` AND a.chart_info_id in (?) `
- pars = append(pars, chartInfoIdList)
- }
- sql += ` GROUP BY a.chart_info_id `
- err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
- return
- }
- // GetShareChartInfoListPageList
- // @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 []*ChartInfoList
- // @return err error
- func (m ChartInfoShare) GetShareChartInfoListPageList(condition string, pars []interface{}, startSize, pageSize int) (total int32, list []*ChartInfoView, err error) {
- baseSql := `SELECT b."chart_info_id",
- MAX(a.share_type) AS share_type,
- MAX(a.create_time) as share_time,
- MAX(b."chart_name") "chart_name",
- MAX(b."source") "source",
- MAX(b."chart_type") "chart_type",
- MAX(b."chart_classify_id") "chart_classify_id",
- MAX(b."chart_public_classify_id") "chart_public_classify_id",
- MAX(b."unique_code") "unique_code",
- MAX(b."chart_image") "chart_image",
- MAX(b."sys_user_id") "sys_user_id",
- MAX(b."sys_user_real_name") "sys_user_real_name",
- MAX(b."create_time") "create_time",
- MAX(b."modify_time") "modify_time",
- MAX(b."sort") AS "sort"
- FROM chart_info_share AS a
- JOIN chart_info AS b ON a.chart_info_id = b.chart_info_id
- WHERE 1=1 `
- baseSql += condition
- baseSql += ` GROUP BY b.chart_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
- }
- type UserShareChartClassifyItem struct {
- UserId int `description:"用户id"`
- UserName string `description:"用户名称"`
- ChartClassifyId int `description:"分类id"`
- ChartInfoId int `description:"指标id"`
- ChartClassifyName string
- ChartClassifyNameEn string
- ParentId int
- Level int `description:"层级"`
- Sort int `description:"排序字段,越小越靠前,默认值:10"`
- UniqueCode string `description:"唯一编码"`
- Source int `description:"来源id"`
- SourceName string `description:"来源名称"`
- SysUserId int `description:"创建人id"`
- SysUserRealName string `description:"创建人姓名"`
- DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
- StartDate string `description:"自定义开始日期"`
- EndDate string `description:"自定义结束日期"`
- ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
- Calendar string `description:"公历/农历"`
- SeasonStartDate string `description:"季节性图开始日期"`
- SeasonEndDate string `description:"季节性图开始日期"`
- Children []*UserShareChartClassifyItem `gorm:"-"`
- Button ChartClassifyItemsButton `gorm:"-" description:"按钮权限"`
- IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
- HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
- ChartClassifyIdPath string `description:"分类的完整路径,格式为:父级ID,当前ID"`
- }
- type UserShareChartClassifyItemList []*UserShareChartClassifyItem
- func (m UserShareChartClassifyItemList) Len() int {
- return len(m)
- }
- func (m UserShareChartClassifyItemList) Less(i, j int) bool {
- return m[i].Sort < m[j].Sort
- }
- func (m UserShareChartClassifyItemList) Swap(i, j int) {
- m[i], m[j] = m[j], m[i]
- }
|