123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- package sandbox
- import (
- "eta_gn/eta_api/global"
- "eta_gn/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "time"
- )
- // Sandbox 沙盘推演主表
- //type Sandbox struct {
- // SandboxId int `orm:"column(sandbox_id);pk" description:"沙盘id"`
- // Name string `description:"沙盘名称"`
- // ChartPermissionId int `description:"品种id"`
- // ChartPermissionName string `description:"品种名称"`
- // CurrVersion int `description:"当前版本"`
- // Code string `description:"沙盘code"`
- // Content string `description:"沙盘数据"`
- // PicUrl string `description:"沙盘图片地址"`
- // OpUserId int `description:"最近一次编辑操作的用户id"`
- // OpUserName string `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
- // IsDelete int8 `description:"是否删除,0:未删除,1:已删除"`
- // ModifyTime time.Time `description:"修改时间"`
- // CreateTime time.Time `description:"创建时间"`
- // SandboxClassifyId int `description:"分类id"`
- // Sort int `description:"排序"`
- //}
- //type Sandbox struct {
- // SandboxId int `orm:"column(sandbox_id);pk" description:"沙盘id"`
- // Name string `description:"沙盘名称"`
- // Code string `description:"沙盘code"`
- // Content string `description:"沙盘数据"`
- // MindmapData string `description:"思维导图数据"`
- // PicUrl string `description:"沙盘图片地址"`
- // SysUserId int `description:"作者id"`
- // SysUserName string `description:"作者名称"`
- // IsDelete int8 `description:"是否删除,0:未删除,1:已删除"`
- // ModifyTime time.Time `description:"修改时间"`
- // CreateTime time.Time `description:"创建时间"`
- // SandboxClassifyId int `description:"分类id"`
- // Sort int `description:"排序"`
- // Style int `description:"风格"`
- //}
- type Sandbox struct {
- SandboxId int `gorm:"primaryKey;column:sandbox_id;type:int(9) unsigned;not null"` // 沙盘id
- Name string `gorm:"index:idx_name;column:name;type:varchar(64);not null;default:''"` // 沙盘名称
- Code string `gorm:"column:code;type:varchar(255);not null"` // 沙盘code
- Content string `gorm:"column:content;type:longtext;not null"` // 沙盘内容
- MindmapData string `gorm:"column:mindmap_data;type:longtext;not null"` // 思维导图内容,
- PicUrl string `gorm:"column:pic_url;type:varchar(255);not null;default:''"` // 沙盘图片
- IsDelete int `gorm:"column:is_delete;type:tinyint(9) unsigned;not null;default:0"` // 是否删除,0:未删除,1:已删除
- ModifyTime time.Time `gorm:"column:modify_time;type:timestamp;default:CURRENT_TIMESTAMP"` // 最近一次更新时间
- CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP"` // 沙盘创建时间
- Sort int `gorm:"column:sort;type:int(9);default:0"` // 排序字段,数字越小越排前面
- SandboxClassifyId int `gorm:"column:sandbox_classify_id;type:int(11);default:0"` // 分类id
- SysUserId int `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0"` // 创建人
- SysUserName string `gorm:"column:sys_user_name;type:varchar(32);not null;default:''"` // 创建人名
- ChartPermissionId int `gorm:"column:chart_permission_id;type:int(9) unsigned;not null;default:0"` // 品种id
- ChartPermissionName string `gorm:"column:chart_permission_name;type:varchar(32);not null;default:''"` // 品种名称(冗余字段,避免列表页查询时再去关联表查询)
- Style int `gorm:"column:style;type:int(8)"` // 风格
- }
- // Update 沙盘字段变更
- func (sandbox *Sandbox) Update(cols []string) (err error) {
- //o := orm.NewOrmUsingDB("data")
- //_, err = o.Update(sandbox, cols...)
- err = global.DmSQL["data"].Select(cols).Updates(sandbox).Error
- return
- }
- // GetSandboxById 根据沙盘id获取沙盘详情
- func GetSandboxById(sandboxId int) (sandboxInfo *Sandbox, err error) {
- //o := orm.NewOrmUsingDB("data")
- sql := `select * from sandbox where sandbox_id = ? and is_delete = 0`
- //err = o.Raw(sql, sandboxId).QueryRow(&sandboxInfo)
- err = global.DmSQL["data"].Raw(sql, sandboxId).First(&sandboxInfo).Error
- return
- }
- // AddNewSandbox 添加一个新的沙盘
- //func AddNewSandbox(sandboxInfo *Sandbox, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft) (err error) {
- // o := orm.NewOrmUsingDB("data")
- // to, err := o.Begin()
- // if err != nil {
- // return
- // }
- // defer func() {
- // if err != nil {
- // _ = to.Rollback()
- // } else {
- // _ = to.Commit()
- // }
- // }()
- //
- // id, err := to.Insert(sandboxInfo)
- // if err != nil {
- // return
- // }
- // sandboxInfo.SandboxId = int(id)
- //
- // // 新增版本
- // sandboxVersion.SandboxId = sandboxInfo.SandboxId
- // id, err = to.Insert(sandboxVersion)
- // if err != nil {
- // return
- // }
- // sandboxVersion.SandboxVersionId = int(id)
- //
- // // 新增草稿
- // sandboxDraft.SandboxId = sandboxInfo.SandboxId
- // id, err = to.Insert(sandboxDraft)
- // if err != nil {
- // return
- // }
- // sandboxDraft.SandboxDraftId = int(id)
- // return
- //}
- // UpdateSandbox 更新沙盘
- //func UpdateSandbox(sandboxInfo *Sandbox, updateSandboxColumn []string, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft) (err error) {
- // o := orm.NewOrmUsingDB("data")
- // to, err := o.Begin()
- // if err != nil {
- // return
- // }
- // defer func() {
- // if err != nil {
- // _ = to.Rollback()
- // } else {
- // _ = to.Commit()
- // }
- // }()
- //
- // _, err = to.Update(sandboxInfo, updateSandboxColumn...)
- // if err != nil {
- // return
- // }
- //
- // // 新增版本
- // sandboxVersion.SandboxId = sandboxInfo.SandboxId
- // id, err := to.Insert(sandboxVersion)
- // if err != nil {
- // return
- // }
- // sandboxVersion.SandboxVersionId = int(id)
- //
- // // 新增草稿
- // sandboxDraft.SandboxId = sandboxInfo.SandboxId
- // id, err = to.Insert(sandboxDraft)
- // if err != nil {
- // return
- // }
- // sandboxDraft.SandboxDraftId = int(id)
- // return
- //}
- // UpdateSandboxName 更新沙盘(仅仅更新名称)
- //func UpdateSandboxName(sandboxInfo *Sandbox, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft, updateSandboxColumn, updateSandboxVersionColumn []string) (err error) {
- // o := orm.NewOrmUsingDB("data")
- // to, err := o.Begin()
- // if err != nil {
- // return
- // }
- // defer func() {
- // if err != nil {
- // _ = to.Rollback()
- // } else {
- // _ = to.Commit()
- // }
- // }()
- //
- // _, err = to.Update(sandboxInfo, updateSandboxColumn...)
- // if err != nil {
- // return
- // }
- //
- // // 更新版本
- // _, err = to.Update(sandboxVersion, updateSandboxVersionColumn...)
- // if err != nil {
- // return
- // }
- //
- // // 新增草稿
- // sandboxDraft.SandboxId = sandboxInfo.SandboxId
- // id, err := to.Insert(sandboxDraft)
- // if err != nil {
- // return
- // }
- // sandboxDraft.SandboxDraftId = int(id)
- // return
- //}
- // SandboxListItem 沙盘推演列表数据
- type SandboxListItem struct {
- SandboxId int `description:"沙盘id"`
- Name string `description:"沙盘名称"`
- ChartPermissionId int `description:"品种id"`
- ChartPermissionName string `description:"品种名称"`
- CurrVersion int `description:"当前版本"`
- Code string `description:"沙盘code"`
- VersionCode string `description:"沙盘版本code"`
- //Content string `description:"沙盘数据"`
- PicUrl string `description:"沙盘图片地址"`
- OpUserId int `description:"最近一次编辑操作的用户id"`
- OpUserName string `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
- IsDelete int8 `description:"是否删除,0:未删除,1:已删除" json:"is_delete"`
- CanEdit bool `description:"是否可编辑"`
- Editor string `description:"编辑人"`
- VersionTotal int `description:"历史版本数量"`
- ModifyTime time.Time `description:"修改时间"`
- CreateTime time.Time `description:"创建时间"`
- }
- // GetList 获取沙盘列表页
- func GetList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*Sandbox, err error) {
- //o := orm.NewOrmUsingDB("data")
- sql := "select a.sandbox_id,a.name,a.code,a.pic_url,a.sys_user_id,a.sys_user_name,a.modify_time,a.create_time from sandbox as a where 1=1 AND a.is_delete = 0 "
- sql += condition
- sql += ` order by a.modify_time desc,a.sandbox_id desc`
- totalSql := `select count(1) total from (` + sql + `) z `
- err = global.DmSQL["data"].Raw(totalSql, pars...).Scan(&total).Error
- //err = o.Raw(totalSql, pars).QueryRow(&total)
- if err != nil {
- return
- }
- sql += ` LIMIT ?,? `
- //_, err = o.Raw(sql, pars...).QueryRows(&list)
- pars = append(pars, startSize)
- pars = append(pars, pageSize)
- err = global.DmSQL["data"].Raw(sql, pars...).Find(&list).Error
- return
- }
- // SandboxSaveResp 保存沙盘响应体
- type SandboxSaveResp struct {
- *Sandbox
- VersionCode string `description:"版本号"`
- }
- //func GetSandboxAll() (list []*SandboxClassifyItems, err error) {
- // o := orm.NewOrmUsingDB("data")
- // sql := `SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name, sort
- // FROM sandbox `
- // _, err = o.Raw(sql).QueryRows(&list)
- // return
- //}
- // CheckOpSandboxPermission 判断沙盘操作权限
- //func CheckOpSandboxPermission(sysUser *system.Admin, createUserId int) (ok bool) {
- // if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
- // ok = true
- // }
- // // 如果图表创建人与当前操作人相同的话,那么就是允许操作
- // if ok == false && createUserId == sysUser.AdminId {
- // ok = true
- // }
- // // 如果图表权限id 是 1 ,那么允许编辑
- // if ok == false && sysUser.ChartPermission == 1 {
- // ok = true
- // }
- // return
- //}
- // GetSandboxInfoByAdminId 获取所有我创建的沙盘,用于分类展示
- func GetSandboxInfoByAdminId(adminId int) (items []*SandboxClassifyItems, err error) {
- //o := orm.NewOrmUsingDB("data")
- sql := ` SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name,code,
- sys_user_id,sys_user_name
- FROM sandbox where sys_user_id = ? ORDER BY sort asc,create_time ASC `
- //_, err = o.Raw(sql, adminId).QueryRows(&items)
- err = global.DmSQL["data"].Raw(sql, adminId).Find(&items).Error
- return
- }
- func GetSandboxClassify(sandboxClassifyId int) (sandbox_classify_id string, err error) {
- //o := orm.NewOrmUsingDB("data")
- sql := `SELECT GROUP_CONCAT(t.sandbox_classify_id) AS sandbox_classify_id FROM (
- SELECT a.sandbox_classify_id FROM sandbox_classify AS a
- WHERE a.sandbox_classify_id=?
- UNION ALL
- SELECT a.sandbox_classify_id FROM sandbox_classify AS a
- WHERE a.parent_id=? UNION ALL
- SELECT
- sandbox_classify_id
- FROM
- sandbox_classify
- WHERE
- parent_id IN ( SELECT sandbox_classify_id FROM sandbox_classify WHERE parent_id = ? )
- )AS t`
- //err = o.Raw(sql, sandboxClassifyId, sandboxClassifyId, sandboxClassifyId).QueryRow(&sandbox_classify_id)
- err = global.DmSQL["data"].Raw(sql, sandboxClassifyId, sandboxClassifyId, sandboxClassifyId).Scan(&sandbox_classify_id).Error
- return
- }
- type SandboxListItems struct {
- Sandbox
- ParentIds string `gorm:"-"`
- }
- func GetSandboxListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*SandboxListItems, err error) {
- //o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM sandbox WHERE 1=1 `
- if condition != "" {
- sql += condition
- }
- sql += " ORDER BY create_time DESC LIMIT ?,? "
- //_, err = o.Raw(sql, pars...).QueryRows(&item)
- pars = append(pars, startSize)
- pars = append(pars, pageSize)
- err = global.DmSQL["data"].Raw(sql, pars...).Find(&item).Error
- return
- }
- func GetSandboxListCountByCondition(condition string, pars []interface{}) (count int, err error) {
- //o := orm.NewOrmUsingDB("data")
- sql := ` SELECT COUNT(1) AS count FROM sandbox WHERE 1=1 `
- if condition != "" {
- sql += condition
- }
- //err = o.Raw(sql, pars).QueryRow(&count)
- err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
- return
- }
- type SandboxListResp struct {
- Paging *paging.PagingItem
- List []*SandboxListItems
- }
- func AddSandbox(item *Sandbox) (lastId int64, err error) {
- //o := orm.NewOrmUsingDB("data")
- //lastId, err = o.Insert(item)
- err = global.DmSQL["data"].Create(item).Error
- return
- }
- //func GetSandboxItemsByClassifyId(sandboxClassifyId int) (list []*SandboxClassifyItems, err error) {
- // o := orm.NewOrmUsingDB("data")
- // sql := `SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name, sort
- // FROM sandbox WHERE sandbox_classify_id = ? AND is_delete = 0 ORDER BY sort `
- // _, err = o.Raw(sql, sandboxClassifyId).QueryRows(&list)
- // return
- //}
- func GetSandboxAllParentByClassifyId(sandboxClassifyId int) (ids string, err error) {
- //o := orm.NewOrmUsingDB("data")
- sql := `SELECT
- GROUP_CONCAT(DISTINCT m.sandbox_classify_id ORDER BY m.level) AS ids
- FROM
- (
- SELECT
- @id AS _id,(
- SELECT
- @id := parent_id
- FROM
- sandbox_classify
- WHERE
- sandbox_classify_id = _id
- )
- FROM
- (
- SELECT
- @id :=(
- SELECT
- parent_id
- FROM
- sandbox_classify
- WHERE
- sandbox_classify_id = ?
- )) vm,
- sandbox_classify m
- WHERE
- @id IS NOT NULL
- ) vm
- INNER JOIN sandbox_classify m
- WHERE
- sandbox_classify_id = vm._id `
- //err = o.Raw(sql, sandboxClassifyId).QueryRow(&ids)
- err = global.DmSQL["data"].Raw(sql, sandboxClassifyId).Scan(&ids).Error
- return
- }
- type MoveSandboxReq struct {
- SandboxId int `description:"沙盘ID"`
- PrevSandboxId int `description:"上一个沙盘ID"`
- NextSandboxId int `description:"下一个沙盘ID"`
- SandboxClassifyId int `description:"分类id"`
- }
- func GetSandboxClassifyCountById(classifyId int) (count int, err error) {
- //o := orm.NewOrmUsingDB("data")
- sql := `SELECT count(1) AS count FROM sandbox_classify WHERE sandbox_classify_id=? `
- //err = o.Raw(sql, classifyId).QueryRow(&count)
- err = global.DmSQL["data"].Raw(sql, classifyId).Scan(&count).Error
- return
- }
- // GetSandboxByClassifyIdAndName 根据分类id和沙盘名获取图表信息
- //func GetSandboxByClassifyIdAndName(classifyId int, name string) (item *Sandbox, err error) {
- // o := orm.NewOrmUsingDB("data")
- // sql := ` SELECT * FROM sandbox WHERE sandbox_classify_id = ? and name=? `
- // err = o.Raw(sql, classifyId, name).QueryRow(&item)
- // return
- //}
- // GetSandboxNameByIds 根据沙盘名称
- func GetSandboxNameByIds(ids []int) (items []*Sandbox, err error) {
- if len(ids) == 0 {
- return
- }
- //o := orm.NewOrmUsingDB("data")
- sql := ` SELECT sandbox_id, name FROM sandbox WHERE sandbox_id in (` + utils.GetOrmInReplace(len(ids)) + `) `
- //_, err = o.Raw(sql, ids).QueryRows(&items)
- err = global.DmSQL["data"].Raw(sql, ids).Find(&items).Error
- return
- }
- func MoveSandbox(sandboxId, classifyId int) (err error) {
- //o := orm.NewOrmUsingDB("data")
- sql := ` UPDATE sandbox
- SET
- sandbox_classify_id = ?
- WHERE sandbox_id = ?`
- //_, err = o.Raw(sql, classifyId, sandboxId).Exec()
- err = global.DmSQL["data"].Exec(sql, classifyId, sandboxId).Error
- return
- }
- // UpdateSandboxSortByClassifyId 根据沙盘id更新排序
- func UpdateSandboxSortByClassifyId(classifyId, nowSort, prevSandboxId int, updateSort string) (err error) {
- //o := orm.NewOrmUsingDB("data")
- sql := ` update sandbox set sort = ` + updateSort + ` WHERE sandbox_classify_id=? AND `
- if prevSandboxId > 0 {
- sql += ` (sort > ? or (sandbox_id > ` + fmt.Sprint(prevSandboxId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
- }
- //_, err = o.Raw(sql, classifyId, nowSort).Exec()
- err = global.DmSQL["data"].Exec(sql, classifyId, nowSort).Error
- return
- }
- // GetFirstSandboxByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
- func GetFirstSandboxByClassifyId(classifyId int) (item *Sandbox, err error) {
- //o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM sandbox WHERE sandbox_classify_id=? order by sort asc,sandbox_id asc limit 1`
- //err = o.Raw(sql, classifyId).QueryRow(&item)
- err = global.DmSQL["data"].Raw(sql, classifyId).First(&item).Error
- return
- }
- // ContentDataStruct 沙盘内容结构体
- type ContentDataStruct struct {
- Cells []struct {
- Data *NodeData `json:"data,omitempty"`
- } `json:"cells"`
- }
- type NodeData struct {
- LinkData []*LinkData `json:"linkData"`
- LinkFold bool `json:"linkFold"`
- }
- type LinkData struct {
- RId string `json:"RId"`
- Id int `json:"Id"`
- Name string `json:"Name"`
- Type int `json:"Type"`
- Editing bool `json:"editing"`
- DatabaseType int `json:"databaseType"`
- DetailParams DetailParams `json:"detailParams"`
- }
- type DetailParams struct {
- Code string `json:"code"`
- Id int `json:"id"`
- ClassifyId int `json:"classifyId"`
- }
- // UpdateSandboxContent 更新沙盘内容
- func UpdateSandboxContent(list []Sandbox) (err error) {
- //o := orm.NewOrmUsingDB("data")
- //to, err := o.Begin()
- //if err != nil {
- // return
- //}
- //defer func() {
- // if err != nil {
- // _ = to.Rollback()
- // } else {
- // _ = to.Commit()
- // }
- //}()
- //
- ////循环更新沙盘内容
- //for _, sandbox := range list {
- // _, err = to.Update(&sandbox, "Content", "ModifyTime")
- // if err != nil {
- // return
- // }
- //}
- tx := global.DmSQL["data"].Begin()
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- return
- }
- _ = tx.Commit()
- }()
- cols := []string{"Content", "ModifyTime"}
- for _, sandbox := range list {
- err = tx.Select(cols).Updates(sandbox).Error
- if err != nil {
- return
- }
- }
- return
- }
|