From cbdd1057416d0d13a14f4bd8011efaee44db35b0 Mon Sep 17 00:00:00 2001
From: Marvin <marvinhong@163.com>
Date: Tue, 22 Mar 2022 16:21:43 +0800
Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E6=94=B9=E4=B8=8D=E8=89=AF=E7=8E=B0?=
 =?UTF-8?q?=E8=B1=A1=E5=8E=9F=E5=9B=A0=E7=BB=B4=E6=8A=A4=EF=BC=8C=E5=A2=9E?=
 =?UTF-8?q?=E5=8A=A0=E5=90=AF=E7=94=A8/=E5=81=9C=E7=94=A8=E7=8A=B6?=
 =?UTF-8?q?=E6=80=81=202.=E4=BF=AE=E6=94=B9=E8=89=AF=E7=8E=87=E6=9F=A5?=
 =?UTF-8?q?=E8=AF=A2=EF=BC=8C=E4=BF=AE=E6=94=B9=E8=89=AF=E7=8E=87=E8=AE=A1?=
 =?UTF-8?q?=E7=AE=97=E5=85=AC=E5=BC=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../Controllers/PPSController.cs              | 23 ++++-
 .../Controllers/QRSController.cs              | 99 ++++++++++++++++++-
 AMESCoreStudio.Web/HttpApis/AMES/IPPS.cs      |  9 +-
 AMESCoreStudio.Web/Views/PPS/PPS010.cshtml    | 58 ++++++++++-
 AMESCoreStudio.Web/Views/PPS/PPS010U.cshtml   |  1 +
 AMESCoreStudio.Web/Views/QRS/QRS010.cshtml    |  7 +-
 .../Controllers/AMES/NGReasonsController.cs   | 68 ++++++++++++-
 .../Controllers/AMES/WipClassController.cs    |  2 +-
 AMESCoreStudio.WebApi/Models/AMES/NGReason.cs |  9 ++
 9 files changed, 256 insertions(+), 20 deletions(-)

diff --git a/AMESCoreStudio.Web/Controllers/PPSController.cs b/AMESCoreStudio.Web/Controllers/PPSController.cs
index 45eb641f..e9e85eed 100644
--- a/AMESCoreStudio.Web/Controllers/PPSController.cs
+++ b/AMESCoreStudio.Web/Controllers/PPSController.cs
@@ -1162,6 +1162,24 @@ namespace AMESCoreStudio.Web.Controllers
             return View();
         }
 
+        [HttpPost]
+        public async Task<JsonResult> PPS010ActiveStopAsync(string ngReasonNo, string statusNo)
+        {
+            await _ppsApi.PutNGReasonStatus(ngReasonNo, statusNo);
+
+            string _msg;
+
+            if (statusNo == "A")
+            {
+                _msg = "啟用成功!";
+            }
+            else
+            {
+                _msg = "停用成功!";
+            }
+            return Json(_msg);
+        }
+
         //新增頁面
         public async Task<IActionResult> PPS010C(string id)
         {
@@ -1273,12 +1291,11 @@ namespace AMESCoreStudio.Web.Controllers
                 ngClassNo = "*";
             }
 
-            var result_total = await _ppsApi.GetNGReasonsByClass(ngClassNo, 0, limit);
             var result = await _ppsApi.GetNGReasonsByClass(ngClassNo, page,limit);
 
-            if (result.Count > 0)
+            if (result.DataTotal > 0)
             {
-                return Json(new Table() { code = 0, msg = "", data = result, count = result_total.Count });
+                return Json(new Table() { code = 0, msg = "", data = result.Data, count = result.DataTotal });
             }
 
             return Json(new Table() { count = 0, data = null });
diff --git a/AMESCoreStudio.Web/Controllers/QRSController.cs b/AMESCoreStudio.Web/Controllers/QRSController.cs
index e63be453..d31293cb 100644
--- a/AMESCoreStudio.Web/Controllers/QRSController.cs
+++ b/AMESCoreStudio.Web/Controllers/QRSController.cs
@@ -7,6 +7,7 @@ using Newtonsoft.Json.Linq;
 using AMESCoreStudio.Web.Models;
 using System.Collections.Generic;
 using Microsoft.AspNetCore.Mvc.Rendering;
+using AMESCoreStudio.Web.ViewModels.QRS;
 
 namespace AMESCoreStudio.Web.Controllers
 {
@@ -467,13 +468,59 @@ namespace AMESCoreStudio.Web.Controllers
 
         public async Task<IActionResult> QRS010QueryAsync(string unitNo, string lineID, string stationID, string mfgTypeNo, string wipNO, string itemNO,  string dateStart, string dateEnd, int page = 0, int limit = 10)
         {
-
             var result = await _pcsApi.GetYieldData4QRS010(unitNo, lineID, stationID, mfgTypeNo, wipNO, itemNO, dateStart, dateEnd, page, limit);
 
+            var query = new QRS010ViewModel();
+
+            int failQty = 0;
+            foreach (var data in result.Data)
+            {
+                JObject jo = JObject.Parse(data.ToString());
+
+                if (jo["ruleStatus"].ToString() == "P")
+                {
+                    int pass_cnt = 0;
+                    foreach (var item in result.Data)
+                    {
+                        JObject joItem = JObject.Parse(item.ToString());
+                        
+                        if (jo["ruleStationID"].ToString() == joItem["ruleStationID"].ToString() && joItem["ruleStatus"].ToString() == "F")
+                        {
+                            pass_cnt = int.Parse(jo["firstCnt"].ToString()) + int.Parse(joItem["firstCnt"].ToString());
+                            failQty = failQty + 1;
+                            break;
+                        }
+                        
+                    }
+
+                    if (pass_cnt == 0)
+                    {
+                        pass_cnt = int.Parse(jo["passCnt"].ToString());
+                    }
+
+                    query.YieldDatas.Add(new YieldData
+                    {
+                        WipID = int.Parse(jo["wipID"].ToString()),
+                        WipNO = jo["wipNO"].ToString(),
+                        PlanQTY = int.Parse(jo["planQTY"].ToString()),
+                        RuleStationID = int.Parse(jo["ruleStationID"].ToString()),
+                        StationID = int.Parse(jo["stationID"].ToString()),
+                        StationDesc = jo["stationDesc"].ToString(),
+                        ItemNO = jo["itemNO"].ToString(),
+                        LineDesc = jo["lineDesc"].ToString(),
+                        ModelNO = jo["modelNO"].ToString(),
+                        FirstCnt = int.Parse(jo["firstCnt"].ToString()),
+                        PassCnt = pass_cnt,
+                        Yield = int.Parse(jo["firstCnt"].ToString()) * 1.0 / pass_cnt * 100.0
+                    });
+                }
+            }
+
             if (result.DataTotal > 0)
             {
-                return Json(new Table() { code = 0, msg = "", data = result.Data, count = result.DataTotal });
+                return Json(new Table() { code = 0, msg = "", data = query.YieldDatas, count = result.DataTotal - failQty });
             }
+
             return Json(new Table() { count = 0, data = null });
         }
 
@@ -490,7 +537,53 @@ namespace AMESCoreStudio.Web.Controllers
 
             var result = await _pcsApi.GetYieldData4QRS010(param[0], param[1], param[2], param[3], param[4], param[5], param[6], param[7], 0, 10);
 
-            return Json(result.Data);
+            var query = new QRS010ViewModel();
+
+            int failQty = 0;
+            foreach (var data in result.Data)
+            {
+                JObject jo = JObject.Parse(data.ToString());
+
+                if (jo["ruleStatus"].ToString() == "P")
+                {
+                    int pass_cnt = 0;
+                    foreach (var item in result.Data)
+                    {
+                        JObject joItem = JObject.Parse(item.ToString());
+
+                        if (jo["ruleStationID"].ToString() == joItem["ruleStationID"].ToString() && joItem["ruleStatus"].ToString() == "F")
+                        {
+                            pass_cnt = int.Parse(jo["firstCnt"].ToString()) + int.Parse(joItem["firstCnt"].ToString());
+                            failQty = failQty + 1;
+                            break;
+                        }
+
+                    }
+
+                    if (pass_cnt == 0)
+                    {
+                        pass_cnt = int.Parse(jo["passCnt"].ToString());
+                    }
+
+                    query.YieldDatas.Add(new YieldData
+                    {
+                        WipID = int.Parse(jo["wipID"].ToString()),
+                        WipNO = jo["wipNO"].ToString(),
+                        PlanQTY = int.Parse(jo["planQTY"].ToString()),
+                        RuleStationID = int.Parse(jo["ruleStationID"].ToString()),
+                        StationID = int.Parse(jo["stationID"].ToString()),
+                        StationDesc = jo["stationDesc"].ToString(),
+                        ItemNO = jo["itemNO"].ToString(),
+                        LineDesc = jo["lineDesc"].ToString(),
+                        ModelNO = jo["modelNO"].ToString(),
+                        FirstCnt = int.Parse(jo["firstCnt"].ToString()),
+                        PassCnt = pass_cnt,
+                        Yield = int.Parse(jo["firstCnt"].ToString()) * 1.0 / pass_cnt * 100.0
+                    });
+                }
+            }
+
+            return Json(query.YieldDatas);
         }
 
         public async Task<IActionResult> GetYieldData4QRS010ListAsync(string id)
diff --git a/AMESCoreStudio.Web/HttpApis/AMES/IPPS.cs b/AMESCoreStudio.Web/HttpApis/AMES/IPPS.cs
index c960e17e..f9b606a3 100644
--- a/AMESCoreStudio.Web/HttpApis/AMES/IPPS.cs
+++ b/AMESCoreStudio.Web/HttpApis/AMES/IPPS.cs
@@ -361,6 +361,13 @@ namespace AMESCoreStudio.Web
         [WebApiClient.Attributes.HttpPut("api/NGReasons/{id}")]
         ITask<ResultModel<NGReason>> PutNGReason(string id, [FromBody, RawJsonContent] string model);
 
+        /// <summary>
+        /// 更新不良現象原因
+        /// </summary>
+        /// <returns></returns>
+        [WebApiClient.Attributes.HttpPut("api/NGReasons/{id}/{statusno}")]
+        ITask<ResultModel<NGReason>> PutNGReasonStatus(string id, string statusno);
+
         /// <summary>
         /// 刪除不良現象原因
         /// </summary>
@@ -387,7 +394,7 @@ namespace AMESCoreStudio.Web
         /// </summary>
         /// <returns></returns>
         [WebApiClient.Attributes.HttpGet("api/NGReasons/Class/{no}")]
-        ITask<List<NGReason>> GetNGReasonsByClass(string no, int page = 0, int limit = 10);
+        ITask<ResultModel<NGReason>> GetNGReasonsByClass(string no, int page = 0, int limit = 10);
 
         #endregion
 
diff --git a/AMESCoreStudio.Web/Views/PPS/PPS010.cshtml b/AMESCoreStudio.Web/Views/PPS/PPS010.cshtml
index c83835ff..ad2e8287 100644
--- a/AMESCoreStudio.Web/Views/PPS/PPS010.cshtml
+++ b/AMESCoreStudio.Web/Views/PPS/PPS010.cshtml
@@ -40,6 +40,11 @@
 </div>
 
 @section Scripts{
+
+    <script type="text/html" id="activeStopTpl">
+        <input type="checkbox" name="ACTIVE" value="{{d.ngReasonNo}}" lay-skin="switch" lay-text="Active|Stop" lay-filter="ACTIVE" {{ d.status == "A" ? 'checked' : '' }}>
+    </script>
+
     <script type="text/javascript">
 
         function getNGClassList(data)
@@ -59,7 +64,7 @@
                             $("#ngClass").append(new Option(item.text, item.value));// 下拉菜单里添加元素
                             //$("#errorClass").append($("<option>").text(item.text).val(item.value))
                         });
-                        
+
                         layui.form.render("select");//重新渲染 固定写法
                     },
                     error: function (result)
@@ -87,6 +92,41 @@
                 var qs = $('button[lay-filter="querysubmit"]');
                 qs.click();
             });
+
+            //監聽Actice/Stop操作
+            form.on('switch(ACTIVE)', function (obj) {
+                var checked = obj.elem.checked;
+                var ng_reason_no = this.value.split('-');
+                if (checked)
+                    statusNo = "A"
+                else
+                    statusNo = "S"
+
+                var confirmMsg = confirm('您確認啟用/停用該不良現象代碼?');
+
+                if (confirmMsg) {
+                    $.ajax({
+                        url: '@Url.Action("PPS010ActiveStop", "PPS")',
+                        dataType: 'json',
+                        data: { "ngReasonNo": ng_reason_no, "statusNo": statusNo},
+                        cache: false,
+                        type: "POST",
+                        success: function (data, textStatus, jqXHR) {
+                            var qs = $('button[lay-filter="querysubmit"]');
+                            qs.click();
+                        },
+                        error: function (jqXHR, textStatus, errorThrown) {
+                            alert("Found error when using Ajax!!");
+                        }
+                    });
+                }
+                else {
+                    if (checked)
+                        obj.elem.checked = false; else obj.elem.checked = true;
+                    form.render();
+                }
+                //layer.tips(this.value + ' ' + this.name + ':' + obj.elem.checked, obj.othis);
+            });
         });
 
         //监听表单提交事件
@@ -96,9 +136,14 @@
         var tableCols = [[
             {
                 field: 'ngReasonNo',
-                width: 200,
+                width: 160,
                 title: '不良現象原因代碼'
             },
+            {
+                field: 'ngClassNo',
+                width: 160,
+                title: '不良現象類別代碼'
+            },
             {
                 field: 'ngReasonDesc',
                 width: 400,
@@ -108,13 +153,20 @@
                 field: 'ngReasonDescEn',
                 title: '不良現象原因英文描述'
             },
+            {
+                field: 'status',
+                title: '啟用/停用',
+                width: 120,
+                templet: '#activeStopTpl',
+                unresize: true
+            },
             {
                 field: 'right',
                 width: 200,
                 title: '操作',
                 fixed: 'right',
                 templet: function (d) {
-                    return '<a class="layui-btn layui-btn-normal layui-btn-xs layui-icon layui-icon-edit" lay-event="edit">修改</a> <a class="layui-btn layui-btn-danger layui-btn-xs layui-icon layui-icon-delete" lay-event="del">删除</a>'
+                    return '<a class="layui-btn layui-btn-normal layui-btn-xs layui-icon layui-icon-edit" lay-event="edit">修改</a> '
                 }
             }]
         ];
diff --git a/AMESCoreStudio.Web/Views/PPS/PPS010U.cshtml b/AMESCoreStudio.Web/Views/PPS/PPS010U.cshtml
index a32c6276..b7e495d6 100644
--- a/AMESCoreStudio.Web/Views/PPS/PPS010U.cshtml
+++ b/AMESCoreStudio.Web/Views/PPS/PPS010U.cshtml
@@ -15,6 +15,7 @@
     <div class="col-sm-12">
         <form enctype="multipart/form-data" method="post" asp-action="PPS010USave">
             <div asp-validation-summary="ModelOnly" class="text-danger"></div>
+            <input type="hidden" asp-for="Status" />
 
             <div class="form-group form-inline my-sm-1">
                 <label asp-for="NGReasonNo" class="control-label col-sm-3"></label>
diff --git a/AMESCoreStudio.Web/Views/QRS/QRS010.cshtml b/AMESCoreStudio.Web/Views/QRS/QRS010.cshtml
index c65c80ca..0d0f2dae 100644
--- a/AMESCoreStudio.Web/Views/QRS/QRS010.cshtml
+++ b/AMESCoreStudio.Web/Views/QRS/QRS010.cshtml
@@ -305,12 +305,6 @@
                 title: '站別',
                 sort: true
             },
-            {
-                field: 'ruleStatus',
-                title: '過站狀態',
-                width: 100,
-                sort: true
-            },
             {
                 field: 'firstCnt',
                 title: '首次過站數量',
@@ -336,3 +330,4 @@
 
     </script>
 }
+                                                                                                                                                                                             
\ No newline at end of file
diff --git a/AMESCoreStudio.WebApi/Controllers/AMES/NGReasonsController.cs b/AMESCoreStudio.WebApi/Controllers/AMES/NGReasonsController.cs
index 01e73733..14b0b1f3 100644
--- a/AMESCoreStudio.WebApi/Controllers/AMES/NGReasonsController.cs
+++ b/AMESCoreStudio.WebApi/Controllers/AMES/NGReasonsController.cs
@@ -54,8 +54,10 @@ namespace AMESCoreStudio.WebApi.Controllers.AMES
         /// <returns></returns>
         // GET: api/NGReasons/Class/S
         [HttpGet("Class/{no}")]
-        public async Task<ActionResult<IEnumerable<NGReason>>> GetNGReasonByClass(string no, int page = 0, int limit = 10)
+        public async Task<ResultModel<NGReason>> GetNGReasonByClass(string no, int page = 0, int limit = 10)
         {
+            ResultModel<NGReason> result = new ResultModel<NGReason>();
+
             IQueryable<NGReason> q = _context.NGReasons;
 
             if (no != null)
@@ -66,6 +68,8 @@ namespace AMESCoreStudio.WebApi.Controllers.AMES
                 }
             }
 
+            result.DataTotal = q.ToList().Count;
+
             if (page > 0)
             {
                 q = q.OrderBy(p => p.NGClassNo + p.NGReasonNo).Skip((page - 1) * limit).Take(limit);
@@ -77,12 +81,18 @@ namespace AMESCoreStudio.WebApi.Controllers.AMES
             
             var ngReason = await q.ToListAsync();
 
+            result.Data = ngReason;
+
             if (ngReason == null)
             {
-                return NotFound();
+                result.Msg = "查無資料";
+                result.Success = false;
+                return result;
             }
 
-            return ngReason;
+            result.Success = true;
+            result.Msg = "OK";
+            return result;
         }
 
         /// <summary>
@@ -153,6 +163,58 @@ namespace AMESCoreStudio.WebApi.Controllers.AMES
             return result;
         }
 
+        /// <summary>
+        /// 更新不良現象原因狀態
+        /// </summary>
+        /// <param name="id"></param>
+        /// <param name="statusno"></param>
+        /// <returns></returns>
+        // PUT: api/RuleStations/5
+        // To protect from overposting attacks, enable the specific properties you want to bind to, for
+        // more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
+        [HttpPut("{id}/{statusno}")]
+        public async Task<ResultModel<NGReason>> PutNGReasonStatus(string id, string statusno = null)
+        {
+            ResultModel<NGReason> result = new ResultModel<NGReason>();
+
+            var ngReason = await _context.NGReasons.FindAsync(id);
+            if (ngReason == null)
+            {
+                result.Success = false;
+                result.Msg = "原因代碼不存在";
+                return result;
+            }
+
+            ngReason.Status = statusno;
+
+            _context.NGReasons.Attach(ngReason);
+
+            // 指定更新某個欄位
+            _context.Entry(ngReason).Property(p => p.Status).IsModified = true;
+
+            try
+            {
+                await _context.SaveChangesAsync();
+            }
+            catch (DbUpdateConcurrencyException)
+            {
+                if (!NGReasonExists(id))
+                {
+                    result.Success = false;
+                    result.Msg = "原因代碼不存在";
+                    return result;
+                }
+                else
+                {
+                    throw;
+                }
+            }
+
+            result.Success = true;
+            result.Msg = "OK";
+            return result;
+        }
+
         /// <summary>
         /// 
         /// </summary>
diff --git a/AMESCoreStudio.WebApi/Controllers/AMES/WipClassController.cs b/AMESCoreStudio.WebApi/Controllers/AMES/WipClassController.cs
index 987cf7e9..bce5e22c 100644
--- a/AMESCoreStudio.WebApi/Controllers/AMES/WipClassController.cs
+++ b/AMESCoreStudio.WebApi/Controllers/AMES/WipClassController.cs
@@ -250,7 +250,7 @@ namespace AMESCoreStudio.WebApi.Controllers.AMES
                 PassCnt = y.Sum(x => x.PassCnt),
                 Yield = (y.Sum(x => x.FirstCnt) / y.Sum(x => x.PassCnt) * 100)
             });
-
+            
             //紀錄筆數
             result.DataTotal = g.Count();
 
diff --git a/AMESCoreStudio.WebApi/Models/AMES/NGReason.cs b/AMESCoreStudio.WebApi/Models/AMES/NGReason.cs
index 79697987..bb1728bb 100644
--- a/AMESCoreStudio.WebApi/Models/AMES/NGReason.cs
+++ b/AMESCoreStudio.WebApi/Models/AMES/NGReason.cs
@@ -62,5 +62,14 @@ namespace AMESCoreStudio.WebApi.Models.AMES
         [Display(Name = "責任單位")]
         public int ResponseDept { get; set; }
 
+        /// <summary>
+        /// 状态
+        /// </summary>
+        [Column("STATUS")]
+        [Required(ErrorMessage = "{0},不能空白")]
+        [Display(Name = "状态")]
+        [StringLength(100, ErrorMessage = "{0},不能大于{1}")]
+        [DataMember]
+        public string Status { get; set; } = "A";
     }
 }