From 4a12a7d0613d69f189c536f376da577398cc6b1b Mon Sep 17 00:00:00 2001
From: Marvin <marvinhong@163.com>
Date: Mon, 6 Feb 2023 10:41:11 +0800
Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E6=94=B9=E5=B7=A5=E5=8D=95KeyParts?=
 =?UTF-8?q?=E6=9F=A5=E8=AF=A2=201.1.=E4=BF=AE=E6=94=B9=E7=BB=84=E4=BB=B6?=
 =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=9B=B8=E5=85=B3=201.2.=E5=A2=9E=E5=8A=A0?=
 =?UTF-8?q?=E7=8B=AC=E7=AB=8B=E5=AF=BC=E5=87=BAexcel=E6=8C=89=E9=92=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 AMESCoreStudio.Web/Views/QRS/QRS013.cshtml    | 135 ++++++++++---
 .../Controllers/AMES/WipInfosController.cs    | 179 +++++++++++++++++-
 2 files changed, 290 insertions(+), 24 deletions(-)

diff --git a/AMESCoreStudio.Web/Views/QRS/QRS013.cshtml b/AMESCoreStudio.Web/Views/QRS/QRS013.cshtml
index 101ffe79..a516b958 100644
--- a/AMESCoreStudio.Web/Views/QRS/QRS013.cshtml
+++ b/AMESCoreStudio.Web/Views/QRS/QRS013.cshtml
@@ -27,11 +27,104 @@
     </div>
     <div class="layui-card-body">
         <table class="layui-hide" id="test" lay-filter="test"></table>
+        <div class="layui-inline">
+            <button type="button" id="btnExport" class="layui-btn layui-btn-normal layui-btn-sm"><i class="fa fa-file-excel-o"></i>導出數據</button>
+        </div>
     </div>
 </div>
 
 @section Scripts{
     <script type="text/javascript">
+        layui.config({
+            base: '../lib/layui_ext/',
+        }).extend({
+            excel: 'excel',
+        });
+    </script>
+
+    <script type="text/javascript">
+
+        function formatJSONDate(jsonDate) {
+            if (jsonDate == null) {
+                return "";
+            }
+            else {
+                var date = eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
+                var myDate = new Date(date);
+                var yy = myDate.getFullYear();
+                var mm = (myDate.getMonth() + 1 < 10 ? '0' : '') + (myDate.getMonth() + 1);
+                var dd = (myDate.getDate() < 10 ? '0' : '') + myDate.getDate();
+                var h = (myDate.getHours() < 10 ? '0' : '') + myDate.getHours();
+                var m = (myDate.getMinutes() < 10 ? '0' : '') + myDate.getMinutes();
+                var s = (myDate.getSeconds() < 10 ? '0' : '') + myDate.getSeconds();
+                //mm + "/" + dd + " " +
+                //var output = yy + "-" + mm + "-" + dd + " " + h + ":" + m + ":" + s;
+                var output = yy + mm + dd + h + m + s;
+                // myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds();
+                return output;
+            }
+        }
+
+        layui.use(['jquery', 'excel', 'layer'], function () {
+            var $ = layui.jquery;
+            var layer = layui.layer;
+            var excel = layui.excel;
+
+            $('#btnExport').on('click', function () {
+
+                $.ajax({
+                    url: '@Url.Action("QRS013A", "QRS")',
+                    data: { "wipNO": $("#wipNO").val() },
+                    dataType: 'json',
+                    success(res) {
+                        var data = res.data;
+
+                        var keys = [];
+                        var cols1 = [];
+                        var cols2 = [];
+                        var cols = {};
+                        var i = 0;
+                        var cols_title = "";
+                        for (var item in data[0])
+                        {
+                            //alert(item);
+                            //alert(data[0][item]);
+                            keys.push(item);
+
+                            cols1[i] = item;
+                            cols_title = cols_title + item.toString() + ",";
+                            //cols2[i] = item.toString() + ":\"" + item.toString() + "\"";
+                            //cols2[i] = item + ":" + item.toString();
+                            i = i + 1;
+                            cols2.push(item);
+                        }
+                        
+                        var cols_header = cols_title.substring(0, cols_title.length - 1);
+
+
+                        //data.unshift({ wipno: "wipno" });
+                        
+                        //data = excel.filterExportData(data, cols2);
+                        //data.unshift({ keys });
+                        //data.unshift(cols1);
+                        data.unshift({ wipno: "WipNo", barcodeno: "BarcodeNo", extrabarcodeno: "ExtraBarcodeNo", modelno: "ModelNo", itemno: "ItemNo" });
+
+                        var timestart = Date.now();
+                        excel.downloadExcel(data, '組件明細' + formatJSONDate(Date.now().toString()), 'xlsx');                        
+
+                        //excel.exportExcel(data, '組件明細' + formatJSONDate(Date.now().toString()) + '.xlsx', 'xlsx');
+
+                        var timeend = Date.now();
+
+                        var spent = (timeend - timestart) / 1000;
+                        layer.alert('導出數據耗時 ' + spent + ' s');
+                    }
+                    , error() {
+                        layer.alert('導出數據失敗');
+                    }
+                });
+            });
+        });
 
         layui.use(['form', 'layer'], function () {
             form = layui.form;
@@ -125,48 +218,44 @@
                 title: '料號'
             },
             {
-                field: 'kP_01',
-                title: '組件01'
-            },
-            {
-                field: 'kP_02',
-                title: '組件02'
+                field: 'kP_CPU',
+                title: 'CPU'
             },
             {
-                field: 'kP_03',
-                title: '組件03'
+                field: 'kP_RAM',
+                title: 'RAM'
             },
             {
-                field: 'kP_04',
-                title: '組件04'
+                field: 'kP_HDD',
+                title: 'HDD'
             },
             {
-                field: 'kP_05',
-                title: '組件05'
+                field: 'kP_Mini_Board',
+                title: 'Mini Board'
             },
             {
-                field: 'kP_06',
-                title: '組件06'
+                field: 'kP_PANEL',
+                title: 'PANEL'
             }            ,
             {
-                field: 'kP_07',
-                title: '組件07'
+                field: 'kP_LICENSE',
+                title: 'LICENSE'
             },
             {
-                field: 'kP_08',
-                title: '組件08'
+                field: 'kP_PSU',
+                title: 'PSU'
             },
             {
-                field: 'kP_09',
-                title: '組件09'
+                field: 'kP_Adaptcr',
+                title: 'Adaptcr'
             },
             {
                 field: 'kP_MB',
-                title: '組件MB'
+                title: 'MB'
             },
             {
-                field: 'kP_MAC',
-                title: '組件MAC'
+                field: 'kP_MAC1',
+                title: 'MB_MAC1'
             }
         ]];
 
diff --git a/AMESCoreStudio.WebApi/Controllers/AMES/WipInfosController.cs b/AMESCoreStudio.WebApi/Controllers/AMES/WipInfosController.cs
index d35a745c..fa7153ce 100644
--- a/AMESCoreStudio.WebApi/Controllers/AMES/WipInfosController.cs
+++ b/AMESCoreStudio.WebApi/Controllers/AMES/WipInfosController.cs
@@ -400,6 +400,183 @@ namespace AMESCoreStudio.WebApi.Controllers.AMES
                 await conn.OpenAsync();
             }
 
+            using (var cmd = conn.CreateCommand())
+            {
+                cmd.CommandText = sql;
+
+                using (var reader = await cmd.ExecuteReaderAsync())
+                {
+                    if (reader.HasRows)
+                    {
+                        List<dynamic> list = new List<dynamic>();
+                        DataTable table = new DataTable();
+
+                        table = DataReaderToDataTable(reader);
+
+                        //判断MB组件增加查询MAC
+                        try
+                        {
+                            if (wip_kp.Count > 0)
+                            {
+                                /*
+                                for (int i = 0; i < wip_kp.Count; i++)
+                                {
+                                    if (wip_kp[i].KpNo.StartsWith("MB"))
+                                    {
+                                        table.Columns.Add("MAC");
+                                        table.AcceptChanges();
+                                        break;
+                                    }
+                                }
+                                */
+
+                                ESUNContext _esun_context = new ESUNContext();
+
+                                //读取MAC
+                                DbConnection esun_conn = _esun_context.Database.GetDbConnection();
+                                if (esun_conn.State != System.Data.ConnectionState.Open)
+                                {
+                                    await esun_conn.OpenAsync();
+                                }
+
+                                bool createMacCol = true;
+
+                                for (int j = 0; j < table.Rows.Count; j++)
+                                {
+                                    string barcode_no = table.Rows[j]["BarcodeNo"].ToString();
+
+                                    string mac_sql = string.Format(@"select distinct a.工單編號 as mo_id,e.客戶工單編號 as customer_moid,a.工單序號 as product_sn,                        
+                               case substring(f.avalue_kp_typeid, 1, 3) when 'MAC' then isnull(z.item_prefix,'')+b.SUB_ITEM_SN else b.SUB_ITEM_SN end as part_barcode,
+                               f.avalue_kp_typeid as class,c.material_id,d.r_stn as routeid,'' as workerid,rtrim(g.sn_date) + ' ' + rtrim(g.sn_time) as create_date
+                          from or_sn_story a
+                        left join sub_item_db b on a.工單編號 = b.or_sn and a.工單序號 = b.or_sal
+                        left join jh_sub_item c on a.工單編號 = c.mo_id and b.class = c.part_typeid
+                        left join jh_sub_item_prefix z on c.mo_id = z.mo_id
+                        left join or_sub_db d on a.工單編號 = d.or_sn and b.class = d.class
+                             join or_list e on a.工單編號 = e.工單編號 and e.[客戶] = 'EV'
+                        left join jh_sub_item_mapping f on b.class = f.eversun_kp_typeid
+                             join jh_sn_list g on a.工單序號 = g.sn
+                         where g.sn_result = 'OK'
+       AND a.工單序號 = '{0}'
+       AND c.material_id LIKE 'MAC%'", barcode_no);
+
+                                    using (var esun_cmd = esun_conn.CreateCommand())
+                                    {
+                                        esun_cmd.CommandText = mac_sql;
+
+                                        using (var esun_reader = await esun_cmd.ExecuteReaderAsync())
+                                        {
+                                            if (esun_reader.HasRows)
+                                            {
+                                                List<dynamic> esun_list = new List<dynamic>();
+                                                DataTable esun_table = new DataTable();
+
+                                                esun_table = DataReaderToDataTable(esun_reader);
+
+                                                if (esun_table.Rows.Count > 0)
+                                                {
+                                                    if (createMacCol)
+                                                    {
+                                                        for (int k = 0; k < esun_table.Rows.Count; k++)
+                                                        {
+                                                            table.Columns.Add("KP_MAC" + (k + 1).ToString());
+                                                        }
+                                                        table.AcceptChanges();
+                                                        createMacCol = false;
+                                                    }
+
+                                                    string mac = "";
+                                                    for (int k = 0; k < esun_table.Rows.Count; k++)
+                                                    {
+                                                        mac = esun_table.Rows[k]["part_barcode"].ToString().Trim();
+                                                        table.Rows[j]["KP_MAC" + (k + 1).ToString()] = mac;
+                                                        table.AcceptChanges();
+                                                    }
+                                                }
+                                            }
+                                        }
+                                    }
+
+                                }
+                            }
+                        }
+                        catch { }
+
+
+                        foreach (DataRow row in table.Rows)
+                        {
+                            dynamic dyn = new ExpandoObject();
+                            list.Add(dyn);
+                            foreach (DataColumn column in table.Columns)
+                            {
+                                var dic = (IDictionary<string, object>)dyn;
+                                dic[column.ColumnName] = row[column];
+                            }
+                        }
+
+                        result.DataTotal = list.Count();
+                        result.Data = list;
+                    }
+                }
+            }
+
+            if (result == null)
+            {
+                result.Msg = "查無資料";
+                result.Success = false;
+                return result;
+            }
+
+            result.Success = true;
+            result.Msg = "OK";
+            return result;
+        }
+
+        /// <summary>
+        /// 查詢工單KeyParts資料QRS013
+        /// </summary>
+        /// <param name="wipNO"></param>
+        /// <param name="factoryNo"></param>
+        /// <param name="page"></param>
+        /// <param name="limit"></param>
+        /// <returns></returns>
+        [Route("[action]")]
+        [HttpGet]
+        public async Task<ResultModel<dynamic>> GetWipInfo4QRS013V2(string wipNO, string factoryNo, int page, int limit)
+        {
+            ResultModel<dynamic> result = new ResultModel<dynamic>();
+            Helper helper = new Helper(_context);
+
+            var wip_kp = await _context.WipKps.FromSqlRaw("SELECT * FROM JHAMES.WIP_KP WHERE WIP_NO = '" + wipNO + "' ORDER BY KP_NO").ToListAsync();
+
+            string sql = @" select c.wip_no as WipNo,b.barcode_no as BarcodeNo,b.extra_barcode_no as ExtraBarcodeNo,d.model_no as ModelNO,d.item_no as ItemNO";
+            string sql1 = "", sql2 = "", sql3 = "";
+            if (wip_kp.Count > 0)
+            {
+                for (int i = 0; i < wip_kp.Count; i++)
+                {
+                    sql1 = sql1 + ",k" + (i + 1).ToString() + ".part_no as KP_" + wip_kp[i].KpNo;
+                    sql2 = sql2 + ",(select barcode_id,part_no from jhames.barcode_item where item_no = '" + wip_kp[i].KpNo + "') k" + (i + 1).ToString();
+                    sql3 = sql3 + " and b.barcode_id = k" + (i + 1).ToString() + ".barcode_id(+)";
+                }
+            }
+
+            sql = sql + sql1;
+            sql = sql + " from jhames.barcode_info b,jhames.wip_info c,jhames.wip_att d";
+            sql = sql + sql2;
+
+            sql = sql + " where b.wip_id = c.wip_id and c.wip_no = d.wip_no";
+
+            sql = sql + sql3;
+
+            sql = sql + " and c.wip_no = '" + wipNO + "'";
+
+            DbConnection conn = _context.Database.GetDbConnection();
+            if (conn.State != System.Data.ConnectionState.Open)
+            {
+                await conn.OpenAsync();
+            }
+
             using (var cmd = conn.CreateCommand())
             {
                 cmd.CommandText = sql;
@@ -538,7 +715,7 @@ namespace AMESCoreStudio.WebApi.Controllers.AMES
         /// <returns></returns>
         [Route("[action]")]
         [HttpGet]
-        public async Task<ResultModel<dynamic>> GetWipInfo4QRS013Old(string wipNO, string factoryNo, int page, int limit)
+        public async Task<ResultModel<dynamic>> GetWipInfo4QRS013V1(string wipNO, string factoryNo, int page, int limit)
         {
             ResultModel<dynamic> result = new ResultModel<dynamic>();
             Helper helper = new Helper(_context);