/// <summary>
 /// 取得新的參與學生清單
 /// </summary>
 public List<SCJoin> GetNewSCJoinList()
 {
     List<SCJoin> list = new List<SCJoin>();
     if (_new_Club != null && _scj.Count != 0)
     {
         foreach (SCJoin each in _scj)
         {
             SCJoin scj = new SCJoin();
             scj.RefStudentID = each.RefStudentID;
             scj.RefClubID = _new_Club.UID;
             scj.Lock = each.Lock; //鎖定
             scj.Phase = each.Phase;
             scj.Group = each.Group;
             list.Add(scj);
         }
     }
     return list;
 }
        /// <summary>
        /// 排序 參與學生 清單
        /// </summary>
        private int SortSCJ(SCJoin scj1, SCJoin scj2)
        {
            StudentRecord sr1 = studentDic[scj1.RefStudentID];
            StudentRecord sr2 = studentDic[scj2.RefStudentID];

            string stringStudent1 = (string.IsNullOrEmpty(sr1.RefClassID) ? "" : sr1.Class.Name).PadLeft(10);
            string stringStudent2 = (string.IsNullOrEmpty(sr2.RefClassID) ? "" : sr2.Class.Name).PadLeft(10);

            stringStudent1 += (sr1.SeatNo.HasValue ? sr1.SeatNo.Value.ToString() : "").PadLeft(10);
            stringStudent2 += (sr2.SeatNo.HasValue ? sr2.SeatNo.Value.ToString() : "").PadLeft(10);

            stringStudent1 += sr1.Name;
            stringStudent2 += sr2.Name;

            return stringStudent1.CompareTo(stringStudent2);
        }
        /// <summary>
        /// 將傳入的學生ID,加入此課程
        /// </summary>
        private void AddListViewInTemp(List<string> IsSaft, int phase)
        {
            if (IsSaft.Count != 0)
            {
                cso = new ClubStudObj();

                //Log
                StringBuilder sb_Message = new StringBuilder();

                #region 可加入的學生清單

                //排除已存在本課程的學生
                cso.CheckTempStudentInCourse(IsSaft, scMAG.SCJoin_Dic, _CLUBRecord, phase);

                StringBuilder sb_Log = new StringBuilder();
                sb_Log.AppendLine(string.Format("加入「{0}」名课程参与学生:(学年度「{1}」学期「{2}」课程「{3}」阶段「{4}」)", cso.InsertList.Count.ToString(), _CLUBRecord.SchoolYear.ToString(), _CLUBRecord.Semester.ToString(), _CLUBRecord.ClubName, phase));

                if (cso.ReMoveTemp.Count != 0)
                {
                    #region 是否有重覆加入本社學生 - 錯誤

                    sb_Message.AppendLine("共有" + cso.ReMoveTemp.Count + "名学生,存在本课程!!");
                    List<StudentRecord> studlist = Student.SelectByIDs(cso.ReMoveTemp);
                    studlist = SortClassIndex.K12Data_StudentRecord(studlist);
                    foreach (StudentRecord stud in studlist)
                    {
                        string class_981 = string.IsNullOrEmpty(stud.RefClassID) ? "" : stud.Class.Name;
                        string SeatNo_981 = stud.SeatNo.HasValue ? stud.SeatNo.Value.ToString() : "";
                        sb_Message.AppendLine("班级「" + class_981 + "」学号「" + SeatNo_981 + "」姓名「" + stud.Name + "」");
                    }

                    #endregion

                    MsgBox.Show(sb_Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (cso.ReDoubleTemp.Count != 0)
                {
                    #region 重覆參與其它社團記錄 -警告

                    sb_Message.AppendLine("共有" + cso.ReDoubleTemp.Count + "笔,重复参与其它课程记录");


                    //取得重覆社團
                    List<CLUBRecord> cludlist = tool._A.Select<CLUBRecord>("uid in ('" + string.Join("','", cso.GetClubID()) + "')");
                    Dictionary<string, CLUBRecord> clubDic = new Dictionary<string, CLUBRecord>();
                    foreach (CLUBRecord each in cludlist)
                    {
                        if (!clubDic.ContainsKey(each.UID))
                            clubDic.Add(each.UID, each);

                    }

                    studentDic.Clear();
                    foreach (StudentRecord each in Student.SelectByIDs(cso.GetStudentID()))
                    {
                        if (!studentDic.ContainsKey(each.ID))
                            studentDic.Add(each.ID, each);

                    }
                    cso.ReDoubleTemp.Sort(SortSCJ);
                    foreach (SCJoin SCJ in cso.ReDoubleTemp)
                    {
                        StudentRecord stud = studentDic[SCJ.RefStudentID];
                        CLUBRecord cr = clubDic[SCJ.RefClubID];

                        string class_981 = string.IsNullOrEmpty(stud.RefClassID) ? "" : stud.Class.Name;
                        string SeatNo_981 = stud.SeatNo.HasValue ? stud.SeatNo.Value.ToString() : "";
                        sb_Message.AppendLine("班级「" + class_981 + "」学号「" + SeatNo_981 + "」姓名「" + stud.Name + "」课程「" + cr.ClubName + "」阶段「" + SCJ.Phase + "」");
                    }

                    #endregion

                    DialogResult dr = MsgBox.Show(sb_Message.ToString() + "\n您是否要继续进行此作业?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (dr != DialogResult.Yes)
                    {
                        MsgBox.Show("作业已中止!!");
                        return;
                    }
                }

                if (cso.InsertList.Count != 0)
                {
                    #region InsertList

                    //取得Log學生物件
                    cso.GetLogStudent();

                    List<SCJoin> SCJoinlist = new List<SCJoin>();
                    foreach (string each in cso.InsertList)
                    {
                        SCJoin JHs = new SCJoin();
                        JHs.RefStudentID = each; //修課學生
                        JHs.RefClubID = this.PrimaryKey;
                        JHs.Phase = 1;
                        SCJoinlist.Add(JHs);

                        //加入修課LOG
                        if (!string.IsNullOrEmpty(GetLogMessage(each)))
                            sb_Log.AppendLine(GetLogMessage(each));

                    }

                    try
                    {
                        tool._A.InsertValues(SCJoinlist);
                    }
                    catch (Exception ex)
                    {
                        MsgBox.Show("新增资料失败\n" + ex.Message);
                        SmartSchool.ErrorReporting.ReportingService.ReportException(ex);
                        return;
                    }

                    //移出待處理
                    StringBuilder sbHelp = new StringBuilder();
                    sbHelp.AppendLine("已由待处理加入\n共「" + cso.InsertList.Count.ToString() + "」名学生\n");
                    MsgBox.Show(sbHelp.ToString());

                    FISCA.LogAgent.ApplicationLog.Log("课程", "加入", sb_Log.ToString());
                    K12.Presentation.NLDPanels.Student.RemoveFromTemp(cso.InsertList);

                    ClubEvents.RaiseAssnChanged();

                    #endregion
                }

                #endregion
            }
            else
            {
                string message = "请检查\n1.待处理无学生\n2.学生状态有误(非一般生)";
                FISCA.Presentation.Controls.MsgBox.Show(message);
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            //將指定好的學生
            //建立社團參與記錄
            //並加入該社團內

            btnSave.Enabled = false;

            SCJoinList.Clear();
            foreach (DataGridViewRow row in dataGridViewX1.Rows)
            {
                if (row.Cells[Column指定1].Tag != null)
                {
                    StudRecord sr = (StudRecord)row.Tag;
                    CLUBRecord cr = (CLUBRecord)row.Cells[Column指定1].Tag;
                    SCJoin sc = new SCJoin();
                    sc.RefClubID = cr.UID;
                    sc.RefStudentID = sr.id;
                    sc.Phase = 1;
                    SCJoinList.Add(sc);
                }

                if (row.Cells[Column指定2].Tag != null)
                {
                    StudRecord sr = (StudRecord)row.Tag;
                    CLUBRecord cr = (CLUBRecord)row.Cells[Column指定2].Tag;
                    SCJoin sc = new SCJoin();
                    sc.RefClubID = cr.UID;
                    sc.RefStudentID = sr.id;
                    sc.Phase = 2;
                    SCJoinList.Add(sc);
                }

                if (row.Cells[Column指定3].Tag != null)
                {
                    StudRecord sr = (StudRecord)row.Tag;
                    CLUBRecord cr = (CLUBRecord)row.Cells[Column指定3].Tag;
                    SCJoin sc = new SCJoin();
                    sc.RefClubID = cr.UID;
                    sc.RefStudentID = sr.id;
                    sc.Phase = 3;
                    SCJoinList.Add(sc);
                }
            }

            BGWSave.RunWorkerAsync(SCJoinList);
        }
        /// <summary>
        /// 排除已存在於本社團之學生
        /// </summary>
        public void CheckTempStudentInCourse(List<string> IsSaft, Dictionary<string, List<SCJoin>> SCJoin_Dic, CLUBRecord _CLUBRecord, int phase)
        {
            //檢查已經加入本階段修課的學生
            foreach (string each in IsSaft)
            {
                if (!SCJoin_Dic.ContainsKey(each))
                {
                    //可加入社團之學生
                    InsertList.Add(each);
                }
                else
                {
                    SCJoin scjK = new SCJoin();
                    foreach (SCJoin scj in SCJoin_Dic[each])
                    {
                        if (string.IsNullOrEmpty(scjK.UID))
                        {
                            scjK = scj;
                            continue;
                        }

                        //如果是相同階段別之學生
                        if (scjK.Phase == scj.Phase)
                        {
                            //重覆加入社團之學生
                            ReMoveTemp.Add(each);
                        }
                    }

                }
            }
            //取得可加入學生的社團參與記錄(所有學期)
            List<SCJoin> scjList = tool._A.Select<SCJoin>("ref_student_id in ('" + string.Join("','", InsertList) + "')");

            if (scjList.Count != 0)
            {
                Dictionary<string, CLUBRecord> clubDic = GetDistinctClub(scjList);

                foreach (SCJoin each in scjList)
                {    //增加判斷已不存在的社團
                    if (clubDic.ContainsKey(each.RefClubID))
                    {
                        //學年度學期相同
                        if (clubDic[each.RefClubID].SchoolYear == _CLUBRecord.SchoolYear && clubDic[each.RefClubID].Semester == _CLUBRecord.Semester)
                        {
                            //同階段已加入
                            if (each.Phase == phase)
                                ReDoubleTemp.Add(each);
                            //已加入長課程,或者已加入其他課程但是要加入長課程
                            else if (clubDic[each.RefClubID].FullPhase == true || _CLUBRecord.FullPhase == true)

                                ReDoubleTemp.Add(each);

                            ////判斷待處理學生,是否已經有社團參與記錄(SCJoin)
                            //if (IsSaft.Contains(each.RefStudentID))
                            //{
                            //    //有記錄則先剔除
                            //    IsSaft.Remove(each.RefStudentID);
                            //}
                        }
                    }
                }
            }
        }