FrmAnalyze.cs 6.3 KB

using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OnlineStore.AutoCountClient
{
    partial class FrmAnalyze : FrmBase
    {
        public FrmAnalyze()
        {
            InitializeComponent();
        }
        public static DateTime PreStartTime = new DateTime(0);
        public static DateTime PreEndTime = new DateTime(0);
        private void FrmAnalyze_Load(object sender, EventArgs e)
        {
            if (PreStartTime.Ticks > 0 && PreEndTime.Ticks > 0)
            {
                dtpStartTime.Value = PreStartTime;
                dtpEndTime.Value = PreEndTime;
            }
            else
            {
                DateTime time1 = DateTime.Now.AddDays(-4);
                dtpStartTime.Value = new DateTime(time1.Year, time1.Month, time1.Day, 0, 0, 0);
                DateTime time = DateTime.Now.AddDays(-1);
                dtpEndTime.Value = new DateTime(time.Year, time.Month, time.Day, 23, 59, 59);
                dtpEndTime.Value = DateTime.Now;
            }
        }


        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (RobotManager.robot.sQLite == null)
            {
                MessageBox.Show("数据未初始化完成,请稍后!");
                return;
            }

            DateTime startTime = dtpStartTime.Value;
            DateTime endTime = dtpEndTime.Value;

            if (startTime < DateTime.Now.AddDays(-30))
            {
                MessageBox.Show("请输入正确的时间(只能查询近30天的日志)");
                dtpStartTime.Focus();
                return;
            } 
            if (startTime> endTime)
            {
                MessageBox.Show("请输入正确的时间(开始时间必须小于结束时间)");
                dtpEndTime.Focus();
                return;
            }
            PreStartTime = startTime;
            PreEndTime = endTime;
            string code = txtCode.Text.Trim();
            lastList = new List<XRayHistory>();
            string[][] array = null;
            bool result = RobotManager.robot.sQLite.Select(code, startTime.ToString(), endTime.ToString(), out array);
            LogUtil.error("查数据【" + code + "】【" + startTime.ToString() + "】【" + endTime.ToString() + "】结果:" + result + "," + RobotManager.robot.sQLite.ErrInfo);
            if (array != null && array.Length > 0)
            {
                LogUtil.info("共查询到【" + array.Length + "】行数据");

                this.dataGridView1.Rows.Clear();
                int index = 1;
                foreach (string[] a in array)
                {
                    DataGridViewRow view = new DataGridViewRow();
                    view.CreateCells(dataGridView1);
                    view.Cells[0].Value = index;

                    lastList.Add(new XRayHistory(index, a[0], a[1], a[2], a[3], a[4]));
                    for (int i = 1; i < a.Length; i++)
                    {
                        view.Cells[i].Value = a[i];
                    }
                    //view.Cells[0].Value = point.pointNum.ToString();
                    //view.Cells[1].Value = point.PartNum.ToString();
                    //view.Cells[2].Value = point.pointName;
                    //view.Cells[3].Value = point.PositionX.ToString();
                    //view.Cells[4].Value = point.PositionY.ToString();
                    //view.Cells[5].Value = point.NodePositionX.ToString();
                    //view.Cells[6].Value = point.NodePositionY.ToString();
                    //view.Cells[7].Value = point.PositionNum.ToString();
                    //view.Cells[8].Value = point.NeedSoldering;
                    //view.Cells[9].Value = point.WeldTemp;

                    dataGridView1.Rows.Add(view);
                    index++;
                }
            }
        }
        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private List<XRayHistory> lastList = new List<XRayHistory>();
        private void btnExport_Click(object sender, EventArgs e)
        {
            if (lastList.Count > 0)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.FileName = "";
                sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                sfd.FileName = "点料记录" + "-" + DateTime.Now.ToString("yyyy:MM:dd:HH:mm").Replace(":", "");
                sfd.Filter = @"csv|*.csv";
                DialogResult result = sfd.ShowDialog();

                if (result.Equals(DialogResult.OK))
                {
                    string filePath = sfd.FileName;
                    {
                        if (File.Exists(filePath))
                        {
                            File.Delete(filePath);
                        }

                        List<string> list = new List<string>();
                        list.Add("编号,条码,高度,宽度,数量,图片文件名");
                        foreach(XRayHistory obj in lastList)
                        {
                            list.Add(obj.ToCSVStr());
                        }

                        File.WriteAllLines(filePath, list.ToArray<string>(), Encoding.GetEncoding("gbk"));
                        MessageBox.Show("成功导出程序到文件:" + "\r\n" + filePath);
                    }
                }
            }
            else
            {
                MessageBox.Show("暂无数据可导出");
            }
        }
    }


    public class XRayHistory
    {
      public   XRayHistory(int num,string code, string h, string w, string c, string fname)
        {
            this.Code = code;
            this.Height = h;
            this.Width = w;
            this.Count = c;
            this.FileName = fname;
        }

        public int Num = 0;
        public string Code = "";

        public string Height = "";

        public string Width = "";

        public string Count = "";

        public string FileName = "";

        public string ToCSVStr()
        {
            return Num+","+ Code + "," + Height + "," + Width + "," + Count + "," + FileName + ",";
        }
    }
}