sandbox.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. package sandbox
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/models/system"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "time"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. )
  10. // Sandbox 沙盘推演主表
  11. //type Sandbox struct {
  12. // SandboxId int `orm:"column(sandbox_id);pk" gorm:"primaryKey" description:"沙盘id"`
  13. // Name string `description:"沙盘名称"`
  14. // ChartPermissionId int `description:"品种id"`
  15. // ChartPermissionName string `description:"品种名称"`
  16. // CurrVersion int `description:"当前版本"`
  17. // Code string `description:"沙盘code"`
  18. // Content string `description:"沙盘数据"`
  19. // PicUrl string `description:"沙盘图片地址"`
  20. // OpUserId int `description:"最近一次编辑操作的用户id"`
  21. // OpUserName string `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
  22. // IsDelete int8 `description:"是否删除,0:未删除,1:已删除"`
  23. // ModifyTime time.Time `description:"修改时间"`
  24. // CreateTime time.Time `description:"创建时间"`
  25. // SandboxClassifyId int `description:"分类id"`
  26. // Sort int `description:"排序"`
  27. //}
  28. type Sandbox struct {
  29. SandboxId int `orm:"column(sandbox_id);pk" gorm:"primaryKey" description:"沙盘id"`
  30. Name string `description:"沙盘名称"`
  31. Code string `description:"沙盘code"`
  32. Content string `description:"沙盘数据"`
  33. MindmapData string `description:"思维导图数据"`
  34. PicUrl string `description:"沙盘图片地址"`
  35. SysUserId int `description:"作者id"`
  36. SysUserName string `description:"作者名称"`
  37. IsDelete int8 `description:"是否删除,0:未删除,1:已删除"`
  38. ModifyTime time.Time `description:"修改时间"`
  39. CreateTime time.Time `description:"创建时间"`
  40. SandboxClassifyId int `description:"分类id"`
  41. Sort int `description:"排序"`
  42. Style int `description:"风格"`
  43. }
  44. // Update 沙盘字段变更
  45. func (sandbox *Sandbox) Update(cols []string) (err error) {
  46. o := global.DbMap[utils.DbNameIndex]
  47. err = o.Select(cols).Updates(sandbox).Error
  48. return
  49. }
  50. // GetSandboxById 根据沙盘id获取沙盘详情
  51. func GetSandboxById(sandboxId int) (sandboxInfo *Sandbox, err error) {
  52. o := global.DbMap[utils.DbNameIndex]
  53. sql := `select * from sandbox where sandbox_id = ? and is_delete = 0`
  54. err = o.Raw(sql, sandboxId).First(&sandboxInfo).Error
  55. return
  56. }
  57. // AddNewSandbox 添加一个新的沙盘
  58. func AddNewSandbox(sandboxInfo *Sandbox, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft) (err error) {
  59. o := global.DbMap[utils.DbNameIndex]
  60. to := o.Begin()
  61. defer func() {
  62. if err != nil {
  63. _ = to.Rollback()
  64. } else {
  65. _ = to.Commit()
  66. }
  67. }()
  68. err = to.Create(sandboxInfo).Error
  69. if err != nil {
  70. return
  71. }
  72. // 新增版本
  73. sandboxVersion.SandboxId = sandboxInfo.SandboxId
  74. err = to.Create(sandboxVersion).Error
  75. if err != nil {
  76. return
  77. }
  78. // 新增草稿
  79. sandboxDraft.SandboxId = sandboxInfo.SandboxId
  80. err = to.Create(sandboxDraft).Error
  81. if err != nil {
  82. return
  83. }
  84. return
  85. }
  86. // UpdateSandbox 更新沙盘
  87. func UpdateSandbox(sandboxInfo *Sandbox, updateSandboxColumn []string, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft) (err error) {
  88. o := global.DbMap[utils.DbNameIndex]
  89. to := o.Begin()
  90. defer func() {
  91. if err != nil {
  92. _ = to.Rollback()
  93. } else {
  94. _ = to.Commit()
  95. }
  96. }()
  97. err = to.Select(updateSandboxColumn).Updates(sandboxInfo).Error
  98. if err != nil {
  99. return
  100. }
  101. // 新增版本
  102. sandboxVersion.SandboxId = sandboxInfo.SandboxId
  103. err = to.Create(sandboxVersion).Error
  104. if err != nil {
  105. return
  106. }
  107. // 新增草稿
  108. sandboxDraft.SandboxId = sandboxInfo.SandboxId
  109. err = to.Create(sandboxDraft).Error
  110. if err != nil {
  111. return
  112. }
  113. return
  114. }
  115. // UpdateSandboxName 更新沙盘(仅仅更新名称)
  116. func UpdateSandboxName(sandboxInfo *Sandbox, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft, updateSandboxColumn, updateSandboxVersionColumn []string) (err error) {
  117. o := global.DbMap[utils.DbNameIndex]
  118. to := o.Begin()
  119. defer func() {
  120. if err != nil {
  121. _ = to.Rollback()
  122. } else {
  123. _ = to.Commit()
  124. }
  125. }()
  126. err = to.Select(updateSandboxColumn).Updates(sandboxInfo).Error
  127. if err != nil {
  128. return
  129. }
  130. // 更新版本
  131. err = to.Select(updateSandboxVersionColumn).Updates(sandboxVersion).Error
  132. if err != nil {
  133. return
  134. }
  135. // 新增草稿
  136. sandboxDraft.SandboxId = sandboxInfo.SandboxId
  137. err = to.Create(sandboxDraft).Error
  138. if err != nil {
  139. return
  140. }
  141. return
  142. }
  143. // SandboxListItem 沙盘推演列表数据
  144. type SandboxListItem struct {
  145. SandboxId int `description:"沙盘id"`
  146. Name string `description:"沙盘名称"`
  147. ChartPermissionId int `description:"品种id"`
  148. ChartPermissionName string `description:"品种名称"`
  149. CurrVersion int `description:"当前版本"`
  150. Code string `description:"沙盘code"`
  151. VersionCode string `description:"沙盘版本code"`
  152. //Content string `description:"沙盘数据"`
  153. PicUrl string `description:"沙盘图片地址"`
  154. OpUserId int `description:"最近一次编辑操作的用户id"`
  155. OpUserName string `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
  156. IsDelete int8 `description:"是否删除,0:未删除,1:已删除" json:"is_delete"`
  157. CanEdit bool `description:"是否可编辑"`
  158. Editor string `description:"编辑人"`
  159. VersionTotal int `description:"历史版本数量"`
  160. ModifyTime time.Time `description:"修改时间"`
  161. CreateTime time.Time `description:"创建时间"`
  162. }
  163. // GetList 获取沙盘列表页
  164. func GetList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*Sandbox, err error) {
  165. o := global.DbMap[utils.DbNameIndex]
  166. 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 "
  167. sql += condition
  168. sql += ` order by a.modify_time desc,a.sandbox_id desc`
  169. totalSql := `select count(1) total from (` + sql + `) z `
  170. err = o.Raw(totalSql, pars...).Scan(&total).Error
  171. if err != nil {
  172. return
  173. }
  174. sql += ` LIMIT ?,? `
  175. pars = append(pars, startSize, pageSize)
  176. err = o.Raw(sql, pars...).Find(&list).Error
  177. return
  178. }
  179. // SandboxSaveResp 保存沙盘响应体
  180. type SandboxSaveResp struct {
  181. *Sandbox
  182. VersionCode string `description:"版本号"`
  183. }
  184. func GetSandboxAll() (list []*SandboxClassifyItems, err error) {
  185. o := global.DbMap[utils.DbNameIndex]
  186. sql := `SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name, sort
  187. FROM sandbox `
  188. err = o.Raw(sql).Find(&list).Error
  189. return
  190. }
  191. // CheckOpSandboxPermission 判断沙盘操作权限
  192. func CheckOpSandboxPermission(sysUser *system.Admin, createUserId int) (ok bool) {
  193. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  194. ok = true
  195. }
  196. // 如果图表创建人与当前操作人相同的话,那么就是允许操作
  197. if ok == false && createUserId == sysUser.AdminId {
  198. ok = true
  199. }
  200. // 如果图表权限id 是 1 ,那么允许编辑
  201. if ok == false && sysUser.ChartPermission == 1 {
  202. ok = true
  203. }
  204. return
  205. }
  206. // GetSandboxInfoByAdminId 获取所有我创建的沙盘,用于分类展示
  207. func GetSandboxInfoByAdminId(adminId int) (items []*SandboxClassifyItems, err error) {
  208. o := global.DbMap[utils.DbNameIndex]
  209. sql := ` SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name,code,
  210. sys_user_id,sys_user_name
  211. FROM sandbox where sys_user_id = ? ORDER BY sort asc,create_time ASC `
  212. err = o.Raw(sql, adminId).Find(&items).Error
  213. return
  214. }
  215. func GetSandboxClassify(sandboxClassifyId int) (sandbox_classify_id string, err error) {
  216. o := global.DbMap[utils.DbNameIndex]
  217. var sql string
  218. var pars []interface{}
  219. if utils.DbDriverName == utils.DbDriverByDm {
  220. sql = `WITH RECURSIVE classify_tree (sandbox_classify_id, level) AS (
  221. -- 获取指定分类
  222. SELECT sandbox_classify_id, 1 as level
  223. FROM sandbox_classify
  224. WHERE sandbox_classify_id = ?
  225. UNION ALL
  226. -- 获取子分类和孙子分类
  227. SELECT sc.sandbox_classify_id, ct.level + 1
  228. FROM sandbox_classify sc
  229. JOIN classify_tree ct ON sc.parent_id = ct.sandbox_classify_id
  230. WHERE ct.level <= 2 -- 限制只查询到孙子层级
  231. )
  232. SELECT LISTAGG(sandbox_classify_id, ',') WITHIN GROUP (ORDER BY level, sandbox_classify_id) AS sandbox_classify_id
  233. FROM classify_tree`
  234. pars = append(pars, sandboxClassifyId)
  235. }else{
  236. sql = `SELECT GROUP_CONCAT(t.sandbox_classify_id) AS sandbox_classify_id FROM (
  237. SELECT a.sandbox_classify_id FROM sandbox_classify AS a
  238. WHERE a.sandbox_classify_id=?
  239. UNION ALL
  240. SELECT a.sandbox_classify_id FROM sandbox_classify AS a
  241. WHERE a.parent_id=? UNION ALL
  242. SELECT
  243. sandbox_classify_id
  244. FROM
  245. sandbox_classify
  246. WHERE
  247. parent_id IN ( SELECT sandbox_classify_id FROM sandbox_classify WHERE parent_id = ? )
  248. )AS t`
  249. pars = append(pars, sandboxClassifyId, sandboxClassifyId, sandboxClassifyId)
  250. }
  251. err = o.Raw(sql, pars...).Scan(&sandbox_classify_id).Error
  252. return
  253. }
  254. type SandboxListItems struct {
  255. Sandbox
  256. ParentIds string
  257. }
  258. func GetSandboxListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*SandboxListItems, err error) {
  259. o := global.DbMap[utils.DbNameIndex]
  260. sql := ` SELECT * FROM sandbox WHERE 1=1 `
  261. if condition != "" {
  262. sql += condition
  263. }
  264. sql += " ORDER BY create_time DESC LIMIT ?,? "
  265. pars = append(pars, startSize, pageSize)
  266. err = o.Raw(sql, pars...).Find(&item).Error
  267. return
  268. }
  269. func GetSandboxListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  270. o := global.DbMap[utils.DbNameIndex]
  271. sql := ` SELECT COUNT(1) AS count FROM sandbox WHERE 1=1 `
  272. if condition != "" {
  273. sql += condition
  274. }
  275. err = o.Raw(sql, pars...).Scan(&count).Error
  276. return
  277. }
  278. type SandboxListResp struct {
  279. Paging *paging.PagingItem
  280. List []*SandboxListItems
  281. }
  282. func AddSandbox(item *Sandbox) (lastId int64, err error) {
  283. o := global.DbMap[utils.DbNameIndex]
  284. err = o.Create(item).Error
  285. if err != nil {
  286. return
  287. }
  288. lastId = int64(item.SandboxId)
  289. return
  290. }
  291. func GetSandboxItemsByClassifyId(sandboxClassifyId int) (list []*SandboxClassifyItems, err error) {
  292. o := global.DbMap[utils.DbNameIndex]
  293. sql := `SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name, sort
  294. FROM sandbox WHERE sandbox_classify_id = ? AND is_delete = 0 ORDER BY sort `
  295. err = o.Raw(sql, sandboxClassifyId).Find(&list).Error
  296. return
  297. }
  298. func GetSandboxAllParentByClassifyId(sandboxClassifyId int) (ids string, err error) {
  299. o := global.DbMap[utils.DbNameIndex]
  300. sql := `SELECT
  301. GROUP_CONCAT(DISTINCT m.sandbox_classify_id ORDER BY m.level) AS ids
  302. FROM
  303. (
  304. SELECT
  305. @id AS _id,(
  306. SELECT
  307. @id := parent_id
  308. FROM
  309. sandbox_classify
  310. WHERE
  311. sandbox_classify_id = _id
  312. )
  313. FROM
  314. (
  315. SELECT
  316. @id :=(
  317. SELECT
  318. parent_id
  319. FROM
  320. sandbox_classify
  321. WHERE
  322. sandbox_classify_id = ?
  323. )) vm,
  324. sandbox_classify m
  325. WHERE
  326. @id IS NOT NULL
  327. ) vm
  328. INNER JOIN sandbox_classify m
  329. WHERE
  330. sandbox_classify_id = vm._id `
  331. err = o.Raw(sql, sandboxClassifyId).Scan(&ids).Error
  332. return
  333. }
  334. type MoveSandboxReq struct {
  335. SandboxId int `description:"沙盘ID"`
  336. PrevSandboxId int `description:"上一个沙盘ID"`
  337. NextSandboxId int `description:"下一个沙盘ID"`
  338. SandboxClassifyId int `description:"分类id"`
  339. }
  340. func GetSandboxClassifyCountById(classifyId int) (count int, err error) {
  341. o := global.DbMap[utils.DbNameIndex]
  342. sql := `SELECT count(1) AS count FROM sandbox_classify WHERE sandbox_classify_id=? `
  343. err = o.Raw(sql, classifyId).Scan(&count).Error
  344. return
  345. }
  346. // GetSandboxByClassifyIdAndName 根据分类id和沙盘名获取图表信息
  347. func GetSandboxByClassifyIdAndName(classifyId int, name string) (item *Sandbox, err error) {
  348. o := global.DbMap[utils.DbNameIndex]
  349. sql := ` SELECT * FROM sandbox WHERE sandbox_classify_id = ? and name=? `
  350. err = o.Raw(sql, classifyId, name).First(&item).Error
  351. return
  352. }
  353. // GetSandboxNameByIds 根据沙盘名称
  354. func GetSandboxNameByIds(ids []int) (items []*Sandbox, err error) {
  355. o := global.DbMap[utils.DbNameIndex]
  356. sql := ` SELECT sandbox_id, name FROM sandbox WHERE sandbox_id in (` + utils.GetOrmInReplace(len(ids)) + `) `
  357. err = o.Raw(sql, ids).Find(&items).Error
  358. return
  359. }
  360. func MoveSandbox(sandboxId, classifyId int) (err error) {
  361. o := global.DbMap[utils.DbNameIndex]
  362. sql := ` UPDATE sandbox
  363. SET
  364. sandbox_classify_id = ?
  365. WHERE sandbox_id = ?`
  366. err = o.Exec(sql, classifyId, sandboxId).Error
  367. return
  368. }
  369. // UpdateSandboxSortByClassifyId 根据沙盘id更新排序
  370. func UpdateSandboxSortByClassifyId(classifyId, nowSort, prevSandboxId int, updateSort string) (err error) {
  371. o := global.DbMap[utils.DbNameIndex]
  372. sql := ` update sandbox set sort = ` + updateSort + ` WHERE sandbox_classify_id=? AND `
  373. if prevSandboxId > 0 {
  374. sql += ` (sort > ? or (sandbox_id > ` + fmt.Sprint(prevSandboxId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
  375. }
  376. err = o.Exec(sql, classifyId, nowSort).Error
  377. return
  378. }
  379. // GetFirstSandboxByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  380. func GetFirstSandboxByClassifyId(classifyId int) (item *Sandbox, err error) {
  381. o := global.DbMap[utils.DbNameIndex]
  382. sql := ` SELECT * FROM sandbox WHERE sandbox_classify_id=? order by sort asc,sandbox_id asc limit 1`
  383. err = o.Raw(sql, classifyId).First(&item).Error
  384. return
  385. }
  386. // ContentDataStruct 沙盘内容结构体
  387. type ContentDataStruct struct {
  388. Cells []struct {
  389. Data *NodeData `json:"data,omitempty"`
  390. } `json:"cells"`
  391. }
  392. type NodeData struct {
  393. LinkData []*LinkData `json:"linkData"`
  394. LinkFold bool `json:"linkFold"`
  395. }
  396. type LinkData struct {
  397. RId string `json:"RId"`
  398. Id int `json:"Id"`
  399. Name string `json:"Name"`
  400. Type int `json:"Type"`
  401. Editing bool `json:"editing"`
  402. DatabaseType int `json:"databaseType"`
  403. DetailParams DetailParams `json:"detailParams"`
  404. }
  405. type DetailParams struct {
  406. Code string `json:"code"`
  407. Id int `json:"id"`
  408. ClassifyId int `json:"classifyId"`
  409. }
  410. // UpdateSandboxContent 更新沙盘内容
  411. func UpdateSandboxContent(list []Sandbox) (err error) {
  412. o := global.DbMap[utils.DbNameIndex]
  413. to := o.Begin()
  414. defer func() {
  415. if err != nil {
  416. _ = to.Rollback()
  417. } else {
  418. _ = to.Commit()
  419. }
  420. }()
  421. //循环更新沙盘内容
  422. for _, sandbox := range list {
  423. err = to.Select([]string{"Content", "ModifyTime"}).Updates(&sandbox).Error
  424. if err != nil {
  425. return
  426. }
  427. }
  428. return
  429. }