private TreeNode AddDepartmentNode(Department dept, out int userCount)
        {
            int count = 0;
            List<Staff> users = null;
            if (LoadUser)
            {
                users = _Users.Where(item => item.DepartmentID == dept.ID).ToList();
            }

            TreeNode parent = new TreeNode();
            parent.Text = string.Format("[{0}] {1}", dept.ID, dept.Name);
            parent.Tag = dept;

            List<Department> children = _Depts.Where(item => item.ParentID == dept.ID).ToList();
            if (children != null && children.Count > 0)
            {
                foreach (Department child in children)
                {
                    if (OnlyShowCurrentOperatorDepts && !Operator.CurrentOperator.IsAdmin && !Operator.CurrentOperator.Depts.Exists(item => item == child.ID))
                    {
                    }
                    else
                    {
                        int uc = 0;
                        TreeNode dnode = AddDepartmentNode(child, out uc);
                        dnode.ImageIndex = 0;
                        dnode.SelectedImageIndex = 0;
                        parent.Nodes.Add(dnode);
                        _AllDeptNodes.Add(dnode);
                        count += uc;
                    }
                }
            }
            if (LoadUser)
            {
                foreach (Staff user in users)
                {
                    if (user.Resigned && !ShowResigedStaff)
                    {
                    }
                    else
                    {
                        TreeNode unode = new TreeNode(user.Name);
                        unode.Tag = user;
                        unode.ImageIndex = 1;
                        unode.SelectedImageIndex = 1;
                        parent.Nodes.Add(unode);
                        _AllUserNodes.Add(unode);
                        count++;
                    }
                }
            }
            if (LoadUser) parent.Text = string.Format("[{0}] {1} ({2}人)", dept.ID, dept.Name, count);
            userCount = count;
            return parent;
        }
 private void mnu_Add_Click(object sender, EventArgs e)
 {
     _UpdatingItem = null;
     Department parent = null;
     if (departmentTreeview1.SelectedNode != null) parent = departmentTreeview1.SelectedNode.Tag as Department;
     txtDeptID.Text = (new DepartmentBLL(AppSettings.CurrentSetting.ConnectUri)).CreateADeptID(parent);
     txtDeptName.Text = string.Empty;
     txtDeptName.Focus();
     txtMemo.Text = string.Empty;
     txtParentID.Text = parent != null ? parent.ID : string.Empty;
 }
 public CommandResult Update(Department info)
 {
     Department original = ProviderFactory.Create<IDepartmentProvider>(_RepoUri).GetByID(info.ID).QueryObject;
     if (original != null)
     {
         return ProviderFactory.Create<IDepartmentProvider>(_RepoUri).Update(info, original);
     }
     else
     {
         return new CommandResult(ResultCode.NoRecord, ResultCodeDecription.GetDescription(ResultCode.NoRecord));
     }
 }
 private void FillRow(Staff staff, List<AttendanceResult> ars, Department dept, Worksheet sheet)
 {
     int row = _CurrentRow;
     Range r = sheet.get_Range(GetColName(1) + row, Type.Missing); //卡号
     r.Value = staff.CardID;
     r = sheet.get_Range(GetColName(2) + row, Type.Missing); //人员编号
     r.Value = staff.Certificate;
     r = sheet.get_Range(GetColName(3) + row, Type.Missing);//姓名
     r.Value = staff.Name;
     r = sheet.get_Range(GetColName(4) + row, Type.Missing);//电邮
     //r.Value = staff.Email;
     r = sheet.get_Range(GetColName(5) + row, Type.Missing);//所在单位
     r.Value = dept != null ? dept.Name : string.Empty;
     foreach (AttendanceResult ar in ars)
     {
         r = sheet.get_Range(GetColName(6) + row, Type.Missing);//日期
         r.Value = ar.ShiftDate;
         r = sheet.get_Range(GetColName(7) + row, Type.Missing);//应上班
         r.Value = ar.StartTime.ToString("HH:mm");
         r = sheet.get_Range(GetColName(8) + row, Type.Missing);//实上班
         r.Value = ar.OnDutyTime != null ? ar.OnDutyTime.Value.ToString("HH:mm") : "--";
         if (ar.OnDutyTime == null || ar.Result == AttendanceResultCode.Late || ar.Result == AttendanceResultCode.LateEarly)
         {
             r.Font.Color = System.Drawing.Color.Red;
             r.Font.Bold = 1;
             sheet.get_Range(GetColName(6) + row, GetColName(13) + row).Interior.Color = System.Drawing.Color.Yellow;
         }
         r = sheet.get_Range(GetColName(9) + row, Type.Missing);//应下班
         r.Value = ar.EndTime.ToString("HH:mm");
         r = sheet.get_Range(GetColName(10) + row, Type.Missing);//实下班
         r.Value = ar.OffDutyTime != null ? ar.OffDutyTime.Value.ToString("HH:mm") : "--";
         if (ar.OffDutyTime == null || ar.Result == AttendanceResultCode.LeaveEarly || ar.Result == AttendanceResultCode.LateEarly)
         {
             r.Font.Color = System.Drawing.Color.Red;
             r.Font.Bold = 1;
             sheet.get_Range(GetColName(6) + row, GetColName(13) + row).Interior.Color = System.Drawing.Color.Yellow;
         }
         r = sheet.get_Range(GetColName(11) + row, Type.Missing);//考勤结果
         r.Value = ar.ResultDescr;
         r = sheet.get_Range(GetColName(12) + row, Type.Missing);//应出勤(h)
         r.Value = (ar.ShiftTime / 60).Trim();
         r = sheet.get_Range(GetColName(13) + row, Type.Missing);//实出勤(h)
         r.Value = (ar.Present / 60).Trim();
         row++;
     }
     sheet.get_Range(GetColName(1) + _CurrentRow, GetColName(1) + (row - 1)).Merge(Type.Missing);
     sheet.get_Range(GetColName(2) + _CurrentRow, GetColName(2) + (row - 1)).Merge(Type.Missing);
     sheet.get_Range(GetColName(3) + _CurrentRow, GetColName(3) + (row - 1)).Merge(Type.Missing);
     sheet.get_Range(GetColName(4) + _CurrentRow, GetColName(4) + (row - 1)).Merge(Type.Missing);
     sheet.get_Range(GetColName(5) + _CurrentRow, GetColName(5) + (row - 1)).Merge(Type.Missing);
     _CurrentRow = row;
 }
 private void departmentTreeview1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
 {
     departmentTreeview1.SelectedNode = e.Node;
     if (e.Button == MouseButtons.Left)
     {
         Department dept = e.Node.Tag as Department;
         if (dept != null)
         {
             txtDeptID.Text = dept.ID;
             txtDeptID.Enabled = false;
             txtDeptName.Text = dept.Name;
             txtDeptName.Focus();
             txtMemo.Text = dept.Memo;
             txtParentID.Text = dept.ParentID;
             _UpdatingItem = dept;
         }
     }
 }
 public CommandResult Delete(Department info)
 {
     List<Department> depts = ProviderFactory.Create<IDepartmentProvider>(_RepoUri).GetItems(null).QueryObjects;
     IUnitWork unitWork = ProviderFactory.Create<IUnitWork>(_RepoUri);
     foreach (Department dept in depts.Where(item => item.ID.IndexOf(info.ID, 0) == 0))
     {
         ProviderFactory.Create<IDepartmentProvider>(_RepoUri).Delete(dept, unitWork);
     }
     List<Staff> staffs = ProviderFactory.Create<IStaffProvider>(_RepoUri).GetItems(null).QueryObjects;
     foreach (Staff staff in staffs)
     {
         if (staff.DepartmentID == info.ID)
         {
             Staff staff1 = staff.Clone();
             staff.DepartmentID = null;
             ProviderFactory.Create<IStaffProvider>(_RepoUri).Update(staff, staff1, unitWork);
         }
     }
     return unitWork.Commit();
 }
 public string CreateADeptID(Department parent)
 {
     List<Department> depts = ProviderFactory.Create<IDepartmentProvider>(_RepoUri).GetItems(null).QueryObjects;
     List<Department> children = null;
     if (parent == null)
     {
         children = depts.Where(item => string.IsNullOrEmpty(item.ParentID)).ToList();
     }
     else
     {
         children = depts.Where(item => item.ParentID == parent.ID).ToList();
     }
     string parentID = parent == null ? string.Empty : parent.ID;
     for (int i = 1; i < 100; i++)
     {
         string childID = parentID + i.ToString("D2");
         if (!children.Exists(item => item.ID == childID))
         {
             return childID;
         }
     }
     return string.Empty;
 }
 private Department GetRoot(List<Department> depts, Department dept)
 {
     Department d = dept;
     while (!string.IsNullOrEmpty(d.ParentID))
     {
         Department temp = depts.SingleOrDefault(it => it.ID == d.ParentID);
         if (temp == null) break;
         d = temp;
     }
     return d;
 }
 public CommandResult Add(Department info)
 {
     return ProviderFactory.Create<IDepartmentProvider>(_RepoUri).Insert(info);
 }
 private void btnSave_Click(object sender, EventArgs e)
 {
     if (CheckInput() == false) return;
     Department dept = _UpdatingItem;
     if (dept == null)
     {
         dept = new Department();
     }
     dept.ID = txtDeptID.Text;
     dept.Name = txtDeptName.Text;
     dept.Memo = txtMemo.Text;
     dept.ParentID = txtParentID.Text;
     if (_UpdatingItem == null)
     {
         CommandResult ret = (new DepartmentBLL(AppSettings.CurrentSetting.ConnectUri)).Add(dept);
         if (ret.Result == ResultCode.Successful)
         {
             TreeNode node = new TreeNode(string.Format("[{0}] {1}", dept.ID, dept.Name));
             node.Tag = dept;
             if (departmentTreeview1.SelectedNode == null)
             {
                 departmentTreeview1.Nodes[0].Nodes.Add(node);
                 departmentTreeview1.Nodes[0].Expand();
             }
             else
             {
                 departmentTreeview1.SelectedNode.Nodes.Add(node);
                 departmentTreeview1.SelectedNode.Expand();
             }
             if (!Operator.CurrentOperator.IsAdmin)
             {
                 Operator.CurrentOperator.Depts.Add(dept.ID);
                 (new OperatorBll(AppSettings.CurrentSetting.ConnectUri)).Update(Operator.CurrentOperator);
             }
             _UpdatingItem = dept;
             this.departmentTreeview1.SelectedNode = node;
         }
         else
         {
             MessageBox.Show(ret.Message);
         }
     }
     else
     {
         CommandResult ret = (new DepartmentBLL(AppSettings.CurrentSetting.ConnectUri)).Update(dept);
         if (ret.Result == ResultCode.Successful)
         {
             this.departmentTreeview1.SelectedNode.Text = string.Format("[{0}] {1}", dept.ID, dept.Name);
             this.departmentTreeview1.SelectedNode.Tag = dept;
         }
         else
         {
             MessageBox.Show(ret.Message);
         }
     }
 }