public NewErrorModel DefaultRead(string taskId, int CompanyId = 0)
        {
            try
            {
                if (CompanyId == 0)  //研究院
                {
                    ProcessingProgress processingProgress = new ProcessingProgress();
                    using (DDContext context = new DDContext())
                    {
                        //判断流程是否存在
                        List <Models.DingModels.Tasks> tasksListNew = context.Tasks.Where(t => t.TaskId.ToString() == taskId && t.FlowId.ToString() == "6").ToList();
                        if (tasksListNew.Count == 0)
                        {
                            return(new NewErrorModel()
                            {
                                error = new Error(1, "流水号不存在!", "")
                                {
                                },
                            });
                        }
                        //判断流程是否已结束
                        List <Models.DingModels.Tasks> tasksList = context.Tasks.Where(t => t.TaskId.ToString() == taskId && t.IsSend != true && t.State == 0).ToList();
                        if (tasksList.Count > 0)
                        {
                            return(new NewErrorModel()
                            {
                                error = new Error(1, "流程未结束!", "")
                                {
                                },
                            });
                        }
                        Models.DingModels.Tasks tasksFinish = context.Tasks.Where(t => t.TaskId.ToString() == taskId && t.NodeId == 5).FirstOrDefault();
                        Models.DingModels.Tasks tasks       = context.Tasks.Where(t => t.TaskId.ToString() == taskId && t.NodeId == 0).FirstOrDefault();
                        if (tasks == null)
                        {
                            return(new NewErrorModel()
                            {
                                error = new Error(1, "流水号不存在", "")
                                {
                                },
                            });
                        }

                        Models.DingModels.ProjectInfo projectInfo = context.ProjectInfo.Where(p => p.ProjectId == tasks.ProjectId.ToString()).FirstOrDefault();
                        Models.DingModels.Purchase    purchase    = context.Purchase.Where(p => p.TaskId == taskId).FirstOrDefault();
                        processingProgress.ProjectType      = projectInfo.ProjectType;
                        processingProgress.ProjectSmallType = projectInfo.ProjectSmallType;
                        processingProgress.ProjectId        = projectInfo.ProjectId;
                        processingProgress.ProjectName      = projectInfo.ProjectName;
                        processingProgress.TaskId           = taskId;
                        processingProgress.Bom        = projectInfo.ProjectName + "(流水号" + taskId + ")";
                        processingProgress.Designer   = JsonConvert.DeserializeObject <DesignerModel>(tasks.counts).Designer;
                        processingProgress.DesignerId = JsonConvert.DeserializeObject <DesignerModel>(tasks.counts).DesignerId;
                        processingProgress.BomTime    = DateTime.Parse(tasksFinish.ApplyTime).ToString("yyyy-MM-dd");
                        processingProgress.TwoD       = DateTime.Parse(tasksFinish.ApplyTime).ToString("yyyy-MM-dd");
                        processingProgress.ThreeD     = DateTime.Parse(tasksFinish.ApplyTime).ToString("yyyy-MM-dd");
                        processingProgress.NeedTime   = purchase.NeedTime;
                        processingProgress.NeedCount  = tasks.Remark;
                    }
                    return(new NewErrorModel()
                    {
                        data = processingProgress,
                        error = new Error(0, "读取成功!", "")
                        {
                        },
                    });
                }
                else
                {
                    ProcessingProgress processingProgress = new ProcessingProgress();
                    using (DDContextHs context = new DDContextHs())
                    {
                        //判断流程是否存在
                        List <Models.DingModelsHs.Tasks> tasksListNew = context.Tasks.Where(t => t.TaskId.ToString() == taskId && t.FlowId.ToString() == "6").ToList();
                        if (tasksListNew.Count == 0)
                        {
                            return(new NewErrorModel()
                            {
                                error = new Error(1, "流水号不存在!", "")
                                {
                                },
                            });
                        }
                        //判断流程是否已结束
                        List <Models.DingModelsHs.Tasks> tasksList = context.Tasks.Where(t => t.TaskId.ToString() == taskId && t.IsSend != true && t.State == 0).ToList();
                        if (tasksList.Count > 0)
                        {
                            return(new NewErrorModel()
                            {
                                error = new Error(1, "流程未结束!", "")
                                {
                                },
                            });
                        }
                        Models.DingModelsHs.Tasks tasksFinish = context.Tasks.Where(t => t.TaskId.ToString() == taskId && t.NodeId == 5).FirstOrDefault();
                        Models.DingModelsHs.Tasks tasks       = context.Tasks.Where(t => t.TaskId.ToString() == taskId && t.NodeId == 0).FirstOrDefault();
                        if (tasks == null)
                        {
                            return(new NewErrorModel()
                            {
                                error = new Error(1, "流水号不存在", "")
                                {
                                },
                            });
                        }

                        Models.DingModelsHs.ProjectInfo projectInfo = context.ProjectInfo.Where(p => p.ProjectId == tasks.ProjectId.ToString()).FirstOrDefault();
                        Models.DingModelsHs.Purchase    purchase    = context.Purchase.Where(p => p.TaskId == taskId).FirstOrDefault();
                        processingProgress.ProjectType      = projectInfo.ProjectType;
                        processingProgress.ProjectSmallType = projectInfo.ProjectSmallType;
                        processingProgress.ProjectId        = projectInfo.ProjectId;
                        processingProgress.ProjectName      = projectInfo.ProjectName;
                        processingProgress.TaskId           = taskId;
                        processingProgress.Bom        = projectInfo.ProjectName + "(流水号" + taskId + ")";
                        processingProgress.Designer   = JsonConvert.DeserializeObject <DesignerModel>(tasks.counts).Designer;
                        processingProgress.DesignerId = JsonConvert.DeserializeObject <DesignerModel>(tasks.counts).DesignerId;
                        processingProgress.BomTime    = DateTime.Parse(tasksFinish.ApplyTime).ToString("yyyy-MM-dd");
                        processingProgress.TwoD       = DateTime.Parse(tasksFinish.ApplyTime).ToString("yyyy-MM-dd");
                        processingProgress.ThreeD     = DateTime.Parse(tasksFinish.ApplyTime).ToString("yyyy-MM-dd");
                        processingProgress.NeedTime   = purchase.NeedTime;
                        processingProgress.NeedCount  = tasks.Remark;
                    }
                    return(new NewErrorModel()
                    {
                        data = processingProgress,
                        error = new Error(0, "读取成功!", "")
                        {
                        },
                    });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <NewErrorModel> Save(ProcessingProgressModel processingProgressModel)
        {
            try
            {
                if (processingProgressModel.CompanyId == 0)  //研究院
                {
                    DDContext dDContext = new DDContext();
                    string    eappUrl   = string.Format("eapp://page/start/productionMonitoring/productionMonitoring?taskid={0}&companyId={1}", processingProgressModel.processingProgresses[0].TaskId, processingProgressModel.CompanyId);
                    if (dDContext.Roles.Where(r => r.RoleName == "生产加工进度发起人" && r.UserId == processingProgressModel.applyManId).ToList().Count == 0)
                    {
                        return(new NewErrorModel()
                        {
                            error = new Error(1, "没有权限上传!", "")
                            {
                            },
                        });
                    }

                    List <Models.DingModels.ProjectInfo> projectInfos = dDContext.ProjectInfo.ToList();

                    if (processingProgressModel.IsExcelUpload)
                    {
                        foreach (var processingProgresse in processingProgressModel.processingProgresses)
                        {
                            //校对数据
                            if (!string.IsNullOrEmpty(processingProgresse.TaskId))
                            {
                                processingProgresse.CompanyId = processingProgressModel.CompanyId.ToString();
                                List <ProcessingProgress> ProcessingProgressList = dDContext.ProcessingProgress.Where(p => p.TaskId == processingProgresse.TaskId && p.CompanyId == processingProgressModel.CompanyId.ToString()).ToList();
                                if (ProcessingProgressList.Count > 0)
                                {
                                    return(new NewErrorModel()
                                    {
                                        error = new Error(1, string.Format("保存失败,系统中已存在流水号 {0} 的数据", processingProgresse.TaskId), "")
                                        {
                                        },
                                    });
                                }
                                if (projectInfos.Where(p => p.ProjectId == processingProgresse.ProjectId &&
                                                       p.ProjectName == processingProgresse.ProjectName).ToList().Count == 0)
                                {
                                    return(new NewErrorModel()
                                    {
                                        error = new Error(1, string.Format("保存失败,项目Id {0} 、 项目名 {1} 与系统中的数据不吻合!", processingProgresse.ProjectId, processingProgresse.ProjectName), "")
                                        {
                                        },
                                    });
                                }
                                else
                                {
                                    if (projectInfos.Where(p => p.ProjectId == processingProgresse.ProjectId &&
                                                           p.ProjectName == p.ProjectName && p.ProjectType == processingProgresse.ProjectType &&
                                                           p.ProjectSmallType == processingProgresse.ProjectSmallType).ToList().Count == 0)
                                    {
                                        return(new NewErrorModel()
                                        {
                                            error = new Error(1, string.Format("保存失败,项目Id {0} 、 项目名 {1} 与系统中的大类、小类不吻合!", processingProgresse.ProjectId, processingProgresse.ProjectName), "")
                                            {
                                            },
                                        });
                                    }
                                    else
                                    {
                                        List <Models.DingModels.Tasks> tasksDesigner          = dDContext.Tasks.Where(t => t.ApplyMan.Contains(processingProgresse.Designer)).ToList();
                                        List <Models.DingModels.Tasks> tasksNoteTaker         = dDContext.Tasks.Where(t => t.ApplyMan.Contains(processingProgresse.NoteTaker)).ToList();
                                        List <Models.DingModels.Tasks> tasksHeadOfDepartments = dDContext.Tasks.Where(t => t.ApplyMan.Contains(processingProgresse.HeadOfDepartments)).ToList();
                                        List <Models.DingModels.Tasks> tasksTabulator         = dDContext.Tasks.Where(t => t.ApplyMan.Contains(processingProgresse.Tabulator)).ToList();
                                        if (tasksDesigner.Count == 0 || tasksNoteTaker.Count == 0 || tasksHeadOfDepartments.Count == 0 || tasksTabulator.Count == 0)
                                        {
                                            if (tasksTabulator.Count == 0)
                                            {
                                                return(new NewErrorModel()
                                                {
                                                    error = new Error(1, string.Format("保存失败,系统中找不到:制表人 {0} 的Id   !", processingProgresse.Tabulator), "")
                                                    {
                                                    },
                                                });
                                            }
                                            if (tasksDesigner.Count == 0)
                                            {
                                                return(new NewErrorModel()
                                                {
                                                    error = new Error(1, string.Format("保存失败,系统中找不到:设计员 {0} 的Id   !", processingProgresse.NoteTaker), "")
                                                    {
                                                    },
                                                });
                                            }
                                            if (tasksNoteTaker.Count == 0)
                                            {
                                                return(new NewErrorModel()
                                                {
                                                    error = new Error(1, string.Format("保存失败,系统中找不到:记录员 {0} 的Id   !", processingProgresse.Designer), "")
                                                    {
                                                    },
                                                });
                                            }
                                            if (tasksHeadOfDepartments.Count == 0)
                                            {
                                                return(new NewErrorModel()
                                                {
                                                    error = new Error(1, string.Format("保存失败,系统中找不到:部门负责人 {0} 的Id   !", processingProgresse.HeadOfDepartments), "")
                                                    {
                                                    },
                                                });
                                            }
                                        }
                                        else
                                        {
                                            processingProgresse.TabulatorId         = tasksTabulator[0].ApplyManId;
                                            processingProgresse.DesignerId          = tasksDesigner[0].ApplyManId;
                                            processingProgresse.NoteTakerId         = tasksNoteTaker[0].ApplyManId;
                                            processingProgresse.HeadOfDepartmentsId = tasksHeadOfDepartments[0].ApplyManId;
                                            processingProgresse.CreateTime          = DateTime.Now.ToString("yyyy-MM-dd");
                                            dDContext.ProcessingProgress.Add(processingProgresse);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (!processingProgressModel.IsExcelUpload)  //操作界面添加
                    {
                        List <ProcessingProgress> ProcessingProgressList = new List <ProcessingProgress>();

                        foreach (ProcessingProgress processingProgresse in processingProgressModel.processingProgresses)
                        {
                            List <ProcessingProgress> ProcessingProgressListNew = dDContext.ProcessingProgress.Where(p => p.TaskId == processingProgresse.TaskId && p.CompanyId == processingProgressModel.CompanyId.ToString()).ToList();
                            if (ProcessingProgressListNew.Count > 0)
                            {
                                return(new NewErrorModel()
                                {
                                    error = new Error(1, string.Format("保存失败,系统中已存在流水号 {0} 的数据", processingProgresse.TaskId), "")
                                    {
                                    },
                                });
                            }
                            processingProgresse.CompanyId = processingProgressModel.CompanyId.ToString();
                            Roles roles = dDContext.Roles.Where(r => r.RoleName == "生产加工进度分配人").FirstOrDefault();
                            //推送钉钉消息给设计人员和部门负责人(胡工)
                            DingTalkServersController dingTalkServersController = new DingTalkServersController();
                            await dingTalkServersController.SendProcessingProgress(processingProgresse.DesignerId, 0, processingProgressModel.applyMan, processingProgresse.Bom
                                                                                   , processingProgresse.TaskId, processingProgresse.CompanyName, processingProgresse.SpeedOfProgress, processingProgresse.IsAlreadyRead, eappUrl);

                            await dingTalkServersController.SendProcessingProgress(roles.UserId, 0, processingProgressModel.applyMan, processingProgresse.Bom
                                                                                   , processingProgresse.TaskId, processingProgresse.CompanyName, processingProgresse.SpeedOfProgress, processingProgresse.IsAlreadyRead, eappUrl);

                            processingProgresse.CreateTime = DateTime.Now.ToString("yyyy-MM-dd");
                            ProcessingProgressList.Add(processingProgresse);
                        }
                        dDContext.ProcessingProgress.AddRange(ProcessingProgressList);
                    }
                    dDContext.SaveChanges();
                    return(new NewErrorModel()
                    {
                        error = new Error(0, string.Format("保存成功!共计{0}条数据 ", processingProgressModel.processingProgresses.Count), "")
                        {
                        },
                    });
                }
                else  //华数
                {
                    DDContext   dDContext   = new DDContext();
                    DDContextHs dDContextHs = new DDContextHs();
                    string      eappUrl     = string.Format("eapp://page/start/productionMonitoring/productionMonitoring?taskid={0}&companyId={1}", processingProgressModel.processingProgresses[0].TaskId, processingProgressModel.CompanyId);
                    if (dDContext.Roles.Where(r => r.RoleName == "生产加工进度发起人" && r.UserId == processingProgressModel.applyManId).ToList().Count == 0)
                    {
                        return(new NewErrorModel()
                        {
                            error = new Error(1, "没有权限上传!", "")
                            {
                            },
                        });
                    }

                    List <Models.DingModelsHs.ProjectInfo> projectInfos = dDContextHs.ProjectInfo.ToList();
                    if (processingProgressModel.IsExcelUpload)  //操作界面添加
                    {
                        foreach (var processingProgresse in processingProgressModel.processingProgresses)
                        {
                            //校对数据
                            if (!string.IsNullOrEmpty(processingProgresse.TaskId))
                            {
                                processingProgresse.CompanyId = processingProgressModel.CompanyId.ToString();
                                List <ProcessingProgress> ProcessingProgressList = dDContext.ProcessingProgress.Where(p => p.TaskId == processingProgresse.TaskId).ToList();
                                if (ProcessingProgressList.Count > 0)
                                {
                                    return(new NewErrorModel()
                                    {
                                        error = new Error(1, string.Format("保存失败,系统中已存在流水号 {0} 的数据", processingProgresse.TaskId), "")
                                        {
                                        },
                                    });
                                }
                                if (projectInfos.Where(p => p.ProjectId == processingProgresse.ProjectId &&
                                                       p.ProjectName == processingProgresse.ProjectName).ToList().Count == 0)
                                {
                                    return(new NewErrorModel()
                                    {
                                        error = new Error(1, string.Format("保存失败,项目Id {0} 、 项目名 {1} 与系统中的数据不吻合!", processingProgresse.ProjectId, processingProgresse.ProjectName), "")
                                        {
                                        },
                                    });
                                }
                                else
                                {
                                    //华数不校对项目类别
                                    //  if (projectInfos.Where(p => p.ProjectId == processingProgresse.ProjectId &&
                                    //p.ProjectName == p.ProjectName && p.ProjectType == processingProgresse.ProjectType
                                    //&& p.ProjectSmallType == processingProgresse.ProjectSmallType).ToList().Count == 0)
                                    //  {
                                    //      return new NewErrorModel()
                                    //      {
                                    //          error = new Error(1, string.Format("保存失败,项目Id {0} 、 项目名 {1} 与系统中的大类、小类不吻合!", processingProgresse.ProjectId, processingProgresse.ProjectName), "") { },
                                    //      };
                                    //  }
                                    //  else
                                    //  {
                                    List <Models.DingModelsHs.Tasks> tasksDesigner          = dDContextHs.Tasks.Where(t => t.ApplyMan.Contains(processingProgresse.Designer)).ToList();
                                    List <Models.DingModelsHs.Tasks> tasksNoteTaker         = dDContextHs.Tasks.Where(t => t.ApplyMan.Contains(processingProgresse.NoteTaker)).ToList();
                                    List <Models.DingModelsHs.Tasks> tasksHeadOfDepartments = dDContextHs.Tasks.Where(t => t.ApplyMan.Contains(processingProgresse.HeadOfDepartments)).ToList();
                                    List <Models.DingModelsHs.Tasks> tasksTabulator         = dDContextHs.Tasks.Where(t => t.ApplyMan.Contains(processingProgresse.Tabulator)).ToList();
                                    if (tasksDesigner.Count == 0 || tasksNoteTaker.Count == 0 || tasksHeadOfDepartments.Count == 0 || tasksTabulator.Count == 0)
                                    {
                                        if (tasksTabulator.Count == 0)
                                        {
                                            return(new NewErrorModel()
                                            {
                                                error = new Error(1, string.Format("保存失败,系统中找不到:制表人 {0} 的Id   !", processingProgresse.Tabulator), "")
                                                {
                                                },
                                            });
                                        }
                                        if (tasksDesigner.Count == 0)
                                        {
                                            return(new NewErrorModel()
                                            {
                                                error = new Error(1, string.Format("保存失败,系统中找不到:设计员 {0} 的Id   !", processingProgresse.NoteTaker), "")
                                                {
                                                },
                                            });
                                        }
                                        if (tasksNoteTaker.Count == 0)
                                        {
                                            return(new NewErrorModel()
                                            {
                                                error = new Error(1, string.Format("保存失败,系统中找不到:记录员 {0} 的Id   !", processingProgresse.Designer), "")
                                                {
                                                },
                                            });
                                        }
                                        if (tasksHeadOfDepartments.Count == 0)
                                        {
                                            return(new NewErrorModel()
                                            {
                                                error = new Error(1, string.Format("保存失败,系统中找不到:部门负责人 {0} 的Id   !", processingProgresse.HeadOfDepartments), "")
                                                {
                                                },
                                            });
                                        }
                                    }
                                    else
                                    {
                                        processingProgresse.TabulatorId         = tasksTabulator[0].ApplyManId;
                                        processingProgresse.DesignerId          = tasksDesigner[0].ApplyManId;
                                        processingProgresse.NoteTakerId         = tasksNoteTaker[0].ApplyManId;
                                        processingProgresse.HeadOfDepartmentsId = tasksHeadOfDepartments[0].ApplyManId;
                                        processingProgresse.CreateTime          = DateTime.Now.ToString("yyyy-MM-dd");
                                        dDContext.ProcessingProgress.Add(processingProgresse);
                                    }
                                }
                            }
                        }
                    }
                    if (!processingProgressModel.IsExcelUpload)  //操作界面添加
                    {
                        List <ProcessingProgress> ProcessingProgressList = new List <ProcessingProgress>();
                        foreach (var processingProgresse in processingProgressModel.processingProgresses)
                        {
                            List <ProcessingProgress> ProcessingProgressListNew = dDContext.ProcessingProgress.Where(p => p.TaskId == processingProgresse.TaskId && p.CompanyId == processingProgressModel.CompanyId.ToString()).ToList();
                            if (ProcessingProgressListNew.Count > 0)
                            {
                                return(new NewErrorModel()
                                {
                                    error = new Error(1, string.Format("保存失败,系统中已存在流水号 {0} 的数据", processingProgresse.TaskId), "")
                                    {
                                    },
                                });
                            }
                            processingProgresse.CompanyId = processingProgressModel.CompanyId.ToString();
                            Roles roles = dDContext.Roles.Where(r => r.RoleName == "生产加工进度分配人").FirstOrDefault();
                            //推送钉钉消息给设计人员和部门负责人(胡工)
                            DingTalkServersController dingTalkServersController = new DingTalkServersController();
                            await dingTalkServersController.SendProcessingProgress(processingProgresse.DesignerId, 0, processingProgressModel.applyMan, processingProgresse.Bom
                                                                                   , processingProgresse.TaskId, processingProgresse.CompanyName, processingProgresse.SpeedOfProgress, processingProgresse.IsAlreadyRead, eappUrl);

                            await dingTalkServersController.SendProcessingProgress(roles.UserId, 0, processingProgressModel.applyMan, processingProgresse.Bom
                                                                                   , processingProgresse.TaskId, processingProgresse.CompanyName, processingProgresse.SpeedOfProgress, processingProgresse.IsAlreadyRead, eappUrl);

                            processingProgresse.CreateTime = DateTime.Now.ToString("yyyy-MM-dd");
                            ProcessingProgressList.Add(processingProgresse);
                        }
                        dDContext.ProcessingProgress.AddRange(ProcessingProgressList);
                    }
                    dDContext.SaveChanges();
                    return(new NewErrorModel()
                    {
                        error = new Error(0, string.Format("保存成功!共计{0}条数据 ", processingProgressModel.processingProgresses.Count), "")
                        {
                        },
                    });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }