public ProgressResult AddScore(Volunteer vol, Project pro, CreditRecord.CreditScore score)
 {
     if (vol != null && pro != null && score.PncScore <= 5.0 && score.SrvScore <= 5.0 && score.CmmScore <= 5.0)
     {
         if (vol.CreditRecords.FirstOrDefault(o => o.Project.Id == pro.Id) != null)
         {
             return(EditScore(vol, pro, score));
         }
         else
         {
             CreditRecord cr = new CreditRecord
             {
                 UID         = Guid.NewGuid(),
                 Project     = pro,
                 Participant = vol,
                 Score       = score
             };
             database.CreditRecords.Add(cr);
             //pro.CreditRecords.Add(cr);
             //vol.CreditRecords.Add(cr);
             vol.AddCredit(score);
             Save();
             return(ProgressResult.Success());
         }
     }
     else
     {
         return(ProgressResult.Error("评分失败,项目或志愿者不存在."));
     }
 }
        public ProgressResult CreatNewProject(Organization org, DateTime Time, string Name = "", string Place = "", string Detail = "", int Maximum = 70)
        {
            ProgressResult result;

            if (Time == null)
            {
                return(ProgressResult.Error("新项目" + Name + "时间不合法,请重新输入"));
            }
            lock (database)
            {
                var Project = new Project();
                Project.Time           = Time;
                Project.CreatTime      = DateTime.Now;
                Project.Maximum        = Maximum;
                Project.Managers       = new List <AppUser>();
                Project.Place          = Place;
                Project.Name           = Name;
                Project.Details        = Detail;
                Project.Condition      = ProjectCondition.Ongoing;
                Project.ScoreCondition = ProjectScoreCondition.UnScored;
                Project.Organization   = org;
                Project.Volunteers     = new List <Volunteer>();
                database.Projects.Add(Project);
                Save();
                result = ProgressResult.Success();
                return(result);
            }
        }
        public ProgressResult SingleVolunteerInputById(int num, Project pro)
        {
            ProgressResult result;
            var            Volunteer = database.Volunteers.SingleOrDefault(r => r.StudentNum == num);

            pro = database.Projects.Single(p => p.Id == pro.Id);
            if (Volunteer == null)
            {
                return(ProgressResult.Error("志愿者不存在于数据库中"));
            }
            if (IsInProject(pro, Volunteer))
            {
                return(ProgressResult.Error("学号为[" + Volunteer.StudentNum + "]姓名为[" + Volunteer.Name + "]的志愿者已经存在于该项目中."));
            }
            if (pro.Maximum <= pro.Volunteers?.Count)
            {
                return(ProgressResult.Error(ProgressResult.ProgressErrorEnum.BeyondMaxium));
            }
            lock (database)
            {
                if (pro.Volunteers == null)
                {
                    pro.Volunteers = new List <Volunteer>();
                }
                pro.Volunteers.Add(Volunteer);
                Save();
            }
            result = ProgressResult.Success();
            return(result);
        }
        public Project FindProjectByProjectId(int ProjectId)
        {
            var Project = database.Projects.SingleOrDefault(r => r.Id == ProjectId);

            if (Project == null)
            {
                ProgressResult.Error("项目不存在");
            }
            return(Project);
        }
        public ProgressResult ProjectDelete(Project Pro)
        {
            ProgressResult result;

            if (database.Projects.Where(p => p.Id == Pro.Id).Count() == 0 || Pro.Condition == ProjectCondition.Finished)//可以用contains?
            {
                ProgressResult.Error("删除失败,项目不存在或已经结项");
            }
            List <LogRecord>       loglist   = database.LogRecords.Where(l => l.TargetProject.Id == Pro.Id).ToList();
            List <BlackListRecord> blacklist = database.BlackListRecords.Where(b => b.Project.Id == Pro.Id).ToList();

            database.Projects.Remove(Pro);
            Save();
            result = ProgressResult.Success();
            return(result);
        }
        public ProgressResult DeleteVolunteerFromProject(Volunteer Vol, Project Pro)
        {
            ProgressResult result;

            if (!Pro.Volunteers.Contains(Vol))
            {
                return(ProgressResult.Error("志愿者不在该项目中."));
            }
            else
            {
                Pro.Volunteers.Remove(Vol);
                Save();
            }
            result = ProgressResult.Success();
            return(result);
        }
        public ProgressResult DeletManager(int StuNum, Project Pro)
        {
            var Manager = database.Users.SingleOrDefault(o => o.StudentNum == StuNum);

            if (Pro == null || Pro.Condition == ProjectCondition.Finished)
            {
                return(ProgressResult.Error("修改项目时失败!项目不存在或已结项."));
            }
            if (Manager == null || Manager.Status != AppUserStatus.Enabled)
            {
                return(ProgressResult.Error("待删除的用户身份非法."));
            }

            Pro.Managers.Remove(Manager);
            Save();
            return(ProgressResult.Success());
        }
        public ProgressResult ProjectMessageInput(string Name, string Detail, string Place, int Max, DateTime Time, List <AppUser> Managers, Project Pro)
        {
            ProgressResult result;

            if (Name == "" || Detail == "" || Place == "" || Time == null || Managers == null)
            {
                ProgressResult.Error("信息不完整,无法输入信息");
            }
            Pro.Place    = Place;
            Pro.Maximum  = Max;
            Pro.Time     = Time;
            Pro.Managers = Managers;
            Pro.Details  = Detail;
            Save();
            result = ProgressResult.Success();
            return(result);
        }
        //      public List<AppUser> FindManagerListByStudentNum(params string[] StuNums)  //将原本是string类型的参数改为 int
        public List <AppUser> FindManagerListByStudentNum(params int[] StuNums)
        {
            List <AppUser> Managers = new List <AppUser>();

            foreach (int StuNum in StuNums)
            {
                var Manager = database.Users.SingleOrDefault(o => o.StudentNum == StuNum);
                if (Manager == null)
                {
                    ProgressResult.Error("用户不存在");
                }
                else
                {
                    Managers.Add(Manager);
                }
            }
            return(Managers);
        }
        public ProgressResult FinishProject(Project Pro)
        {
            ProgressResult result;

            if (Pro.Condition == ProjectCondition.Finished || Pro.ScoreCondition == ProjectScoreCondition.UnScored)
            {
                return(ProgressResult.Error("项目不满足结项条件,请检查项目状态和评分"));
            }
            else
            {
                lock (database)
                {
                    Pro.Condition = ProjectCondition.Finished;
                    Save();
                }
            }
            result = ProgressResult.Success();
            return(result);
        }
 public ProgressResult DeleteCreditRecord(CreditRecord crecord)//重灾区
 {
     if (crecord == null)
     {
         return(ProgressResult.Error("不存在对应的征信记录."));
     }
     else
     {
         try
         {
             database.CreditRecords.Remove(crecord);
             Save();
             return(ProgressResult.Success());
         }
         catch (Exception)
         {
             throw;
         }
     }
 }
        public ProgressResult ScoringDefaultForVolunteers(Project Pro, CreditRecord.CreditScore score)
        {
            ProgressResult result;

            Pro = database.Projects.SingleOrDefault(p => p.Id == Pro.Id);
            if (Pro == null)
            {
                return(ProgressResult.Error("数据库中不存在该项目!"));
            }
            List <Volunteer> selectedvolunteers = Pro.Volunteers;

            /*var Volunteers = database.Volunteers.Where(o => o.Project.Count() > 0).ToList();
             * List<Volunteer> selectedvolunteers = new List<Volunteer>();
             * foreach(var item in Volunteers)
             * {
             *  if( item.Project.Contains(Pro))
             *  {
             *      selectedvolunteers.Add(item);
             *  }
             * }*/
            foreach (var item in selectedvolunteers)
            {
                if (!item.CreditRecords.Exists(o => o.Project.Id == Pro.Id))
                {
                    item.AddCredit(score);
                    CreditRecord cr = new CreditRecord();
                    cr.Participant = item;
                    cr.Project     = Pro;
                    cr.Score       = score;
                    item.CreditRecords.Add(cr);
                }
            }
            Pro.ScoreCondition = ProjectScoreCondition.Scored;
            Save();
            result = ProgressResult.Success();
            return(result);
        }
        public ProgressResult EditScore(Volunteer volunteer, Project project, CreditRecord.CreditScore score)
        {
            CreditRecord crecord = database.CreditRecords.SingleOrDefault(r => r.Participant.UID == volunteer.UID && r.Project.Id == project.Id);

            if (crecord == null)
            {
                return(ProgressResult.Error("不存在对应的征信记录."));
            }
            else
            {
                try
                {
                    volunteer.DeleteCredit(crecord.Score);
                    volunteer.AddCredit(score);
                    crecord.Score = score;
                    Save();
                    return(ProgressResult.Success());
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }