iT邦幫忙

0

排班表問題再次請教

https://ithelp.ithome.com.tw/upload/images/20190618/201101320t7gdxARXR.jpg

這個是我目前的總畫面
問了這兩位大大後

https://ithelp.ithome.com.tw/questions/10194041
https://ithelp.ithome.com.tw/questions/10194089

目前已經產生畫面,也可成功拖拉,而我自己則是拖拉後成功地將資料存進資料庫
目前剩下最後一個問題,雖然可拖拉也可將拖拉的資料存進資料庫,但是重新載入
後畫面就消失,所以想請問這部分我該如何處理?目前我有抓到存進去的資料
就剩下如何把它顯示出來

controller

 public ActionResult DrSchedule(DrScheduleViewModel model)
        {
           
            UserInfoSession user = SessionUserInfo();
            DrScheduleService  svc = new DrScheduleService();
            //班表上人員
            model.DrEmp = svc.FindDrsch(user.CID);
            //醫師名單
            model.DrEmpList= svc.FindDrScheduleByMonth(user.CID, "");
  
            return View(model);
        }

FindDrsch方法

  public List<DrScheduleDto> FindDrsch(string cid)
        {
            var query = (from x in _repo.DR_SCHEDULE
                         where x.CID == cid &&
                         x.SCH_DATE.Month == DateTime.Today.Month
                         select new DrScheduleDto
                         {
                             GUID = x.GUID,
                             CID = x.CID,
                             SCH_DATE = x.SCH_DATE,
                             SCH_SPELL = x.SCH_SPELL,
                             DR_EMPNO = x.DR_EMPNO,
                             CRE_DATE = x.CRE_DATE,
                             CRE_UID = x.CRE_UID,
                             MOD_DATE = x.MOD_DATE,
                             MOD_UID = x.MOD_UID
                         }).ToList<DrScheduleDto>();
            foreach (DrScheduleDto dto in query)
            {
                dto.EMP_NAME = _repo.EMPLOYEE.Where(x => x.CID == dto.CID && x.EMP_ID == dto.DR_EMPNO).FirstOrDefault<EMPLOYEE>().EMP_NAME;
            }

            return query;
        }

DR_SCHEDULE的資料類型
https://ithelp.ithome.com.tw/upload/images/20190620/20110132Xd5pdR0a6I.jpg

屬性

public partial class DR_SCHEDULE
    {
        public string GUID { get; set; }
        public string CID { get; set; }
        public System.DateTime SCH_DATE { get; set; }
        public string SCH_SPELL { get; set; }
        public string DR_EMPNO { get; set; }
        public Nullable<System.DateTime> CRE_DATE { get; set; }
        public string CRE_UID { get; set; }
        public Nullable<System.DateTime> MOD_DATE { get; set; }
        public string MOD_UID { get; set; }
        public string BED_AREA_ID { get; set; }
        public string BED_NO { get; set; }
    }

https://ithelp.ithome.com.tw/upload/images/20190620/20110132db6Df3No8s.jpg

html

<div class="container-fluid">
    <div class="row">
        <!--床位排程資訊-->
        <div class="col-md-10 panel-right panel-20 cal">
            <div class="box box-primary ">
                <div class="box-header">
                </div><!-- /.box-header -->
                <div id="schTable">
                    <table id="dataTable" class="table table-bordered table-hover caltable" style="margin: 0px 0px;">
                        <tbody id="dataTbody">
                            @foreach (var row in DateForHelper.getMonthArray(@Model.Year, @Model.Month, @Model.DrEmp))
                            {

                                <tr>
                                    <th class="sch-left-th"></th>
                                    @foreach (string text in row)
                                    {
                                        var arr = text.Split('&');

                                        if (!String.IsNullOrEmpty(text))
                                        {
                                            <th colspan="3" class="th-weekday"><span class="th-date">@arr[0] @arr[1]</span></th>
                                        }
                                        else
                                        {
                                            <th colspan="3" rowspan="3" class="th-weekday"></th>
                                        }
                                    }
                                </tr>
                                <tr>
                                    <th class="sch-left-th">時段</th>
                                    @foreach (string text in row)
                                    {
                                        if (!String.IsNullOrEmpty(text))
                                        {
                                            <th>早</th>
                                            <th>午</th>
                                            <th>晚</th>
                                        }
                                    }
                                </tr>
                                <tr>
                                    <th class="sch-left-th">排班醫師</th>
                                    @foreach (string text in row)
                                    {
                                        if (!String.IsNullOrEmpty(text))
                                        {

                                            var arr = text.Split('&');
                                            //if (arr[0]==emplist.day)
                                            <td id="spell-1" data-id="@arr[0]"></td>
                                            <td id="spell-2" data-id="@arr[0]"></td>
                                            <td id="spell-3" data-id="@arr[0]"></td>
                                        }
                                        @*else
                                            {
                                                var arr = text.Split('&');
                                                <td id="spell-1" data-id="@arr[0]"></td>
                                                <td id="spell-2" data-id="@arr[0]"></td>
                                                <td id="spell-3" data-id="@arr[0]"></td>
                                            }*@
                                    }
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
                <!-- /.box-body -->
            </div>
            <!-- /.box -->
        </div>
        <!--醫師資訊-->
        <div class="col-md-2 panel-right panel-20 doc">
            <div class="box box-primary">
                <div class="box-header">
                </div><!-- /.box-header -->
                <div class="box-body">
                    <div class="lstdiv">
                        <table id="NameTable" class="table table-bordered table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>醫師名單</th>
                                </tr>
                            </thead>
                            <tbody id="NameTbody">
                                @if (Model.DrEmpList.Count > 0)
                                {
                                    foreach (var item in Model.DrEmpList)
                                    {
                                        <tr>
                                            <td class="fn-item td-draggable name" id="@item.DR_EMPNO" name="@item.EMP_NAME" draggable="true">@item.EMP_NAME</td>
                                        </tr>
                                    }
                                }
                            </tbody>
                        </table>
                    </div>
                </div>
                <!-- /.box-body -->
            </div><!-- /.box -->
        </div>  <!-- /.col -->
    </div>
</div>

getMonthArray方法

 public static string[][] getMonthArray(int year, int month ,List<DrScheduleDto> dto)
        {
            string[] weekArray = new string[] { "日", "一", "二", "三", "四", "五", "六" };
            DateTime dt = new DateTime(year, month, 1, 0, 0, 0);
            int lastDay = dt.AddMonths(1).AddDays(-1).Day; //該月最後一天
            int wDay = (int)dt.DayOfWeek; //該月第一天是星期幾
            int nRows = (wDay + lastDay + 6) / 7; //計算共需要幾列來顯示
            string[][] result = new string[nRows][];
       
           
            for (int i = 0; i < nRows; ++i)
            {
                result[i] = new string[7];
                for (int j = 0; j < 7; ++j)
                {
                    
                    int d = i * 7 + j - wDay + 1;                   
                    if (d > 0 && d <= lastDay)
                    {
                        result[i][j] = year + "/" + month + "/" + d +"&"+ weekArray[(d - 1 + wDay) % 7];                                                 
                    }
                    else
                    {
                        result[i][j] = "";
                    }
                }
            }
            return result;
        }
dragonH iT邦超人 5 級 ‧ 2019-06-18 15:49:16 檢舉
C# 我不熟

也沒看到你的資料是如何存的

不過你的資料應該至少會有存

1 . 日期

2 . 班別

這兩個資料吧

如果有的話

基本上就是畫表格的時候就一起畫出來了

寫個判斷之類的
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
Ks
iT邦新手 3 級 ‧ 2019-06-21 16:02:50
最佳解答

由於你的代碼提供的代碼真的太亂了~

我只能用猜的和大概模擬了一段代碼

/images/emoticon/emoticon06.gif

針對以下代碼這段開始,

<th class="sch-left-th">排班醫師</th>

我先假設這段是你讀取出醫師每個月值班的代碼,

model.DrEmp = svc.FindDrsch(user.CID);

然後你的row是針對某星期的一個陣列, 所以會有那個星期七天的日期

你就可以把這個7天的日期來跟DrEmp這個list的排班日期做比較,

一樣就顯示就行了,試試看吧~

<tr>
    <th class="sch-left-th">排班醫師</th>
    @foreach (string text in row)
    {
        if (!String.IsNullOrEmpty(text))
        {
            var arr = text.Split('&');
            if(arr.length > 0)
            {
                DateTime getDateTime;
                bool IsTryParseDT = DateTime.TryParseExact(arr[0],
                    "yyyy/M/d", CultureInfo.InvariantCulture,
                    DateTimeStyles.None,
                    out getDateTime);
                if (!IsTryParseDT)
                {
                    <td colspan="3">日期格式錯誤,請聯絡管理員</td>
                }
                else
                {
                    //我先假設DrEmp是這個醫師這個月的所有班表時間
                    //model.DrEmp = svc.FindDrsch(user.CID);
                    var data = Model.DrEmp.where(x.SCH_DATE.Date == getDateTime);
                    //獲取今天的日期
                    <td id="spell-1" data-id="@arr[0]">
                        var spell1 = data.where(x.SCH_SPELL == "1").toList();
                        foreach(var item in spell1)
                        {
                            <p>@item.DR_EMPNO</p>
                        }
                    </td>
                    <td id="spell-2" data-id="@arr[0]">
                        var spell2 = data.where(x.SCH_SPELL == "2").toList();
                        foreach(var item in spell2)
                        {
                            <p>@item.DR_EMPNO</p>
                        }
                    </td>
                    <td id="spell-3" data-id="@arr[0]">
                        var spell3 = data.where(x.SCH_SPELL == "3").toList();
                        foreach(var item in spell3)
                        {
                            <p>@item.DR_EMPNO</p>
                        }
                    </td>
                }
            }
            else
            {
                <td id="spell-1" data-id="@arr[0]"></td>
                <td id="spell-2" data-id="@arr[0]"></td>
                <td id="spell-3" data-id="@arr[0]"></td>
            }
        }
    }
</tr>
看更多先前的回應...收起先前的回應...
tenno081 iT邦研究生 4 級 ‧ 2019-06-24 09:38:12 檢舉

感謝解答

目前是遇到這樣的狀況
https://ithelp.ithome.com.tw/upload/images/20190624/20110132YE8XPvQwy5.jpg

在foreach加@則變成
https://ithelp.ithome.com.tw/upload/images/20190624/20110132oSi6iFKSAb.jpg

能否請問我可以在getMonthArray方法裡回傳兩個值嗎?
一個是日期,一個是醫生的姓名

又或者是直接用AJAX來處理呢?

  public string[][] getMonthArray(int year, int month, List<DrScheduleDto> dto)
        {
            string[] weekArray = new string[] { "日", "一", "二", "三", "四", "五", "六" };
            DateTime dt = new DateTime(year, month, 1, 0, 0, 0);
            int lastDay = dt.AddMonths(1).AddDays(-1).Day; //該月最後一天
            int wDay = (int)dt.DayOfWeek; //該月第一天是星期幾
            int nRows = (wDay + lastDay + 6) / 7; //計算共需要幾列來顯示
            string[][] result = new string[nRows][];
            Dictionary<string, string> emplist = new Dictionary<string, string>();
            for (int i = 0; i < nRows; ++i)
            {
                result[i] = new string[7];
                for (var j = 0; j < 7; ++j)
                {

                    int d = i * 7 + j - wDay + 1;
                    //照日期排出
                    if (d > 0 && d <= lastDay)
                    {
                        result[i][j] = year + "/" + month + "/" + d + "&" + weekArray[(d - 1 + wDay) % 7];
                        string code = result[i][j];
                        string[] splitCode = code.Split('&');
                        var date = Convert.ToDateTime(splitCode[0]);
                        var DrEmpList = dto.Where(x => x.SCH_DATE == date).OrderBy(x => x.SCH_DATE).ThenBy(x => x.SCH_SPELL);

                        foreach (var r in DrEmpList)
                        {
                            Console.Write(r);
                            //emplist.Add("day", date.ToString());
                            //emplist.Add("spell", r.SCH_SPELL);
                            //emplist.Add("emp", r.EMP_NAME);
                        }
                    }
                    else
                    {
                        result[i][j] = "";
                    }
                }
            }
            return result;
        }

目前我只有暫時把排班的員工按照日期班別一起塞入foreach迴圈裡

Ks iT邦新手 3 級 ‧ 2019-06-24 11:44:05 檢舉

在var spell前面加上@試試看,如果不行就試試看

@{
    var spell1 = data.where(x.SCH_SPELL == "1").toList();
    foreach(var item in spell1)
    {
        <p>@item.DR_EMPNO</p>
     }
}

醫師的名字加入getMonthArray,

你是指這裏需要用到醫師名字嗎@item.DR_EMPNO

如果是的話,可以考虑在DrEmp這個table裡面加一個欄位,
把醫師名字裝進去
[NotMapped]
public string DrEmp { get; set; }

如果你的結構是資料表Mapping對應Dto的話處理的話,
就不需要[NotMapped]

tenno081 iT邦研究生 4 級 ‧ 2019-06-24 13:28:21 檢舉

太謝謝你了,這個問題真的處理完畢了,謝謝

tenno081 iT邦研究生 4 級 ‧ 2019-06-24 13:29:55 檢舉

另外想請問一下,我那時問其他人,他們是打算在result那裏
再回傳名字,然後用dictionary來實現,請問這有可能嗎?

Ks iT邦新手 3 級 ‧ 2019-06-24 16:55:15 檢舉

我也不確定耶

你如果有想到專案又不是很急的話就可以自己試試看吧

/images/emoticon/emoticon08.gif

我要發表回答

立即登入回答