public IHttpActionResult GetMealCalendar([FromUri]ParmGetMealCalendar parm)
        {
            MonthObject Mobj = null;
            DateTime standard_day = DateTime.Parse(parm.year + "/" + parm.month + "/1");//基準日期(以此天為標準產生月曆)

            using (db0 = getDB0())
            {

                //取得該月第一天及最後一天
                var getCalendarFirstDay = standard_day.CalendarFirstDay();
                var getCalendarLastDay = standard_day.CalendarLastDay();

                //取得該月天數
                var getDateSection = (getCalendarLastDay - getCalendarFirstDay).TotalDays + 1;

                var Yesterday = DateTime.Parse(DateTime.Now.ToShortDateString()).AddDays(-1);
                bool check_meal_start = db0.DailyMeal.Any(x => x.meal_day < Yesterday &&
                                             x.record_deatil_id == parm.record_deatil_id);

                Mobj = new MonthObject()
                {
                    year = standard_day.Year,
                    month = standard_day.Month,
                    isMealStart = check_meal_start
                };

                List<WeekObject> wObj = new List<WeekObject>();
                WeekObject weekObject = null;
                List<DayObject> dObj = new List<DayObject>();



                for (int i = 0; i < getDateSection; i++)
                {
                    var setDayObj = getCalendarFirstDay.AddDays(i);

                    if (setDayObj.DayOfWeek == DayOfWeek.Sunday) //遇到星期日 要製作新的Week物件
                    {
                        if (dObj.Count() > 0) //製作Week物件時 要將之前的資料取出來,但第一排第一天這時是不會有日期資料
                        {
                            weekObject.dayInfo = dObj.ToArray();
                            dObj.Clear();
                            wObj.Add(weekObject);
                        }

                        weekObject = new WeekObject(); //產生新的week物件
                    }
                    var check_meal = db0.DailyMeal.Where(x => x.meal_day.Year == setDayObj.Year &&
                                                             x.meal_day.Month == setDayObj.Month &&
                                                             x.meal_day.Day == setDayObj.Day &&
                                                             x.record_deatil_id == parm.record_deatil_id).FirstOrDefault();
                    bool haveMeal = false;
                    MealState bState = MealState.NotShow, lState = MealState.NotShow, dState = MealState.NotShow;
                    int? daily_meal_id = null;
                    if (check_meal != null)
                    {
                        haveMeal = true;
                        bState = (MealState)check_meal.breakfast_state;
                        lState = (MealState)check_meal.lunch_state;
                        dState = (MealState)check_meal.dinner_state;
                        daily_meal_id = check_meal.daily_meal_id;
                    }
                    dObj.Add(new DayObject()
                    {
                        meal_day = setDayObj,
                        isNowMonth = (setDayObj >= standard_day.MonthFirstDay() && setDayObj <= standard_day.MonthLastDay()),
                        isHaveMeal = haveMeal,
                        breakfast = bState,
                        lunch = lState,
                        dinner = dState,
                        record_deatil_id = parm.record_deatil_id,
                        daily_meal_id = daily_meal_id
                    });
                }

                if (dObj.Count() > 0)
                {
                    weekObject.dayInfo = dObj.ToArray();
                    dObj.Clear();
                    wObj.Add(weekObject);
                }

                Mobj.weekInfo = wObj.ToArray();

                return Ok(Mobj);
            }
        }
        public string getCalendarInfo()
        {
            rAjaxGetData<ApplyCalendar> rAjaxResult = new rAjaxGetData<ApplyCalendar>();

            try
            {
                ApplyCalendar calObj = null;
                var getLogic = openLogic();

                using (db0 = getLogic.getDB0)
                {

                    //參數:取得申請日期最大期限
                    int get_N_Max = (int)getLogic.getParmValue(ParmDefine.bufferNorth_Max);
                    int get_S_Max = (int)getLogic.getParmValue(ParmDefine.bufferSouth_Max);

                    int get_Apply_Max_Day = get_N_Max >= get_S_Max ? get_N_Max : get_S_Max;

                    //參數:取得申請人數最大上限
                    int get_N_num = (int)getLogic.getParmValue(ParmDefine.N_Max_joinnum);
                    int get_S_num = (int)getLogic.getParmValue(ParmDefine.S_Max_joinnum);

                    DateTime setWorkFistDay = DateTime.Now.AddDays(15);//提前七天才可申請
                    DateTime setWokrLastDay = setWorkFistDay.AddDays(get_Apply_Max_Day);

                    var item_apply = db0.Apply
                        .Where(x => x.event_strattime != null &&
                                  (DateTime)x.event_strattime >= setWorkFistDay &&
                                  (DateTime)x.event_strattime <= setWokrLastDay &&
                                   (x.audit_state == CodeSheet.audit_state.notaudit.Code ||
                                   x.audit_state == CodeSheet.audit_state.pass.Code))
                        .Select(x => new { x.apply_id, x.event_strattime, x.event_area, x.join_num, x.audit_state }).ToList(); //加ToList 強制把資料先讀取出來。

                    var item_calendar = db0.CalendarDate
                        .Where(x => x.set_date >= setWorkFistDay &&
                                    x.set_date <= setWokrLastDay)
                        .Select(x => new { x.calendardate_id, x.set_date, x.state, x.eventarea_code }).ToList();

                    #region 日期UI資料排列處理

                    var getCalendarFirstDay = setWorkFistDay.WeekFirstDay();
                    var getCalendarLastDay = setWokrLastDay.WeekLastDay();

                    var getDateSection = (getCalendarLastDay - getCalendarFirstDay).TotalDays + 1;

                    calObj = new ApplyCalendar()
                    {
                        year = DateTime.Now.Year,
                        month = DateTime.Now.Month
                    };

                    List<WeekObject> wObj = new List<WeekObject>();
                    WeekObject weekObject = null;

                    List<DayObject> dObj = new List<DayObject>();

                    for (int i = 0; i < getDateSection; i++)
                    {

                        var setDayObj = getCalendarFirstDay.AddDays(i);

                        if (setDayObj.DayOfWeek == DayOfWeek.Sunday) //遇到星期日 要製作新的Week物件
                        {
                            if (dObj.Count() > 0) //製作Week物件時 要將之前的資料取出來,但第一排第一天這時是不會有日期資料
                            {
                                weekObject.dayInfo = dObj.ToArray();
                                dObj.Clear();
                                wObj.Add(weekObject);
                            }

                            weekObject = new WeekObject(); //產生新的week物件
                        }

                        int getNCount = item_apply.Where(x => ((DateTime)x.event_strattime).Year == setDayObj.Year &&
                                                               ((DateTime)x.event_strattime).Month == setDayObj.Month &&
                                                               ((DateTime)x.event_strattime).Day == setDayObj.Day &&
                                                               (x.audit_state == CodeSheet.audit_state.notaudit.Code ||
                                                                x.audit_state == CodeSheet.audit_state.pass.Code) &&
                                                                x.event_area == CodeSheet.apply_area.bufferNorth.Code.CInt())
                                                  .Sum(x => x.join_num);
                        int getSCount = item_apply.Where(x => ((DateTime)x.event_strattime).Year == setDayObj.Year &&
                                                               ((DateTime)x.event_strattime).Month == setDayObj.Month &&
                                                               ((DateTime)x.event_strattime).Day == setDayObj.Day &&
                                                               (x.audit_state == CodeSheet.audit_state.notaudit.Code ||
                                                                x.audit_state == CodeSheet.audit_state.pass.Code) &&
                                                              x.event_area == CodeSheet.apply_area.bufferSouth.Code.CInt())
                                                  .Sum(x => x.join_num);
                        var NDayState = item_calendar.Where(x => x.set_date.Year == setDayObj.Year &&
                                                            x.set_date.Month == setDayObj.Month &&
                                                            x.set_date.Day == setDayObj.Day &&
                                                            x.eventarea_code == CodeSheet.apply_area.bufferNorth.Code.CInt());
                        var SDayState = item_calendar.Where(x => x.set_date.Year == setDayObj.Year &&
                                                                 x.set_date.Month == setDayObj.Month &&
                                                                 x.set_date.Day == setDayObj.Day &&
                                                                 x.eventarea_code == CodeSheet.apply_area.bufferSouth.Code.CInt());
                        int Nstate = 0;
                        int Sstate = 0;
                        if (NDayState.Any())
                        {
                            Nstate = 1;
                        }
                        if (SDayState.Any())
                        {
                            Sstate = 1;
                        }

                        dObj.Add(new DayObject()
                        {
                            apply_date = setDayObj,
                            isNowMonth = (setDayObj >= setWorkFistDay && setDayObj <= setWokrLastDay),
                            EventAreaNorth = new ApplyInfo() { code = CodeSheet.apply_area.bufferNorth.Code.CInt(), left_available = get_N_num - getNCount, dayState = Nstate },
                            EventAreaSouth = new ApplyInfo() { code = CodeSheet.apply_area.bufferSouth.Code.CInt(), left_available = get_S_num - getSCount, dayState = Sstate },
                        });
                    }

                    if (dObj.Count() > 0)
                    {
                        weekObject.dayInfo = dObj.ToArray();
                        dObj.Clear();
                        wObj.Add(weekObject);
                    }

                    calObj.weekInfo = wObj.ToArray();
                    rAjaxResult.data = calObj;
                    #endregion
                }

                rAjaxResult.result = true;
                return defJSON(rAjaxResult);
            }
            catch (Exception ex)
            {
                rAjaxResult.result = false;
                rAjaxResult.message = ex.Message;
                return defJSON(rAjaxResult);
            }
        }