123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- package sandbox
- import (
- "eta/eta_api/models/system"
- "eta/eta_api/utils"
- "github.com/beego/beego/v2/client/orm"
- "time"
- )
- // Sandbox 沙盘推演主表
- type Sandbox struct {
- SandboxId int `orm:"column(sandbox_id);pk" description:"沙盘id"`
- Name string `description:"沙盘名称"`
- ChartPermissionId int `description:"品种id"`
- ChartPermissionName string `description:"品种名称"`
- CurrVersion int `description:"当前版本"`
- Code string `description:"沙盘code"`
- Content string `description:"沙盘数据"`
- PicUrl string `description:"沙盘图片地址"`
- OpUserId int `description:"最近一次编辑操作的用户id"`
- OpUserName string `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
- IsDelete int8 `description:"是否删除,0:未删除,1:已删除"`
- ModifyTime time.Time `description:"修改时间"`
- CreateTime time.Time `description:"创建时间"`
- SandboxClassifyId int `description:"分类id"`
- Sort int `description:"排序"`
- }
- // Update 沙盘字段变更
- func (sandbox *Sandbox) Update(cols []string) (err error) {
- o := orm.NewOrmUsingDB("data")
- _, err = o.Update(sandbox, cols...)
- return
- }
- // GetSandboxById 根据沙盘id获取沙盘详情
- func GetSandboxById(sandboxId int) (sandboxInfo *Sandbox, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := `select * from sandbox where sandbox_id = ? and is_delete = 0`
- err = o.Raw(sql, sandboxId).QueryRow(&sandboxInfo)
- return
- }
- // AddNewSandbox 添加一个新的沙盘
- func AddNewSandbox(sandboxInfo *Sandbox, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft) (err error) {
- o := orm.NewOrmUsingDB("data")
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- id, err := to.Insert(sandboxInfo)
- if err != nil {
- return
- }
- sandboxInfo.SandboxId = int(id)
- // 新增版本
- sandboxVersion.SandboxId = sandboxInfo.SandboxId
- id, err = to.Insert(sandboxVersion)
- if err != nil {
- return
- }
- sandboxVersion.SandboxVersionId = int(id)
- // 新增草稿
- sandboxDraft.SandboxId = sandboxInfo.SandboxId
- id, err = to.Insert(sandboxDraft)
- if err != nil {
- return
- }
- sandboxDraft.SandboxDraftId = int(id)
- return
- }
- // UpdateSandbox 更新沙盘
- func UpdateSandbox(sandboxInfo *Sandbox, updateSandboxColumn []string, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft) (err error) {
- o := orm.NewOrmUsingDB("data")
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- _, err = to.Update(sandboxInfo, updateSandboxColumn...)
- if err != nil {
- return
- }
- // 新增版本
- sandboxVersion.SandboxId = sandboxInfo.SandboxId
- id, err := to.Insert(sandboxVersion)
- if err != nil {
- return
- }
- sandboxVersion.SandboxVersionId = int(id)
- // 新增草稿
- sandboxDraft.SandboxId = sandboxInfo.SandboxId
- id, err = to.Insert(sandboxDraft)
- if err != nil {
- return
- }
- sandboxDraft.SandboxDraftId = int(id)
- return
- }
- // UpdateSandboxName 更新沙盘(仅仅更新名称)
- func UpdateSandboxName(sandboxInfo *Sandbox, sandboxVersion *SandboxVersion, sandboxDraft *SandboxDraft, updateSandboxColumn, updateSandboxVersionColumn []string) (err error) {
- o := orm.NewOrmUsingDB("data")
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- _, err = to.Update(sandboxInfo, updateSandboxColumn...)
- if err != nil {
- return
- }
- // 更新版本
- _, err = to.Update(sandboxVersion, updateSandboxVersionColumn...)
- if err != nil {
- return
- }
- // 新增草稿
- sandboxDraft.SandboxId = sandboxInfo.SandboxId
- id, err := to.Insert(sandboxDraft)
- if err != nil {
- return
- }
- sandboxDraft.SandboxDraftId = int(id)
- return
- }
- // SandboxListItem 沙盘推演列表数据
- type SandboxListItem struct {
- SandboxId int `description:"沙盘id"`
- Name string `description:"沙盘名称"`
- ChartPermissionId int `description:"品种id"`
- ChartPermissionName string `description:"品种名称"`
- CurrVersion int `description:"当前版本"`
- Code string `description:"沙盘code"`
- VersionCode string `description:"沙盘版本code"`
- //Content string `description:"沙盘数据"`
- PicUrl string `description:"沙盘图片地址"`
- OpUserId int `description:"最近一次编辑操作的用户id"`
- OpUserName string `description:"最近一次编辑的用户名称(冗余字段,避免查表)"`
- IsDelete int8 `description:"是否删除,0:未删除,1:已删除" json:"is_delete"`
- CanEdit bool `description:"是否可编辑"`
- Editor string `description:"编辑人"`
- VersionTotal int `description:"历史版本数量"`
- ModifyTime time.Time `description:"修改时间"`
- CreateTime time.Time `description:"创建时间"`
- }
- // GetList 获取沙盘列表页
- func GetList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*SandboxListItem, err error) {
- o := orm.NewOrmUsingDB("data")
- 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 "
- sql += condition
- sql += ` order by a.modify_time desc,a.sandbox_id desc`
- totalSql := `select count(1) total from (` + sql + `) z `
- err = o.Raw(totalSql, pars).QueryRow(&total)
- if err != nil {
- return
- }
- sql += ` LIMIT ?,? `
- _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
- return
- }
- // SandboxSaveResp 保存沙盘响应体
- type SandboxSaveResp struct {
- *Sandbox
- VersionCode string `description:"版本号"`
- }
- func GetSandboxAll(sandboxClassifyId int) (list []*SandboxClassifyItems, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := `SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name, sort
- FROM sandbox WHERE chart_classify_id = ? `
- _, err = o.Raw(sql, sandboxClassifyId).QueryRows(&list)
- return
- }
- // CheckOpSandboxPermission 判断沙盘操作权限
- func CheckOpSandboxPermission(sysUser *system.Admin, createUserId int) (ok bool) {
- if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
- ok = true
- }
- // 如果图表创建人与当前操作人相同的话,那么就是允许操作
- if ok == false && createUserId == sysUser.AdminId {
- ok = true
- }
- // 如果图表权限id 是 1 ,那么允许编辑
- if ok == false && sysUser.ChartPermission == 1 {
- ok = true
- }
- return
- }
- // GetSandboxInfoByAdminId 获取所有我创建的沙盘,用于分类展示
- func GetSandboxInfoByAdminId(adminId int) (items []*SandboxClassifyItems, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT sandbox_id,sandbox_classify_id,name AS sandbox_classify_name,code,
- sys_user_id,sys_user_real_name
- FROM sandbox where sys_user_id = ? ORDER BY sort asc,create_time ASC `
- _, err = o.Raw(sql, adminId).QueryRows(&items)
- return
- }
|