sandbox.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. func GetList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*SandboxListItem, err error) {
  177. o := orm.NewOrmUsingDB("data")
  178. 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 "
  179. sql += condition
  180. sql += ` order by a.modify_time desc,a.sandbox_id desc`
  181. totalSql := `select count(1) total from (` + sql + `) z `
  182. err = o.Raw(totalSql, pars).QueryRow(&total)
  183. if err != nil {
  184. return
  185. }
  186. sql += ` LIMIT ?,? `
  187. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  188. return
  189. }
  190. // SandboxSaveResp 保存沙盘响应体
  191. type SandboxSaveResp struct {
  192. *Sandbox
  193. VersionCode string `description:"版本号"`
  194. }
  195. func GetSandboxAll(sandboxClassifyId int) (list []*SandboxClassifyItems, err error) {
  196. o := orm.NewOrmUsingDB("data")
  197. sql := `SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name, sort
  198. FROM sandbox WHERE chart_classify_id = ? `
  199. _, err = o.Raw(sql, sandboxClassifyId).QueryRows(&list)
  200. return
  201. }
  202. // CheckOpSandboxPermission 判断沙盘操作权限
  203. func CheckOpSandboxPermission(sysUser *system.Admin, createUserId int) (ok bool) {
  204. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  205. ok = true
  206. }
  207. // 如果图表创建人与当前操作人相同的话,那么就是允许操作
  208. if ok == false && createUserId == sysUser.AdminId {
  209. ok = true
  210. }
  211. // 如果图表权限id 是 1 ,那么允许编辑
  212. if ok == false && sysUser.ChartPermission == 1 {
  213. ok = true
  214. }
  215. return
  216. }
  217. // GetSandboxInfoByAdminId 获取所有我创建的沙盘,用于分类展示
  218. func GetSandboxInfoByAdminId(adminId int) (items []*SandboxClassifyItems, err error) {
  219. o := orm.NewOrmUsingDB("data")
  220. sql := ` SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name,code,
  221. sys_user_id,sys_user_real_name
  222. FROM sandbox where sys_user_id = ? ORDER BY sort asc,create_time ASC `
  223. _, err = o.Raw(sql, adminId).QueryRows(&items)
  224. return
  225. }
  226. func GetSandboxClassify(sandboxClassifyId int) (sandbox_classify_id string, err error) {
  227. o := orm.NewOrmUsingDB("data")
  228. sql := `SELECT GROUP_CONCAT(t.sandbox_classify_id) AS sandbox_classify_id FROM (
  229. SELECT a.sandbox_classify_id FROM sandbox_classify AS a
  230. WHERE a.sandbox_classify_id=?
  231. UNION ALL
  232. SELECT a.sandbox_classify_id FROM sandbox_classify AS a
  233. WHERE a.parent_id=? UNION ALL
  234. SELECT
  235. sandbox_classify_id
  236. FROM
  237. sandbox_classify
  238. WHERE
  239. parent_id IN ( SELECT sandbox_classify_id FROM sandbox_classify WHERE parent_id = ? )
  240. )AS t`
  241. err = o.Raw(sql, sandboxClassifyId, sandboxClassifyId, sandboxClassifyId).QueryRow(&sandbox_classify_id)
  242. return
  243. }
  244. func GetSandboxListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*Sandbox, err error) {
  245. o := orm.NewOrmUsingDB("data")
  246. sql := ` SELECT * FROM sandbox WHERE 1=1 `
  247. if condition != "" {
  248. sql += condition
  249. }
  250. sql += " ORDER BY create_time DESC LIMIT ?,? "
  251. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  252. return
  253. }
  254. func GetSandboxListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  255. o := orm.NewOrmUsingDB("data")
  256. sql := ` SELECT COUNT(1) AS count FROM sandbox WHERE 1=1 `
  257. if condition != "" {
  258. sql += condition
  259. }
  260. err = o.Raw(sql, pars).QueryRow(&count)
  261. return
  262. }
  263. type SandboxListResp struct {
  264. Paging *paging.PagingItem
  265. List []*Sandbox
  266. }
  267. func AddSandbox(item *Sandbox) (lastId int64, err error) {
  268. o := orm.NewOrmUsingDB("data")
  269. lastId, err = o.Insert(item)
  270. return
  271. }