sandbox.go 18 KB

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