sandbox.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. "github.com/rdlucklib/rdluck_tools/paging"
  7. "time"
  8. )
  9. // Sandbox 沙盘推演主表
  10. //type Sandbox struct {
  11. // SandboxId int `orm:"column(sandbox_id);pk" description:"沙盘id"`
  12. // Name string `description:"沙盘名称"`
  13. // ChartPermissionId int `description:"品种id"`
  14. // ChartPermissionName string `description:"品种名称"`
  15. // CurrVersion int `description:"当前版本"`
  16. // Code string `description:"沙盘code"`
  17. // Content string `description:"沙盘数据"`
  18. // PicUrl string `description:"沙盘图片地址"`
  19. // OpUserId int `description:"最近一次编辑操作的用户id"`
  20. // OpUserName string `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
  21. // IsDelete int8 `description:"是否删除,0:未删除,1:已删除"`
  22. // ModifyTime time.Time `description:"修改时间"`
  23. // CreateTime time.Time `description:"创建时间"`
  24. // SandboxClassifyId int `description:"分类id"`
  25. // Sort int `description:"排序"`
  26. //}
  27. type Sandbox struct {
  28. SandboxId int `orm:"column(sandbox_id);pk" description:"沙盘id"`
  29. Name string `description:"沙盘名称"`
  30. Code string `description:"沙盘code"`
  31. Content string `description:"沙盘数据"`
  32. PicUrl string `description:"沙盘图片地址"`
  33. SysUserId int `description:"作者id"`
  34. SysUserName string `description:"作者名称"`
  35. IsDelete int8 `description:"是否删除,0:未删除,1:已删除"`
  36. ModifyTime time.Time `description:"修改时间"`
  37. CreateTime time.Time `description:"创建时间"`
  38. SandboxClassifyId int `description:"分类id"`
  39. Sort int `description:"排序"`
  40. }
  41. // Update 沙盘字段变更
  42. func (sandbox *Sandbox) Update(cols []string) (err error) {
  43. o := orm.NewOrmUsingDB("data")
  44. _, err = o.Update(sandbox, cols...)
  45. return
  46. }
  47. // GetSandboxById 根据沙盘id获取沙盘详情
  48. func GetSandboxById(sandboxId int) (sandboxInfo *Sandbox, err error) {
  49. o := orm.NewOrmUsingDB("data")
  50. sql := `select * from sandbox where sandbox_id = ? and is_delete = 0`
  51. err = o.Raw(sql, sandboxId).QueryRow(&sandboxInfo)
  52. return
  53. }
  54. // AddNewSandbox 添加一个新的沙盘
  55. func AddNewSandbox(sandboxInfo *Sandbox, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft) (err error) {
  56. o := orm.NewOrmUsingDB("data")
  57. to, err := o.Begin()
  58. if err != nil {
  59. return
  60. }
  61. defer func() {
  62. if err != nil {
  63. _ = to.Rollback()
  64. } else {
  65. _ = to.Commit()
  66. }
  67. }()
  68. id, err := to.Insert(sandboxInfo)
  69. if err != nil {
  70. return
  71. }
  72. sandboxInfo.SandboxId = int(id)
  73. // 新增版本
  74. sandboxVersion.SandboxId = sandboxInfo.SandboxId
  75. id, err = to.Insert(sandboxVersion)
  76. if err != nil {
  77. return
  78. }
  79. sandboxVersion.SandboxVersionId = int(id)
  80. // 新增草稿
  81. sandboxDraft.SandboxId = sandboxInfo.SandboxId
  82. id, err = to.Insert(sandboxDraft)
  83. if err != nil {
  84. return
  85. }
  86. sandboxDraft.SandboxDraftId = int(id)
  87. return
  88. }
  89. // UpdateSandbox 更新沙盘
  90. func UpdateSandbox(sandboxInfo *Sandbox, updateSandboxColumn []string, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft) (err error) {
  91. o := orm.NewOrmUsingDB("data")
  92. to, err := o.Begin()
  93. if err != nil {
  94. return
  95. }
  96. defer func() {
  97. if err != nil {
  98. _ = to.Rollback()
  99. } else {
  100. _ = to.Commit()
  101. }
  102. }()
  103. _, err = to.Update(sandboxInfo, updateSandboxColumn...)
  104. if err != nil {
  105. return
  106. }
  107. // 新增版本
  108. sandboxVersion.SandboxId = sandboxInfo.SandboxId
  109. id, err := to.Insert(sandboxVersion)
  110. if err != nil {
  111. return
  112. }
  113. sandboxVersion.SandboxVersionId = int(id)
  114. // 新增草稿
  115. sandboxDraft.SandboxId = sandboxInfo.SandboxId
  116. id, err = to.Insert(sandboxDraft)
  117. if err != nil {
  118. return
  119. }
  120. sandboxDraft.SandboxDraftId = int(id)
  121. return
  122. }
  123. // UpdateSandboxName 更新沙盘(仅仅更新名称)
  124. func UpdateSandboxName(sandboxInfo *Sandbox, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft, updateSandboxColumn, updateSandboxVersionColumn []string) (err error) {
  125. o := orm.NewOrmUsingDB("data")
  126. to, err := o.Begin()
  127. if err != nil {
  128. return
  129. }
  130. defer func() {
  131. if err != nil {
  132. _ = to.Rollback()
  133. } else {
  134. _ = to.Commit()
  135. }
  136. }()
  137. _, err = to.Update(sandboxInfo, updateSandboxColumn...)
  138. if err != nil {
  139. return
  140. }
  141. // 更新版本
  142. _, err = to.Update(sandboxVersion, updateSandboxVersionColumn...)
  143. if err != nil {
  144. return
  145. }
  146. // 新增草稿
  147. sandboxDraft.SandboxId = sandboxInfo.SandboxId
  148. id, err := to.Insert(sandboxDraft)
  149. if err != nil {
  150. return
  151. }
  152. sandboxDraft.SandboxDraftId = int(id)
  153. return
  154. }
  155. // SandboxListItem 沙盘推演列表数据
  156. type SandboxListItem struct {
  157. SandboxId int `description:"沙盘id"`
  158. Name string `description:"沙盘名称"`
  159. ChartPermissionId int `description:"品种id"`
  160. ChartPermissionName string `description:"品种名称"`
  161. CurrVersion int `description:"当前版本"`
  162. Code string `description:"沙盘code"`
  163. VersionCode string `description:"沙盘版本code"`
  164. //Content string `description:"沙盘数据"`
  165. PicUrl string `description:"沙盘图片地址"`
  166. OpUserId int `description:"最近一次编辑操作的用户id"`
  167. OpUserName string `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
  168. IsDelete int8 `description:"是否删除,0:未删除,1:已删除" json:"is_delete"`
  169. CanEdit bool `description:"是否可编辑"`
  170. Editor string `description:"编辑人"`
  171. VersionTotal int `description:"历史版本数量"`
  172. ModifyTime time.Time `description:"修改时间"`
  173. CreateTime time.Time `description:"创建时间"`
  174. }
  175. // GetList 获取沙盘列表页
  176. // todo 测试服兼容
  177. func GetList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*SandboxListItem, err error) {
  178. o := orm.NewOrmUsingDB("data")
  179. 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_bak_20231018 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 "
  180. sql += condition
  181. sql += ` order by a.modify_time desc,a.sandbox_id desc`
  182. totalSql := `select count(1) total from (` + sql + `) z `
  183. err = o.Raw(totalSql, pars).QueryRow(&total)
  184. if err != nil {
  185. return
  186. }
  187. sql += ` LIMIT ?,? `
  188. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  189. return
  190. }
  191. // SandboxSaveResp 保存沙盘响应体
  192. type SandboxSaveResp struct {
  193. *Sandbox
  194. VersionCode string `description:"版本号"`
  195. }
  196. func GetSandboxAll() (list []*SandboxClassifyItems, err error) {
  197. o := orm.NewOrmUsingDB("data")
  198. sql := `SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name, sort
  199. FROM sandbox `
  200. _, err = o.Raw(sql).QueryRows(&list)
  201. return
  202. }
  203. // CheckOpSandboxPermission 判断沙盘操作权限
  204. func CheckOpSandboxPermission(sysUser *system.Admin, createUserId int) (ok bool) {
  205. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  206. ok = true
  207. }
  208. // 如果图表创建人与当前操作人相同的话,那么就是允许操作
  209. if ok == false && createUserId == sysUser.AdminId {
  210. ok = true
  211. }
  212. // 如果图表权限id 是 1 ,那么允许编辑
  213. if ok == false && sysUser.ChartPermission == 1 {
  214. ok = true
  215. }
  216. return
  217. }
  218. // GetSandboxInfoByAdminId 获取所有我创建的沙盘,用于分类展示
  219. func GetSandboxInfoByAdminId(adminId int) (items []*SandboxClassifyItems, err error) {
  220. o := orm.NewOrmUsingDB("data")
  221. sql := ` SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name,code,
  222. sys_user_id,sys_user_name
  223. FROM sandbox where sys_user_id = ? ORDER BY sort asc,create_time ASC `
  224. _, err = o.Raw(sql, adminId).QueryRows(&items)
  225. return
  226. }
  227. func GetSandboxClassify(sandboxClassifyId int) (sandbox_classify_id string, err error) {
  228. o := orm.NewOrmUsingDB("data")
  229. sql := `SELECT GROUP_CONCAT(t.sandbox_classify_id) AS sandbox_classify_id FROM (
  230. SELECT a.sandbox_classify_id FROM sandbox_classify AS a
  231. WHERE a.sandbox_classify_id=?
  232. UNION ALL
  233. SELECT a.sandbox_classify_id FROM sandbox_classify AS a
  234. WHERE a.parent_id=? UNION ALL
  235. SELECT
  236. sandbox_classify_id
  237. FROM
  238. sandbox_classify
  239. WHERE
  240. parent_id IN ( SELECT sandbox_classify_id FROM sandbox_classify WHERE parent_id = ? )
  241. )AS t`
  242. err = o.Raw(sql, sandboxClassifyId, sandboxClassifyId, sandboxClassifyId).QueryRow(&sandbox_classify_id)
  243. return
  244. }
  245. func GetSandboxListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*Sandbox, err error) {
  246. o := orm.NewOrmUsingDB("data")
  247. sql := ` SELECT * FROM sandbox WHERE 1=1 `
  248. if condition != "" {
  249. sql += condition
  250. }
  251. sql += " ORDER BY create_time DESC LIMIT ?,? "
  252. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  253. return
  254. }
  255. func GetSandboxListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  256. o := orm.NewOrmUsingDB("data")
  257. sql := ` SELECT COUNT(1) AS count FROM sandbox WHERE 1=1 `
  258. if condition != "" {
  259. sql += condition
  260. }
  261. err = o.Raw(sql, pars).QueryRow(&count)
  262. return
  263. }
  264. type SandboxListResp struct {
  265. Paging *paging.PagingItem
  266. List []*Sandbox
  267. }
  268. func AddSandbox(item *Sandbox) (lastId int64, err error) {
  269. o := orm.NewOrmUsingDB("data")
  270. lastId, err = o.Insert(item)
  271. return
  272. }
  273. func GetSandboxItemsByClassifyId(sandboxClassifyId int) (list []*SandboxClassifyItems, err error) {
  274. o := orm.NewOrmUsingDB("data")
  275. sql := `SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name, sort
  276. FROM sandbox WHERE sandbox_classify_id = ?`
  277. _, err = o.Raw(sql,sandboxClassifyId).QueryRows(&list)
  278. return
  279. }