sandbox.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. type Sandbox struct {
  10. SandboxId int `gorm:"primaryKey;column:sandbox_id;type:int(9) unsigned;not null"` // 沙盘id
  11. Name string `gorm:"index:idx_name;column:name;type:varchar(64);not null;default:''"` // 沙盘名称
  12. Code string `gorm:"column:code;type:varchar(255);not null"` // 沙盘code
  13. Content string `gorm:"column:content;type:longtext;not null"` // 沙盘内容
  14. MindmapData string `gorm:"column:mindmap_data;type:longtext;not null"` // 思维导图内容,
  15. PicUrl string `gorm:"column:pic_url;type:varchar(255);not null;default:''"` // 沙盘图片
  16. IsDelete int `gorm:"column:is_delete;type:tinyint(9) unsigned;not null;default:0"` // 是否删除,0:未删除,1:已删除
  17. ModifyTime time.Time `gorm:"column:modify_time;type:timestamp;default:CURRENT_TIMESTAMP"` // 最近一次更新时间
  18. CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP"` // 沙盘创建时间
  19. Sort int `gorm:"column:sort;type:int(9);default:0"` // 排序字段,数字越小越排前面
  20. SandboxClassifyId int `gorm:"column:sandbox_classify_id;type:int(11);default:0"` // 分类id
  21. SysUserId int `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0"` // 创建人
  22. SysUserName string `gorm:"column:sys_user_name;type:varchar(32);not null;default:''"` // 创建人名
  23. ChartPermissionId int `gorm:"column:chart_permission_id;type:int(9) unsigned;not null;default:0"` // 品种id
  24. ChartPermissionName string `gorm:"column:chart_permission_name;type:varchar(32);not null;default:''"` // 品种名称(冗余字段,避免列表页查询时再去关联表查询)
  25. Style int `gorm:"column:style;type:int(8)"` // 风格
  26. }
  27. // Update 沙盘字段变更
  28. func (sandbox *Sandbox) Update(cols []string) (err error) {
  29. err = global.DmSQL["data"].Select(cols).Updates(sandbox).Error
  30. return
  31. }
  32. // GetSandboxById 根据沙盘id获取沙盘详情
  33. func GetSandboxById(sandboxId int) (sandboxInfo *Sandbox, err error) {
  34. sql := `select * from sandbox where sandbox_id = ? and is_delete = 0`
  35. err = global.DmSQL["data"].Raw(sql, sandboxId).First(&sandboxInfo).Error
  36. return
  37. }
  38. // SandboxListItem 沙盘推演列表数据
  39. type SandboxListItem struct {
  40. SandboxId int `description:"沙盘id"`
  41. Name string `description:"沙盘名称"`
  42. ChartPermissionId int `description:"品种id"`
  43. ChartPermissionName string `description:"品种名称"`
  44. CurrVersion int `description:"当前版本"`
  45. Code string `description:"沙盘code"`
  46. VersionCode string `description:"沙盘版本code"`
  47. //Content string `description:"沙盘数据"`
  48. PicUrl string `description:"沙盘图片地址"`
  49. OpUserId int `description:"最近一次编辑操作的用户id"`
  50. OpUserName string `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
  51. IsDelete int8 `description:"是否删除,0:未删除,1:已删除" json:"is_delete"`
  52. CanEdit bool `description:"是否可编辑"`
  53. Editor string `description:"编辑人"`
  54. VersionTotal int `description:"历史版本数量"`
  55. ModifyTime time.Time `description:"修改时间"`
  56. CreateTime time.Time `description:"创建时间"`
  57. }
  58. // GetList 获取沙盘列表页
  59. func GetList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*Sandbox, err error) {
  60. 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 "
  61. sql += condition
  62. sql += ` order by a.modify_time desc,a.sandbox_id desc`
  63. totalSql := `select count(1) total from (` + sql + `) z `
  64. err = global.DmSQL["data"].Raw(totalSql, pars...).Scan(&total).Error
  65. if err != nil {
  66. return
  67. }
  68. sql += ` LIMIT ?,? `
  69. pars = append(pars, startSize)
  70. pars = append(pars, pageSize)
  71. err = global.DmSQL["data"].Raw(sql, pars...).Find(&list).Error
  72. return
  73. }
  74. // SandboxSaveResp 保存沙盘响应体
  75. type SandboxSaveResp struct {
  76. *Sandbox
  77. VersionCode string `description:"版本号"`
  78. }
  79. // GetSandboxInfoByAdminId 获取所有我创建的沙盘,用于分类展示
  80. func GetSandboxInfoByAdminId(adminId int) (items []*SandboxClassifyItems, err error) {
  81. sql := ` SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name,code,
  82. sys_user_id,sys_user_name
  83. FROM sandbox where sys_user_id = ? ORDER BY sort asc,create_time ASC `
  84. err = global.DmSQL["data"].Raw(sql, adminId).Find(&items).Error
  85. return
  86. }
  87. type SandboxListItems struct {
  88. Sandbox
  89. ParentIds string `gorm:"-"`
  90. }
  91. func GetSandboxListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*SandboxListItems, err error) {
  92. sql := ` SELECT * FROM sandbox WHERE 1=1 `
  93. if condition != "" {
  94. sql += condition
  95. }
  96. sql += " ORDER BY create_time DESC LIMIT ?,? "
  97. pars = append(pars, startSize)
  98. pars = append(pars, pageSize)
  99. err = global.DmSQL["data"].Raw(sql, pars...).Find(&item).Error
  100. return
  101. }
  102. func GetSandboxListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  103. sql := ` SELECT COUNT(1) AS count FROM sandbox WHERE 1=1 `
  104. if condition != "" {
  105. sql += condition
  106. }
  107. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
  108. return
  109. }
  110. type SandboxListResp struct {
  111. Paging *paging.PagingItem
  112. List []*SandboxListItems
  113. }
  114. func AddSandbox(item *Sandbox) (err error) {
  115. err = global.DmSQL["data"].Create(item).Error
  116. return
  117. }
  118. type MoveSandboxReq struct {
  119. SandboxId int `description:"沙盘ID"`
  120. PrevSandboxId int `description:"上一个沙盘ID"`
  121. NextSandboxId int `description:"下一个沙盘ID"`
  122. SandboxClassifyId int `description:"分类id"`
  123. }
  124. func GetSandboxClassifyCountById(classifyId int) (count int, err error) {
  125. sql := `SELECT count(1) AS count FROM sandbox_classify WHERE sandbox_classify_id=? `
  126. err = global.DmSQL["data"].Raw(sql, classifyId).Scan(&count).Error
  127. return
  128. }
  129. // GetSandboxNameByIds 根据沙盘名称
  130. func GetSandboxNameByIds(ids []int) (items []*Sandbox, err error) {
  131. if len(ids) == 0 {
  132. return
  133. }
  134. sql := ` SELECT sandbox_id, name FROM sandbox WHERE sandbox_id in (` + utils.GetOrmInReplace(len(ids)) + `) `
  135. err = global.DmSQL["data"].Raw(sql, ids).Find(&items).Error
  136. return
  137. }
  138. func MoveSandbox(sandboxId, classifyId int) (err error) {
  139. sql := ` UPDATE sandbox
  140. SET
  141. sandbox_classify_id = ?
  142. WHERE sandbox_id = ?`
  143. err = global.DmSQL["data"].Exec(sql, classifyId, sandboxId).Error
  144. return
  145. }
  146. // UpdateSandboxSortByClassifyId 根据沙盘id更新排序
  147. func UpdateSandboxSortByClassifyId(classifyId, nowSort, prevSandboxId int, updateSort string) (err error) {
  148. sql := ` update sandbox set sort = ` + updateSort + ` WHERE sandbox_classify_id=? AND `
  149. if prevSandboxId > 0 {
  150. sql += ` (sort > ? or (sandbox_id > ` + fmt.Sprint(prevSandboxId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
  151. }
  152. err = global.DmSQL["data"].Exec(sql, classifyId, nowSort).Error
  153. return
  154. }
  155. // GetFirstSandboxByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  156. func GetFirstSandboxByClassifyId(classifyId int) (item *Sandbox, err error) {
  157. sql := ` SELECT * FROM sandbox WHERE sandbox_classify_id=? order by sort asc,sandbox_id asc limit 1`
  158. err = global.DmSQL["data"].Raw(sql, classifyId).First(&item).Error
  159. return
  160. }
  161. // ContentDataStruct 沙盘内容结构体
  162. type ContentDataStruct struct {
  163. Cells []struct {
  164. Data *NodeData `json:"data,omitempty"`
  165. } `json:"cells"`
  166. }
  167. type NodeData struct {
  168. LinkData []*LinkData `json:"linkData"`
  169. LinkFold bool `json:"linkFold"`
  170. }
  171. type LinkData struct {
  172. RId string `json:"RId"`
  173. Id int `json:"Id"`
  174. Name string `json:"Name"`
  175. Type int `json:"Type"`
  176. Editing bool `json:"editing"`
  177. DatabaseType int `json:"databaseType"`
  178. DetailParams DetailParams `json:"detailParams"`
  179. }
  180. type DetailParams struct {
  181. Code string `json:"code"`
  182. Id int `json:"id"`
  183. ClassifyId int `json:"classifyId"`
  184. }
  185. // UpdateSandboxContent 更新沙盘内容
  186. func UpdateSandboxContent(list []Sandbox) (err error) {
  187. tx := global.DmSQL["data"].Begin()
  188. defer func() {
  189. if err != nil {
  190. _ = tx.Rollback()
  191. return
  192. }
  193. _ = tx.Commit()
  194. }()
  195. cols := []string{"Content", "ModifyTime"}
  196. for _, sandbox := range list {
  197. err = tx.Select(cols).Updates(sandbox).Error
  198. if err != nil {
  199. return
  200. }
  201. }
  202. return
  203. }