1
yangle
2025-04-21 861794e4cc962de1059ab01ca1e2bd81e2bac643
WFormSynchronizeData_SMR/EquipmentCollectionForm/EquipmentCollectionForm.cs
@@ -32,6 +32,13 @@
            this.txtLj.ReadOnly = true;
            this.btnSave.Enabled = false;
            this.txtHMaterNumber.ReadOnly = true;
            //this.btnRest.Enabled = false;
            //制单编码只读
            this.txtHMakerNumber.ReadOnly = true;
            //制单人提示
            this.txtHMaker.ForeColor = Color.LightGray;
            this.txtHMaker.Text = "请输入用户编码!";
            bindCbox();
          
        }
@@ -39,13 +46,14 @@
        private  void btnBegin_Click(object sender, EventArgs e)
        {
            if (this.txtHBathNo.Text == "" || this.txtHBathNo.ReadOnly == false)
            //if (false)
            {
                MessageBox.Show("请输入批次号进行确认!");
            }
            else
            {
                //this.txtBegin.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                this.txtBegin.Text = "2024-8-7 15:20:06";
                this.txtBegin.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                //this.txtBegin.Text = "2024-11-21 13:40:27";
                this.btnBegin.Enabled = false;
                if (cmbEquipFile.SelectedIndex == 1)
                {
@@ -62,8 +70,9 @@
                MessageBox.Show("请点击开始按钮进行开始确认!");
            }
            else
            { //this.txtEnd.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                this.txtEnd.Text = "2024-8-7 17:20:06";
            {
                this.txtEnd.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                //this.txtEnd.Text = "2024-09-24 09:52:39";
                this.btnEnd.Enabled = false;
                if (cmbEquipFile.SelectedIndex == 1)
@@ -84,6 +93,7 @@
                //路径赋值给文本
                using (OpenFileDialog openFile = new OpenFileDialog())
                {
                    openFile.Multiselect = true; // 允许选择多个文件
                    DialogResult result = openFile.ShowDialog();
                    if (result == DialogResult.OK)
                    {
@@ -93,21 +103,23 @@
                        string selectedFolderPath = openFile.FileName;
                        this.txtLj.Text = selectedFolderPath;
                        objJsonResult = Xt_CSVReadText(selectedFolderPath, 1);
                        objJsonResult = Xt_CSVReadText(openFile.FileNames, 1);
                        dt = objJsonResult.dataTable;
                        if (objJsonResult.code == "0")
                        {
                            MessageBox.Show(objJsonResult.Message);
                        }
                        else {
                        else
                        {
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                ListData.Items.Add("底孔直径:" + dt.Rows[i]["BottomHoleDiameter"].ToString() +
                                    ",有无螺牙检测数据:" + dt.Rows[i]["TestingData"].ToString() +
                                    ",有无螺牙检测结果:" + dt.Rows[i]["DetectionResult"].ToString() +
                                ",时间:" + dt.Rows[i]["HDate"].ToString());
                        }
                            }
                        }
                    }
                }
@@ -115,7 +127,7 @@
        }
        //读取CSV文件数据  根据文件路径找到对应文件 并获取对应的数据
        public json Xt_CSVReadText(string FilePath, int num)
        public json Xt_CSVReadText(string[] FilePath, int num)
        {
            try
            {
@@ -124,46 +136,53 @@
                dt.Columns.Add("TestingData");
                dt.Columns.Add("DetectionResult");
                dt.Columns.Add("HDate");
                DataTable dt2 = new DataTable();
                List<string[]> records = new List<string[]>();
                using (StreamReader sr = new StreamReader(FilePath, Encoding.GetEncoding("gb2312")))
                for (int u = 0; u < FilePath.Length; u++)
                {
                    string[] headers = sr.ReadLine()?.Split(',');
                    if (headers != null)
                    using (StreamReader sr = new StreamReader(FilePath[u], Encoding.GetEncoding("gb2312")))
                    {
                        foreach (var header in headers)
                        string[] headers = sr.ReadLine()?.Split(',');
                        if (headers != null && u == 0)
                        {
                            dt2.Columns.Add(header.Trim());
                        }
                    }
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        var values = line.Split(',');
                        DataRow row = dt2.NewRow();
                        for (int i = 0; i < values.Length; i++)
                        {
                            // 这里可能需要添加类型转换和错误处理
                            row[i] = values[i].Trim();
                            foreach (var header in headers)
                            {
                                dt2.Columns.Add(header.Trim());
                            }
                        }
                        dt2.Rows.Add(row);
                        string line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            var values = line.Split(',');
                            DataRow row = dt2.NewRow();
                            for (int i = 0; i < values.Length; i++)
                            {
                                // 这里可能需要添加类型转换和错误处理
                                row[i] = values[i].Trim();
                            }
                            dt2.Rows.Add(row);
                        }
                    }
                }
                DateTime HBegin = DateTime.Parse(this.txtBegin.Text);
                DateTime HEnd = DateTime.Parse(this.txtEnd.Text);
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    for (int j = 0; j < 2; j++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["BottomHoleDiameter"] = dt2.Rows[i][dt2.Columns[j]].ToString();
                        dr["TestingData"] = dt2.Rows[i][dt2.Columns[j + 2]].ToString();
                        dr["DetectionResult"] = dt2.Rows[i][dt2.Columns[j + 4]].ToString();
                        dr["HDate"] = DateTime.Parse(dt2.Rows[i][dt2.Columns[6]].ToString() + " " + dt2.Rows[i][dt2.Columns[7]].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                        dt.Rows.Add(dr);
                        DateTime Now = DateTime.Parse(dt2.Rows[i][dt2.Columns[6]].ToString() + " " + dt2.Rows[i][dt2.Columns[7]].ToString());
                        if (Now >= HBegin && Now <= HEnd) {
                            DataRow dr = dt.NewRow();
                            dr["BottomHoleDiameter"] = dt2.Rows[i][dt2.Columns[j]].ToString();
                            dr["TestingData"] = dt2.Rows[i][dt2.Columns[j + 2]].ToString();
                            dr["DetectionResult"] = dt2.Rows[i][dt2.Columns[j + 4]].ToString();
                            dr["HDate"] = DateTime.Parse(dt2.Rows[i][dt2.Columns[6]].ToString() + " " + dt2.Rows[i][dt2.Columns[7]].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                            dt.Rows.Add(dr);
                        }
                    }
                }
@@ -188,8 +207,15 @@
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.txtHMakerNumber.Text == "")
                {
                    MessageBox.Show("请输入制单人信息!");
                }
                else {
                    Get_DisplayBard();
                }
                //this.txtHBathNo.ReadOnly = true;
                Get_DisplayBard();
            }
        }
@@ -239,6 +265,7 @@
            if (dr == DialogResult.Yes) {
                string Url = DBHelper.GetConfigKey(AppDomain.CurrentDomain.BaseDirectory + "Config/SQLAPI.config", "sUrl");
                //通过接口 获取token
                JsonResult jsonResult = await FindAccessToken(Url);
                string EquipFile = this.cmbEquipFile.Text;
@@ -256,7 +283,6 @@
                        break;
                }
                this.btnSave.Enabled = false;
            }
        }
@@ -265,16 +291,18 @@
            try
            {
               
                //获取所有设备数据
                JsonEquipmentData jsonResult =await FindDeviceList(url, token);
                if (jsonResult.code != "0")
                {
                    this.btnSave.Enabled = true;
                    MessageBox.Show("设备列表报错:" + jsonResult.message);
                }
                else
                {
                    string nos = "";
                    //获取去设备编码
                    //获取设备编码
                    for (int i = 0; i < jsonResult.data.Count; i++)
                    {
                        if (jsonResult.data[i].no == this.cmbEquipFile.SelectedValue.ToString())
@@ -282,11 +310,15 @@
                            nos = jsonResult.data[i].no;
                        }
                    }
                    //开始时间
                    string HBegin = this.txtBegin.Text;
                    //结束时间
                    string HEnd = this.txtEnd.Text;
                    //获取不良数,合数数,可疑数 总数
                    jsonResult = await FindDevicePropCalc(url, token, nos, HBegin, HEnd);
                    if (jsonResult.code != "0")
                    {
                        this.btnSave.Enabled = true;
                        MessageBox.Show("数量获取报错:" + jsonResult.message);
                    }
                    else {
@@ -299,7 +331,7 @@
                        {
                            if (jsonResult.data[i].propId == 173)
                            {
                                HInSpectQty = jsonResult.data[i].sum;
                                //HInSpectQty = jsonResult.data[i].sum;
                            }
                            else if (jsonResult.data[i].propId == 469)
                            {
@@ -314,6 +346,10 @@
                                HUnRightQty = jsonResult.data[i].sum;
                            }
                        }
                        //通过双方讨论 最终采纳 检验数量=合格+不合格+可疑
                        HInSpectQty = HRightQty + HUnKnowQty + HUnRightQty;
                        string HNumber = "";
                        int HSourceID = 0;
                        string HSourceNumber = "";
@@ -334,11 +370,12 @@
                        DataSet dsSouce = oCN.RunProcReturn("select * from Gy_Source with(nolock) where HNumber='" + HSourceNumber + "'", "Gy_Source");
                        if (dsSouce.Tables[0].Rows.Count == 0)
                        {
                            this.btnSave.Enabled = true;
                            MessageBox.Show("生产资源查无数据!");
                        }
                        else {
                            HSourceID = int.Parse(dsSouce.Tables[0].Rows[0]["HInterID"].ToString());
                            HSourceID = int.Parse(dsSouce.Tables[0].Rows[0]["HItemID"].ToString());
                            //查询检验方案
                            DataSet dataSet = oCN.RunProcReturn(@"select a.HInterID HQCSchemeID,b.HQCCheckItemID, ch.HNumber HQCCheckItemNumber,m.HNumber,HUpLimit,HDownLimit,HTargetVal,a.HProcID,b.HInspectInstruMentID
@@ -370,9 +407,18 @@
                                    {
                                        int HLastResult = 1;
                                        string HAnalysisMethod = dataSet.Tables[0].Rows[i]["HAnalysisMethod"].ToString();
                                        decimal HUpLimit = 0;
                                        decimal HDownLimit = 0;
                                        decimal HUpLimit = decimal.Parse(dataSet.Tables[0].Rows[i]["HTargetVal"].ToString()) + decimal.Parse(dataSet.Tables[0].Rows[i]["HUpLimit"].ToString());
                                        decimal HDownLimit = decimal.Parse(dataSet.Tables[0].Rows[i]["HTargetVal"].ToString()) + decimal.Parse(dataSet.Tables[0].Rows[i]["HDownLimit"].ToString());
                                        //分析方法 为定量分析
                                        if (HAnalysisMethod == "2")
                                        {
                                           //获取上限
                                            HUpLimit = decimal.Parse(dataSet.Tables[0].Rows[i]["HTargetVal"].ToString()) + decimal.Parse(dataSet.Tables[0].Rows[i]["HUpLimit"].ToString());
                                            //获取下限
                                            HDownLimit = decimal.Parse(dataSet.Tables[0].Rows[i]["HTargetVal"].ToString()) + decimal.Parse(dataSet.Tables[0].Rows[i]["HDownLimit"].ToString());
                                        }
                                        for (int j = 0; j < dt.Columns.Count - 1; j++)
                                        {
@@ -391,16 +437,19 @@
                                                    break;
                                            }
                                            //SRM@101.2@VEN00005@CGDD000167@1.02.002.002@20231129@1000
                                            //判断检验项目的代码 跟列名是否一致
                                            if (dataSet.Tables[0].Rows[i]["HQCCheckItemNumber"].ToString().Contains(HQCCheckItemName))
                                            {
                                                for (int k = 0; k < dt.Rows.Count; k++)
                                                {
                                                    //只取时间范围内的数据
                                                    if (DateTime.Parse(dt.Rows[k]["HDate"].ToString()) >= DateTime.Parse(HBegin) && DateTime.Parse(dt.Rows[k]["HDate"].ToString()) <= DateTime.Parse(HEnd))
                                                    {
                                                        //获取检验值
                                                        decimal HInSpectValue = decimal.Parse(dt.Rows[k][dt.Columns[j].ColumnName].ToString());
                                                        //默认值结论为合格
                                                        int HInSpectResult = 1;
                                                        //定量分析 进行判断是否满足上下限
                                                        if (HAnalysisMethod == "2")
                                                        {
                                                            HInSpectResult = (HInSpectValue > HUpLimit || HDownLimit > HInSpectValue) ? 0 : 1;
@@ -410,7 +459,7 @@
                                                            HInSpectResult = int.Parse(HInSpectValue.ToString());
                                                        }
                                                        //如果当前结论为合格 并且历史结论也是合格 则继续保持合格
                                                        if (HInSpectResult == 1 && HLastResult != 0)
                                                        {
                                                            HLastResult = 1;
@@ -448,7 +497,7 @@
                        ",0,0,'',0,'','" + dataSet.Tables[0].Rows[i]["HTargetVal"].ToString() + "'" +
                        ",'" + dataSet.Tables[0].Rows[i]["HUpLimit"].ToString() + "','" + dataSet.Tables[0].Rows[i]["HDownLimit"].ToString() + "','','',0,'" + HAnalysisMethod + "'," + dataSet.Tables[0].Rows[i]["HInspectInstruMentID"].ToString() + ",'" + HLastResult + "'" +
                        ") ");
                                        //如果当前结论为合格 并且历史结论也是合格 则继续保持合格
                                        if (HLastResult == 1 && HLastResults != 0)
                                        {
                                            HLastResults = 1;
@@ -466,27 +515,30 @@
                                        ",HSourceID,HICMOInterID,HICMOBillNo,HICMOQty,HProcExchInterID,HProcExchEntryID" +
                                        ",HProcExchBillNo,HProcExchQty,HMaterID,HFirstCheckEmp,HLastResult" +
                                        ",HMainSourceInterID,HMainSourceEntryID,HMainSourceBillNo,HMainSourceBillType,HICMOEntryID,HQCSchemeID,HShiftsID,HErrTreatment" +
                                        ",HProcID,HBatchNo" +
                                        ",HProcID,HBatchNo,HInSpectQty ,HRightQty ,HUnRightQty ,HUnKnowQty " +
                                        ") " +
                                        " values('" + BillType + "','" + BillType + "'," + HInterID + ",'" + HBillNo + "',1,getdate(),'',getdate()" +
                                        " values('" + BillType + "','" + BillType + "'," + HInterID + ",'" + HBillNo + "',1,getdate(),'" + this.txtHMaker.Text + "',getdate()" +
                                        "," + DateTime.Now.Year + "," + DateTime.Now.Month + ",''" +
                                        "," + HSourceID + ",0,'',0,0,0" +
                                        ",'',0," + HMaterID + ",'', " + HLastResults +
                                        ",'',0," + HMaterID + ",'" + this.labHMakerID.Text + "', " + HLastResults +
                                        ",0,0,'','',0," + HQCSchemeID + ",0,''" +
                                        "," + HProcID + ",'" + HBatchNo + "') ");
                                        "," + HProcID + ",'" + HBatchNo + "'," + HInSpectQty + "," + HRightQty + ", " + HUnRightQty + ", " + HUnKnowQty + ") ");
                                    oCN.Commit();
                                    MessageBox.Show("保存成功!");
                                    //this.btnRest.Enabled = true;
                                }
                                else
                                {
                                    this.btnSave.Enabled = true;
                                    MessageBox.Show("当前物料的检验方案与Excel的列不匹配!");
                                }
                            }
                            else
                            {
                                this.btnSave.Enabled = true;
                                MessageBox.Show("当前物料没有对应的检验方案!");
                            }
                        }
@@ -495,6 +547,7 @@
            }
            catch (Exception e)
            {
                this.btnSave.Enabled = true;
                MessageBox.Show("保存失败!" + e.Message);
            }
        }
@@ -576,6 +629,49 @@
            }
        }
        public async Task<JsonResult> EquipResultList(string url, StringContent postData, string Type)
        {
            // 创建 HttpClient 实例
            using (HttpClient client = new HttpClient())
            {
                JsonResult jsonResult = new JsonResult();
                try
                {
                    HttpResponseMessage response;
                    if (Type == "POST")
                    {  // 发起 POST 请求
                        response = await client.PostAsync(url, postData);
                    }
                    else
                    {
                        // 发起 get 请求
                        response = await client.GetAsync(url);
                    }
                    // 确保请求成功
                    if (response.IsSuccessStatusCode)
                    {
                        // 读取响应内容
                        string responseBody = await response.Content.ReadAsStringAsync();
                        jsonResult = JsonConvert.DeserializeObject<JsonResult>(responseBody);
                    }
                    else
                    {
                        MessageBox.Show($"请求失败,: {response.StatusCode}");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("请求报错:" + ex.Message);
                }
                return jsonResult;
            }
        }
        //获取ID
        public async Task<JsonResult> FindAccessToken(string url)
        {
@@ -604,8 +700,10 @@
        //获取良品、不良品明细
        public async Task<JsonEquipmentData> FindAllDevicePropsDetailByTimeRange(string url, string token,string nos, string HBegin, string HEnd)
        {
            string basePropIds = DBHelper.GetConfigKey(AppDomain.CurrentDomain.BaseDirectory + "Config/SQLAPI.config", "basePropIds");
            basePropIds = "[" + basePropIds + "]";
            nos = nos.Replace(",", "\",\"");
            string stringJson = "{\"token\":\"" + token + "\",\"deviceNos\":[\"" + nos + "\"],\"startTime\":\"" + HBegin + "\",\"endTime\":\"" + HEnd + "\",\"basePropIds\":[473,474,600,601,602,603,604,605,576,577,578,562,563,564,565,566,567]}";
            string stringJson = "{\"token\":\"" + token + "\",\"deviceNos\":[\"" + nos + "\"],\"startTime\":\"" + HBegin + "\",\"endTime\":\"" + HEnd + "\",\"basePropIds\":" + basePropIds + "}";
            //设备列表接口
            string urls = url + EquipmentType.Nine;
@@ -618,7 +716,7 @@
        //获取不良数,合数数,可疑数 总数
        public async Task<JsonEquipmentData> FindDevicePropCalc(string url, string token,string nos,string HBegin,string HEnd)
        {
            string stringJson = "{\"token\":\"" + token + "\",\"deviceNo\":\"" + nos + "\",\"propIds\":[173,469,470,606],\"beginTime\":\"" + HBegin + "\",\"endTime\":\"" + HEnd + "\"}";
            string stringJson = "{\"token\":\"" + token + "\",\"deviceNo\":\"" + nos + "\",\"propIds\":[606,173,469,470],\"beginTime\":\"" + HBegin + "\",\"endTime\":\"" + HEnd + "\"}";
            var postData = new StringContent(stringJson, System.Text.Encoding.UTF8, "application/json");
            string urls = url + EquipmentType.Seven;
            JsonEquipmentData jsonResult = await EquipmentList(urls, postData, "POST");
@@ -638,12 +736,49 @@
            return jsonResult;
        }
        //获取设备时间段产量
        public async Task<JsonEquipmentData> FindProdByNosAndTime(string url, string token, string nos, string HBegin, string HEnd)
        {
            nos = nos.Replace(",", "\",\"");
            nos = "\"" + nos + "\"";
            string stringJson = "{\"token\":\"" + token + "\",\"nos\":[" + nos + "],\"startTime\":\"" + HBegin + "\",\"endTime\":\"" + HEnd + "\"}";
            var postData = new StringContent(stringJson, System.Text.Encoding.UTF8, "application/json");
            string urls = url + EquipmentType.Four;
            JsonEquipmentData jsonResult = await EquipmentList(urls, postData, "POST");
            return jsonResult;
        }
        //获取设备运行时间 开机 停机 待机  关机
        public async Task<JsonResult> FindDeviceStatusTimeByNo(string url, string token, string nos, string HBegin, string HEnd)
        {
            nos = nos.Replace(",", "\",\"");
            nos = "\"" + nos + "\"";
            string stringJson = "{\"token\":\"" + token + "\",\"no\":" + nos + ",\"startTime\":\"" + HBegin + "\",\"endTime\":\"" + HEnd + "\"}";
            var postData = new StringContent(stringJson, System.Text.Encoding.UTF8, "application/json");
            string urls = url + EquipmentType.Five;
            JsonResult jsonResult = await EquipResultList(urls, postData, "POST");
            return jsonResult;
        }
        //获取注塑机的 开机
        public async Task<JsonResult> FindDeviceStatusTimeByNosAndOperationMode(string url, string token, string nos, string HBegin, string HEnd)
        {
            nos = nos.Replace(",", "\",\"");
            nos = "\"" + nos + "\"";
            string stringJson = "{\"token\":\"" + token + "\",\"no\":" + nos + ",\"startTime\":\"" + HBegin + "\",\"endTime\":\"" + HEnd + "\"}";
            var postData = new StringContent(stringJson, System.Text.Encoding.UTF8, "application/json");
            string urls = url + EquipmentType.Ten;
            JsonResult jsonResult = await EquipResultList(urls, postData, "POST");
            return jsonResult;
        }
        //下拉框绑定数据
        private void bindCbox()
        {
            IList<Info> infoList = new List<Info>();
            Info info1 = new Info() { Id = "CMR_QC_SE_001", Name = "螺母全检" };
            Info info2 = new Info() { Id = "CMR_ZZ_SE_001", Name = "螺母压装" };
            Info info1 = new Info() { Id = "CMR-QC-SE-001", Name = "螺母全检" };
            Info info2 = new Info() { Id = "CMR-ZZ-SE-001", Name = "螺母压装" };
            Info info3 = new Info() { Id = "ZSJ07", Name = "注塑机#07" };
            //Info info3 = new Info() { Id = "3", Name = "王五" };
            infoList.Add(info1);
@@ -672,9 +807,11 @@
                if (dr == DialogResult.Yes)
                {
                    this.txtHBathNo.ReadOnly = true;
                    this.txtBegin.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    this.txtBegin.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:00");
                    //this.txtBegin.Text = "2024-11-21 13:40:27";
                    this.btnBegin.Enabled = false;
                    this.timer.Enabled = true;
                    this.Time_CLHB.Enabled = true;
                }
            }
        }
@@ -682,6 +819,9 @@
        //定时获取数据
        private async void timer_Tick(object sender, EventArgs e)
        {
            //测试全检
            //this.timer.Enabled = false;
            string Url = DBHelper.GetConfigKey(AppDomain.CurrentDomain.BaseDirectory + "Config/SQLAPI.config", "sUrl");
            string urls = Url + EquipmentType.One;
            // 准备要发送的表单数据
@@ -692,7 +832,7 @@
                    // 添加更多键值对,根据您的需要
                });
            JsonResult jsonResult = await getUrl(urls, formData);
            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:00");
            if (cmbEquipFile.SelectedIndex == 1)
            {
@@ -725,14 +865,14 @@
                //获取去设备编码
                for (int i = 0; i < jsonResult.data.Count; i++)
                {
                    if (jsonResult.data[i].no.Contains("CMR_ZZ_SE_001"))
                    if (jsonResult.data[i].no.Contains("CMR-ZZ-SE-001"))
                    {
                        nos += jsonResult.data[i].no + ",";
                    }
                }
                nos = nos.Substring(0, nos.Length - 1);
                TimeDate = "2024-08-14 08:30:39";
                time = "2024-08-14 15:05:39";
                //TimeDate = "2024-08-14 08:30:39";
                //time = "2024-08-14 15:05:39";
                JsonEquipmentData jsonEquipment = await FindAllDevicePropsDetailByTimeRange(Url, token, nos, TimeDate, time);
@@ -792,9 +932,10 @@
            }
            //this.timer.Enabled = false;
            //TimeDate = "2024-08-14 17:00:39";
            //time = "2024-08-14 17:05:39";
            //TimeDate = "2025-02-10 13:10:50";
            //time = "2025-02-10 13:11:50";
            //获取所有设备数据
            JsonEquipmentData jsonResult = await FindDeviceList(Url, token);
            if (jsonResult.code != "0")
            {
@@ -803,33 +944,46 @@
            else {
                string nos = "";
                //获取去设备编码
                //获取设备编码
                for (int i = 0; i < jsonResult.data.Count; i++)
                {
                    if (jsonResult.data[i].no.Contains("CMR-ZS-SE"))
                    {
                        nos += jsonResult.data[i].no + ",";
                    }
                    else if (jsonResult.data[i].no.Contains("CMR_ZZ_SE_"))
                    {
                        nos += jsonResult.data[i].no + ",";
                    }
                    nos += jsonResult.data[i].no + ",";
                    //if (jsonResult.data[i].no.Contains("CMR-ZS-SE"))
                    //{
                    //    nos += jsonResult.data[i].no + ",";
                    //}
                    //else if (jsonResult.data[i].no.Contains("CMR_ZZ_SE_"))
                    //{
                    //    nos += jsonResult.data[i].no + ",";
                    //}
                }
                nos = nos.Substring(0, nos.Length - 1);
                //获取参数 获取每个设备对应的所有参数
                JsonEquipmentData jsonEquipment = await FindAllDevicePropsDetailByTimeRange(Url, token, nos, TimeDate, time);
                var JsonData = "{\"data\":[";
                CustomWriteLog("01" , DateTime.Now.ToString("yyyy-MM-dd"));
                //通过每个设备进行循环赋值
                for (int i = 0; i < jsonEquipment.data.Count; i++)
                {
                    if (jsonEquipment.data[i].deviceNo.Contains("CMR_ZZ_SE_"))
                    //获取设备对应的编码 判断编码是否满足为螺母压装设备
                    if (jsonEquipment.data[i].deviceNo.Contains("CMR-ZZ-SE-"))
                    {
                        //循环 螺母压装设备对应的所有工艺参数
                        for (int j = 0; j < jsonEquipment.data[i].props.Count; j++)
                        {
                            for (int k = 0; k < jsonEquipment.data[0].props[j].datas.Count; k++)
                            //循环 每个参数对应的所有数据
                            for (int k = 0; k < jsonEquipment.data[i].props[j].datas.Count; k++)
                            {
                                //日志
                                CustomWriteLog("1-" + k + ":" + jsonEquipment.data[i].props[j].datas[k].value, DateTime.Now.ToString("yyyy-MM-dd"));
                                //获取参数的类型
                                valueData value = JsonConvert.DeserializeObject<valueData>(jsonEquipment.data[i].props[j].datas[k].value);
                                //日志
                                CustomWriteLog("2", DateTime.Now.ToString("yyyy-MM-dd"));
                                if (value.type == "良品")
                                {
                                    JsonData += "{\"HDate\":\"" + jsonEquipment.data[i].props[j].datas[k].createTime + "\",\"HTechParamName\":\"螺母是否凸出\",\"HResult\":\"OK\",\"HCount\":1,\"HSourceCode\":\"" + jsonEquipment.data[i].deviceNo + "\"},";
@@ -852,27 +1006,76 @@
                                JsonData += "{\"HDate\":\"" + time + "\",\"HTechParamName\":\"良品数\",\"HResult\":\"OK\",\"HCount\":" + data.data[j].sum + ",\"HSourceCode\":\"" + jsonEquipment.data[i].deviceNo + "\"},";
                            }
                            //不良数
                            else if (data.data[i].propId == 470)
                            else if (data.data[j].propId == 470)
                            {
                                JsonData += "{\"HDate\":\"" + time + "\",\"HTechParamName\":\"NG数量\",\"HResult\":\"NG\",\"HCount\":" + data.data[j].sum + ",\"HSourceCode\":\"" + jsonEquipment.data[i].deviceNo + "\"},";
                            }
                        }
                    }
                    else {
                        //循环 螺母压装设备对应的所有工艺参数
                        for (int j = 0; j < jsonEquipment.data[i].props.Count; j++)
                        {
                            //循环 每个参数对应的所有数据
                            for (int k = 0; k < jsonEquipment.data[i].props[j].datas.Count; k++)
                            {
                                JsonData += "{\"HDate\":\"" + jsonEquipment.data[i].props[j].datas[k].createTime + "\",\"HTechParamName\":\"" + jsonEquipment.data[i].props[j].propName + "\",\"HResult\":\"\",\"HCount\":" + jsonEquipment.data[i].props[j].datas[k].value + ",\"HSourceCode\":\"" + jsonEquipment.data[i].deviceNo + "\"},";
                                JsonData += "{\"HDate\":\"" + jsonEquipment.data[i].props[j].datas[k].createTime + "\",\"HTechParamName\":\"" + jsonEquipment.data[i].props[j].propName + "\",\"HResult\":\""+ jsonEquipment.data[i].props[j].datas[k].value + "\",\"HCount\":1,\"HSourceCode\":\"" + jsonEquipment.data[i].deviceNo + "\"},";
                            }
                        }
                        //获取设备时间段产量
                        JsonEquipmentData data = await FindProdByNosAndTime(Url, token, jsonEquipment.data[i].deviceNo, TimeDate, time);
                        for (int j = 0; j < data.data.Count; j++)
                        {
                            JsonData += "{\"HDate\":\"" + time + "\",\"HTechParamName\":\"生产数量\",\"HResult\":\"OK\",\"HCount\":" + data.data[j].prod + ",\"HSourceCode\":\"" + jsonEquipment.data[i].deviceNo + "\"},";
                        }
                    }
                }
                var  EquipmentStaus = await FindDeviceStatusByNos(Url, token, nos);
                CustomWriteLog("02", DateTime.Now.ToString("yyyy-MM-dd"));
                //获取设备运行状态
                var EquipmentStaus = await FindDeviceStatusByNos(Url, token, nos);
                for (int i = 0; i < EquipmentStaus.data.Count; i++)
                {
                    //卷绕  注塑  浸渍  组装  的设备运行 开关停机时间
                    if (EquipmentStaus.data[i].no.Contains("CMR-JR-SD-") || EquipmentStaus.data[i].no.Contains("CMR-JZ-SD-") || EquipmentStaus.data[i].no.Contains("CMR-HX-SD-")
                        || EquipmentStaus.data[i].no.Contains("CMR-ZD-SD")|| EquipmentStaus.data[i].no.Contains("CMR-ZS-SE-"))
                    {
                        string HBegDate = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                        string HEndDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:00");
                        if (DateTime.Parse(HEndDate).AddMinutes(1).ToString("yyyy-MM-dd") != DateTime.Parse(HEndDate).ToString("yyyy-MM-dd")) {
                            HEndDate= DateTime.Parse(HEndDate).AddMinutes(1).ToString("yyyy-MM-dd HH:mm:00");
                        }
                        JsonResult HEqDate = await FindDeviceStatusTimeByNo(Url, token, EquipmentStaus.data[i].no, HBegDate, HEndDate);
                        JsonData += "{\"HDate\":\"" + HEndDate + "\",\"HTechParamName\":\"开机时长\",\"HResult\":\"" + ((float)(HEqDate.data.onlineTime / 60 / 60)).ToString("0.00") + "\",\"HCount\":1,\"HSourceCode\":\"" + EquipmentStaus.data[i].no + "\"},";
                        JsonData += "{\"HDate\":\"" + HEndDate + "\",\"HTechParamName\":\"关机时长\",\"HResult\":\"" + ((float)(HEqDate.data.offlineTime / 60 / 60)).ToString("0.00") + "\",\"HCount\":1,\"HSourceCode\":\"" + EquipmentStaus.data[i].no + "\"},";
                        JsonData += "{\"HDate\":\"" + HEndDate + "\",\"HTechParamName\":\"停机时长\",\"HResult\":\"" + ((float)(HEqDate.data.standbyTime / 60 / 60)).ToString("0.00") + "\",\"HCount\":1,\"HSourceCode\":\"" + EquipmentStaus.data[i].no + "\"},";
                        JsonData += "{\"HDate\":\"" + HEndDate + "\",\"HTechParamName\":\"运行时长\",\"HResult\":\"" + ((float)(HEqDate.data.runTime / 60 / 60)).ToString("0.00") + "\",\"HCount\":1,\"HSourceCode\":\"" + EquipmentStaus.data[i].no + "\"},";
                    }
                    //卷绕  注塑  浸渍  组装  的设备运行 开关停机时间
                    //if (EquipmentStaus.data[i].no.Contains("CMR-ZS-SE-"))
                    //{
                    //    string HBegDate = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                    //    string HEndDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    //    JsonResult kj = await FindDeviceStatusTimeByNosAndOperationMode(Url, token, EquipmentStaus.data[i].no, HBegDate, HEndDate);
                    //    JsonData += "{\"HDate\":\"" + HEndDate + "\",\"HTechParamName\":\"开机时长\",\"HResult\":\"" + ((float)(kj.data.onlineTime / 60 / 60)).ToString("0.00") + "\",\"HCount\":1,\"HSourceCode\":\"" + EquipmentStaus.data[i].no + "\"},";
                    //    JsonResult HEqDate = await FindDeviceStatusTimeByNo(Url, token, EquipmentStaus.data[i].no, HBegDate, HEndDate);
                    //    JsonData += "{\"HDate\":\"" + HEndDate + "\",\"HTechParamName\":\"关机时长\",\"HResult\":\"" + ((float)(HEqDate.data.offlineTime / 60 / 60)).ToString("0.00") + "\",\"HCount\":1,\"HSourceCode\":\"" + EquipmentStaus.data[i].no + "\"},";
                    //    JsonData += "{\"HDate\":\"" + HEndDate + "\",\"HTechParamName\":\"停机时长\",\"HResult\":\"" + ((float)(HEqDate.data.standbyTime / 60 / 60)).ToString("0.00") + "\",\"HCount\":1,\"HSourceCode\":\"" + EquipmentStaus.data[i].no + "\"},";
                    //    JsonData += "{\"HDate\":\"" + HEndDate + "\",\"HTechParamName\":\"运行时长\",\"HResult\":\"" + ((float)(HEqDate.data.runTime / 60 / 60)).ToString("0.00") + "\",\"HCount\":1,\"HSourceCode\":\"" + EquipmentStaus.data[i].no + "\"},";
                    //}
                    string staus = "";
                    switch (EquipmentStaus.data[i].status)
                    {
@@ -903,20 +1106,35 @@
                    }
                }
                //查询当前开工的设备 获取对应的物料信息
                DataSet datable = oCN.RunProcReturn(@"select m.HNumber,eq.HEquipFileNo from Sc_ICMOBillStatus_Tmp a
                                            inner join Gy_EquipFileBillMain eq on a.HSourceID = eq.HSourceID
                                            inner join Gy_Material m on a.HMaterID = m.HItemID
                                            where a.HICMOStatus = '1' ", "Sc_ICMOBillStatus_Tmp");
                if (datable.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < datable.Tables[0].Rows.Count; i++)
                    {
                        string HSourceCode = datable.Tables[0].Rows[i]["HEquipFileNo"].ToString();
                        string HNumber = datable.Tables[0].Rows[i]["HNumber"].ToString();
                        JsonData += "{\"HDate\":\"" + DateTime.Now.ToString() + "\",\"HTechParamName\":\"物料代码\",\"HResult\":\"" + HNumber + "\",\"HCount\":1,\"HSourceCode\":\"" + HSourceCode + "\"},";
                    }
                }
                CustomWriteLog("03", DateTime.Now.ToString("yyyy-MM-dd"));
                JsonData = JsonData.Substring(0, JsonData.Length - 1);
                JsonData += "]}";
                //保存数据
                DataSave(JsonData);
                if (TimeDate != "")
                {
                    TimeDate = time;
                }
            }
        }
        //数据保存
@@ -933,6 +1151,201 @@
                ListData.Items.Add("时间:" + data.data[i].HDate +
                                  ",工艺参数:" + data.data[i].HTechParamName +
                                  ",工艺参数值:" + data.data[i].HResult);
            }
        }
        //定时生成产量汇报单
        private async void Time_CLHB_Tick(object sender, EventArgs e)
        {
            //this.Time_CLHB.Enabled = false;
            string Url = DBHelper.GetConfigKey(AppDomain.CurrentDomain.BaseDirectory + "Config/SQLAPI.config", "sUrl");
            string urls = Url + EquipmentType.One;
            // 准备要发送的表单数据
            var formData = new FormUrlEncodedContent(new[]
            {
                    new KeyValuePair<string, string>("userName", "admin"),
                    new KeyValuePair<string, string>("password", "admin")
                    // 添加更多键值对,根据您的需要
                });
            JsonResult jsonResult = await getUrl(urls, formData);
            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string token = jsonResult.data.token;
            //获取所有的设备数据
            JsonEquipmentData jsonEquipment = await FindDeviceList(Url, token);
            if (jsonEquipment.code != "0")
            {
                MessageBox.Show("设备列表报错:" + jsonEquipment.message);
            }
            else
            {
                string nos = "";
                //获取去设备编码
                for (int i = 0; i < jsonEquipment.data.Count; i++)
                {
                    if (jsonEquipment.data[i].no.Contains("CMR-ZS-SE"))
                    {
                        nos += "''" + jsonEquipment.data[i].no + "'',";
                    }
                    else if (jsonEquipment.data[i].no.Contains("CMR-ZZ-SE-"))
                    {
                        nos += "''" + jsonEquipment.data[i].no + "'',";
                    }
                }
                nos = nos.Substring(0, nos.Length - 1);
                //this.Time_CLHB.Enabled = false;
                //查询开工的设备信息 以及 查询当天设备对应的开工时间 和完工时间
                DataSet ds = oCN.RunProcReturn("exec h_p_Sc_TimeICMOReporList '" + nos + "'", "h_p_Sc_TimeICMOReporList");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    //根据当天只开工没有完工对应的产量数据 生成产量汇报单
                    DataTable dt = ds.Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        nos = dt.Rows[i]["设备编码"].ToString();
                        string HICMInterID = dt.Rows[i]["HInterID"].ToString();
                        string HBegDate = DateTime.Parse(dt.Rows[i]["日期"].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                        string HEndDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        jsonEquipment = await FindProdByNosAndTime(Url, token, nos, HBegDate, HEndDate);
                        if (decimal.Parse(jsonEquipment.data[0].prod) > 0) {
                            //生成产量汇报单
                            oCN.RunProc("exec h_p_Sc_AddTimeICMORepor " + HICMInterID + "," + jsonEquipment.data[0].prod+",'"+ HEndDate + "'");
                        }
                    }
                }
                if (ds.Tables[1].Rows.Count > 0)
                {
                    //根据当天开工并且完工对应的产量数据 生成产量汇报单
                    DataTable dt = ds.Tables[1];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        nos = dt.Rows[i]["设备编码"].ToString();
                        string HICMInterID = dt.Rows[i]["HInterID"].ToString();
                        string HBegDate = DateTime.Parse(dt.Rows[i]["开始日期"].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                        string HEndDate = DateTime.Parse(dt.Rows[i]["结束日期"].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                        jsonEquipment = await FindProdByNosAndTime(Url, token, nos, HBegDate, HEndDate);
                        if (decimal.Parse(jsonEquipment.data[0].prod) > 0)
                        {
                            //生成产量汇报单
                            oCN.RunProc("exec h_p_Sc_AddTimeICMORepor " + HICMInterID + "," + jsonEquipment.data[0].prod + ",'" + HEndDate + "'");
                        }
                    }
                }
            }
        }
        private void EquipmentCollectionForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("确定要关闭吗?", "确认", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                e.Cancel = true;
            }
        }
        private void txtHMaker_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtHMaker.Text))
            {
                this.txtHMaker.ForeColor = Color.LightGray;
                txtHMaker.Text = "请输入用户编码!";
            }
        }
        private void txtHMaker_Enter(object sender, EventArgs e)
        {
            if (txtHMaker.Text.Trim() == "请输入用户编码!")
            {
                txtHMaker.Text = "";
                this.txtHMaker.ForeColor = Color.Black;
            }
        }
        private void txtHMaker_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SelectHMaker(this.txtHMaker.Text);
            }
        }
        //查询职员信息
        public void SelectHMaker(string txtHMaker)
        {
            try
            {
                DataSet ds = oCN.RunProcReturn("select  HItemID,HName,HNumber from Gy_Employee where HNumber='" + txtHMaker + "' or HName ='" + txtHMaker + "'", "Gy_Employee");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    string HName = ds.Tables[0].Rows[0]["HName"].ToString();
                    string HMakerID = ds.Tables[0].Rows[0]["HItemID"].ToString();
                    string HNumber = ds.Tables[0].Rows[0]["HNumber"].ToString();
                    this.txtHMaker.Text = HName;
                    this.txtHMakerNumber.Text = HNumber;
                    this.labHMakerID.Text = HMakerID;
                    this.txtHMaker.ReadOnly = true;
                }
                else
                {
                    MessageBox.Show(this, "编码:" + txtHMaker + ",查无数据,可能是输入编码有误!", "提示");
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(this, e.Message, "提示");
            }
        }
        private void EquipmentCollectionForm_Activated(object sender, EventArgs e)
        {
            //光标选中制单人
            this.txtHMaker.Focus();
        }
        private static readonly object lockObj = new object();
        //写日志
        public static void CustomWriteLog(object obj, string FileName, string filePath = "Vlog", bool isAppend = true)
        {
            try
            {
                lock (lockObj)
                {
                    filePath = $@"{filePath}\{FileName}.txt";
                    filePath = AppDomain.CurrentDomain.BaseDirectory + filePath;
                    if (!System.IO.Directory.Exists(Path.GetDirectoryName(filePath)))
                    {
                        System.IO.Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                    }
                    bool fileExists = System.IO.File.Exists(filePath);
                    //不存在 则创建该文件
                    if (!fileExists)
                    {
                        System.IO.File.Create(filePath).Close();
                    }
                    using (StreamWriter writer = new StreamWriter(filePath, isAppend))
                    {
                        //存在的时候才写一行
                        if (fileExists && isAppend)
                        {
                            writer.WriteLine();
                        }
                        var content = obj is string ? obj : JsonConvert.SerializeObject(obj);
                        writer.WriteLine($"{DateTime.Now} {content}");
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
@@ -960,6 +1373,10 @@
    public class JsonData
    {
        public double onlineTime { get; set; }
        public double offlineTime { get; set; }
        public double standbyTime { get; set; }
        public double runTime { get; set; }
        public string expirationTime { get; set; }
        public string token { get; set; }
        public string deviceNo { get; set; }
@@ -1021,9 +1438,9 @@
        //一段时间内设备状态的获取
        public static string Eight = "/api/third/dayDeviceStatusTimeline";
        //良品 不良品 明细 注塑机数据
        public static string Nine = "/api/third/findAllDevicePropsDetailByTimeRange";
        //注塑机数据
        public static string Ten = "/api/third/findAllDevicePropsDetailByTimeRange";
        public static string Nine = "/api/third/findAllDevicePropsDetailByTimeRange_v1";
        //注塑机开机时长 根据 操作模式采集
        public static string Ten = "/api/third/findDeviceStatusTimeByNosAndOperationMode";
        //public static string Eleven = "Monday";
        //public static string Twelve = "Monday";
    }
@@ -1033,5 +1450,5 @@
        public string Id { get; set; }
        public string Name { get; set; }
    }
}