sandbox.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package sandbox
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. // Sandbox 沙盘推演主表
  7. type Sandbox struct {
  8. SandboxId int `orm:"column(sandbox_id);pk" description:"沙盘id"`
  9. Name string `description:"沙盘名称"`
  10. ChartPermissionId int `description:"品种id"`
  11. ChartPermissionName string `description:"品种名称"`
  12. CurrVersion int `description:"当前版本"`
  13. Code string `description:"沙盘code"`
  14. Content string `description:"沙盘数据"`
  15. PicUrl string `description:"沙盘图片地址"`
  16. OpUserId int `description:"最近一次编辑操作的用户id"`
  17. OpUserName string `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
  18. IsDelete int8 `description:"是否删除,0:未删除,1:已删除"`
  19. ModifyTime time.Time `description:"修改时间"`
  20. CreateTime time.Time `description:"创建时间"`
  21. }
  22. // Update 沙盘字段变更
  23. func (sandbox *Sandbox) Update(cols []string) (err error) {
  24. o := orm.NewOrmUsingDB("data")
  25. _, err = o.Update(sandbox, cols...)
  26. return
  27. }
  28. // GetSandboxById 根据沙盘id获取沙盘详情
  29. func GetSandboxById(sandboxId int) (sandboxInfo *Sandbox, err error) {
  30. o := orm.NewOrmUsingDB("data")
  31. sql := `select * from sandbox where sandbox_id = ? and is_delete = 0`
  32. err = o.Raw(sql, sandboxId).QueryRow(&sandboxInfo)
  33. return
  34. }
  35. // AddNewSandbox 添加一个新的沙盘
  36. func AddNewSandbox(sandboxInfo *Sandbox, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft) (err error) {
  37. o := orm.NewOrmUsingDB("data")
  38. to, err := o.Begin()
  39. if err != nil {
  40. return
  41. }
  42. defer func() {
  43. if err != nil {
  44. _ = to.Rollback()
  45. } else {
  46. _ = to.Commit()
  47. }
  48. }()
  49. id, err := to.Insert(sandboxInfo)
  50. if err != nil {
  51. return
  52. }
  53. sandboxInfo.SandboxId = int(id)
  54. // 新增版本
  55. sandboxVersion.SandboxId = sandboxInfo.SandboxId
  56. id, err = to.Insert(sandboxVersion)
  57. if err != nil {
  58. return
  59. }
  60. sandboxVersion.SandboxVersionId = int(id)
  61. // 新增草稿
  62. sandboxDraft.SandboxId = sandboxInfo.SandboxId
  63. id, err = to.Insert(sandboxDraft)
  64. if err != nil {
  65. return
  66. }
  67. sandboxDraft.SandboxDraftId = int(id)
  68. return
  69. }
  70. // UpdateSandbox 更新沙盘
  71. func UpdateSandbox(sandboxInfo *Sandbox, updateSandboxColumn []string, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft) (err error) {
  72. o := orm.NewOrmUsingDB("data")
  73. to, err := o.Begin()
  74. if err != nil {
  75. return
  76. }
  77. defer func() {
  78. if err != nil {
  79. _ = to.Rollback()
  80. } else {
  81. _ = to.Commit()
  82. }
  83. }()
  84. _, err = to.Update(sandboxInfo, updateSandboxColumn...)
  85. if err != nil {
  86. return
  87. }
  88. // 新增版本
  89. sandboxVersion.SandboxId = sandboxInfo.SandboxId
  90. id, err := to.Insert(sandboxVersion)
  91. if err != nil {
  92. return
  93. }
  94. sandboxVersion.SandboxVersionId = int(id)
  95. // 新增草稿
  96. sandboxDraft.SandboxId = sandboxInfo.SandboxId
  97. id, err = to.Insert(sandboxDraft)
  98. if err != nil {
  99. return
  100. }
  101. sandboxDraft.SandboxDraftId = int(id)
  102. return
  103. }
  104. // UpdateSandboxName 更新沙盘(仅仅更新名称)
  105. func UpdateSandboxName(sandboxInfo *Sandbox, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft, updateSandboxColumn, updateSandboxVersionColumn []string) (err error) {
  106. o := orm.NewOrmUsingDB("data")
  107. to, err := o.Begin()
  108. if err != nil {
  109. return
  110. }
  111. defer func() {
  112. if err != nil {
  113. _ = to.Rollback()
  114. } else {
  115. _ = to.Commit()
  116. }
  117. }()
  118. _, err = to.Update(sandboxInfo, updateSandboxColumn...)
  119. if err != nil {
  120. return
  121. }
  122. // 更新版本
  123. _, err = to.Update(sandboxVersion, updateSandboxVersionColumn...)
  124. if err != nil {
  125. return
  126. }
  127. // 新增草稿
  128. sandboxDraft.SandboxId = sandboxInfo.SandboxId
  129. id, err := to.Insert(sandboxDraft)
  130. if err != nil {
  131. return
  132. }
  133. sandboxDraft.SandboxDraftId = int(id)
  134. return
  135. }
  136. // SandboxListItem 沙盘推演列表数据
  137. type SandboxListItem struct {
  138. SandboxId int `description:"沙盘id"`
  139. Name string `description:"沙盘名称"`
  140. ChartPermissionId int `description:"品种id"`
  141. ChartPermissionName string `description:"品种名称"`
  142. CurrVersion int `description:"当前版本"`
  143. Code string `description:"沙盘code"`
  144. VersionCode string `description:"沙盘版本code"`
  145. //Content string `description:"沙盘数据"`
  146. PicUrl string `description:"沙盘图片地址"`
  147. OpUserId int `description:"最近一次编辑操作的用户id"`
  148. OpUserName string `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
  149. IsDelete int8 `description:"是否删除,0:未删除,1:已删除" json:"is_delete"`
  150. CanEdit bool `description:"是否可编辑"`
  151. Editor string `description:"编辑人"`
  152. VersionTotal int `description:"历史版本数量"`
  153. ModifyTime time.Time `description:"修改时间"`
  154. CreateTime time.Time `description:"创建时间"`
  155. }
  156. // GetList 获取沙盘列表页
  157. func GetList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*SandboxListItem, err error) {
  158. o := orm.NewOrmUsingDB("data")
  159. sql := "select a.sandbox_id,a.name,a.chart_permission_id,a.chart_permission_name,a.curr_version,a.code,a.pic_url,a.op_user_id,a.op_user_name,a.modify_time,a.create_time,b.version_code from sandbox as a join sandbox_version b on a.sandbox_id=b.sandbox_id and a.curr_version=b.curr_version where 1=1 AND a.is_delete = 0 "
  160. sql += condition
  161. sql += ` order by a.modify_time desc,a.sandbox_id desc`
  162. totalSql := `select count(1) total from (` + sql + `) z `
  163. err = o.Raw(totalSql, pars).QueryRow(&total)
  164. if err != nil {
  165. return
  166. }
  167. sql += ` LIMIT ?,? `
  168. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  169. return
  170. }
  171. // SandboxSaveResp 保存沙盘响应体
  172. type SandboxSaveResp struct {
  173. *Sandbox
  174. VersionCode string `description:"版本号"`
  175. }