/// <summary>
    /// 载入已有的工作项
    /// </summary>
    private void BindData()
    {
        JN_WorkingPlanBLL bll = new JN_WorkingPlanBLL((int)ViewState["PlanID"]);

        ViewState["WorkingPlanData"] = LoadWorkingPlanDetail(bll.Model.ID);
        ViewState["BeginDate"]       = bll.Model.BeginDate;
        ViewState["EndDate"]         = bll.Model.EndDate;
        ViewState["PlanStaff"]       = bll.Model.Staff;

        if ((int)Session["UserID"] != (int)ViewState["PlanStaff"])
        {
            cb_DisplayCheckedOnly.Visible = false;
            cb_DisplayCheckedOnly.Checked = true;
            bt_AddJournal.Visible         = false;
            bt_Save.Visible = false;
        }
        else if (bll.Model.EndDate < DateTime.Today)
        {
            cb_DisplayCheckedOnly.Checked = true;
        }


        pl_detail.BindData(bll.Model);

        BindGrid();
        UploadFile1.RelateID = bll.Model.ID;
        UploadFile1.DataBind();
    }
    protected void bt_Submit_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["ID"] > 0)
        {
            JN_WorkingPlanBLL bll = new JN_WorkingPlanBLL((int)ViewState["ID"]);

            #region 发起工作流
            NameValueCollection dataobjects = new NameValueCollection();
            dataobjects.Add("ID", ViewState["ID"].ToString());
            dataobjects.Add("Position", new Org_StaffBLL(bll.Model.Staff).Model.Position.ToString());
            dataobjects.Add("OrganizeCity", bll.Model.OrganizeCity.ToString());
            dataobjects.Add("TotalCost", bll.Model["TotalCost"]);

            string title = select_PlanStaff.SelectText + " 开始日期:" + tbx_begindate.Text + "至" + tbx_enddate.Text + bll.Model.Title;

            int TaskID = EWF_TaskBLL.NewTask("JN_WorkingPlan_Apply", (int)Session["UserID"], title, "~/SubModule/OA/Journal/WorkingPlan_CalendarList.aspx?ID=" + ViewState["ID"].ToString(), dataobjects);
            new EWF_TaskBLL(TaskID).Start();        //直接启动流程
            #endregion

            if (TaskID > 0)
            {
                bll.Submit((int)Session["UserID"], TaskID);
            }

            Response.Redirect("~/SubModule/EWF/TaskDetail.aspx?TaskID=" + TaskID.ToString());
        }
    }
    /// <summary>
    /// 载入已有的工作项
    /// </summary>
    private void BindData()
    {
        cb_DisplayCheckedOnly.Checked = true;

        JN_WorkingPlanBLL bll = new JN_WorkingPlanBLL((int)ViewState["ID"]);

        tr_OrganizeCity.SelectValue  = bll.Model.OrganizeCity.ToString();
        tbx_begindate.Text           = bll.Model.BeginDate.ToString("yyyy-MM-dd");
        tbx_enddate.Text             = bll.Model.EndDate.ToString("yyyy-MM-dd");
        select_PlanStaff.SelectValue = bll.Model.Staff.ToString();
        select_PlanStaff.SelectText  = new Org_StaffBLL(bll.Model.Staff).Model.RealName;

        pl_detail.BindData(bll.Model);
        InitGridView(bll.Model.BeginDate, bll.Model.EndDate);

        ViewState["WorkingPlanData"] = LoadWorkingPlanDetail((int)ViewState["ID"]);
        ViewState["BeginDate"]       = bll.Model.BeginDate;
        ViewState["EndDate"]         = bll.Model.EndDate;

        BindGrid();

        tbx_begindate.Enabled          = false;
        tbx_enddate.Enabled            = false;
        select_PlanStaff.Enabled       = false;
        tr_OrganizeCity.Enabled        = false;
        cbx_GenarateSynergetic.Visible = false;
        bt_Create.Visible = false;

        tbx_Begin.Text = tbx_begindate.Text;
        tbx_End.Text   = tbx_begindate.Text;

        if (bll.Model.BeginDate < DateTime.Today)
        {
            bt_Delete.Visible = false;
        }

        if (bll.Model.State == 2 || bll.Model.State == 3 || bll.Model.ApproveFlag == 1 || bll.Model.InsertStaff != (int)Session["UserID"])
        {
            //提交待审批、已审批
            tr_adddetail.Visible = false;
            pl_detail.SetControlsEnable(false);
            bt_Save.Visible   = false;
            bt_Delete.Visible = false;
            bt_Submit.Visible = false;

            UploadFile1.CanUpload = false;
            UploadFile1.CanDelete = false;

            gv_List.Columns[0].Visible = false;
            gv_List.SetControlsEnable(false);
        }

        UploadFile1.RelateID = (int)ViewState["ID"];
        UploadFile1.BindGrid();
    }
    protected void bt_Delete_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["ID"] > 0)
        {
            JN_WorkingPlanBLL bll = new JN_WorkingPlanBLL((int)ViewState["ID"]);
            bll.DeleteDetail();
            bll.Delete();

            Response.Redirect("WorkingPlan_List.aspx");
        }
    }
示例#5
0
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (ViewState["Staff"] != null && (int)ViewState["Staff"] > 0)
        {
            e.Cell.HorizontalAlign = HorizontalAlign.Left;

            if (e.Day.Date <= DateTime.Today && MCSTabControl1.SelectedIndex == 0)
            {
                ImageButton ibt = new ImageButton();
                ibt.ImageUrl      = "~/Images/gif/gif-0489.gif";
                ibt.BorderWidth   = new Unit(0);
                ibt.ToolTip       = "新增工作日志";
                ibt.OnClientClick = "javascript:NewJournal('" + e.Day.Date.DayOfYear.ToString() + "')";
                ibt.Click        += new ImageClickEventHandler(ibt_Click);
                e.Cell.Controls.Add(ibt);
            }

            #region 获取该员工当天工作日志
            if (cbx_Journal.Checked)
            {
                IList <JN_Journal> journallists = JN_JournalBLL.GetModelList("'" + e.Day.Date.ToString("yyyy-MM-dd") + "' BETWEEN Convert(varchar(10),BeginTime,120) And EndTime AND Staff=" + ViewState["Staff"].ToString());
                if (journallists.Count > 0)
                {
                    BulletedList bt = new BulletedList();
                    foreach (JN_Journal j in journallists)
                    {
                        ListItem item = new ListItem();
                        item.Text = "";
                        if (j.JournalType == 1)
                        {
                            //日报
                            if (j.BeginTime.Hour != 0 && j.EndTime.Hour != 0)
                            {
                                item.Text += j.BeginTime.ToString("HH:mm") + "~" + j.EndTime.ToString("HH:mm") + " ";
                            }
                            if (j.WorkingClassify != 0)
                            {
                                item.Text += DictionaryBLL.GetDicCollections("OA_WorkingClassify")[j.WorkingClassify.ToString()].Name + " ";
                                switch (j.WorkingClassify)
                                {
                                case 1:    //门店拜访
                                    if (j.RelateClient != 0)
                                    {
                                        //门店拜访
                                        CM_Client client = new CM_ClientBLL(j.RelateClient).Model;
                                        if (client != null)
                                        {
                                            item.Text += client.ShortName + " ";
                                        }

                                        if (j["VisitClientPurpose"] != "" && j["VisitClientPurpose"] != "0")
                                        {
                                            item.Text += DictionaryBLL.GetDicCollections("OA_JN_VisitClientPurpose")[j["VisitClientPurpose"]].Name + " ";
                                        }
                                    }
                                    break;

                                case 2:     //协同拜访
                                    if (j.RelateStaff != 0)
                                    {
                                        Org_Staff staff = new Org_StaffBLL(j.RelateStaff).Model;
                                        if (staff != null)
                                        {
                                            item.Text += staff.RealName;
                                        }
                                    }
                                    break;

                                default:
                                    item.Text += j.Title;
                                    break;
                                }
                            }
                            else
                            {
                                item.Text += j.Title;
                            }
                        }
                        else
                        {
                            //周报、月报
                            item.Text += DictionaryBLL.GetDicCollections("OA_JournalType")[j.JournalType.ToString()].Name + " ";
                            item.Text += j.Title;
                        }
                        item.Value = "javascript:OpenJournal(" + j.ID.ToString() + ")";
                        bt.Items.Add(item);
                    }
                    bt.DisplayMode    = BulletedListDisplayMode.HyperLink;
                    bt.BulletImageUrl = "~/Images/gif/gif-0162.gif";
                    bt.BulletStyle    = BulletStyle.CustomImage;
                    bt.CssClass       = "calaitem";
                    e.Cell.Controls.Add(bt);
                }
            }
            #endregion

            #region 获取访员工当天的工作计划
            if (cbx_Plan.Checked)
            {
                if (ViewState["PlanDetails"] == null || ((IList <JN_WorkingPlanDetail>)ViewState["PlanDetails"]).Where
                        (p => (p.BeginTime.Date <= e.Day.Date && p.EndTime.Date >= e.Day.Date)).Count() == 0)
                {
                    IList <JN_WorkingPlan> plans = JN_WorkingPlanBLL.GetModelList("'" + e.Day.Date.ToString("yyyy-MM-dd") +
                                                                                  "' BETWEEN BeginDate AND EndDate AND Staff=" + ViewState["Staff"].ToString());
                    if (plans.Count > 0)
                    {
                        IList <JN_WorkingPlanDetail> plandetails = new JN_WorkingPlanBLL(plans[0].ID).Items;
                        ViewState["PlanDetails"] = plandetails;
                    }
                    else
                    {
                        ViewState["PlanDetails"] = null;
                    }
                }

                if (ViewState["PlanDetails"] != null)
                {
                    IList <JN_WorkingPlanDetail> plandetails = (IList <JN_WorkingPlanDetail>)ViewState["PlanDetails"];

                    BulletedList bt = new BulletedList();

                    foreach (JN_WorkingPlanDetail plan in plandetails.Where(p => (p.BeginTime.Date <= e.Day.Date && p.EndTime.Date >= e.Day.Date)))
                    {
                        ListItem item = new ListItem();
                        item.Text = "";

                        if (plan.WorkingClassify == 0)
                        {
                            continue;
                        }
                        item.Text += DictionaryBLL.GetDicCollections("OA_WorkingClassify")[plan.WorkingClassify.ToString()].Name + " ";
                        switch (plan.WorkingClassify)
                        {
                        case 1:    //门店拜访
                            if (plan.RelateClient != 0)
                            {
                                //门店拜访
                                CM_Client client = new CM_ClientBLL(plan.RelateClient).Model;
                                if (client != null)
                                {
                                    item.Text += client.ShortName + " ";
                                }
                            }
                            break;

                        case 2:     //协同拜访
                            if (plan.RelateStaff != 0)
                            {
                                Org_Staff staff = new Org_StaffBLL(plan.RelateStaff).Model;
                                if (staff != null)
                                {
                                    item.Text += staff.RealName + " ";
                                }
                            }
                            break;

                        default:
                            item.Text += plan.Description + " ";
                            break;
                        }
                        if (plan.OfficialCity > 1)
                        {
                            item.Text += TreeTableBLL.GetFullPathName("MCS_SYS.dbo.Addr_OfficialCity", plan.OfficialCity).Replace("->", "");
                        }
                        bt.Items.Add(item);
                    }
                    bt.DisplayMode    = BulletedListDisplayMode.Text;
                    bt.BulletImageUrl = "~/Images/gif/gif-0163.gif";
                    bt.BulletStyle    = BulletStyle.CustomImage;
                    bt.CssClass       = "calaitem";
                    e.Cell.Controls.Add(bt);
                }
            }
            #endregion
        }
    }
    protected void bt_Save_Click(object sender, EventArgs e)
    {
        if (ViewState["WorkingPlanData"] != null)
        {
            DataTable dt        = (DataTable)ViewState["WorkingPlanData"];
            DateTime  begindate = (DateTime)ViewState["BeginDate"];
            DateTime  enddate   = (DateTime)ViewState["EndDate"];

            #region 将界面中已选择的日志放入IList中
            IList <JN_Journal> NewJournals = new List <JN_Journal>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                JN_Journal jn  = null;
                DateTime   day = begindate;
                while (day <= enddate)
                {
                    if ((bool)dt.Rows[i]["J" + day.ToString("MMdd")])
                    {
                        #region 新增日志
                        if (jn == null)
                        {
                            jn                 = new JN_Journal();
                            jn.Title           = (string)dt.Rows[i]["Description"];
                            jn.OrganizeCity    = new Org_StaffBLL((int)ViewState["PlanStaff"]).Model.OrganizeCity;
                            jn.JournalType     = 1;
                            jn.Staff           = (int)ViewState["PlanStaff"];
                            jn.BeginTime       = day.AddHours(8.5);
                            jn.EndTime         = day.AddHours(17.5);
                            jn.WorkingClassify = (int)dt.Rows[i]["WorkingClassify"];
                            if (jn.WorkingClassify == 1)
                            {
                                jn.RelateClient = (int)dt.Rows[i]["RelateClient"];
                                jn.Title        = "[客户拜访]" + dt.Rows[i]["RelateClientName"] + jn.Title;
                            }
                            if (jn.WorkingClassify == 2)
                            {
                                jn.RelateStaff          = (int)dt.Rows[i]["RelateStaff"];
                                jn["CCSynergeticStaff"] = "N";
                                jn.Title = "[协同拜访]" + dt.Rows[i]["RelateStaffName"] + jn.Title;
                            }
                            jn.OfficialCity = (int)dt.Rows[i]["OfficialCity"];

                            jn["IPAddress"]  = Request.UserHostAddress;
                            jn["IPLocation"] = Const_IPLocationBLL.FindByIP(Request.UserHostAddress).Location;
                            jn.ApproveFlag   = 2;
                            jn.InsertStaff   = (int)Session["UserID"];

                            NewJournals.Add(jn);

                            //如果每天都生成一条独立的日志,则启用这一行
                            jn = null;
                        }
                        else
                        {
                            jn.EndTime = day.AddHours(17);
                        }
                        #endregion
                    }
                    else
                    {
                        jn = null;
                    }

                    day = day.AddDays(1);
                }
            }
            #endregion

            #region 将数据库中已存的计划与IList作比较,相同的从List中移除,不存在于List中的从数据中移除
            JN_WorkingPlanBLL bll = new JN_WorkingPlanBLL((int)ViewState["PlanID"]);
            string            con = "Staff = " + bll.Model.Staff.ToString() +
                                    " AND BeginTime BETWEEN '" + bll.Model.BeginDate.ToString("yyyy-MM-dd") + "' AND '" + bll.Model.EndDate.ToString("yyyy-MM-dd 23:59:59") + "' " +
                                    " AND JournalType = 1";
            IList <JN_Journal> OrgJournals = JN_JournalBLL.GetModelList(con);
            foreach (JN_Journal orgitem in OrgJournals)
            {
                JN_Journal m = null;
                if (orgitem.WorkingClassify == 1)
                {
                    m = NewJournals.FirstOrDefault(p => p.WorkingClassify == orgitem.WorkingClassify &&
                                                   p.RelateClient == orgitem.RelateClient &&
                                                   p.BeginTime.Date == orgitem.BeginTime.Date && p.EndTime.Date == orgitem.EndTime.Date);
                }
                else if (orgitem.WorkingClassify == 2)
                {
                    m = NewJournals.FirstOrDefault(p => p.WorkingClassify == orgitem.WorkingClassify &&
                                                   p.RelateStaff == orgitem.RelateStaff &&
                                                   p.BeginTime.Date == orgitem.BeginTime.Date && p.EndTime.Date == orgitem.EndTime.Date);
                }
                else
                {
                    m = NewJournals.FirstOrDefault(p => p.WorkingClassify == orgitem.WorkingClassify &&
                                                   p.OfficialCity == orgitem.OfficialCity && p.Title == orgitem.Title &&
                                                   p.BeginTime.Date == orgitem.BeginTime.Date && p.EndTime.Date == orgitem.EndTime.Date);
                }

                if (m == null)
                {
                    new JN_JournalBLL(orgitem.ID).Delete();   //删除日志
                }
                else
                {
                    NewJournals.Remove(m);
                }
            }
            #endregion

            #region 将剩余下来的计划,新增到数据库中
            foreach (JN_Journal item in NewJournals)
            {
                JN_JournalBLL jnbll = new JN_JournalBLL();
                jnbll.Model = item;
                jnbll.Add();
            }
            #endregion

            BindGrid();

            MessageBox.ShowAndRedirect(this, "与该计划相关联的实际工作日志填报保存成功!", "JournalOnWorkingPlan.aspx?PlanID=" + ViewState["PlanID"].ToString());
        }
    }
    private DataTable LoadWorkingPlanDetail(int planid)
    {
        JN_WorkingPlanBLL bll = new JN_WorkingPlanBLL(planid);

        DataTable dt = GenareateDataTable(bll.Model.BeginDate, bll.Model.EndDate);

        #region 载入原工作计划
        foreach (JN_WorkingPlanDetail detail in bll.Items.OrderBy(p => p.WorkingClassify))
        {
            DataRow[] rows = dt.Select("WorkingClassify = " + detail.WorkingClassify.ToString() +
                                       " AND Description = '" + detail.Description + "' " +
                                       " AND RelateStaff = " + detail.RelateStaff.ToString() +
                                       " AND RelateClient = " + detail.RelateClient.ToString() +
                                       " AND OfficialCity = " + detail.OfficialCity.ToString());
            if (rows.Length > 0)
            {
                DateTime date = detail.BeginTime;
                while (date <= detail.EndTime)
                {
                    rows[0]["P" + date.ToString("MMdd")] = true;
                    date = date.AddDays(1);
                }
            }
            else
            {
                DataRow dr = dt.NewRow();

                dr["WorkingClassify"]     = detail.WorkingClassify;
                dr["WorkingClassifyName"] = DictionaryBLL.GetDicCollections("OA_WorkingClassify")[detail.WorkingClassify.ToString()].Name;
                dr["Description"]         = detail.Description;

                dr["RelateClient"] = detail.RelateClient;
                if (detail.RelateClient > 0)
                {
                    dr["RelateClientName"] = new CM_ClientBLL(detail.RelateClient).Model.FullName;
                }

                dr["RelateStaff"] = detail.RelateStaff;
                if (detail.RelateStaff > 0)
                {
                    dr["RelateStaffName"] = new Org_StaffBLL(detail.RelateStaff).Model.RealName;
                }

                dr["OfficialCity"] = detail.OfficialCity;
                if (detail.OfficialCity > 0)
                {
                    dr["OfficialCityName"] = TreeTableBLL.GetFullPathName("MCS_SYS.dbo.Addr_OfficialCity", detail.OfficialCity).Replace("->", " ");
                }

                DateTime date = detail.BeginTime;
                while (date <= detail.EndTime)
                {
                    dr["P" + date.ToString("MMdd")] = true;
                    date = date.AddDays(1);
                }

                if (detail["Cost1"] != "")
                {
                    dr["Cost1"] = decimal.Parse(detail["Cost1"]);
                }
                else
                {
                    dr["Cost1"] = 0;
                }

                if (detail["Cost2"] != "")
                {
                    dr["Cost2"] = decimal.Parse(detail["Cost2"]);
                }
                else
                {
                    dr["Cost2"] = 0;
                }

                dt.Rows.Add(dr);
            }
        }
        #endregion

        #region 加载数据库中已填报的日志
        string con = "Staff = " + bll.Model.Staff.ToString() +
                     " AND BeginTime BETWEEN '" + bll.Model.BeginDate.ToString("yyyy-MM-dd") + "' AND '" + bll.Model.EndDate.ToString("yyyy-MM-dd 23:59:59") + "' " +
                     " AND JournalType = 1";
        IList <JN_Journal> Journals = JN_JournalBLL.GetModelList(con);

        foreach (JN_Journal journal in Journals.OrderBy(p => p.WorkingClassify))
        {
            DataRow[] rows = dt.Select("WorkingClassify = " + journal.WorkingClassify.ToString() +
                                       " AND Description = '" + journal.Title + "' " +
                                       " AND RelateStaff = " + (journal.WorkingClassify == 2 ? journal.RelateStaff.ToString() : "0") +
                                       " AND RelateClient = " + (journal.WorkingClassify == 1 ? journal.RelateClient.ToString() : "0") +
                                       " AND OfficialCity = " + journal.OfficialCity.ToString());

            if (rows.Length == 0)
            {
                rows = dt.Select("WorkingClassify = " + journal.WorkingClassify.ToString() +
                                 " AND Description = '' " +
                                 " AND RelateStaff = " + (journal.WorkingClassify == 2 ? journal.RelateStaff.ToString() : "0") +
                                 " AND RelateClient = " + (journal.WorkingClassify == 1 ? journal.RelateClient.ToString() : "0") +
                                 " AND OfficialCity = " + journal.OfficialCity.ToString());
            }

            if (rows.Length > 0)
            {
                rows[0]["J" + journal.BeginTime.ToString("MMdd")]   = true;
                rows[0]["JID" + journal.BeginTime.ToString("MMdd")] = journal.ID;

                DateTime date = journal.BeginTime;
                while (date <= journal.EndTime)
                {
                    rows[0]["J" + date.ToString("MMdd")]   = true;
                    rows[0]["JID" + date.ToString("MMdd")] = journal.ID;
                    date = date.AddDays(1);
                }
            }
            else
            {
                #region 增加无计划的日志
                DataRow dr = dt.NewRow();

                dr["WorkingClassify"] = journal.WorkingClassify;
                if (journal.WorkingClassify > 0)
                {
                    dr["WorkingClassifyName"] = DictionaryBLL.GetDicCollections("OA_WorkingClassify")[journal.WorkingClassify.ToString()].Name;
                }
                dr["Description"] = journal.Title;

                dr["RelateClient"] = journal.RelateClient;
                if (journal.RelateClient > 0)
                {
                    dr["RelateClientName"] = new CM_ClientBLL(journal.RelateClient).Model.FullName;
                }

                dr["RelateStaff"] = journal.RelateStaff;
                if (journal.RelateStaff > 0)
                {
                    dr["RelateStaffName"] = new Org_StaffBLL(journal.RelateStaff).Model.RealName;
                }

                dr["OfficialCity"] = journal.OfficialCity;
                if (journal.OfficialCity > 0)
                {
                    dr["OfficialCityName"] = TreeTableBLL.GetFullPathName("MCS_SYS.dbo.Addr_OfficialCity", journal.OfficialCity).Replace("->", " ");
                }

                DateTime date = journal.BeginTime;
                while (date <= journal.EndTime)
                {
                    dr["J" + date.ToString("MMdd")]   = true;
                    dr["JID" + date.ToString("MMdd")] = journal.ID;
                    date = date.AddDays(1);
                }

                dt.Rows.Add(dr);
                #endregion
            }
        }
        #endregion
        return(dt);
    }
    protected void Page_Init(object sender, EventArgs e)
    {
        JN_WorkingPlanBLL bll = new JN_WorkingPlanBLL(int.Parse(Request.QueryString["PlanID"]));

        InitGridView(bll.Model.BeginDate, bll.Model.EndDate);
    }
    /// <summary>
    /// 载入已有的工作项
    /// </summary>
    private void BindData()
    {
        JN_WorkingPlanBLL bll = new JN_WorkingPlanBLL((int)ViewState["PlanID"]);

        ViewState["WorkingPlanData"] = LoadWorkingPlanDetail(bll.Model.ID);
        ViewState["BeginDate"] = bll.Model.BeginDate;
        ViewState["EndDate"] = bll.Model.EndDate;
        ViewState["PlanStaff"] = bll.Model.Staff;

        if ((int)Session["UserID"] != (int)ViewState["PlanStaff"])
        {
            cb_DisplayCheckedOnly.Visible = false;
            cb_DisplayCheckedOnly.Checked = true;
            bt_AddJournal.Visible = false;
            bt_Save.Visible = false;
        }
        else if (bll.Model.EndDate < DateTime.Today)
            cb_DisplayCheckedOnly.Checked = true;

        pl_detail.BindData(bll.Model);

        BindGrid();
        UploadFile1.RelateID = bll.Model.ID;
        UploadFile1.DataBind();
    }
    protected void bt_Create_Click(object sender, EventArgs e)
    {
        DateTime begindate = DateTime.Parse(tbx_begindate.Text);
        DateTime enddate   = DateTime.Parse(tbx_enddate.Text);

        #region 规则校验
        if (select_PlanStaff.SelectValue == "")
        {
            MessageBox.Show(this, "请正确选择要填报计划的员工!");
            return;
        }
        if (begindate < DateTime.Today)
        {
            MessageBox.Show(this, "开始日期不能小于今天!");
            return;
        }

        if (enddate < begindate)
        {
            MessageBox.Show(this, "截止日期必须大于开始日期!");
            return;
        }

        if ((enddate - begindate).Days > 31)
        {
            MessageBox.Show(this, "日期范围不能超过一个月!");
            return;
        }

        if (JN_WorkingPlanBLL.GetModelList("Staff = " + select_PlanStaff.SelectValue +
                                           " AND ( (BeginDate BETWEEN '" + begindate.ToString("yyyy-MM-dd") + "' AND '" + enddate.ToString("yyyy-MM-dd") +
                                           "') OR ('" + begindate.ToString("yyyy-MM-dd") + "' BETWEEN BeginDate AND EndDate) )").Count > 0)
        {
            MessageBox.Show(this, "日期范围与系统中已填报的计划有日期重叠!");
            return;
        }
        #endregion

        InitGridView(begindate, enddate);
        DataTable dt = GenareateDataTable(begindate, enddate);

        #region 页面控件使能控制
        tr_adddetail.Visible     = true;
        tbx_begindate.Enabled    = false;
        tbx_enddate.Enabled      = false;
        select_PlanStaff.Enabled = false;
        tr_OrganizeCity.Enabled  = false;

        cbx_GenarateSynergetic.Visible = false;
        bt_Create.Visible = false;
        bt_Save.Visible   = true;
        #endregion

        #region 载入该操作员所有负责客户,加入客户拜访记录计划中
        IList <CM_Client> clients = CM_ClientBLL.GetModelList("ClientManager=" + select_PlanStaff.SelectValue + " AND ActiveFlag=1 AND ApproveFlag=1");

        foreach (CM_Client client in clients)
        {
            DataRow dr = dt.NewRow();

            dr["WorkingClassify"]     = "1";
            dr["WorkingClassifyName"] = DictionaryBLL.GetDicCollections("OA_WorkingClassify")["1"].Name;

            dr["RelateClient"]     = client.ID;
            dr["RelateClientName"] = client.FullName;

            dr["RelateStaff"]     = 0;
            dr["RelateStaffName"] = "";

            dr["OfficialCity"] = client.OfficalCity;
            if (client.OfficalCity > 0)
            {
                dr["OfficialCityName"] = TreeTableBLL.GetFullPathName("MCS_SYS.dbo.Addr_OfficialCity", client.OfficalCity).Replace("->", " ");
            }

            dt.Rows.Add(dr);
        }
        #endregion

        #region 加入协同拜访工作项,管理片区非总部员工,将所有下级员工加入
        if (cbx_GenarateSynergetic.Checked && int.Parse(tr_OrganizeCity.SelectValue) > 1)
        {
            #region 所有下级职位(不含本级职位)
            string    positions          = "";
            int       position           = new Org_StaffBLL(int.Parse(select_PlanStaff.SelectValue)).Model.Position;
            DataTable dtAllChilePosition = TreeTableBLL.GetAllChildByNodes("MCS_SYS.dbo.Org_Position", "ID", "SuperID", position.ToString());
            for (int i = 0; i < dtAllChilePosition.Rows.Count; i++)
            {
                positions += dtAllChilePosition.Rows[i]["ID"].ToString();
                if (i != dtAllChilePosition.Rows.Count - 1)
                {
                    positions += ",";
                }
            }
            #endregion

            #region 所有本级及下级管理片区
            string orgcitys = "";
            Addr_OrganizeCityBLL orgcity = new Addr_OrganizeCityBLL(int.Parse(tr_OrganizeCity.SelectValue), true);
            orgcitys = orgcity.GetAllChildNodeIDs();
            if (orgcitys != "")
            {
                orgcitys += ",";
            }
            orgcitys += tr_OrganizeCity.SelectValue;
            #endregion

            if (positions != "" && orgcitys != "")
            {
                IList <Org_Staff> staffs = Org_StaffBLL.GetStaffList("OrganizeCity IN (" + orgcitys + ") AND Position IN (" + positions + ") AND Dimission=1 AND ApproveFlag=1");

                foreach (Org_Staff staff in staffs)
                {
                    DataRow dr = dt.NewRow();

                    dr["WorkingClassify"]     = "2";
                    dr["WorkingClassifyName"] = DictionaryBLL.GetDicCollections("OA_WorkingClassify")["2"].Name;

                    dr["RelateClient"]     = 0;
                    dr["RelateClientName"] = "";

                    dr["RelateStaff"]     = staff.ID;
                    dr["RelateStaffName"] = staff.RealName;

                    dr["OfficialCity"] = staff.OfficialCity;
                    if (staff.OfficialCity > 0)
                    {
                        dr["OfficialCityName"] = TreeTableBLL.GetFullPathName("MCS_SYS.dbo.Addr_OfficialCity", staff.OfficialCity).Replace("->", " ");
                    }

                    dt.Rows.Add(dr);
                }
            }
        }
        #endregion

        #region 加入除客户拜访以外的工作项
        foreach (ListItem item in ddl_WorkingClassify.Items)
        {
            if (int.Parse(item.Value) >= 3)
            {
                DataRow dr = dt.NewRow();
                dr["WorkingClassify"]     = int.Parse(item.Value);
                dr["WorkingClassifyName"] = item.Text;
                dr["Description"]         = "";
                dr["RelateClient"]        = 0;
                dr["RelateClientName"]    = "";

                dr["RelateStaff"]     = 0;
                dr["RelateStaffName"] = "";

                dr["OfficialCity"] = (int)ViewState["StaffOfficialCity"];
                if (ViewState["StaffOfficialCity"] != null && (int)ViewState["StaffOfficialCity"] != 0)
                {
                    dr["OfficialCityName"] = new Addr_OfficialCityBLL((int)ViewState["StaffOfficialCity"]).Model.Name;
                }
                else
                {
                    dr["OfficialCity"] = 0;
                }

                dr["Cost1"] = 0;
                dr["Cost2"] = 0;
                dt.Rows.Add(dr);
            }
        }
        #endregion

        ViewState["WorkingPlanData"] = dt;
        ViewState["BeginDate"]       = begindate;
        ViewState["EndDate"]         = enddate;

        BindGrid();
    }
    private DataTable LoadWorkingPlanDetail(int planid)
    {
        JN_WorkingPlanBLL bll = new JN_WorkingPlanBLL(planid);

        DataTable dt = GenareateDataTable(bll.Model.BeginDate, bll.Model.EndDate);

        foreach (JN_WorkingPlanDetail detail in bll.Items)
        {
            DataRow[] rows = dt.Select("WorkingClassify = " + detail.WorkingClassify.ToString() +
                                       " AND Description = '" + detail.Description + "' " +
                                       " AND RelateStaff = " + detail.RelateStaff.ToString() +
                                       " AND RelateClient = " + detail.RelateClient.ToString() +
                                       " AND OfficialCity = " + detail.OfficialCity.ToString());
            if (rows.Length > 0)
            {
                DateTime date = detail.BeginTime;
                while (date <= detail.EndTime)
                {
                    rows[0]["P" + date.ToString("MMdd")] = true;
                    date = date.AddDays(1);
                }
            }
            else
            {
                DataRow dr = dt.NewRow();

                dr["WorkingClassify"]     = detail.WorkingClassify;
                dr["WorkingClassifyName"] = DictionaryBLL.GetDicCollections("OA_WorkingClassify")[detail.WorkingClassify.ToString()].Name;
                dr["Description"]         = detail.Description;

                dr["RelateClient"] = detail.RelateClient;
                if (detail.RelateClient > 0)
                {
                    dr["RelateClientName"] = new CM_ClientBLL(detail.RelateClient).Model.FullName;
                }

                dr["RelateStaff"] = detail.RelateStaff;
                if (detail.RelateStaff > 0)
                {
                    dr["RelateStaffName"] = new Org_StaffBLL(detail.RelateStaff).Model.RealName;
                }

                dr["OfficialCity"] = detail.OfficialCity;

                if (detail.OfficialCity > 0)
                {
                    dr["OfficialCityName"] = TreeTableBLL.GetFullPathName("MCS_SYS.dbo.Addr_OfficialCity", detail.OfficialCity).Replace("->", " ");
                }

                DateTime date = detail.BeginTime;
                while (date <= detail.EndTime)
                {
                    dr["P" + date.ToString("MMdd")] = true;
                    date = date.AddDays(1);
                }

                if (detail["Cost1"] != "")
                {
                    dr["Cost1"] = decimal.Parse(detail["Cost1"]);
                }
                else
                {
                    dr["Cost1"] = 0;
                }

                if (detail["Cost2"] != "")
                {
                    dr["Cost2"] = decimal.Parse(detail["Cost2"]);
                }
                else
                {
                    dr["Cost2"] = 0;
                }
                dt.Rows.Add(dr);
            }
        }

        return(dt);
    }
    private DataTable LoadWorkingPlanDetail(int planid)
    {
        JN_WorkingPlanBLL bll = new JN_WorkingPlanBLL(planid);

        DataTable dt = GenareateDataTable(bll.Model.BeginDate, bll.Model.EndDate);

        #region 载入原工作计划
        foreach (JN_WorkingPlanDetail detail in bll.Items.OrderBy(p => p.WorkingClassify))
        {
            DataRow[] rows = dt.Select("WorkingClassify = " + detail.WorkingClassify.ToString() +
                " AND Description = '" + detail.Description + "' " +
                " AND RelateStaff = " + detail.RelateStaff.ToString() +
                " AND RelateClient = " + detail.RelateClient.ToString() +
                " AND OfficialCity = " + detail.OfficialCity.ToString());
            if (rows.Length > 0)
            {
                DateTime date = detail.BeginTime;
                while (date <= detail.EndTime)
                {
                    rows[0]["P" + date.ToString("MMdd")] = true;
                    date = date.AddDays(1);
                }
            }
            else
            {
                DataRow dr = dt.NewRow();

                dr["WorkingClassify"] = detail.WorkingClassify;
                dr["WorkingClassifyName"] = DictionaryBLL.GetDicCollections("OA_WorkingClassify")[detail.WorkingClassify.ToString()].Name;
                dr["Description"] = detail.Description;

                dr["RelateClient"] = detail.RelateClient;
                if (detail.RelateClient > 0) dr["RelateClientName"] = new CM_ClientBLL(detail.RelateClient).Model.FullName;

                dr["RelateStaff"] = detail.RelateStaff;
                if (detail.RelateStaff > 0) dr["RelateStaffName"] = new Org_StaffBLL(detail.RelateStaff).Model.RealName;

                dr["OfficialCity"] = detail.OfficialCity;
                if (detail.OfficialCity > 0) dr["OfficialCityName"] = TreeTableBLL.GetFullPathName("MCS_SYS.dbo.Addr_OfficialCity", detail.OfficialCity).Replace("->", " ");

                DateTime date = detail.BeginTime;
                while (date <= detail.EndTime)
                {
                    dr["P" + date.ToString("MMdd")] = true;
                    date = date.AddDays(1);
                }

                if (detail["Cost1"] != "")
                    dr["Cost1"] = decimal.Parse(detail["Cost1"]);
                else
                    dr["Cost1"] = 0;

                if (detail["Cost2"] != "")
                    dr["Cost2"] = decimal.Parse(detail["Cost2"]);
                else
                    dr["Cost2"] = 0;

                dt.Rows.Add(dr);
            }
        }
        #endregion

        #region 加载数据库中已填报的日志
        string con = "Staff = " + bll.Model.Staff.ToString() +
            " AND BeginTime BETWEEN '" + bll.Model.BeginDate.ToString("yyyy-MM-dd") + "' AND '" + bll.Model.EndDate.ToString("yyyy-MM-dd 23:59:59") + "' " +
            " AND JournalType = 1";
        IList<JN_Journal> Journals = JN_JournalBLL.GetModelList(con);

        foreach (JN_Journal journal in Journals.OrderBy(p => p.WorkingClassify))
        {
            DataRow[] rows = dt.Select("WorkingClassify = " + journal.WorkingClassify.ToString() +
                " AND Description = '" + journal.Title + "' " +
                " AND RelateStaff = " + (journal.WorkingClassify == 2 ? journal.RelateStaff.ToString() : "0") +
                " AND RelateClient = " + (journal.WorkingClassify == 1 ? journal.RelateClient.ToString() : "0") +
                " AND OfficialCity = " + journal.OfficialCity.ToString());

            if (rows.Length == 0)
            {
                rows = dt.Select("WorkingClassify = " + journal.WorkingClassify.ToString() +
                " AND Description = '' " +
                " AND RelateStaff = " + (journal.WorkingClassify == 2 ? journal.RelateStaff.ToString() : "0") +
                " AND RelateClient = " + (journal.WorkingClassify == 1 ? journal.RelateClient.ToString() : "0") +
                " AND OfficialCity = " + journal.OfficialCity.ToString());
            }

            if (rows.Length > 0)
            {
                rows[0]["J" + journal.BeginTime.ToString("MMdd")] = true;
                rows[0]["JID" + journal.BeginTime.ToString("MMdd")] = journal.ID;

                DateTime date = journal.BeginTime;
                while (date <= journal.EndTime)
                {
                    rows[0]["J" + date.ToString("MMdd")] = true;
                    rows[0]["JID" + date.ToString("MMdd")] = journal.ID;
                    date = date.AddDays(1);
                }
            }
            else
            {
                #region 增加无计划的日志
                DataRow dr = dt.NewRow();

                dr["WorkingClassify"] = journal.WorkingClassify;
                if (journal.WorkingClassify > 0) dr["WorkingClassifyName"] = DictionaryBLL.GetDicCollections("OA_WorkingClassify")[journal.WorkingClassify.ToString()].Name;
                dr["Description"] = journal.Title;

                dr["RelateClient"] = journal.RelateClient;
                if (journal.RelateClient > 0) dr["RelateClientName"] = new CM_ClientBLL(journal.RelateClient).Model.FullName;

                dr["RelateStaff"] = journal.RelateStaff;
                if (journal.RelateStaff > 0) dr["RelateStaffName"] = new Org_StaffBLL(journal.RelateStaff).Model.RealName;

                dr["OfficialCity"] = journal.OfficialCity;
                if (journal.OfficialCity > 0) dr["OfficialCityName"] = TreeTableBLL.GetFullPathName("MCS_SYS.dbo.Addr_OfficialCity", journal.OfficialCity).Replace("->", " ");

                DateTime date = journal.BeginTime;
                while (date <= journal.EndTime)
                {
                    dr["J" + date.ToString("MMdd")] = true;
                    dr["JID" + date.ToString("MMdd")] = journal.ID;
                    date = date.AddDays(1);
                }

                dt.Rows.Add(dr);
                #endregion
            }
        }
        #endregion
        return dt;
    }
    protected void bt_Save_Click(object sender, EventArgs e)
    {
        if (ViewState["WorkingPlanData"] != null)
        {
            DataTable dt = (DataTable)ViewState["WorkingPlanData"];
            DateTime begindate = (DateTime)ViewState["BeginDate"];
            DateTime enddate = (DateTime)ViewState["EndDate"];

            #region 将界面中已选择的日志放入IList中
            IList<JN_Journal> NewJournals = new List<JN_Journal>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                JN_Journal jn = null;
                DateTime day = begindate;
                while (day <= enddate)
                {
                    if ((bool)dt.Rows[i]["J" + day.ToString("MMdd")])
                    {
                        #region 新增日志
                        if (jn == null)
                        {
                            jn = new JN_Journal();
                            jn.Title = (string)dt.Rows[i]["Description"];
                            jn.OrganizeCity = new Org_StaffBLL((int)ViewState["PlanStaff"]).Model.OrganizeCity;
                            jn.JournalType = 1;
                            jn.Staff = (int)ViewState["PlanStaff"];
                            jn.BeginTime = day.AddHours(8.5);
                            jn.EndTime = day.AddHours(17.5);
                            jn.WorkingClassify = (int)dt.Rows[i]["WorkingClassify"];
                            if (jn.WorkingClassify == 1)
                            {
                                jn.RelateClient = (int)dt.Rows[i]["RelateClient"];
                                jn.Title = "[客户拜访]" + dt.Rows[i]["RelateClientName"] + jn.Title;
                            }
                            if (jn.WorkingClassify == 2)
                            {
                                jn.RelateStaff = (int)dt.Rows[i]["RelateStaff"];
                                jn["CCSynergeticStaff"] = "N";
                                jn.Title = "[协同拜访]" + dt.Rows[i]["RelateStaffName"] + jn.Title;
                            }
                            jn.OfficialCity = (int)dt.Rows[i]["OfficialCity"];

                            jn["IPAddress"] = Request.UserHostAddress;
                            jn["IPLocation"] = Const_IPLocationBLL.FindByIP(Request.UserHostAddress).Location;
                            jn.ApproveFlag = 2;
                            jn.InsertStaff = (int)Session["UserID"];

                            NewJournals.Add(jn);

                            //如果每天都生成一条独立的日志,则启用这一行
                            jn = null;
                        }
                        else
                        {
                            jn.EndTime = day.AddHours(17);
                        }
                        #endregion
                    }
                    else
                    {
                        jn = null;
                    }

                    day = day.AddDays(1);
                }
            }
            #endregion

            #region 将数据库中已存的计划与IList作比较,相同的从List中移除,不存在于List中的从数据中移除
            JN_WorkingPlanBLL bll = new JN_WorkingPlanBLL((int)ViewState["PlanID"]);
            string con = "Staff = " + bll.Model.Staff.ToString() +
            " AND BeginTime BETWEEN '" + bll.Model.BeginDate.ToString("yyyy-MM-dd") + "' AND '" + bll.Model.EndDate.ToString("yyyy-MM-dd 23:59:59") + "' " +
            " AND JournalType = 1";
            IList<JN_Journal> OrgJournals = JN_JournalBLL.GetModelList(con);
            foreach (JN_Journal orgitem in OrgJournals)
            {
                JN_Journal m = null;
                if (orgitem.WorkingClassify == 1)
                {
                    m = NewJournals.FirstOrDefault(p => p.WorkingClassify == orgitem.WorkingClassify &&
                        p.RelateClient == orgitem.RelateClient &&
                        p.BeginTime.Date == orgitem.BeginTime.Date && p.EndTime.Date == orgitem.EndTime.Date);
                }
                else if (orgitem.WorkingClassify == 2)
                {
                    m = NewJournals.FirstOrDefault(p => p.WorkingClassify == orgitem.WorkingClassify &&
                       p.RelateStaff == orgitem.RelateStaff &&
                       p.BeginTime.Date == orgitem.BeginTime.Date && p.EndTime.Date == orgitem.EndTime.Date);
                }
                else
                {
                    m = NewJournals.FirstOrDefault(p => p.WorkingClassify == orgitem.WorkingClassify &&
                         p.OfficialCity == orgitem.OfficialCity && p.Title == orgitem.Title &&
                         p.BeginTime.Date == orgitem.BeginTime.Date && p.EndTime.Date == orgitem.EndTime.Date);
                }

                if (m == null)
                    new JN_JournalBLL(orgitem.ID).Delete();   //删除日志
                else
                    NewJournals.Remove(m);
            }
            #endregion

            #region 将剩余下来的计划,新增到数据库中
            foreach (JN_Journal item in NewJournals)
            {
                JN_JournalBLL jnbll = new JN_JournalBLL();
                jnbll.Model = item;
                jnbll.Add();
            }
            #endregion

            BindGrid();

            MessageBox.ShowAndRedirect(this, "与该计划相关联的实际工作日志填报保存成功!", "JournalOnWorkingPlan.aspx?PlanID=" + ViewState["PlanID"].ToString());
        }
    }
    private void BindGrid()
    {
        string ConditionStr = "JN_WorkingPlan.BeginDate Between '" + tbx_begindate.Text + "' AND '" + tbx_enddate.Text + " 23:59' ";


        switch (MCSTabControl1.SelectedTabItem.Value)
        {
        case "0":
            ConditionStr           += " AND JN_WorkingPlan.Staff=" + Session["UserID"].ToString();
            gv_List.OrderFields     = "JN_WorkingPlan_ID";
            gv_List.ConditionString = ConditionStr;
            gv_List.BindGrid();
            break;

        case "1":
            if (tbx_StaffName.Text != "")
            {
                ConditionStr += "AND Org_Staff.RealName LIKE '%" + tbx_StaffName.Text + "%'";
            }

            #region 只显示当前员工的所有下级职位的员工计划
            if (tr_Position.SelectValue != "0")
            {
                if (cb_IncludeChild.Checked || tr_Position.SelectValue == tr_Position.RootValue)
                {
                    Org_PositionBLL p         = new Org_PositionBLL(int.Parse(tr_Position.SelectValue));
                    string          positions = p.GetAllChildPosition();

                    if (tr_Position.SelectValue != tr_Position.RootValue)
                    {
                        if (positions != "")
                        {
                            positions += ",";
                        }
                        positions += tr_Position.SelectValue;
                    }

                    ConditionStr += " AND Org_Staff.Position IN (" + positions + ")";
                }
                else
                {
                    ConditionStr += " AND Org_Staff.Position = " + tr_Position.SelectValue;
                }
            }
            #endregion

            #region 判断当前可查询的管理片区范围
            if (tr_OrganizeCity.SelectValue != "1")
            {
                string orgcitys = "";
                Addr_OrganizeCityBLL orgcity = new Addr_OrganizeCityBLL(int.Parse(tr_OrganizeCity.SelectValue), true);
                orgcitys = orgcity.GetAllChildNodeIDs();
                if (orgcitys != "")
                {
                    orgcitys += ",";
                }
                orgcitys += tr_OrganizeCity.SelectValue;

                if (orgcitys != "")
                {
                    ConditionStr += " AND JN_WorkingPlan.OrganizeCity IN (" + orgcitys + ") ";
                }
            }
            #endregion
            gv_List.OrderFields     = "JN_WorkingPlan_ID";
            gv_List.ConditionString = ConditionStr;
            gv_List.BindGrid();
            break;

        case "2":
            DateTime  dt_begin  = DateTime.Parse(tbx_begindate.Text);
            DateTime  dt_end    = DateTime.Parse(tbx_enddate.Text);
            DataTable dtSummary = JN_WorkingPlanBLL.GetSummary(dt_begin, dt_end, int.Parse(tr_OrganizeCity.SelectValue), int.Parse(tr_Position.SelectValue), tbx_StaffName.Text.Trim(), cb_IncludeChild.Checked ? 1 : 0);
            if (dtSummary.Rows.Count == 0)
            {
                gv_planList.DataBind();
                return;
            }

            dtSummary = MatrixTable.Matrix(dtSummary, new string[] { "营业部", "办事处", "Staff", "员工姓名", "工号", "职位", "出差天数", "负责客户数", "拜访客户数→计划", "拜访客户数→实际", "差旅费→计划", "差旅费→实际差旅费", "差旅费→实际用车费" },
                                           new string[] { "WorkingClassify" }, "WorkCounts", true, false);

            dtSummary.Rows[dtSummary.Rows.Count - 1]["出差天数"]      = dtSummary.Compute("SUM(出差天数)", "true");
            dtSummary.Rows[dtSummary.Rows.Count - 1]["负责客户数"]     = dtSummary.Compute("SUM(负责客户数)", "true");
            dtSummary.Rows[dtSummary.Rows.Count - 1]["拜访客户数→计划"]  = dtSummary.Compute("SUM(拜访客户数→计划)", "true");
            dtSummary.Rows[dtSummary.Rows.Count - 1]["拜访客户数→实际"]  = dtSummary.Compute("SUM(拜访客户数→实际)", "true");
            dtSummary.Rows[dtSummary.Rows.Count - 1]["差旅费→计划"]    = dtSummary.Compute("SUM(差旅费→计划)", "true");
            dtSummary.Rows[dtSummary.Rows.Count - 1]["差旅费→实际差旅费"] = dtSummary.Compute("SUM(差旅费→实际差旅费)", "true");
            dtSummary.Rows[dtSummary.Rows.Count - 1]["差旅费→实际用车费"] = dtSummary.Compute("SUM(差旅费→实际用车费)", "true");

            gv_planList.DataSource = dtSummary;
            gv_planList.DataBind();
            MatrixTable.GridViewMatric(gv_planList);
            gv_planList.Rows[gv_planList.Rows.Count - 1].Cells[0].Text = "";
            break;
        }
    }
    protected void bt_Save_Click(object sender, EventArgs e)
    {
        SaveDataTable();

        if (ViewState["WorkingPlanData"] != null)
        {
            DataTable dt        = (DataTable)ViewState["WorkingPlanData"];
            Decimal   totalcost = (decimal)dt.Compute("SUM(Cost1)+SUM(Cost2)", "");

            JN_WorkingPlanBLL bll;
            if ((int)ViewState["ID"] > 0)
            {
                bll = new JN_WorkingPlanBLL((int)ViewState["ID"]);
            }
            else
            {
                bll = new JN_WorkingPlanBLL();
            }

            bll.Model.OrganizeCity = int.Parse(tr_OrganizeCity.SelectValue);
            bll.Model.BeginDate    = (DateTime)ViewState["BeginDate"];
            bll.Model.EndDate      = (DateTime)ViewState["EndDate"];
            bll.Model.Staff        = int.Parse(select_PlanStaff.SelectValue);
            bll.Model["TotalCost"] = totalcost.ToString("0.#");

            pl_detail.GetData(bll.Model);

            if (bll.Model.Staff == 0)
            {
                MessageBox.Show(this, "请选择该计划的员工!");
                return;
            }

            if ((int)ViewState["ID"] > 0)
            {
                bll.Update();
            }
            else
            {
                bll.Model.InsertStaff = (int)Session["UserID"];
                bll.Model.State       = 1;
                bll.Model.ApproveFlag = 2;
                ViewState["ID"]       = bll.Add();
            }

            #region 将界面中已选择的计划放入IList中
            IList <JN_WorkingPlanDetail> PlanDetails = new List <JN_WorkingPlanDetail>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                JN_WorkingPlanDetail detail = null;
                bool     savedcost          = false;
                DateTime day = bll.Model.BeginDate;
                while (day <= bll.Model.EndDate)
                {
                    if ((bool)dt.Rows[i]["P" + day.ToString("MMdd")])
                    {
                        if (detail == null)
                        {
                            detail = new JN_WorkingPlanDetail();
                            detail.WorkingClassify = (int)dt.Rows[i]["WorkingClassify"];
                            detail.RelateClient    = (int)dt.Rows[i]["RelateClient"];
                            detail.RelateStaff     = (int)dt.Rows[i]["RelateStaff"];
                            detail.OfficialCity    = (int)dt.Rows[i]["OfficialCity"];
                            detail.Description     = (string)dt.Rows[i]["Description"];
                            detail.BeginTime       = day;
                            detail.EndTime         = day;
                            if (!savedcost)
                            {
                                detail["Cost1"] = ((decimal)dt.Rows[i]["Cost1"]).ToString("0.##");
                                detail["Cost2"] = ((decimal)dt.Rows[i]["Cost2"]).ToString("0.##");
                                savedcost       = true;
                            }
                            else
                            {
                                detail["Cost1"] = "0";
                                detail["Cost2"] = "0";
                            }
                            PlanDetails.Add(detail);
                        }
                        else
                        {
                            detail.EndTime = day;
                        }
                    }
                    else
                    {
                        detail = null;
                    }
                    day = day.AddDays(1);
                }
            }
            #endregion

            #region 将数据库中已存的计划与IList作比较,相同的从List中移除,不存在于List中的从数据中移除
            foreach (JN_WorkingPlanDetail orgitem in bll.Items)
            {
                JN_WorkingPlanDetail m = PlanDetails.FirstOrDefault(p => p.WorkingClassify == orgitem.WorkingClassify &&
                                                                    p.RelateClient == orgitem.RelateClient && p.RelateStaff == orgitem.RelateStaff &&
                                                                    p.OfficialCity == orgitem.OfficialCity && p.Description == orgitem.Description &&
                                                                    p.BeginTime == orgitem.BeginTime && p.EndTime == orgitem.EndTime &&
                                                                    p["Cost1"] == orgitem["Cost1"] && p["Cost2"] == orgitem["Cost2"]);

                if (m == null)
                {
                    bll.DeleteDetail(orgitem.ID);
                }
                else
                {
                    PlanDetails.Remove(m);
                }
            }
            #endregion

            #region 将剩余下来的计划,新增到数据库中
            foreach (JN_WorkingPlanDetail item in PlanDetails)
            {
                bll.AddDetail(item);
            }
            #endregion

            Response.Redirect("WorkingPlan_CalendarList.aspx?ID=" + ViewState["ID"].ToString());
        }
    }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (ViewState["Staff"] != null && (int)ViewState["Staff"] > 0)
        {
            e.Cell.HorizontalAlign = HorizontalAlign.Left;

            if (e.Day.Date <= DateTime.Today && MCSTabControl1.SelectedIndex == 0)
            {

                ImageButton ibt = new ImageButton();
                ibt.ImageUrl = "~/Images/gif/gif-0489.gif";
                ibt.BorderWidth = new Unit(0);
                ibt.ToolTip = "新增工作日志";
                ibt.OnClientClick = "javascript:NewJournal('" + e.Day.Date.DayOfYear.ToString() + "')";
                ibt.Click += new ImageClickEventHandler(ibt_Click);
                e.Cell.Controls.Add(ibt);
            }

            #region 获取该员工当天工作日志
            if (cbx_Journal.Checked)
            {
                IList<JN_Journal> journallists = JN_JournalBLL.GetModelList("'" + e.Day.Date.ToString("yyyy-MM-dd") + "' BETWEEN Convert(varchar(10),BeginTime,120) And EndTime AND Staff=" + ViewState["Staff"].ToString());
                if (journallists.Count > 0)
                {
                    BulletedList bt = new BulletedList();
                    foreach (JN_Journal j in journallists)
                    {
                        ListItem item = new ListItem();
                        item.Text = "";
                        if (j.JournalType == 1)
                        {
                            //日报
                            if (j.BeginTime.Hour != 0 && j.EndTime.Hour != 0)
                                item.Text += j.BeginTime.ToString("HH:mm") + "~" + j.EndTime.ToString("HH:mm") + " ";
                            if (j.WorkingClassify != 0)
                            {
                                item.Text += DictionaryBLL.GetDicCollections("OA_WorkingClassify")[j.WorkingClassify.ToString()].Name + " ";
                                switch (j.WorkingClassify)
                                {
                                    case 1://门店拜访
                                        if (j.RelateClient != 0)
                                        {
                                            //门店拜访
                                            CM_Client client = new CM_ClientBLL(j.RelateClient).Model;
                                            if (client != null) item.Text += client.ShortName + " ";

                                            if (j["VisitClientPurpose"] != "" && j["VisitClientPurpose"] != "0")
                                                item.Text += DictionaryBLL.GetDicCollections("OA_JN_VisitClientPurpose")[j["VisitClientPurpose"]].Name + " ";
                                        }
                                        break;
                                    case 2: //协同拜访
                                        if (j.RelateStaff != 0)
                                        {
                                            Org_Staff staff = new Org_StaffBLL(j.RelateStaff).Model;
                                            if (staff != null) item.Text += staff.RealName;
                                        }
                                        break;
                                    default:
                                        item.Text += j.Title;
                                        break;
                                }
                            }
                            else
                            {
                                item.Text += j.Title;
                            }
                        }
                        else
                        {
                            //周报、月报
                            item.Text += DictionaryBLL.GetDicCollections("OA_JournalType")[j.JournalType.ToString()].Name + " ";
                            item.Text += j.Title;
                        }
                        item.Value = "javascript:OpenJournal(" + j.ID.ToString() + ")";
                        bt.Items.Add(item);
                    }
                    bt.DisplayMode = BulletedListDisplayMode.HyperLink;
                    bt.BulletImageUrl = "~/Images/gif/gif-0162.gif";
                    bt.BulletStyle = BulletStyle.CustomImage;
                    bt.CssClass = "calaitem";
                    e.Cell.Controls.Add(bt);
                }
            }
            #endregion

            #region 获取访员工当天的工作计划
            if (cbx_Plan.Checked)
            {
                if (ViewState["PlanDetails"] == null || ((IList<JN_WorkingPlanDetail>)ViewState["PlanDetails"]).Where
                    (p => (p.BeginTime.Date <= e.Day.Date && p.EndTime.Date >= e.Day.Date)).Count() == 0)
                {
                    IList<JN_WorkingPlan> plans = JN_WorkingPlanBLL.GetModelList("'" + e.Day.Date.ToString("yyyy-MM-dd") +
                    "' BETWEEN BeginDate AND EndDate AND Staff=" + ViewState["Staff"].ToString());
                    if (plans.Count > 0)
                    {
                        IList<JN_WorkingPlanDetail> plandetails = new JN_WorkingPlanBLL(plans[0].ID).Items;
                        ViewState["PlanDetails"] = plandetails;
                    }
                    else
                        ViewState["PlanDetails"] = null;
                }

                if (ViewState["PlanDetails"] != null)
                {
                    IList<JN_WorkingPlanDetail> plandetails = (IList<JN_WorkingPlanDetail>)ViewState["PlanDetails"];

                    BulletedList bt = new BulletedList();

                    foreach (JN_WorkingPlanDetail plan in plandetails.Where(p => (p.BeginTime.Date <= e.Day.Date && p.EndTime.Date >= e.Day.Date)))
                    {
                        ListItem item = new ListItem();
                        item.Text = "";

                        if (plan.WorkingClassify == 0) continue;
                        item.Text += DictionaryBLL.GetDicCollections("OA_WorkingClassify")[plan.WorkingClassify.ToString()].Name + " ";
                        switch (plan.WorkingClassify)
                        {
                            case 1://门店拜访
                                if (plan.RelateClient != 0)
                                {
                                    //门店拜访
                                    CM_Client client = new CM_ClientBLL(plan.RelateClient).Model;
                                    if (client != null) item.Text += client.ShortName + " ";
                                }
                                break;
                            case 2: //协同拜访
                                if (plan.RelateStaff != 0)
                                {
                                    Org_Staff staff = new Org_StaffBLL(plan.RelateStaff).Model;
                                    if (staff != null) item.Text += staff.RealName + " ";
                                }
                                break;
                            default:
                                item.Text += plan.Description + " ";
                                break;
                        }
                        if (plan.OfficialCity > 1)
                            item.Text += TreeTableBLL.GetFullPathName("MCS_SYS.dbo.Addr_OfficialCity", plan.OfficialCity).Replace("->", "");
                        bt.Items.Add(item);
                    }
                    bt.DisplayMode = BulletedListDisplayMode.Text;
                    bt.BulletImageUrl = "~/Images/gif/gif-0163.gif";
                    bt.BulletStyle = BulletStyle.CustomImage;
                    bt.CssClass = "calaitem";
                    e.Cell.Controls.Add(bt);
                }
            }
            #endregion
        }
    }
 protected void Page_Init(object sender, EventArgs e)
 {
     JN_WorkingPlanBLL bll = new JN_WorkingPlanBLL(int.Parse(Request.QueryString["PlanID"]));
     InitGridView(bll.Model.BeginDate, bll.Model.EndDate);
 }