sandbox.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. func (sandbox *Sandbox) Update(cols []string) (err error) {
  28. err = global.DmSQL["data"].Select(cols).Updates(sandbox).Error
  29. return
  30. }
  31. func GetSandboxById(sandboxId int) (sandboxInfo *Sandbox, err error) {
  32. sql := `select * from sandbox where sandbox_id = ? and is_delete = 0`
  33. err = global.DmSQL["data"].Raw(sql, sandboxId).First(&sandboxInfo).Error
  34. return
  35. }
  36. type SandboxListItem struct {
  37. SandboxId int `description:"沙盘id"`
  38. Name string `description:"沙盘名称"`
  39. ChartPermissionId int `description:"品种id"`
  40. ChartPermissionName string `description:"品种名称"`
  41. CurrVersion int `description:"当前版本"`
  42. Code string `description:"沙盘code"`
  43. VersionCode string `description:"沙盘版本code"`
  44. PicUrl string `description:"沙盘图片地址"`
  45. OpUserId int `description:"最近一次编辑操作的用户id"`
  46. OpUserName string `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
  47. IsDelete int8 `description:"是否删除,0:未删除,1:已删除" json:"is_delete"`
  48. CanEdit bool `description:"是否可编辑"`
  49. Editor string `description:"编辑人"`
  50. VersionTotal int `description:"历史版本数量"`
  51. ModifyTime time.Time `description:"修改时间"`
  52. CreateTime time.Time `description:"创建时间"`
  53. }
  54. func GetList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*Sandbox, err error) {
  55. 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 "
  56. sql += condition
  57. sql += ` order by a.modify_time desc,a.sandbox_id desc`
  58. totalSql := `select count(1) total from (` + sql + `) z `
  59. err = global.DmSQL["data"].Raw(totalSql, pars...).Scan(&total).Error
  60. if err != nil {
  61. return
  62. }
  63. sql += ` LIMIT ?,? `
  64. pars = append(pars, startSize)
  65. pars = append(pars, pageSize)
  66. err = global.DmSQL["data"].Raw(sql, pars...).Find(&list).Error
  67. return
  68. }
  69. type SandboxSaveResp struct {
  70. *Sandbox
  71. VersionCode string `description:"版本号"`
  72. }
  73. func GetSandboxInfoByAdminId(adminId int) (items []*SandboxClassifyItems, err error) {
  74. sql := ` SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name,code,
  75. sys_user_id,sys_user_name
  76. FROM sandbox where sys_user_id = ? ORDER BY sort asc,create_time ASC `
  77. err = global.DmSQL["data"].Raw(sql, adminId).Find(&items).Error
  78. return
  79. }
  80. type SandboxListItems struct {
  81. Sandbox
  82. ParentIds string `gorm:"-"`
  83. }
  84. func GetSandboxListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*SandboxListItems, err error) {
  85. sql := ` SELECT * FROM sandbox WHERE 1=1 `
  86. if condition != "" {
  87. sql += condition
  88. }
  89. sql += " ORDER BY create_time DESC LIMIT ?,? "
  90. pars = append(pars, startSize)
  91. pars = append(pars, pageSize)
  92. err = global.DmSQL["data"].Raw(sql, pars...).Find(&item).Error
  93. return
  94. }
  95. func GetSandboxListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  96. sql := ` SELECT COUNT(1) AS count FROM sandbox WHERE 1=1 `
  97. if condition != "" {
  98. sql += condition
  99. }
  100. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
  101. return
  102. }
  103. type SandboxListResp struct {
  104. Paging *paging.PagingItem
  105. List []*SandboxListItems
  106. }
  107. func AddSandbox(item *Sandbox) (err error) {
  108. err = global.DmSQL["data"].Create(item).Error
  109. return
  110. }
  111. type MoveSandboxReq struct {
  112. SandboxId int `description:"沙盘ID"`
  113. PrevSandboxId int `description:"上一个沙盘ID"`
  114. NextSandboxId int `description:"下一个沙盘ID"`
  115. SandboxClassifyId int `description:"分类id"`
  116. }
  117. func GetSandboxClassifyCountById(classifyId int) (count int, err error) {
  118. sql := `SELECT count(1) AS count FROM sandbox_classify WHERE sandbox_classify_id=? `
  119. err = global.DmSQL["data"].Raw(sql, classifyId).Scan(&count).Error
  120. return
  121. }
  122. func GetSandboxNameByIds(ids []int) (items []*Sandbox, err error) {
  123. if len(ids) == 0 {
  124. return
  125. }
  126. sql := ` SELECT sandbox_id, name FROM sandbox WHERE sandbox_id in (` + utils.GetOrmInReplace(len(ids)) + `) `
  127. err = global.DmSQL["data"].Raw(sql, ids).Find(&items).Error
  128. return
  129. }
  130. func MoveSandbox(sandboxId, classifyId int) (err error) {
  131. sql := ` UPDATE sandbox
  132. SET
  133. sandbox_classify_id = ?
  134. WHERE sandbox_id = ?`
  135. err = global.DmSQL["data"].Exec(sql, classifyId, sandboxId).Error
  136. return
  137. }
  138. func UpdateSandboxSortByClassifyId(classifyId, nowSort, prevSandboxId int, updateSort string) (err error) {
  139. sql := ` update sandbox set sort = ` + updateSort + ` WHERE sandbox_classify_id=? AND `
  140. if prevSandboxId > 0 {
  141. sql += ` (sort > ? or (sandbox_id > ` + fmt.Sprint(prevSandboxId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
  142. }
  143. err = global.DmSQL["data"].Exec(sql, classifyId, nowSort).Error
  144. return
  145. }
  146. func GetFirstSandboxByClassifyId(classifyId int) (item *Sandbox, err error) {
  147. sql := ` SELECT * FROM sandbox WHERE sandbox_classify_id=? order by sort asc,sandbox_id asc limit 1`
  148. err = global.DmSQL["data"].Raw(sql, classifyId).First(&item).Error
  149. return
  150. }
  151. type ContentDataStruct struct {
  152. Cells []struct {
  153. Data *NodeData `json:"data,omitempty"`
  154. } `json:"cells"`
  155. }
  156. type NodeData struct {
  157. LinkData []*LinkData `json:"linkData"`
  158. LinkFold bool `json:"linkFold"`
  159. }
  160. type LinkData struct {
  161. RId string `json:"RId"`
  162. Id int `json:"Id"`
  163. Name string `json:"Name"`
  164. Type int `json:"Type"`
  165. Editing bool `json:"editing"`
  166. DatabaseType int `json:"databaseType"`
  167. DetailParams DetailParams `json:"detailParams"`
  168. }
  169. type DetailParams struct {
  170. Code string `json:"code"`
  171. Id int `json:"id"`
  172. ClassifyId int `json:"classifyId"`
  173. }
  174. func UpdateSandboxContent(list []Sandbox) (err error) {
  175. tx := global.DmSQL["data"].Begin()
  176. defer func() {
  177. if err != nil {
  178. _ = tx.Rollback()
  179. return
  180. }
  181. _ = tx.Commit()
  182. }()
  183. cols := []string{"Content", "ModifyTime"}
  184. for _, sandbox := range list {
  185. err = tx.Select(cols).Updates(sandbox).Error
  186. if err != nil {
  187. return
  188. }
  189. }
  190. return
  191. }