sandbox.go 14 KB

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