sandbox.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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...).QueryRows(&list)
  220. pars = append(pars, startSize)
  221. pars = append(pars, pageSize)
  222. err = global.DmSQL["data"].Raw(sql, pars...).Find(&list).Error
  223. return
  224. }
  225. // SandboxSaveResp 保存沙盘响应体
  226. type SandboxSaveResp struct {
  227. *Sandbox
  228. VersionCode string `description:"版本号"`
  229. }
  230. //func GetSandboxAll() (list []*SandboxClassifyItems, err error) {
  231. // o := orm.NewOrmUsingDB("data")
  232. // sql := `SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name, sort
  233. // FROM sandbox `
  234. // _, err = o.Raw(sql).QueryRows(&list)
  235. // return
  236. //}
  237. // CheckOpSandboxPermission 判断沙盘操作权限
  238. //func CheckOpSandboxPermission(sysUser *system.Admin, createUserId int) (ok bool) {
  239. // if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  240. // ok = true
  241. // }
  242. // // 如果图表创建人与当前操作人相同的话,那么就是允许操作
  243. // if ok == false && createUserId == sysUser.AdminId {
  244. // ok = true
  245. // }
  246. // // 如果图表权限id 是 1 ,那么允许编辑
  247. // if ok == false && sysUser.ChartPermission == 1 {
  248. // ok = true
  249. // }
  250. // return
  251. //}
  252. // GetSandboxInfoByAdminId 获取所有我创建的沙盘,用于分类展示
  253. func GetSandboxInfoByAdminId(adminId int) (items []*SandboxClassifyItems, err error) {
  254. //o := orm.NewOrmUsingDB("data")
  255. sql := ` SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name,code,
  256. sys_user_id,sys_user_name
  257. FROM sandbox where sys_user_id = ? ORDER BY sort asc,create_time ASC `
  258. //_, err = o.Raw(sql, adminId).QueryRows(&items)
  259. err = global.DmSQL["data"].Raw(sql, adminId).Find(&items).Error
  260. return
  261. }
  262. func GetSandboxClassify(sandboxClassifyId int) (sandbox_classify_id string, err error) {
  263. //o := orm.NewOrmUsingDB("data")
  264. sql := `SELECT GROUP_CONCAT(t.sandbox_classify_id) AS sandbox_classify_id FROM (
  265. SELECT a.sandbox_classify_id FROM sandbox_classify AS a
  266. WHERE a.sandbox_classify_id=?
  267. UNION ALL
  268. SELECT a.sandbox_classify_id FROM sandbox_classify AS a
  269. WHERE a.parent_id=? UNION ALL
  270. SELECT
  271. sandbox_classify_id
  272. FROM
  273. sandbox_classify
  274. WHERE
  275. parent_id IN ( SELECT sandbox_classify_id FROM sandbox_classify WHERE parent_id = ? )
  276. )AS t`
  277. //err = o.Raw(sql, sandboxClassifyId, sandboxClassifyId, sandboxClassifyId).QueryRow(&sandbox_classify_id)
  278. err = global.DmSQL["data"].Raw(sql, sandboxClassifyId, sandboxClassifyId, sandboxClassifyId).Scan(&sandbox_classify_id).Error
  279. return
  280. }
  281. type SandboxListItems struct {
  282. Sandbox
  283. ParentIds string `gorm:"-"`
  284. }
  285. func GetSandboxListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*SandboxListItems, err error) {
  286. //o := orm.NewOrmUsingDB("data")
  287. sql := ` SELECT * FROM sandbox WHERE 1=1 `
  288. if condition != "" {
  289. sql += condition
  290. }
  291. sql += " ORDER BY create_time DESC LIMIT ?,? "
  292. //_, err = o.Raw(sql, pars...).QueryRows(&item)
  293. pars = append(pars, startSize)
  294. pars = append(pars, pageSize)
  295. err = global.DmSQL["data"].Raw(sql, pars...).Find(&item).Error
  296. return
  297. }
  298. func GetSandboxListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  299. //o := orm.NewOrmUsingDB("data")
  300. sql := ` SELECT COUNT(1) AS count FROM sandbox WHERE 1=1 `
  301. if condition != "" {
  302. sql += condition
  303. }
  304. //err = o.Raw(sql, pars).QueryRow(&count)
  305. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
  306. return
  307. }
  308. type SandboxListResp struct {
  309. Paging *paging.PagingItem
  310. List []*SandboxListItems
  311. }
  312. func AddSandbox(item *Sandbox) (lastId int64, err error) {
  313. //o := orm.NewOrmUsingDB("data")
  314. //lastId, err = o.Insert(item)
  315. err = global.DmSQL["data"].Create(item).Error
  316. return
  317. }
  318. //func GetSandboxItemsByClassifyId(sandboxClassifyId int) (list []*SandboxClassifyItems, err error) {
  319. // o := orm.NewOrmUsingDB("data")
  320. // sql := `SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name, sort
  321. // FROM sandbox WHERE sandbox_classify_id = ? AND is_delete = 0 ORDER BY sort `
  322. // _, err = o.Raw(sql, sandboxClassifyId).QueryRows(&list)
  323. // return
  324. //}
  325. func GetSandboxAllParentByClassifyId(sandboxClassifyId int) (ids string, err error) {
  326. //o := orm.NewOrmUsingDB("data")
  327. sql := `SELECT
  328. GROUP_CONCAT(DISTINCT m.sandbox_classify_id ORDER BY m.level) AS ids
  329. FROM
  330. (
  331. SELECT
  332. @id AS _id,(
  333. SELECT
  334. @id := parent_id
  335. FROM
  336. sandbox_classify
  337. WHERE
  338. sandbox_classify_id = _id
  339. )
  340. FROM
  341. (
  342. SELECT
  343. @id :=(
  344. SELECT
  345. parent_id
  346. FROM
  347. sandbox_classify
  348. WHERE
  349. sandbox_classify_id = ?
  350. )) vm,
  351. sandbox_classify m
  352. WHERE
  353. @id IS NOT NULL
  354. ) vm
  355. INNER JOIN sandbox_classify m
  356. WHERE
  357. sandbox_classify_id = vm._id `
  358. //err = o.Raw(sql, sandboxClassifyId).QueryRow(&ids)
  359. err = global.DmSQL["data"].Raw(sql, sandboxClassifyId).Scan(&ids).Error
  360. return
  361. }
  362. type MoveSandboxReq struct {
  363. SandboxId int `description:"沙盘ID"`
  364. PrevSandboxId int `description:"上一个沙盘ID"`
  365. NextSandboxId int `description:"下一个沙盘ID"`
  366. SandboxClassifyId int `description:"分类id"`
  367. }
  368. func GetSandboxClassifyCountById(classifyId int) (count int, err error) {
  369. //o := orm.NewOrmUsingDB("data")
  370. sql := `SELECT count(1) AS count FROM sandbox_classify WHERE sandbox_classify_id=? `
  371. //err = o.Raw(sql, classifyId).QueryRow(&count)
  372. err = global.DmSQL["data"].Raw(sql, classifyId).Scan(&count).Error
  373. return
  374. }
  375. // GetSandboxByClassifyIdAndName 根据分类id和沙盘名获取图表信息
  376. //func GetSandboxByClassifyIdAndName(classifyId int, name string) (item *Sandbox, err error) {
  377. // o := orm.NewOrmUsingDB("data")
  378. // sql := ` SELECT * FROM sandbox WHERE sandbox_classify_id = ? and name=? `
  379. // err = o.Raw(sql, classifyId, name).QueryRow(&item)
  380. // return
  381. //}
  382. // GetSandboxNameByIds 根据沙盘名称
  383. func GetSandboxNameByIds(ids []int) (items []*Sandbox, err error) {
  384. if len(ids) == 0 {
  385. return
  386. }
  387. //o := orm.NewOrmUsingDB("data")
  388. sql := ` SELECT sandbox_id, name FROM sandbox WHERE sandbox_id in (` + utils.GetOrmInReplace(len(ids)) + `) `
  389. //_, err = o.Raw(sql, ids).QueryRows(&items)
  390. err = global.DmSQL["data"].Raw(sql, ids).Find(&items).Error
  391. return
  392. }
  393. func MoveSandbox(sandboxId, classifyId int) (err error) {
  394. //o := orm.NewOrmUsingDB("data")
  395. sql := ` UPDATE sandbox
  396. SET
  397. sandbox_classify_id = ?
  398. WHERE sandbox_id = ?`
  399. //_, err = o.Raw(sql, classifyId, sandboxId).Exec()
  400. err = global.DmSQL["data"].Exec(sql, classifyId, sandboxId).Error
  401. return
  402. }
  403. // UpdateSandboxSortByClassifyId 根据沙盘id更新排序
  404. func UpdateSandboxSortByClassifyId(classifyId, nowSort, prevSandboxId int, updateSort string) (err error) {
  405. //o := orm.NewOrmUsingDB("data")
  406. sql := ` update sandbox set sort = ` + updateSort + ` WHERE sandbox_classify_id=? AND `
  407. if prevSandboxId > 0 {
  408. sql += ` (sort > ? or (sandbox_id > ` + fmt.Sprint(prevSandboxId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
  409. }
  410. //_, err = o.Raw(sql, classifyId, nowSort).Exec()
  411. err = global.DmSQL["data"].Exec(sql, classifyId, nowSort).Error
  412. return
  413. }
  414. // GetFirstSandboxByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  415. func GetFirstSandboxByClassifyId(classifyId int) (item *Sandbox, err error) {
  416. //o := orm.NewOrmUsingDB("data")
  417. sql := ` SELECT * FROM sandbox WHERE sandbox_classify_id=? order by sort asc,sandbox_id asc limit 1`
  418. //err = o.Raw(sql, classifyId).QueryRow(&item)
  419. err = global.DmSQL["data"].Raw(sql, classifyId).First(&item).Error
  420. return
  421. }
  422. // ContentDataStruct 沙盘内容结构体
  423. type ContentDataStruct struct {
  424. Cells []struct {
  425. Data *NodeData `json:"data,omitempty"`
  426. } `json:"cells"`
  427. }
  428. type NodeData struct {
  429. LinkData []*LinkData `json:"linkData"`
  430. LinkFold bool `json:"linkFold"`
  431. }
  432. type LinkData struct {
  433. RId string `json:"RId"`
  434. Id int `json:"Id"`
  435. Name string `json:"Name"`
  436. Type int `json:"Type"`
  437. Editing bool `json:"editing"`
  438. DatabaseType int `json:"databaseType"`
  439. DetailParams DetailParams `json:"detailParams"`
  440. }
  441. type DetailParams struct {
  442. Code string `json:"code"`
  443. Id int `json:"id"`
  444. ClassifyId int `json:"classifyId"`
  445. }
  446. // UpdateSandboxContent 更新沙盘内容
  447. func UpdateSandboxContent(list []Sandbox) (err error) {
  448. //o := orm.NewOrmUsingDB("data")
  449. //to, err := o.Begin()
  450. //if err != nil {
  451. // return
  452. //}
  453. //defer func() {
  454. // if err != nil {
  455. // _ = to.Rollback()
  456. // } else {
  457. // _ = to.Commit()
  458. // }
  459. //}()
  460. //
  461. ////循环更新沙盘内容
  462. //for _, sandbox := range list {
  463. // _, err = to.Update(&sandbox, "Content", "ModifyTime")
  464. // if err != nil {
  465. // return
  466. // }
  467. //}
  468. tx := global.DmSQL["data"].Begin()
  469. defer func() {
  470. if err != nil {
  471. _ = tx.Rollback()
  472. return
  473. }
  474. _ = tx.Commit()
  475. }()
  476. cols := []string{"Content", "ModifyTime"}
  477. for _, sandbox := range list {
  478. err = tx.Select(cols).Updates(sandbox).Error
  479. if err != nil {
  480. return
  481. }
  482. }
  483. return
  484. }