sandbox.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. package sandbox
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/company"
  6. "eta_gn/eta_api/models/sandbox"
  7. "eta_gn/eta_api/models/sandbox/request"
  8. "eta_gn/eta_api/models/system"
  9. "eta_gn/eta_api/services/alarm_msg"
  10. "eta_gn/eta_api/utils"
  11. "fmt"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // AddSandbox 新增沙盘
  17. //func AddSandbox(req request.AddAndEditSandbox, opUserId int, opUserName string, ignoreVariety bool) (resp *sandbox.SandboxSaveResp, err error) {
  18. // resp = new(sandbox.SandboxSaveResp)
  19. // // 获取产品权限详情
  20. // var permissionName string
  21. // if !ignoreVariety {
  22. // chartPermissionInfo, e := company.GetChartPermissionListById(req.ChartPermissionId)
  23. // if e != nil {
  24. // err = e
  25. // return
  26. // }
  27. // permissionName = chartPermissionInfo.PermissionName
  28. // }
  29. // //沙盘主表信息
  30. // sandboxInfo := &sandbox.Sandbox{
  31. // Name: utils.TrimStr(req.Name),
  32. // ChartPermissionId: req.ChartPermissionId,
  33. // ChartPermissionName: permissionName,
  34. // CurrVersion: 1,
  35. // Code: GenerateCode(),
  36. // Content: req.Content,
  37. // PicUrl: utils.TrimStr(req.PicUrl),
  38. // OpUserId: opUserId,
  39. // OpUserName: opUserName,
  40. // IsDelete: 0,
  41. // ModifyTime: time.Now(),
  42. // CreateTime: time.Now(),
  43. // }
  44. // //沙盘版本表信息
  45. // sandboxVersionInfo := &sandbox.SandboxVersion{
  46. // Name: sandboxInfo.Name,
  47. // ChartPermissionId: sandboxInfo.ChartPermissionId,
  48. // ChartPermissionName: sandboxInfo.ChartPermissionName,
  49. // CurrVersion: sandboxInfo.CurrVersion,
  50. // Content: sandboxInfo.Content,
  51. // PicUrl: sandboxInfo.PicUrl,
  52. // OpUserId: sandboxInfo.OpUserId,
  53. // OpUserName: sandboxInfo.OpUserName,
  54. // VersionCode: GenerateVersionCode(sandboxInfo.SandboxId, sandboxInfo.CurrVersion),
  55. // IsDelete: sandboxInfo.IsDelete,
  56. // CreateTime: sandboxInfo.CreateTime,
  57. // }
  58. // //沙盘草稿表信息
  59. // sandboxDraftInfo := &sandbox.SandboxDraft{
  60. // Name: sandboxInfo.Name,
  61. // ChartPermissionId: sandboxInfo.ChartPermissionId,
  62. // ChartPermissionName: sandboxInfo.ChartPermissionName,
  63. // CurrVersion: sandboxInfo.CurrVersion,
  64. // Content: sandboxInfo.Content,
  65. // OpUserId: sandboxInfo.OpUserId,
  66. // OpUserName: sandboxInfo.OpUserName,
  67. // CreateTime: sandboxInfo.CreateTime,
  68. // }
  69. //
  70. // //新增沙盘
  71. // err = sandbox.AddNewSandbox(sandboxInfo, sandboxVersionInfo, sandboxDraftInfo)
  72. // if err != nil {
  73. // return
  74. // }
  75. // resp.Sandbox = sandboxInfo
  76. // resp.VersionCode = sandboxVersionInfo.VersionCode
  77. // return
  78. //}
  79. // UpdateSandbox 更新沙盘
  80. //func UpdateSandbox(req request.AddAndEditSandbox, opUserId int, opUserName string, ignoreVariety bool) (resp *sandbox.SandboxSaveResp, err error, errMsg string) {
  81. // resp = new(sandbox.SandboxSaveResp)
  82. // // 获取沙盘版本信息
  83. // sandboxVersion, err := sandbox.GetSandboxVersionBySandboxVersionCode(req.SandboxVersionCode)
  84. // if err != nil {
  85. // if utils.IsErrNoRow(err) {
  86. // errMsg = "找不到该版本"
  87. // err = errors.New(errMsg)
  88. // }
  89. // return
  90. // }
  91. // // 获取沙盘主表信息
  92. // sandboxInfo, err := sandbox.GetSandboxById(sandboxVersion.SandboxId)
  93. // if err != nil {
  94. // if utils.IsErrNoRow(err) {
  95. // errMsg = "找不到该沙盘"
  96. // err = errors.New(errMsg)
  97. // }
  98. // return
  99. // }
  100. // //沙盘名称是否更改校验
  101. // var isUpdateName, isUpdateContent bool
  102. // if sandboxInfo.Name != utils.TrimStr(req.Name) {
  103. // isUpdateName = true
  104. // }
  105. //
  106. // // 沙盘内容md5比对,不一致则代表有做更改
  107. // //if utils.MD5(sandboxInfo.Content) != utils.MD5(req.Content) {
  108. // // isUpdateContent = true
  109. // //}
  110. // if checkoutContent(sandboxInfo.Content, req.Content) {
  111. // isUpdateContent = true
  112. // }
  113. //
  114. // //如果沙盘名称和沙盘内容都没有做过修改,那么就不做保存
  115. // if isUpdateName == false && isUpdateContent == false {
  116. // return
  117. // }
  118. //
  119. // // 获取产品权限详情
  120. // var permissionName string
  121. // if !ignoreVariety {
  122. // chartPermissionInfo, e := company.GetChartPermissionListById(req.ChartPermissionId)
  123. // if e != nil {
  124. // err = e
  125. // return
  126. // }
  127. // permissionName = chartPermissionInfo.PermissionName
  128. // }
  129. //
  130. // //如果只更新了沙盘名称,那么只去修改最新版本的沙盘名称,而不去累计版本
  131. // if isUpdateName == true && isUpdateContent == false {
  132. // sandboxInfo.Name = utils.TrimStr(req.Name)
  133. // sandboxInfo.ChartPermissionId = req.ChartPermissionId
  134. // sandboxInfo.ChartPermissionName = permissionName
  135. // sandboxInfo.Content = req.Content
  136. // sandboxInfo.PicUrl = utils.TrimStr(req.PicUrl)
  137. // sandboxInfo.OpUserId = opUserId
  138. // sandboxInfo.OpUserName = opUserName
  139. // sandboxInfo.ModifyTime = time.Now()
  140. // var updateSandboxColumn = []string{"Name", "ChartPermissionId", "ChartPermissionName", "PicUrl", "OpUserId", "OpUserName", "ModifyTime"}
  141. //
  142. // //沙盘版本表信息
  143. // sandboxVersionInfo, tmpErr := sandbox.GetSandboxVersionBySandbox2VersionId(sandboxInfo.SandboxId, sandboxInfo.CurrVersion)
  144. // if tmpErr != nil {
  145. // err = tmpErr
  146. // return
  147. // }
  148. // sandboxVersionInfo.Name = sandboxInfo.Name
  149. // sandboxVersionInfo.ChartPermissionId = sandboxInfo.ChartPermissionId
  150. // sandboxVersionInfo.ChartPermissionName = sandboxInfo.ChartPermissionName
  151. // sandboxVersionInfo.PicUrl = sandboxInfo.PicUrl
  152. // sandboxVersionInfo.OpUserId = sandboxInfo.OpUserId
  153. // sandboxVersionInfo.OpUserName = sandboxInfo.OpUserName
  154. // var updateSandboxVersionColumn = []string{"Name", "ChartPermissionId", "ChartPermissionName", "PicUrl", "OpUserId", "OpUserName"}
  155. //
  156. // //沙盘草稿表信息
  157. // sandboxDraftInfo := &sandbox.SandboxDraft{
  158. // Name: sandboxInfo.Name,
  159. // ChartPermissionId: sandboxInfo.ChartPermissionId,
  160. // ChartPermissionName: sandboxInfo.ChartPermissionName,
  161. // CurrVersion: sandboxInfo.CurrVersion,
  162. // Content: sandboxInfo.Content,
  163. // OpUserId: sandboxInfo.OpUserId,
  164. // OpUserName: sandboxInfo.OpUserName,
  165. // CreateTime: time.Now(),
  166. // }
  167. //
  168. // //修改沙盘
  169. // err = sandbox.UpdateSandboxName(sandboxInfo, sandboxVersionInfo, sandboxDraftInfo, updateSandboxColumn, updateSandboxVersionColumn)
  170. // if err != nil {
  171. // return
  172. // }
  173. // resp.Sandbox = sandboxInfo
  174. // resp.VersionCode = sandboxVersionInfo.VersionCode
  175. // } else {
  176. // sandboxInfo.Name = utils.TrimStr(req.Name)
  177. // sandboxInfo.ChartPermissionId = req.ChartPermissionId
  178. // sandboxInfo.ChartPermissionName = permissionName
  179. // sandboxInfo.CurrVersion = sandboxInfo.CurrVersion + 1
  180. // sandboxInfo.Content = req.Content
  181. // sandboxInfo.PicUrl = utils.TrimStr(req.PicUrl)
  182. // sandboxInfo.OpUserId = opUserId
  183. // sandboxInfo.OpUserName = opUserName
  184. // sandboxInfo.ModifyTime = time.Now()
  185. //
  186. // var updateSandbox = []string{"Name", "ChartPermissionId", "ChartPermissionName", "CurrVersion", "Content", "PicUrl", "OpUserId", "OpUserName", "ModifyTime"}
  187. //
  188. // //沙盘版本表信息
  189. // sandboxVersionInfo := &sandbox.SandboxVersion{
  190. // Name: sandboxInfo.Name,
  191. // ChartPermissionId: sandboxInfo.ChartPermissionId,
  192. // ChartPermissionName: sandboxInfo.ChartPermissionName,
  193. // CurrVersion: sandboxInfo.CurrVersion,
  194. // Content: sandboxInfo.Content,
  195. // SvgData: req.SvgData,
  196. // PicUrl: sandboxInfo.PicUrl,
  197. // OpUserId: sandboxInfo.OpUserId,
  198. // OpUserName: sandboxInfo.OpUserName,
  199. // VersionCode: GenerateVersionCode(sandboxInfo.SandboxId, sandboxInfo.CurrVersion),
  200. // IsDelete: sandboxInfo.IsDelete,
  201. // CreateTime: time.Now(),
  202. // }
  203. // //沙盘草稿表信息
  204. // sandboxDraftInfo := &sandbox.SandboxDraft{
  205. // Name: sandboxInfo.Name,
  206. // ChartPermissionId: sandboxInfo.ChartPermissionId,
  207. // ChartPermissionName: sandboxInfo.ChartPermissionName,
  208. // CurrVersion: sandboxInfo.CurrVersion,
  209. // Content: sandboxInfo.Content,
  210. // OpUserId: sandboxInfo.OpUserId,
  211. // OpUserName: sandboxInfo.OpUserName,
  212. // CreateTime: time.Now(),
  213. // }
  214. //
  215. // //修改沙盘
  216. // err = sandbox.UpdateSandbox(sandboxInfo, updateSandbox, sandboxVersionInfo, sandboxDraftInfo)
  217. // if err != nil {
  218. // return
  219. // }
  220. // resp.Sandbox = sandboxInfo
  221. // resp.VersionCode = sandboxVersionInfo.VersionCode
  222. // }
  223. // return
  224. //}
  225. // AddSandboxDraft 新增沙盘草稿
  226. func AddSandboxDraft(sandboxId int, req request.AddAndEditSandbox, opUserId int, opUserName string) (sandboxDraftInfo *sandbox.SandboxDraft, err error) {
  227. //获取最近一条草稿箱的数据
  228. lastSandboxDraft, err := sandbox.GetLastSandboxDraftById(sandboxId)
  229. if err != nil && !utils.IsErrNoRow(err) {
  230. return
  231. }
  232. //如果最近一条沙盘草稿数据不为空的话,那么需要校验下最近一次的图表数据是否与提交的这次一致
  233. if lastSandboxDraft != nil {
  234. //沙盘名称是否更改校验
  235. var isUpdateName, isUpdateContent bool
  236. if lastSandboxDraft.Name != utils.TrimStr(req.Name) {
  237. isUpdateName = true
  238. }
  239. // 沙盘内容md5比对,不一致则代表有做更改
  240. //if utils.MD5(lastSandboxDraft.Content) != utils.MD5(req.Content) {
  241. // isUpdateContent = true
  242. //}
  243. if checkoutContent(lastSandboxDraft.Content, req.Content) {
  244. isUpdateContent = true
  245. }
  246. //如果沙盘名称和沙盘内容都没有做过修改,那么就不做保存
  247. if isUpdateName == false && isUpdateContent == false {
  248. return
  249. }
  250. }
  251. // 获取产品权限详情
  252. chartPermissionInfo, err := company.GetChartPermissionListById(req.ChartPermissionId)
  253. if err != nil {
  254. return
  255. }
  256. //沙盘草稿表信息
  257. sandboxDraftInfo = &sandbox.SandboxDraft{
  258. SandboxId: sandboxId,
  259. Name: utils.TrimStr(req.Name),
  260. ChartPermissionId: req.ChartPermissionId,
  261. ChartPermissionName: chartPermissionInfo.PermissionName,
  262. Content: req.Content,
  263. OpUserId: opUserId,
  264. OpUserName: opUserName,
  265. CreateTime: time.Now(),
  266. }
  267. //新增沙盘草稿
  268. err = sandbox.AddSandboxDraft(sandboxDraftInfo)
  269. return
  270. }
  271. // UpdateSandboxEditMark 更新沙盘当前更新状态
  272. // status 枚举值 1:编辑中,0:完成编辑, 2:只做查询
  273. func UpdateSandboxEditMark(sandboxId, nowUserId, status int, nowUserName string) (ret models.MarkReportResp, err error) {
  274. //更新标记key
  275. key := fmt.Sprint(`crm:sandbox:edit:`, sandboxId)
  276. opUserId, e := utils.Rc.RedisInt(key)
  277. var opUser models.MarkReportItem
  278. if e != nil {
  279. opUserInfoStr, tErr := utils.Rc.RedisString(key)
  280. if tErr == nil {
  281. tErr = json.Unmarshal([]byte(opUserInfoStr), &opUser)
  282. if tErr == nil {
  283. opUserId = opUser.AdminId
  284. }
  285. }
  286. }
  287. if opUserId > 0 && opUserId != nowUserId {
  288. editor := opUser.Editor
  289. if editor == "" {
  290. //查询账号的用户姓名
  291. otherInfo, e := system.GetSysAdminById(opUserId)
  292. if e != nil {
  293. err = fmt.Errorf("查询其他编辑者信息失败")
  294. return
  295. }
  296. editor = otherInfo.RealName
  297. }
  298. ret.Status = 1
  299. ret.Msg = fmt.Sprintf("当前%s正在编辑中", editor)
  300. ret.Editor = editor
  301. return
  302. }
  303. if status == 1 {
  304. nowUser := &models.MarkReportItem{AdminId: nowUserId, Editor: nowUserName}
  305. bt, e := json.Marshal(nowUser)
  306. if e != nil {
  307. err = fmt.Errorf("格式化编辑者信息失败")
  308. return
  309. }
  310. if opUserId > 0 {
  311. utils.Rc.Do("SETEX", key, int64(300), string(bt)) //3分钟缓存
  312. } else {
  313. utils.Rc.SetNX(key, string(bt), time.Second*60*5) //3分钟缓存
  314. }
  315. } else if status == 0 {
  316. //清除编辑缓存
  317. _ = utils.Rc.Delete(key)
  318. }
  319. return
  320. }
  321. // ResetDraftToLastVersion 重置沙盘草稿至最新版本
  322. //func ResetDraftToLastVersion(sandboxId, opUserId int, opUserName string) (sandboxDraftInfo *sandbox.SandboxDraft, err error) {
  323. // // 获取沙盘主表信息
  324. // sandboxInfo, err := sandbox.GetSandboxById(sandboxId)
  325. // if err != nil {
  326. // return
  327. // }
  328. //
  329. // //沙盘草稿表信息
  330. // sandboxDraftInfo = &sandbox.SandboxDraft{
  331. // SandboxId: sandboxInfo.SandboxId,
  332. // Name: sandboxInfo.Name,
  333. // ChartPermissionId: sandboxInfo.ChartPermissionId,
  334. // ChartPermissionName: sandboxInfo.ChartPermissionName,
  335. // Content: sandboxInfo.Content,
  336. // OpUserId: opUserId,
  337. // OpUserName: opUserName,
  338. // CreateTime: time.Now(),
  339. // }
  340. //
  341. // //新增沙盘草稿
  342. // err = sandbox.AddSandboxDraft(sandboxDraftInfo)
  343. // return
  344. //}
  345. // DeleteSandbox 删除沙盘
  346. func DeleteSandbox(sandboxId int) (err error) {
  347. // 获取沙盘主表信息
  348. sandboxInfo, err := sandbox.GetSandboxById(sandboxId)
  349. if err != nil {
  350. return
  351. }
  352. sandboxInfo.IsDelete = 1
  353. var updateSandboxColumn = []string{"IsDelete"}
  354. err = sandboxInfo.Update(updateSandboxColumn)
  355. return
  356. }
  357. // DeleteSandboxVersion 删除沙盘版本
  358. //func DeleteSandboxVersion(sandboxVersionCode string, opUserId int) (err error, errMsg string) {
  359. // // 获取沙盘版本信息
  360. // sandboxVersion, err := sandbox.GetSandboxVersionBySandboxVersionCode(sandboxVersionCode)
  361. // if err != nil {
  362. // if utils.IsErrNoRow(err) {
  363. // errMsg = "找不到该版本"
  364. // err = errors.New(errMsg)
  365. // }
  366. // return
  367. // }
  368. //
  369. // /*key := fmt.Sprint(`crm:sandbox:edit:`, sandboxVersion.SandboxId)
  370. // nowOpUserId, _ := utils.Rc.RedisInt(key)
  371. // //如果当前有人操作,且获取当前操作人不是本人,那么不允许删除
  372. // if nowOpUserId > 0 && nowOpUserId != opUserId {
  373. // errMsg = "当前有其他人正在编辑,不允许删除该沙盘"
  374. // err = errors.New(errMsg)
  375. // return
  376. // }*/
  377. //
  378. // markStatus, err := UpdateSandboxEditMark(sandboxVersion.SandboxId, opUserId, 2, "")
  379. // if err != nil {
  380. // errMsg = "查询标记状态失败"
  381. // err = errors.New("查询标记状态失败,Err:" + err.Error())
  382. // return
  383. // }
  384. // if markStatus.Status == 1 {
  385. // errMsg = fmt.Sprintf("当前%s正在编辑,不允许删除该沙盘", markStatus.Editor)
  386. // err = errors.New(errMsg)
  387. // return
  388. // }
  389. //
  390. // // 获取沙盘主表信息
  391. // sandboxInfo, err := sandbox.GetSandboxById(sandboxVersion.SandboxId)
  392. // if err != nil {
  393. // return
  394. // }
  395. //
  396. // // 删除最新版本,需要将上一个版本的给找出来覆盖
  397. // if sandboxVersion.CurrVersion == sandboxInfo.CurrVersion {
  398. // lastSandboxVersion, tmpErr := sandbox.GetLastSandboxVersionBySandbox2VersionId(sandboxInfo.SandboxId, sandboxVersion.CurrVersion)
  399. // if tmpErr != nil {
  400. // // 如果找不到,说明是删除整个沙盘,不仅仅是某个版本
  401. // if utils.IsErrNoRow(tmpErr) {
  402. // sandboxInfo.IsDelete = 1
  403. // var updateSandboxColumn = []string{"IsDelete"}
  404. // err = sandboxInfo.Update(updateSandboxColumn)
  405. // return
  406. // }
  407. // err = tmpErr
  408. // return
  409. // } else {
  410. // //将当前沙盘信息修复到上一个版本
  411. // sandboxInfo.Content = lastSandboxVersion.Content
  412. // sandboxInfo.CurrVersion = lastSandboxVersion.CurrVersion
  413. // sandboxInfo.PicUrl = lastSandboxVersion.PicUrl
  414. // err = sandboxInfo.Update([]string{"Content", "CurrVersion", "PicUrl"})
  415. // if err != nil {
  416. // return
  417. // }
  418. // }
  419. // }
  420. // //将原来的版本标记删除
  421. // sandboxVersion.IsDelete = 1
  422. // err = sandboxVersion.Update([]string{"IsDelete"})
  423. //
  424. // return
  425. //}
  426. // GetSandboxVersionDetailByCode 获取沙盘的版本数据
  427. func GetSandboxVersionDetailByCode(sandboxVersionCode string) (sandboxVersionInfo *sandbox.SandboxVersion, err error) {
  428. // 获取沙盘版本信息
  429. sandboxVersionInfo, err = sandbox.GetSandboxVersionBySandboxVersionCode(sandboxVersionCode)
  430. return
  431. }
  432. // GetLastSandboxInfo 获取最后一次操作的沙盘数据
  433. func GetLastSandboxInfo(sandboxId int) (sandboxInfo *sandbox.Sandbox, err error) {
  434. // 获取沙盘主表信息
  435. sandboxInfo, err = sandbox.GetSandboxById(sandboxId)
  436. return
  437. //if err != nil {
  438. // return
  439. //}
  440. //
  441. ////获取最近一条草稿箱的数据(写到一半没有保存,然后退出去了)
  442. //lastSandboxDraft, err := sandbox.GetLastSandboxDraftById(sandboxId)
  443. //if err != nil && !utils.IsErrNoRow(err) {
  444. // return
  445. //}
  446. ////如果最近一条沙盘草稿数据不为空的话,那么需要校验下最近一次的图表数据是否与提交的这次一致
  447. //if lastSandboxDraft != nil {
  448. // sandboxInfo = &sandbox.Sandbox{
  449. // SandboxId: sandboxId,
  450. // Name: lastSandboxDraft.Name,
  451. // ChartPermissionId: lastSandboxDraft.ChartPermissionId,
  452. // ChartPermissionName: lastSandboxDraft.ChartPermissionName,
  453. // CurrVersion: lastSandboxDraft.CurrVersion,
  454. // Content: lastSandboxDraft.Content,
  455. // OpUserId: lastSandboxDraft.OpUserId,
  456. // OpUserName: lastSandboxDraft.OpUserName,
  457. // IsDelete: 0,
  458. // ModifyTime: lastSandboxDraft.CreateTime,
  459. // CreateTime: lastSandboxDraft.CreateTime,
  460. // }
  461. //}
  462. return
  463. }
  464. // GenerateCode 生成沙盘code
  465. func GenerateCode() string {
  466. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  467. return utils.MD5(fmt.Sprint("sandbox_") + timestamp)
  468. }
  469. // GenerateVersionCode 生成沙盘版本code
  470. func GenerateVersionCode(sandboxId, sandboxVersion int) string {
  471. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  472. return utils.MD5(fmt.Sprint("sandbox_version_", sandboxId, "_", sandboxVersion) + timestamp)
  473. }
  474. // ContentStruct 沙盘内容结构体
  475. type ContentStruct struct {
  476. Cells []struct {
  477. Attrs struct {
  478. Line struct {
  479. SourceMarker bool `json:"sourceMarker"`
  480. Stroke string `json:"stroke"`
  481. StrokeDasharray string `json:"strokeDasharray"`
  482. } `json:"line"`
  483. Rect struct {
  484. Fill string `json:"fill"`
  485. Stroke string `json:"stroke"`
  486. StrokeDasharray interface{} `json:"strokeDasharray"`
  487. StrokeWidth int64 `json:"strokeWidth"`
  488. } `json:"rect"`
  489. Text struct {
  490. Fill string `json:"fill"`
  491. FontSize float64 `json:"fontSize"`
  492. FontWeight string `json:"fontWeight"`
  493. LineHeight float64 `json:"lineHeight"`
  494. Text string `json:"text"`
  495. TextWrap struct {
  496. Text string `json:"text"`
  497. Width int64 `json:"width"`
  498. } `json:"textWrap"`
  499. } `json:"text"`
  500. } `json:"attrs"`
  501. Data struct {
  502. Key string `json:"key"`
  503. } `json:"data"`
  504. ID string `json:"id"`
  505. Ports struct {
  506. Groups struct {
  507. Port_bottom struct {
  508. Attrs struct {
  509. Circle struct {
  510. Fill string `json:"fill"`
  511. Magnet bool `json:"magnet"`
  512. R int64 `json:"r"`
  513. Stroke string `json:"stroke"`
  514. StrokeWidth int64 `json:"strokeWidth"`
  515. } `json:"circle"`
  516. } `json:"attrs"`
  517. Position string `json:"position"`
  518. ZIndex int64 `json:"zIndex"`
  519. } `json:"port-bottom"`
  520. Port_left struct {
  521. Attrs struct {
  522. Circle struct {
  523. Fill string `json:"fill"`
  524. Magnet bool `json:"magnet"`
  525. R int64 `json:"r"`
  526. Stroke string `json:"stroke"`
  527. StrokeWidth int64 `json:"strokeWidth"`
  528. } `json:"circle"`
  529. } `json:"attrs"`
  530. Position string `json:"position"`
  531. ZIndex int64 `json:"zIndex"`
  532. } `json:"port-left"`
  533. Port_right struct {
  534. Attrs struct {
  535. Circle struct {
  536. Fill string `json:"fill"`
  537. Magnet bool `json:"magnet"`
  538. R int64 `json:"r"`
  539. Stroke string `json:"stroke"`
  540. StrokeWidth int64 `json:"strokeWidth"`
  541. } `json:"circle"`
  542. } `json:"attrs"`
  543. Position string `json:"position"`
  544. ZIndex int64 `json:"zIndex"`
  545. } `json:"port-right"`
  546. Port_top struct {
  547. Attrs struct {
  548. Circle struct {
  549. Fill string `json:"fill"`
  550. Magnet bool `json:"magnet"`
  551. R int64 `json:"r"`
  552. Stroke string `json:"stroke"`
  553. StrokeWidth int64 `json:"strokeWidth"`
  554. } `json:"circle"`
  555. } `json:"attrs"`
  556. Position string `json:"position"`
  557. ZIndex int64 `json:"zIndex"`
  558. } `json:"port-top"`
  559. } `json:"groups"`
  560. Items []struct {
  561. Group string `json:"group"`
  562. ID string `json:"id"`
  563. } `json:"items"`
  564. } `json:"ports"`
  565. Position struct {
  566. X float64 `json:"x"`
  567. Y float64 `json:"y"`
  568. } `json:"position"`
  569. Shape string `json:"shape"`
  570. Size struct {
  571. Height float64 `json:"height"`
  572. Width float64 `json:"width"`
  573. } `json:"size"`
  574. Source struct {
  575. Cell string `json:"cell"`
  576. Port string `json:"port"`
  577. } `json:"source"`
  578. Target struct {
  579. Cell string `json:"cell"`
  580. Port string `json:"port"`
  581. } `json:"target"`
  582. ZIndex int64 `json:"zIndex"`
  583. } `json:"cells"`
  584. }
  585. type SendBoxNodeData struct {
  586. linkData []SandBoxLinkData `json:"linkData"`
  587. linkFold bool `json:"linkFold"`
  588. }
  589. type SandBoxLinkData struct {
  590. RId string `json:"RId"`
  591. Id int `json:"Id"`
  592. Name string `json:"Name"`
  593. Type int `json:"Type"`
  594. Editing bool `json:"editing"`
  595. DatabaseType int `json:"databaseType"`
  596. DetailParams SandBoxDetailParams `json:"detailParams"`
  597. }
  598. type SandBoxDetailParams struct {
  599. Code string `json:"code"`
  600. Id int `json:"id"`
  601. ClassifyId int `json:"classifyId"`
  602. }
  603. // checkoutContent 校验内容是否变更
  604. func checkoutContent(oldContent, reqContent string) (isUpdate bool) {
  605. defer func() {
  606. // 沙盘内容md5比对,不一致则代表有做更改
  607. if utils.MD5(oldContent) != utils.MD5(reqContent) {
  608. isUpdate = true
  609. }
  610. }()
  611. var oldContentInfo, reqContentInfo ContentStruct
  612. err := json.Unmarshal([]byte(oldContent), &oldContentInfo)
  613. if err != nil {
  614. fmt.Println("old json.Unmarshal err:", err)
  615. return
  616. }
  617. oldContentInfoByte, err := json.Marshal(oldContentInfo)
  618. if err != nil {
  619. fmt.Println("old json.Marshal err:", err)
  620. return
  621. }
  622. oldContent = string(oldContentInfoByte)
  623. err = json.Unmarshal([]byte(reqContent), &reqContentInfo)
  624. if err != nil {
  625. fmt.Println("req json.Unmarshal err:", err)
  626. return
  627. }
  628. reqContentInfoByte, err := json.Marshal(reqContentInfo)
  629. if err != nil {
  630. fmt.Println("req json.Marshal err:", err)
  631. return
  632. }
  633. reqContent = string(reqContentInfoByte)
  634. return
  635. }
  636. func sandboxClassifyHaveChild(allNode []*sandbox.SandboxClassifyItems, node *sandbox.SandboxClassifyItems) (childs []*sandbox.SandboxClassifyItems, yes bool) {
  637. for _, v := range allNode {
  638. if v.ParentId == node.SandboxClassifyId {
  639. childs = append(childs, v)
  640. }
  641. }
  642. if len(childs) > 0 {
  643. yes = true
  644. }
  645. return
  646. }
  647. func SandboxClassifyItemsMakeTree(sysUser *system.Admin, allNode []*sandbox.SandboxClassifyItems, node *sandbox.SandboxClassifyItems) {
  648. childs, _ := sandboxClassifyHaveChild(allNode, node) //判断节点是否有子节点并返回
  649. if len(childs) > 0 {
  650. node.Children = append(node.Children, childs[0:]...) //添加子节点
  651. for _, v := range childs { //查询子节点的子节点,并添加到子节点
  652. _, has := sandboxClassifyHaveChild(allNode, v)
  653. if has {
  654. SandboxClassifyItemsMakeTree(sysUser, allNode, v) //递归添加节点
  655. } else {
  656. childrenArr := make([]*sandbox.SandboxClassifyItems, 0)
  657. v.Children = childrenArr
  658. }
  659. }
  660. } else {
  661. childrenArr := make([]*sandbox.SandboxClassifyItems, 0)
  662. node.Children = childrenArr
  663. }
  664. }
  665. // GetSandboxClassifyListForMe 获取我创建的沙盘
  666. func GetSandboxClassifyListForMe(adminInfo system.Admin, resp *sandbox.SandboxClassifyListResp, sandboxClassifyId int) (errMsg string, err error) {
  667. rootList, err := sandbox.GetSandboxClassifyByParentId(sandboxClassifyId)
  668. if err != nil && !utils.IsErrNoRow(err) {
  669. errMsg = "获取失败"
  670. return
  671. }
  672. classifyAll, err := sandbox.GetSandboxClassifyByParentId(sandboxClassifyId)
  673. if err != nil && !utils.IsErrNoRow(err) {
  674. errMsg = "获取失败"
  675. return
  676. }
  677. sandboxAll, err := sandbox.GetSandboxInfoByAdminId(adminInfo.AdminId)
  678. if err != nil && !utils.IsErrNoRow(err) {
  679. errMsg = "获取失败"
  680. return
  681. }
  682. sandListMap := make(map[int][]*sandbox.SandboxClassifyItems)
  683. for _, v := range sandboxAll {
  684. if _, ok := sandListMap[v.SandboxClassifyId]; !ok {
  685. list := make([]*sandbox.SandboxClassifyItems, 0)
  686. list = append(list, v)
  687. sandListMap[v.SandboxClassifyId] = list
  688. } else {
  689. sandListMap[v.SandboxClassifyId] = append(sandListMap[v.SandboxClassifyId], v)
  690. }
  691. }
  692. nodeAll := make([]*sandbox.SandboxClassifyItems, 0)
  693. for k := range rootList {
  694. rootNode := rootList[k]
  695. SandboxClassifyItemsMakeTree(&adminInfo, classifyAll, rootNode)
  696. nodeAll = append(nodeAll, rootNode)
  697. }
  698. //for k := range nodeAll {
  699. //
  700. //}
  701. newAll := SandboxItemsMakeTree(nodeAll, sandListMap, sandboxClassifyId)
  702. resp.AllNodes = newAll
  703. return
  704. }
  705. // HandleNoPermissionSandbox 图表列表返回,将没有权限的图表移除
  706. func HandleNoPermissionSandbox(allNodes []*sandbox.SandboxClassifyItems, noPermissionChartIdMap map[int]bool) (newAllNodes []*sandbox.SandboxClassifyItems) {
  707. // 移除没有权限的图表
  708. newAllNodes = make([]*sandbox.SandboxClassifyItems, 0)
  709. for _, node := range allNodes {
  710. // 二级分类
  711. tmpNodeInfo := *node
  712. tmpNodeList := make([]*sandbox.SandboxClassifyItems, 0)
  713. if node.Children != nil {
  714. for _, chartList := range node.Children {
  715. tmpInfo := *chartList
  716. tmpList := make([]*sandbox.SandboxClassifyItems, 0)
  717. if chartList.Children != nil {
  718. for _, chartInfo := range chartList.Children {
  719. thirdInfo := *chartInfo
  720. thirdList := make([]*sandbox.SandboxClassifyItems, 0)
  721. // 如果指标不可见,那么就不返回该指标
  722. if _, ok := noPermissionChartIdMap[chartInfo.SandboxId]; ok {
  723. continue
  724. }
  725. tmpList = append(tmpList, chartInfo)
  726. if chartInfo.Children != nil {
  727. for _, thirdChart := range chartInfo.Children {
  728. // 如果指标不可见,那么就不返回该指标
  729. if _, ok := noPermissionChartIdMap[chartInfo.SandboxId]; ok {
  730. continue
  731. }
  732. thirdList = append(thirdList, thirdChart)
  733. }
  734. }
  735. thirdInfo.Children = thirdList
  736. tmpList = append(tmpList, &thirdInfo)
  737. }
  738. }
  739. tmpInfo.Children = tmpList
  740. tmpNodeList = append(tmpNodeList, &tmpInfo)
  741. }
  742. }
  743. tmpNodeInfo.Children = tmpNodeList
  744. newAllNodes = append(newAllNodes, &tmpNodeInfo)
  745. }
  746. return
  747. }
  748. // AddSandboxV2 新增沙盘
  749. func AddSandboxV2(req request.AddAndEditSandboxV2, opUserId int, opUserName string) (resp *sandbox.SandboxSaveResp, err error) {
  750. resp = new(sandbox.SandboxSaveResp)
  751. //沙盘主表信息
  752. sandboxInfo := &sandbox.Sandbox{
  753. Name: utils.TrimStr(req.Name),
  754. Code: GenerateCode(),
  755. Content: req.Content,
  756. MindmapData: req.MindmapData,
  757. PicUrl: utils.TrimStr(req.PicUrl),
  758. SysUserId: opUserId,
  759. SysUserName: opUserName,
  760. IsDelete: 0,
  761. ModifyTime: time.Now(),
  762. CreateTime: time.Now(),
  763. SandboxClassifyId: req.SandboxClassifyId,
  764. Sort: 0,
  765. Style: req.Style,
  766. }
  767. //新增沙盘
  768. err = sandbox.AddSandbox(sandboxInfo)
  769. if err != nil {
  770. return
  771. }
  772. resp.Sandbox = sandboxInfo
  773. return
  774. }
  775. func SandboxItemsMakeTree(allNode []*sandbox.SandboxClassifyItems, sandListMap map[int][]*sandbox.SandboxClassifyItems, sandboxClassifyId int) (nodeAll []*sandbox.SandboxClassifyItems) {
  776. for k := range allNode {
  777. if len(allNode[k].Children) > 0 {
  778. SandboxItemsMakeTree(allNode[k].Children, sandListMap, sandboxClassifyId)
  779. allNode = append(allNode, sandListMap[allNode[k].ParentId]...)
  780. nodeAll = allNode
  781. } else if k == len(allNode)-1 {
  782. allNode = append(allNode, sandListMap[allNode[k].ParentId]...)
  783. nodeAll = allNode
  784. }
  785. }
  786. if len(allNode) == 0 {
  787. nodeAll = append(nodeAll, sandListMap[sandboxClassifyId]...)
  788. }
  789. return
  790. }
  791. func SandboxClassifyHaveChild(allNode []*sandbox.SandboxClassifyItems, node *sandbox.SandboxClassifyItems) (childs []*sandbox.SandboxClassifyItems, yes bool) {
  792. for _, v := range allNode {
  793. if v.ParentId == node.SandboxClassifyId {
  794. childs = append(childs, v)
  795. }
  796. }
  797. if len(childs) > 0 {
  798. yes = true
  799. }
  800. return
  801. }
  802. func SandboxClassifyItemsMakeTreeV2(sysUser *system.Admin, allNode []*sandbox.SandboxClassifyItems, node *sandbox.SandboxClassifyItems) {
  803. childs, _ := sandboxClassifyHaveChildV2(allNode, node) //判断节点是否有子节点并返回
  804. if len(childs) > 0 {
  805. node.Children = append(node.Children, childs[0:]...) //添加子节点
  806. for _, v := range childs { //查询子节点的子节点,并添加到子节点
  807. _, has := sandboxClassifyHaveChildV2(allNode, v)
  808. if has {
  809. SandboxClassifyItemsMakeTreeV2(sysUser, allNode, v) //递归添加节点
  810. } else {
  811. //childrenArr := make([]*sandbox.SandboxClassifyItems, 0)
  812. //v.Children = childrenArr
  813. }
  814. }
  815. } else {
  816. //childrenArr := make([]*sandbox.SandboxClassifyItems, 0)
  817. //node.Children = childrenArr
  818. }
  819. }
  820. func sandboxClassifyHaveChildV2(allNode []*sandbox.SandboxClassifyItems, node *sandbox.SandboxClassifyItems) (childs []*sandbox.SandboxClassifyItems, yes bool) {
  821. for _, v := range allNode {
  822. if v.ParentId == node.SandboxClassifyId && node.SandboxId == 0 {
  823. childs = append(childs, v)
  824. }
  825. }
  826. if len(childs) > 0 {
  827. yes = true
  828. }
  829. return
  830. }
  831. func GetSandBoxEdbIdsByContent(content string) (edbInfoIds []int, err error) {
  832. var contentInfo sandbox.ContentDataStruct
  833. err = json.Unmarshal([]byte(content), &contentInfo)
  834. if err != nil {
  835. err = fmt.Errorf("json.Unmarshal err:%s", err.Error())
  836. return
  837. }
  838. // 遍历所有节点
  839. for _, node := range contentInfo.Cells {
  840. if node.Data == nil {
  841. continue
  842. }
  843. for _, v := range node.Data.LinkData {
  844. if v.Type == 1 {
  845. edbInfoIds = append(edbInfoIds, v.Id)
  846. }
  847. }
  848. }
  849. return
  850. }
  851. func ReplaceEdbInSandbox(oldEdbInfoId, newEdbInfoId, userId int) (err error) {
  852. updateTotal := 0
  853. logMsg := ""
  854. //分页处理沙盘表
  855. defer func() {
  856. if err != nil {
  857. go alarm_msg.SendAlarmMsg("替换沙盘中的指标记录失败提醒,errmsg:"+err.Error(), 3)
  858. }
  859. if logMsg != "" {
  860. utils.FileLog.Info(fmt.Sprintf("替换ETA逻辑的指标记录,替换总数:%d,旧的指标id:%d,新的指标id:%d;%s", updateTotal, oldEdbInfoId, newEdbInfoId, logMsg))
  861. }
  862. }()
  863. //查询沙盘总数
  864. total, err := sandbox.GetSandboxListCountByCondition(" AND sys_user_id = ? ", []interface{}{userId})
  865. if err != nil {
  866. err = fmt.Errorf("查询沙盘总数失败 Err:%s", err)
  867. return
  868. }
  869. // 根据沙盘列表总数,分页查询
  870. // 计算总页数
  871. totalPage := (total + 99) / 100 // 使用整数除法,并添加一页以防有余数
  872. updateSandBox := make([]sandbox.Sandbox, 0)
  873. //查询沙盘列表
  874. for i := 0; i < totalPage; i += 1 {
  875. startSize := i * 100
  876. list, e := sandbox.GetSandboxListByCondition(" AND sys_user_id = ? ", []interface{}{userId}, startSize, 100)
  877. if e != nil {
  878. err = fmt.Errorf("查询沙盘列表失败 Err:%s", e)
  879. return
  880. }
  881. for _, v := range list {
  882. sandOldEdbId := fmt.Sprintf(`"RId":"1-%d","Id":%d,`, oldEdbInfoId, oldEdbInfoId)
  883. if strings.Contains(v.Content, sandOldEdbId) {
  884. sandNewEdbId := fmt.Sprintf(`"RId":"1-%d","Id":%d,`, newEdbInfoId, newEdbInfoId)
  885. v.Sandbox.Content = strings.ReplaceAll(v.Content, sandOldEdbId, sandNewEdbId)
  886. updateSandBox = append(updateSandBox, v.Sandbox)
  887. logMsg += `涉及到的逻辑id:` + strconv.Itoa(v.Sandbox.SandboxId) + ";"
  888. if len(updateSandBox) > 100 {
  889. err = sandbox.UpdateSandboxContent(updateSandBox)
  890. if err != nil {
  891. err = fmt.Errorf("更新沙盘表失败 Err:%s", err)
  892. return
  893. }
  894. updateTotal += len(updateSandBox)
  895. updateSandBox = make([]sandbox.Sandbox, 0)
  896. }
  897. }
  898. }
  899. }
  900. if len(updateSandBox) > 0 {
  901. err = sandbox.UpdateSandboxContent(updateSandBox)
  902. if err != nil {
  903. err = fmt.Errorf("更新沙盘表失败 Err:%s", err)
  904. return
  905. }
  906. updateTotal += len(updateSandBox)
  907. }
  908. return
  909. }
  910. func GetSandBoxParentIds(classifyId int, classifymap map[int]*sandbox.SandboxClassifyItems, parentIds *[]int) {
  911. if item, ok := classifymap[classifyId]; ok {
  912. if item.ParentId == 0 {
  913. return
  914. }
  915. *parentIds = append(*parentIds, item.ParentId)
  916. GetSandBoxParentIds(item.ParentId, classifymap, parentIds)
  917. }
  918. }
  919. func GetSandBoxClassifyChildIds(classifyId int, classifymap map[int]*sandbox.SandboxClassifyItems, childIds *[]int) {
  920. for _, item := range classifymap {
  921. if item.ParentId == classifyId {
  922. *childIds = append(*childIds, item.SandboxClassifyId)
  923. GetSandBoxClassifyChildIds(item.SandboxClassifyId, classifymap, childIds)
  924. }
  925. }
  926. }