123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978 |
- package sandbox
- import (
- "encoding/json"
- "eta_gn/eta_api/models"
- "eta_gn/eta_api/models/company"
- "eta_gn/eta_api/models/sandbox"
- "eta_gn/eta_api/models/sandbox/request"
- "eta_gn/eta_api/models/system"
- "eta_gn/eta_api/services/alarm_msg"
- "eta_gn/eta_api/utils"
- "fmt"
- "strconv"
- "strings"
- "time"
- )
- func AddSandboxDraft(sandboxId int, req request.AddAndEditSandbox, opUserId int, opUserName string) (sandboxDraftInfo *sandbox.SandboxDraft, err error) {
-
- lastSandboxDraft, err := sandbox.GetLastSandboxDraftById(sandboxId)
- if err != nil && !utils.IsErrNoRow(err) {
- return
- }
-
- if lastSandboxDraft != nil {
-
- var isUpdateName, isUpdateContent bool
- if lastSandboxDraft.Name != utils.TrimStr(req.Name) {
- isUpdateName = true
- }
-
-
-
-
- if checkoutContent(lastSandboxDraft.Content, req.Content) {
- isUpdateContent = true
- }
-
- if isUpdateName == false && isUpdateContent == false {
- return
- }
- }
-
- chartPermissionInfo, err := company.GetChartPermissionListById(req.ChartPermissionId)
- if err != nil {
- return
- }
-
- sandboxDraftInfo = &sandbox.SandboxDraft{
- SandboxId: sandboxId,
- Name: utils.TrimStr(req.Name),
- ChartPermissionId: req.ChartPermissionId,
- ChartPermissionName: chartPermissionInfo.PermissionName,
- Content: req.Content,
- OpUserId: opUserId,
- OpUserName: opUserName,
- CreateTime: time.Now(),
- }
-
- err = sandbox.AddSandboxDraft(sandboxDraftInfo)
- return
- }
- func UpdateSandboxEditMark(sandboxId, nowUserId, status int, nowUserName string) (ret models.MarkReportResp, err error) {
-
- key := fmt.Sprint(`crm:sandbox:edit:`, sandboxId)
- opUserId, e := utils.Rc.RedisInt(key)
- var opUser models.MarkReportItem
- if e != nil {
- opUserInfoStr, tErr := utils.Rc.RedisString(key)
- if tErr == nil {
- tErr = json.Unmarshal([]byte(opUserInfoStr), &opUser)
- if tErr == nil {
- opUserId = opUser.AdminId
- }
- }
- }
- if opUserId > 0 && opUserId != nowUserId {
- editor := opUser.Editor
- if editor == "" {
-
- otherInfo, e := system.GetSysAdminById(opUserId)
- if e != nil {
- err = fmt.Errorf("查询其他编辑者信息失败")
- return
- }
- editor = otherInfo.RealName
- }
- ret.Status = 1
- ret.Msg = fmt.Sprintf("当前%s正在编辑中", editor)
- ret.Editor = editor
- return
- }
- if status == 1 {
- nowUser := &models.MarkReportItem{AdminId: nowUserId, Editor: nowUserName}
- bt, e := json.Marshal(nowUser)
- if e != nil {
- err = fmt.Errorf("格式化编辑者信息失败")
- return
- }
- if opUserId > 0 {
- utils.Rc.Do("SETEX", key, int64(300), string(bt))
- } else {
- utils.Rc.SetNX(key, string(bt), time.Second*60*5)
- }
- } else if status == 0 {
-
- _ = utils.Rc.Delete(key)
- }
- return
- }
- func DeleteSandbox(sandboxId int) (err error) {
-
- sandboxInfo, err := sandbox.GetSandboxById(sandboxId)
- if err != nil {
- return
- }
- sandboxInfo.IsDelete = 1
- var updateSandboxColumn = []string{"IsDelete"}
- err = sandboxInfo.Update(updateSandboxColumn)
- return
- }
- func GetSandboxVersionDetailByCode(sandboxVersionCode string) (sandboxVersionInfo *sandbox.SandboxVersion, err error) {
-
- sandboxVersionInfo, err = sandbox.GetSandboxVersionBySandboxVersionCode(sandboxVersionCode)
- return
- }
- func GetLastSandboxInfo(sandboxId int) (sandboxInfo *sandbox.Sandbox, err error) {
-
- sandboxInfo, err = sandbox.GetSandboxById(sandboxId)
- return
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return
- }
- func GenerateCode() string {
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- return utils.MD5(fmt.Sprint("sandbox_") + timestamp)
- }
- func GenerateVersionCode(sandboxId, sandboxVersion int) string {
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- return utils.MD5(fmt.Sprint("sandbox_version_", sandboxId, "_", sandboxVersion) + timestamp)
- }
- type ContentStruct struct {
- Cells []struct {
- Attrs struct {
- Line struct {
- SourceMarker bool `json:"sourceMarker"`
- Stroke string `json:"stroke"`
- StrokeDasharray string `json:"strokeDasharray"`
- } `json:"line"`
- Rect struct {
- Fill string `json:"fill"`
- Stroke string `json:"stroke"`
- StrokeDasharray interface{} `json:"strokeDasharray"`
- StrokeWidth int64 `json:"strokeWidth"`
- } `json:"rect"`
- Text struct {
- Fill string `json:"fill"`
- FontSize float64 `json:"fontSize"`
- FontWeight string `json:"fontWeight"`
- LineHeight float64 `json:"lineHeight"`
- Text string `json:"text"`
- TextWrap struct {
- Text string `json:"text"`
- Width int64 `json:"width"`
- } `json:"textWrap"`
- } `json:"text"`
- } `json:"attrs"`
- Data struct {
- Key string `json:"key"`
- } `json:"data"`
- ID string `json:"id"`
- Ports struct {
- Groups struct {
- Port_bottom struct {
- Attrs struct {
- Circle struct {
- Fill string `json:"fill"`
- Magnet bool `json:"magnet"`
- R int64 `json:"r"`
- Stroke string `json:"stroke"`
- StrokeWidth int64 `json:"strokeWidth"`
- } `json:"circle"`
- } `json:"attrs"`
- Position string `json:"position"`
- ZIndex int64 `json:"zIndex"`
- } `json:"port-bottom"`
- Port_left struct {
- Attrs struct {
- Circle struct {
- Fill string `json:"fill"`
- Magnet bool `json:"magnet"`
- R int64 `json:"r"`
- Stroke string `json:"stroke"`
- StrokeWidth int64 `json:"strokeWidth"`
- } `json:"circle"`
- } `json:"attrs"`
- Position string `json:"position"`
- ZIndex int64 `json:"zIndex"`
- } `json:"port-left"`
- Port_right struct {
- Attrs struct {
- Circle struct {
- Fill string `json:"fill"`
- Magnet bool `json:"magnet"`
- R int64 `json:"r"`
- Stroke string `json:"stroke"`
- StrokeWidth int64 `json:"strokeWidth"`
- } `json:"circle"`
- } `json:"attrs"`
- Position string `json:"position"`
- ZIndex int64 `json:"zIndex"`
- } `json:"port-right"`
- Port_top struct {
- Attrs struct {
- Circle struct {
- Fill string `json:"fill"`
- Magnet bool `json:"magnet"`
- R int64 `json:"r"`
- Stroke string `json:"stroke"`
- StrokeWidth int64 `json:"strokeWidth"`
- } `json:"circle"`
- } `json:"attrs"`
- Position string `json:"position"`
- ZIndex int64 `json:"zIndex"`
- } `json:"port-top"`
- } `json:"groups"`
- Items []struct {
- Group string `json:"group"`
- ID string `json:"id"`
- } `json:"items"`
- } `json:"ports"`
- Position struct {
- X float64 `json:"x"`
- Y float64 `json:"y"`
- } `json:"position"`
- Shape string `json:"shape"`
- Size struct {
- Height float64 `json:"height"`
- Width float64 `json:"width"`
- } `json:"size"`
- Source struct {
- Cell string `json:"cell"`
- Port string `json:"port"`
- } `json:"source"`
- Target struct {
- Cell string `json:"cell"`
- Port string `json:"port"`
- } `json:"target"`
- ZIndex int64 `json:"zIndex"`
- } `json:"cells"`
- }
- type SendBoxNodeData struct {
- linkData []SandBoxLinkData `json:"linkData"`
- linkFold bool `json:"linkFold"`
- }
- type SandBoxLinkData struct {
- RId string `json:"RId"`
- Id int `json:"Id"`
- Name string `json:"Name"`
- Type int `json:"Type"`
- Editing bool `json:"editing"`
- DatabaseType int `json:"databaseType"`
- DetailParams SandBoxDetailParams `json:"detailParams"`
- }
- type SandBoxDetailParams struct {
- Code string `json:"code"`
- Id int `json:"id"`
- ClassifyId int `json:"classifyId"`
- }
- func checkoutContent(oldContent, reqContent string) (isUpdate bool) {
- defer func() {
-
- if utils.MD5(oldContent) != utils.MD5(reqContent) {
- isUpdate = true
- }
- }()
- var oldContentInfo, reqContentInfo ContentStruct
- err := json.Unmarshal([]byte(oldContent), &oldContentInfo)
- if err != nil {
- fmt.Println("old json.Unmarshal err:", err)
- return
- }
- oldContentInfoByte, err := json.Marshal(oldContentInfo)
- if err != nil {
- fmt.Println("old json.Marshal err:", err)
- return
- }
- oldContent = string(oldContentInfoByte)
- err = json.Unmarshal([]byte(reqContent), &reqContentInfo)
- if err != nil {
- fmt.Println("req json.Unmarshal err:", err)
- return
- }
- reqContentInfoByte, err := json.Marshal(reqContentInfo)
- if err != nil {
- fmt.Println("req json.Marshal err:", err)
- return
- }
- reqContent = string(reqContentInfoByte)
- return
- }
- func sandboxClassifyHaveChild(allNode []*sandbox.SandboxClassifyItems, node *sandbox.SandboxClassifyItems) (childs []*sandbox.SandboxClassifyItems, yes bool) {
- for _, v := range allNode {
- if v.ParentId == node.SandboxClassifyId {
- childs = append(childs, v)
- }
- }
- if len(childs) > 0 {
- yes = true
- }
- return
- }
- func SandboxClassifyItemsMakeTree(sysUser *system.Admin, allNode []*sandbox.SandboxClassifyItems, node *sandbox.SandboxClassifyItems) {
- childs, _ := sandboxClassifyHaveChild(allNode, node)
- if len(childs) > 0 {
- node.Children = append(node.Children, childs[0:]...)
- for _, v := range childs {
- _, has := sandboxClassifyHaveChild(allNode, v)
- if has {
- SandboxClassifyItemsMakeTree(sysUser, allNode, v)
- } else {
- childrenArr := make([]*sandbox.SandboxClassifyItems, 0)
- v.Children = childrenArr
- }
- }
- } else {
- childrenArr := make([]*sandbox.SandboxClassifyItems, 0)
- node.Children = childrenArr
- }
- }
- func GetSandboxClassifyListForMe(adminInfo system.Admin, resp *sandbox.SandboxClassifyListResp, sandboxClassifyId int) (errMsg string, err error) {
- rootList, err := sandbox.GetSandboxClassifyByParentId(sandboxClassifyId)
- if err != nil && !utils.IsErrNoRow(err) {
- errMsg = "获取失败"
- return
- }
- classifyAll, err := sandbox.GetSandboxClassifyByParentId(sandboxClassifyId)
- if err != nil && !utils.IsErrNoRow(err) {
- errMsg = "获取失败"
- return
- }
- sandboxAll, err := sandbox.GetSandboxInfoByAdminId(adminInfo.AdminId)
- if err != nil && !utils.IsErrNoRow(err) {
- errMsg = "获取失败"
- return
- }
- sandListMap := make(map[int][]*sandbox.SandboxClassifyItems)
- for _, v := range sandboxAll {
- if _, ok := sandListMap[v.SandboxClassifyId]; !ok {
- list := make([]*sandbox.SandboxClassifyItems, 0)
- list = append(list, v)
- sandListMap[v.SandboxClassifyId] = list
- } else {
- sandListMap[v.SandboxClassifyId] = append(sandListMap[v.SandboxClassifyId], v)
- }
- }
- nodeAll := make([]*sandbox.SandboxClassifyItems, 0)
- for k := range rootList {
- rootNode := rootList[k]
- SandboxClassifyItemsMakeTree(&adminInfo, classifyAll, rootNode)
- nodeAll = append(nodeAll, rootNode)
- }
-
-
-
- newAll := SandboxItemsMakeTree(nodeAll, sandListMap, sandboxClassifyId)
- resp.AllNodes = newAll
- return
- }
- func HandleNoPermissionSandbox(allNodes []*sandbox.SandboxClassifyItems, noPermissionChartIdMap map[int]bool) (newAllNodes []*sandbox.SandboxClassifyItems) {
-
- newAllNodes = make([]*sandbox.SandboxClassifyItems, 0)
- for _, node := range allNodes {
-
- tmpNodeInfo := *node
- tmpNodeList := make([]*sandbox.SandboxClassifyItems, 0)
- if node.Children != nil {
- for _, chartList := range node.Children {
- tmpInfo := *chartList
- tmpList := make([]*sandbox.SandboxClassifyItems, 0)
- if chartList.Children != nil {
- for _, chartInfo := range chartList.Children {
- thirdInfo := *chartInfo
- thirdList := make([]*sandbox.SandboxClassifyItems, 0)
-
- if _, ok := noPermissionChartIdMap[chartInfo.SandboxId]; ok {
- continue
- }
- tmpList = append(tmpList, chartInfo)
- if chartInfo.Children != nil {
- for _, thirdChart := range chartInfo.Children {
-
- if _, ok := noPermissionChartIdMap[chartInfo.SandboxId]; ok {
- continue
- }
- thirdList = append(thirdList, thirdChart)
- }
- }
- thirdInfo.Children = thirdList
- tmpList = append(tmpList, &thirdInfo)
- }
- }
- tmpInfo.Children = tmpList
- tmpNodeList = append(tmpNodeList, &tmpInfo)
- }
- }
- tmpNodeInfo.Children = tmpNodeList
- newAllNodes = append(newAllNodes, &tmpNodeInfo)
- }
- return
- }
- func AddSandboxV2(req request.AddAndEditSandboxV2, opUserId int, opUserName string) (resp *sandbox.SandboxSaveResp, err error) {
- resp = new(sandbox.SandboxSaveResp)
-
- sandboxInfo := &sandbox.Sandbox{
- Name: utils.TrimStr(req.Name),
- Code: GenerateCode(),
- Content: req.Content,
- MindmapData: req.MindmapData,
- PicUrl: utils.TrimStr(req.PicUrl),
- SysUserId: opUserId,
- SysUserName: opUserName,
- IsDelete: 0,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- SandboxClassifyId: req.SandboxClassifyId,
- Sort: 0,
- Style: req.Style,
- }
-
- err = sandbox.AddSandbox(sandboxInfo)
- if err != nil {
- return
- }
- resp.Sandbox = sandboxInfo
- return
- }
- func SandboxItemsMakeTree(allNode []*sandbox.SandboxClassifyItems, sandListMap map[int][]*sandbox.SandboxClassifyItems, sandboxClassifyId int) (nodeAll []*sandbox.SandboxClassifyItems) {
- for k := range allNode {
- if len(allNode[k].Children) > 0 {
- SandboxItemsMakeTree(allNode[k].Children, sandListMap, sandboxClassifyId)
- allNode = append(allNode, sandListMap[allNode[k].ParentId]...)
- nodeAll = allNode
- } else if k == len(allNode)-1 {
- allNode = append(allNode, sandListMap[allNode[k].ParentId]...)
- nodeAll = allNode
- }
- }
- if len(allNode) == 0 {
- nodeAll = append(nodeAll, sandListMap[sandboxClassifyId]...)
- }
- return
- }
- func SandboxClassifyHaveChild(allNode []*sandbox.SandboxClassifyItems, node *sandbox.SandboxClassifyItems) (childs []*sandbox.SandboxClassifyItems, yes bool) {
- for _, v := range allNode {
- if v.ParentId == node.SandboxClassifyId {
- childs = append(childs, v)
- }
- }
- if len(childs) > 0 {
- yes = true
- }
- return
- }
- func SandboxClassifyItemsMakeTreeV2(sysUser *system.Admin, allNode []*sandbox.SandboxClassifyItems, node *sandbox.SandboxClassifyItems) {
- childs, _ := sandboxClassifyHaveChildV2(allNode, node)
- if len(childs) > 0 {
- node.Children = append(node.Children, childs[0:]...)
- for _, v := range childs {
- _, has := sandboxClassifyHaveChildV2(allNode, v)
- if has {
- SandboxClassifyItemsMakeTreeV2(sysUser, allNode, v)
- } else {
-
-
- }
- }
- } else {
-
-
- }
- }
- func sandboxClassifyHaveChildV2(allNode []*sandbox.SandboxClassifyItems, node *sandbox.SandboxClassifyItems) (childs []*sandbox.SandboxClassifyItems, yes bool) {
- for _, v := range allNode {
- if v.ParentId == node.SandboxClassifyId && node.SandboxId == 0 {
- childs = append(childs, v)
- }
- }
- if len(childs) > 0 {
- yes = true
- }
- return
- }
- func GetSandBoxEdbIdsByContent(content string) (edbInfoIds []int, err error) {
- var contentInfo sandbox.ContentDataStruct
- err = json.Unmarshal([]byte(content), &contentInfo)
- if err != nil {
- err = fmt.Errorf("json.Unmarshal err:%s", err.Error())
- return
- }
-
- for _, node := range contentInfo.Cells {
- if node.Data == nil {
- continue
- }
- for _, v := range node.Data.LinkData {
- if v.Type == 1 {
- edbInfoIds = append(edbInfoIds, v.Id)
- }
- }
- }
- return
- }
- func ReplaceEdbInSandbox(oldEdbInfoId, newEdbInfoId int) (err error) {
- updateTotal := 0
- logMsg := ""
-
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg("替换沙盘中的指标记录失败提醒,errmsg:"+err.Error(), 3)
- }
- if logMsg != "" {
- utils.FileLog.Info(fmt.Sprintf("替换ETA逻辑的指标记录,替换总数:%d,旧的指标id:%d,新的指标id:%d;%s", updateTotal, oldEdbInfoId, newEdbInfoId, logMsg))
- }
- }()
-
- total, err := sandbox.GetSandboxListCountByCondition("", []interface{}{})
- if err != nil {
- err = fmt.Errorf("查询沙盘总数失败 Err:%s", err)
- return
- }
-
-
- totalPage := (total + 99) / 100
- updateSandBox := make([]sandbox.Sandbox, 0)
-
- for i := 0; i < totalPage; i += 1 {
- startSize := i * 100
- list, e := sandbox.GetSandboxListByCondition("", []interface{}{}, startSize, 100)
- if e != nil {
- err = fmt.Errorf("查询沙盘列表失败 Err:%s", e)
- return
- }
- for _, v := range list {
- sandOldEdbId := fmt.Sprintf(`"RId":"1-%d","Id":%d,`, oldEdbInfoId, oldEdbInfoId)
- if strings.Contains(v.Content, sandOldEdbId) {
- sandNewEdbId := fmt.Sprintf(`"RId":"1-%d","Id":%d,`, newEdbInfoId, newEdbInfoId)
- v.Sandbox.Content = strings.ReplaceAll(v.Content, sandOldEdbId, sandNewEdbId)
- updateSandBox = append(updateSandBox, v.Sandbox)
- logMsg += `涉及到的逻辑id:` + strconv.Itoa(v.Sandbox.SandboxId) + ";"
- if len(updateSandBox) > 100 {
- err = sandbox.UpdateSandboxContent(updateSandBox)
- if err != nil {
- err = fmt.Errorf("更新沙盘表失败 Err:%s", err)
- return
- }
- updateTotal += len(updateSandBox)
- updateSandBox = make([]sandbox.Sandbox, 0)
- }
- }
- }
- }
- if len(updateSandBox) > 0 {
- err = sandbox.UpdateSandboxContent(updateSandBox)
- if err != nil {
- err = fmt.Errorf("更新沙盘表失败 Err:%s", err)
- return
- }
- updateTotal += len(updateSandBox)
- }
- return
- }
- func GetSandBoxParentIds(classifyId int, classifymap map[int]*sandbox.SandboxClassifyItems, parentIds *[]int) {
- if item, ok := classifymap[classifyId]; ok {
- if item.ParentId == 0 {
- return
- }
- *parentIds = append(*parentIds, item.ParentId)
- GetSandBoxParentIds(item.ParentId, classifymap, parentIds)
- }
- }
- func GetSandBoxClassifyChildIds(classifyId int, classifymap map[int]*sandbox.SandboxClassifyItems, childIds *[]int) {
- for _, item := range classifymap {
- if item.ParentId == classifyId {
- *childIds = append(*childIds, item.SandboxClassifyId)
- GetSandBoxClassifyChildIds(item.SandboxClassifyId, classifymap, childIds)
- }
- }
- }
|