sandbox.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package sandbox
  2. import (
  3. "eta/eta_api/models/system"
  4. "eta/eta_api/utils"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. // Sandbox 沙盘推演主表
  9. type Sandbox struct {
  10. SandboxId int `orm:"column(sandbox_id);pk" description:"沙盘id"`
  11. Name string `description:"沙盘名称"`
  12. ChartPermissionId int `description:"品种id"`
  13. ChartPermissionName string `description:"品种名称"`
  14. CurrVersion int `description:"当前版本"`
  15. Code string `description:"沙盘code"`
  16. Content string `description:"沙盘数据"`
  17. PicUrl string `description:"沙盘图片地址"`
  18. OpUserId int `description:"最近一次编辑操作的用户id"`
  19. OpUserName string `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
  20. IsDelete int8 `description:"是否删除,0:未删除,1:已删除"`
  21. ModifyTime time.Time `description:"修改时间"`
  22. CreateTime time.Time `description:"创建时间"`
  23. SandboxClassifyId int `description:"分类id"`
  24. Sort int `description:"排序"`
  25. }
  26. // Update 沙盘字段变更
  27. func (sandbox *Sandbox) Update(cols []string) (err error) {
  28. o := orm.NewOrmUsingDB("data")
  29. _, err = o.Update(sandbox, cols...)
  30. return
  31. }
  32. // GetSandboxById 根据沙盘id获取沙盘详情
  33. func GetSandboxById(sandboxId int) (sandboxInfo *Sandbox, err error) {
  34. o := orm.NewOrmUsingDB("data")
  35. sql := `select * from sandbox where sandbox_id = ? and is_delete = 0`
  36. err = o.Raw(sql, sandboxId).QueryRow(&sandboxInfo)
  37. return
  38. }
  39. // AddNewSandbox 添加一个新的沙盘
  40. func AddNewSandbox(sandboxInfo *Sandbox, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft) (err error) {
  41. o := orm.NewOrmUsingDB("data")
  42. to, err := o.Begin()
  43. if err != nil {
  44. return
  45. }
  46. defer func() {
  47. if err != nil {
  48. _ = to.Rollback()
  49. } else {
  50. _ = to.Commit()
  51. }
  52. }()
  53. id, err := to.Insert(sandboxInfo)
  54. if err != nil {
  55. return
  56. }
  57. sandboxInfo.SandboxId = int(id)
  58. // 新增版本
  59. sandboxVersion.SandboxId = sandboxInfo.SandboxId
  60. id, err = to.Insert(sandboxVersion)
  61. if err != nil {
  62. return
  63. }
  64. sandboxVersion.SandboxVersionId = int(id)
  65. // 新增草稿
  66. sandboxDraft.SandboxId = sandboxInfo.SandboxId
  67. id, err = to.Insert(sandboxDraft)
  68. if err != nil {
  69. return
  70. }
  71. sandboxDraft.SandboxDraftId = int(id)
  72. return
  73. }
  74. // UpdateSandbox 更新沙盘
  75. func UpdateSandbox(sandboxInfo *Sandbox, updateSandboxColumn []string, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft) (err error) {
  76. o := orm.NewOrmUsingDB("data")
  77. to, err := o.Begin()
  78. if err != nil {
  79. return
  80. }
  81. defer func() {
  82. if err != nil {
  83. _ = to.Rollback()
  84. } else {
  85. _ = to.Commit()
  86. }
  87. }()
  88. _, err = to.Update(sandboxInfo, updateSandboxColumn...)
  89. if err != nil {
  90. return
  91. }
  92. // 新增版本
  93. sandboxVersion.SandboxId = sandboxInfo.SandboxId
  94. id, err := to.Insert(sandboxVersion)
  95. if err != nil {
  96. return
  97. }
  98. sandboxVersion.SandboxVersionId = int(id)
  99. // 新增草稿
  100. sandboxDraft.SandboxId = sandboxInfo.SandboxId
  101. id, err = to.Insert(sandboxDraft)
  102. if err != nil {
  103. return
  104. }
  105. sandboxDraft.SandboxDraftId = int(id)
  106. return
  107. }
  108. // UpdateSandboxName 更新沙盘(仅仅更新名称)
  109. func UpdateSandboxName(sandboxInfo *Sandbox, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft, updateSandboxColumn, updateSandboxVersionColumn []string) (err error) {
  110. o := orm.NewOrmUsingDB("data")
  111. to, err := o.Begin()
  112. if err != nil {
  113. return
  114. }
  115. defer func() {
  116. if err != nil {
  117. _ = to.Rollback()
  118. } else {
  119. _ = to.Commit()
  120. }
  121. }()
  122. _, err = to.Update(sandboxInfo, updateSandboxColumn...)
  123. if err != nil {
  124. return
  125. }
  126. // 更新版本
  127. _, err = to.Update(sandboxVersion, updateSandboxVersionColumn...)
  128. if err != nil {
  129. return
  130. }
  131. // 新增草稿
  132. sandboxDraft.SandboxId = sandboxInfo.SandboxId
  133. id, err := to.Insert(sandboxDraft)
  134. if err != nil {
  135. return
  136. }
  137. sandboxDraft.SandboxDraftId = int(id)
  138. return
  139. }
  140. // SandboxListItem 沙盘推演列表数据
  141. type SandboxListItem struct {
  142. SandboxId int `description:"沙盘id"`
  143. Name string `description:"沙盘名称"`
  144. ChartPermissionId int `description:"品种id"`
  145. ChartPermissionName string `description:"品种名称"`
  146. CurrVersion int `description:"当前版本"`
  147. Code string `description:"沙盘code"`
  148. VersionCode string `description:"沙盘版本code"`
  149. //Content string `description:"沙盘数据"`
  150. PicUrl string `description:"沙盘图片地址"`
  151. OpUserId int `description:"最近一次编辑操作的用户id"`
  152. OpUserName string `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
  153. IsDelete int8 `description:"是否删除,0:未删除,1:已删除" json:"is_delete"`
  154. CanEdit bool `description:"是否可编辑"`
  155. Editor string `description:"编辑人"`
  156. VersionTotal int `description:"历史版本数量"`
  157. ModifyTime time.Time `description:"修改时间"`
  158. CreateTime time.Time `description:"创建时间"`
  159. }
  160. // GetList 获取沙盘列表页
  161. func GetList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*SandboxListItem, err error) {
  162. o := orm.NewOrmUsingDB("data")
  163. 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 "
  164. sql += condition
  165. sql += ` order by a.modify_time desc,a.sandbox_id desc`
  166. totalSql := `select count(1) total from (` + sql + `) z `
  167. err = o.Raw(totalSql, pars).QueryRow(&total)
  168. if err != nil {
  169. return
  170. }
  171. sql += ` LIMIT ?,? `
  172. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  173. return
  174. }
  175. // SandboxSaveResp 保存沙盘响应体
  176. type SandboxSaveResp struct {
  177. *Sandbox
  178. VersionCode string `description:"版本号"`
  179. }
  180. func GetSandboxAll(sandboxClassifyId int) (list []*SandboxClassifyItems, err error) {
  181. o := orm.NewOrmUsingDB("data")
  182. sql := `SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name, sort
  183. FROM sandbox WHERE chart_classify_id = ? `
  184. _, err = o.Raw(sql, sandboxClassifyId).QueryRows(&list)
  185. return
  186. }
  187. // CheckOpSandboxPermission 判断沙盘操作权限
  188. func CheckOpSandboxPermission(sysUser *system.Admin, createUserId int) (ok bool) {
  189. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  190. ok = true
  191. }
  192. // 如果图表创建人与当前操作人相同的话,那么就是允许操作
  193. if ok == false && createUserId == sysUser.AdminId {
  194. ok = true
  195. }
  196. // 如果图表权限id 是 1 ,那么允许编辑
  197. if ok == false && sysUser.ChartPermission == 1 {
  198. ok = true
  199. }
  200. return
  201. }
  202. // GetSandboxInfoByAdminId 获取所有我创建的沙盘,用于分类展示
  203. func GetSandboxInfoByAdminId(adminId int) (items []*SandboxClassifyItems, err error) {
  204. o := orm.NewOrmUsingDB("data")
  205. sql := ` SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name,code,
  206. sys_user_id,sys_user_real_name
  207. FROM sandbox where sys_user_id = ? ORDER BY sort asc,create_time ASC `
  208. _, err = o.Raw(sql, adminId).QueryRows(&items)
  209. return
  210. }