Ejemplo n.º 1
0
        public JsonResult EditProcess(int processId, string data, string nodeData, string linkData, string imageprocess)
        {
            Process ps = processService.findProcess(processId);

            processService.insertDataJson(ps, data, imageprocess);
            JArray      nodeArray   = JArray.Parse(nodeData);
            JArray      linkArray   = JArray.Parse(linkData);
            JToken      idfirstStep = linkArray.Where(x => (int)x["from"] == -1).FirstOrDefault();
            List <Step> liststep    = db.Steps.Where(z => z.IdProcess == ps.Id).ToList();
            List <int>  keystep     = new List <int>();
            List <int>  keynode     = new List <int>();
            List <int>  b           = new List <int>();
            string      circle      = "Circle";

            for (int i = 0; i < nodeArray.Count; i++)
            {
                if (nodeArray[i]["figure"] != null)
                {
                    if (nodeArray[i]["figure"].ToString() == circle && nodeArray[i]["fill"].ToString() == "#f20000")
                    {
                        b.Add((int)nodeArray[i]["key"]);
                    }
                }
            }

            //chuyển linkstep về mặc định
            //add new step nếu step chưa có trong liststep
            //so sánh 2 list tìm giá trị giống nhau và khác nhau
            //nếu 2 list có các phần tử giống nhau sẽ lưu vào 1 mảng khác để sử giá trị của step đó
            //nếu 2 list có các phần tử khác nhau sẽ lưu vào 1 mảng khác để tạo mới step đó
            //sau đó kt lại nếu step nào không có nextstep1 and nextstep2,step đó sẽ đc xóa
            foreach (Step ls in liststep)
            {
                ls.StartStep = false;
                ls.NextStep1 = 0;
                ls.NextStep2 = 0;
                keystep.Add(ls.Key);
            }

            for (int i = 0; i < nodeArray.Count; i++)
            {
                keynode.Add((int)nodeArray[i]["key"]);
            }

            int[] same = keynode.Intersect(keystep).ToArray();
            int[] diff = keynode.Union(keystep).Except(same).ToArray();

            List <Step> keystepgiong = new List <Step>();
            List <Step> keystepkhac  = new List <Step>();

            foreach (int item in same)
            {
                Step s = db.Steps.Where(p => p.Key == item && p.IdProcess == ps.Id).FirstOrDefault();
                keystepgiong.Add(s);
            }

            for (int i = 0; i < nodeArray.Count; i++)
            {
                int           key  = (int)nodeArray[i]["key"];
                List <JToken> from = linkArray.Where(x => (int)x["from"] == key).ToList();
                Step          step = new Step();
                int           j    = 1;
                if (from != null)
                {
                    foreach (JToken item1 in from)
                    {
                        int to = (int)item1["to"];
                        if (j == 1)
                        {
                            step.NextStep1 = to;
                        }
                        else if (j == 2)
                        {
                            step.NextStep2 = to;
                        }
                        j++;
                    }
                    if (step.NextStep2 == null)
                    {
                        step.NextStep2 = 0;
                    }
                    if (step.NextStep1 == null)
                    {
                        step.NextStep1 = 0;
                    }
                    foreach (int tokey in b)
                    {
                        if (step.NextStep1 == tokey)
                        {
                            step.NextStep1 = 0;
                        }
                    }
                }

                step.IdProcess  = ps.Id;
                step.Name       = nodeArray[i]["text"].ToString();
                step.Key        = key;
                step.StartStep  = (int)idfirstStep["to"] == (int)nodeArray[i]["key"] ? true : false;
                step.IsRun      = false;
                step.Figure     = nodeArray[i]["figure"] == null ? "Step" : nodeArray[i]["figure"].ToString();
                step.Created_At = DateTime.Now;
                step.Updated_At = DateTime.Now;
                foreach (int item in diff)
                {
                    if (item == key)
                    {
                        keystepkhac.Add(step);
                    }
                    else
                    {
                        Step ste = stepService.findStepByKey(ps.Id, item);
                        if (ste != null)
                        {
                            List <TaskProcess> listtaskofstep = db.TaskProcesses.Where(s => s.IdStep == ste.Id).ToList();
                            for (int s = 0; s < listtaskofstep.Count; s++)
                            {
                                db.TaskProcesses.Remove(listtaskofstep[s]);
                                db.SaveChanges();
                            }
                            string stepPath = string.Format("Upload/{0}/{1}/{2}", ps.Group.Id, ps.Id, ste.Id);
                            fileService.removeDirectory(stepPath);
                            stepService.removeStep(ste);
                        }
                    }
                }
            }
            foreach (Step item3 in keystepkhac)
            {
                Step step = new Step();
                step.IdProcess  = ps.Id;
                step.Name       = item3.Name;
                step.Key        = item3.Key;
                step.StartStep  = item3.StartStep;
                step.Figure     = item3.Figure;
                step.Created_At = item3.Created_At;
                step.Updated_At = item3.Updated_At;
                step.NextStep1  = item3.NextStep1;
                step.NextStep2  = item3.NextStep2;
                step.Color      = commonService.getRandomColor();
                if (step.Figure != "Circle")
                {
                    db.Steps.Add(step);
                    db.SaveChanges();
                    string stepPath = string.Format("Upload/{0}/{1}/{2}", ps.Group.Id, ps.Id, step.Id);
                    fileService.createDirectory(stepPath);
                }
            }

            foreach (Step listst in keystepgiong)
            {
                for (int i = 0; i < nodeArray.Count; i++)
                {
                    int key = (int)nodeArray[i]["key"];
                    if (key == listst.Key)
                    {
                        List <JToken> from = linkArray.Where(x => (int)x["from"] == key).ToList();
                        int           j    = 1;//if (from != null)
                        {
                            foreach (JToken item2 in from)
                            {
                                int to = (int)item2["to"];
                                if (j == 1)
                                {
                                    listst.NextStep1 = to;
                                }
                                else if (j == 2)
                                {
                                    listst.NextStep2 = to;
                                }
                                j++;
                            }
                            if (listst.NextStep2 == null)
                            {
                                listst.NextStep2 = 0;
                            }
                            if (listst.NextStep1 == null)
                            {
                                listst.NextStep1 = 0;
                            }
                            foreach (int tokey in b)
                            {
                                if (listst.NextStep1 == tokey)
                                {
                                    listst.NextStep1 = 0;
                                }
                            }
                        }
                        listst.StartStep  = (int)idfirstStep["to"] == (int)nodeArray[i]["key"] ? true : false;
                        listst.Name       = nodeArray[i]["text"].ToString();
                        listst.Updated_At = DateTime.Now;
                    }
                }
            }

            db.SaveChanges();
            return(Json(new { id = ps.IdGroup }));
        }