public void Insert(Model.Flextime flextime, IList <Model.Employee> emplist) { bool flag = false; try { BL.V.BeginTransaction(); foreach (Model.Employee emp in emplist) { Model.Flextime ft = new Book.Model.Flextime(); if (flag) { ft.FlextimeId = Guid.NewGuid().ToString(); } else { ft.FlextimeId = flextime.FlextimeId; } ft.BusinessHours = flextime.BusinessHours; ft.BusinessHoursId = flextime.BusinessHoursId; ft.EmployeeId = emp.EmployeeId; ft.FlexDate = flextime.FlexDate; accessor.Insert(ft); if (DateTime.Now.Date > flextime.FlexDate.Value.Date) { hrDatilyManager.ReCheck(flextime.FlexDate.Value.Date, flextime.Employee); } flag = true; } BL.V.CommitTransaction(); } catch (Exception ex) { BL.V.RollbackTransaction(); throw ex; } }
//按区段排班 private void simpleButton1_Click(object sender, EventArgs e) { //实例化model if (fl == null) { fl = new Book.Model.Flextime(); } //获取选择员工 IList <Model.Employee> mEmps = (from Model.Employee emp in (this.EmployeeSource.DataSource as IList <Model.Employee>) where emp.IsChecked == true select emp).ToList <Model.Employee>(); //非空验证 if (this.lookUpEdit1.Text == "" && (mEmps == null || mEmps.Count == 0)) { MessageBox.Show("請選擇員工!"); return; } string empids = string.Empty; if (mEmps != null && mEmps.Count != 0) { StringBuilder sb1 = new StringBuilder(); foreach (Model.Employee emp in mEmps) { sb1.Append("'" + emp.EmployeeId + "',"); } empids = sb1.ToString().Substring(0, sb1.ToString().Length - 1); //empids = (from Model.Employee emp in mEmps // select emp.EmployeeId).Aggregate((s1, s2) => string.Format("'{0}','{1}'", s1, s2)); } else { Model.Employee aemp = (from Model.Employee em in (this.EmployeeSource.DataSource as IList <Model.Employee>) where em.EmployeeId == this.lookUpEdit1.EditValue.ToString() select em).First <Model.Employee>(); mEmps.Add(aemp); empids = "'" + this.lookUpEdit1.EditValue.ToString() + "'"; } if (this.cmb_business.Text == "") { MessageBox.Show("請選擇班別!"); return; } if (this.radioGroup1.SelectedIndex == 0 && (this.comBox_Day.Properties.Items.GetCheckedValues() == null || this.comBox_Day.Properties.Items.GetCheckedValues().Count == 0)) { MessageBox.Show("請選擇排班日期!"); return; } else if (this.radioGroup1.SelectedIndex == 1 && (this.date_Start.EditValue == null || this.date_End.EditValue == null)) { MessageBox.Show("請完善排班區間"); return; } //获取日期列表 string flexdates = string.Empty; if (this.radioGroup1.SelectedIndex == 0) { string mYearAndMonth = this.comBox_Year.SelectedItem.ToString() + "/" + this.comBox_Month.SelectedItem.ToString() + "/"; IList <string> daylist = (from string s in this.comBox_Day.Properties.Items.GetCheckedValues() select s).ToList <string>(); StringBuilder sb = new StringBuilder(); foreach (string str in daylist) { sb.Append("'" + DateTime.Parse((mYearAndMonth + str)).ToString("yyyy-MM-dd") + "',"); } flexdates = sb.ToString().Substring(0, sb.ToString().Length - 1); } else { for (DateTime date = this.date_Start.DateTime; date <= this.date_End.DateTime; date = date.AddDays(1)) { flexdates += "'" + date.ToString("yyyy-MM-dd") + "',"; } flexdates = flexdates.Substring(0, flexdates.Length - 1); } //已存在记录列表 IList <Model.Flextime> existFlex = this.flextimemanager.selectByEmpidsAndDates(empids, flexdates); fl.BusinessHoursId = cmb_business.EditValue.ToString(); #region 连续排班 //DateTime a = this.dateEdit_start.DateTime.Date; //DateTime b = this.dateEdit_end.DateTime.Date; //while (a <= b) //{ // fl.FlextimeId = Guid.NewGuid().ToString(); // //查詢此員工是否已經排班 // if (this.flextimemanager.getbyempiddate(fl.EmployeeId, a) != null) // { // if (Isconvert == false && Notconvert == false) // { // if (MessageBox.Show(Properties.Resources.ExistObjectIsConvert, this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) // { // Isconvert = true; //設置為覆蓋重複項 // } // else // { // Notconvert = true; //設置為不覆蓋 // } // } // if (Isconvert) // { // this.flextimemanager.DeleteByEmpidDate(fl.EmployeeId, a); // fl.FlexDate = a; // flextimemanager.Insert(fl); // } // a = a.AddDays(1); // } // else // { // fl.FlexDate = a; // flextimemanager.Insert(fl); // a = a.AddDays(1); // } //} #endregion //挑选排班 DateTime mFlexDate; try { BL.V.BeginTransaction(); foreach (Model.Employee emp in mEmps) { if (this.radioGroup1.SelectedIndex == 0) { foreach (string s in this.comBox_Day.Properties.Items.GetCheckedValues()) { fl.Employee = emp; fl.EmployeeId = emp.EmployeeId; fl.FlextimeId = Guid.NewGuid().ToString(); mFlexDate = DateTime.Parse(this.comBox_Year.SelectedItem.ToString() + "/" + this.comBox_Month.SelectedItem.ToString() + "/" + s).Date; //查詢此員工是否已經排班 bool hasFlex = existFlex.Any(le => le.EmployeeId == emp.EmployeeId && le.FlexDate.Value.ToString("yyyy-MM-dd") == mFlexDate.ToString("yyyy-MM-dd")); if (hasFlex) { Isconvert = false; DialogResult d = MessageBox.Show("員工:" + emp.EmployeeName + "," + mFlexDate.ToString("yyyy-MM-dd") + "排班記錄" + Properties.Resources.ExistObjectIsConvert, this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information); switch (d) { case DialogResult.Yes: Isconvert = true; //設置為覆蓋重複項 break; case DialogResult.No: //設置為覆蓋重複項 continue; default: throw new global::Helper.MessageValueException(""); } if (Isconvert) { this.flextimemanager.DeleteByEmpidDate(fl.EmployeeId, mFlexDate); fl.FlexDate = mFlexDate; flextimemanager.Insert(fl); } } else { fl.FlexDate = mFlexDate; flextimemanager.Insert(fl); } } } else { foreach (string s in flexdates.Split(',')) { fl.Employee = emp; fl.EmployeeId = emp.EmployeeId; fl.FlextimeId = Guid.NewGuid().ToString(); mFlexDate = Convert.ToDateTime(s.Replace('\'', ' ')); bool hasFlex = existFlex.Any(le => le.EmployeeId == emp.EmployeeId && le.FlexDate.Value.ToString("yyyy-MM-dd") == mFlexDate.ToString("yyyy-MM-dd")); if (hasFlex) { Isconvert = false; DialogResult d = MessageBox.Show("員工:" + emp.EmployeeName + "," + mFlexDate.ToString("yyyy-MM-dd") + "排班記錄" + Properties.Resources.ExistObjectIsConvert, this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information); switch (d) { case DialogResult.Yes: Isconvert = true; //設置為覆蓋重複項 break; case DialogResult.No: //設置為覆蓋重複項 continue; default: throw new global::Helper.MessageValueException(""); } if (Isconvert) { this.flextimemanager.DeleteByEmpidDate(fl.EmployeeId, mFlexDate); fl.FlexDate = mFlexDate; flextimemanager.Insert(fl); } } else { fl.FlexDate = mFlexDate; flextimemanager.Insert(fl); } } } } BL.V.CommitTransaction(); MessageBox.Show(Properties.Resources.SaveSuccess, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } catch (global::Helper.MessageValueException) { BL.V.RollbackTransaction(); return; } catch { BL.V.RollbackTransaction(); throw; } //弹出操作结果提示 //MessageBox.Show(Properties.Resources.Addsuccess); for (int i = 0; i < this.comBox_Day.Properties.Items.Count; i++) { this.comBox_Day.Properties.Items[i].CheckState = CheckState.Unchecked; } this.FlexTimeSource.DataSource = flextimemanager.getByempid(fl.EmployeeId); }