123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- 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 (?) ", m.TableName())
- err = tx.Exec(sql, edbInfoIdList).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
- }
- // 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
- }
|