FrmAnalyze.cs 7.1 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.AddHours(-6);
                //dtpStartTime.Value = new DateTime(time1.Year, time1.Month, time1.Day, 0, 0, 0);
                dtpStartTime.Value = time1;
                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;
            }
            //RobotManager.robot.sQLite.Insert("", 0, 0, 0, "123");
            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();

            try
            {
                lastList = new List<XRayHistory>();
                string[][] array = null;
                string str = "yyyy-MM-dd HH:mm:ss";
                string startStr = startTime.ToString(str);
                string endStr = endTime.ToString(str);
                bool result = RobotManager.robot.sQLite.Select(code, startStr,endStr, out array);
                LogUtil.error("查数据【" + code + "】【" + startStr + "】【" + endStr + "】结果:" + result + "," + RobotManager.robot.sQLite.ErrInfo);
               
                if (array != null && array.Length > 0)
                {
                    LogUtil.info("共查询到【" + array.Length + "】行数据");
                    label4.Text = "开始时间["+ startStr + "] 结束时间["+ endStr + "] 条码["+ code + "] 共查询到"+ 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;
                        if (a.Length >= 6)
                        {
                            XRayHistory his = new XRayHistory(index, a[0], a[1], a[2], a[3], a[4], a[5]);
                            lastList.Add(his);
                            view.Cells[Column_Code.Index] .Value= his.Code;
                            view.Cells[Column_Count.Index].Value = his.Count;
                            view.Cells[Column_Data.Index].Value = his.DataStr;
                            view.Cells[Column_Height.Index].Value = his.Height;
                            view.Cells[Column_Width.Index].Value = his.Width;
                            view.Cells[Column_ImageName.Index].Value = his.FileName;

                        }
                      
                        dataGridView1.Rows.Add(view);
                        index++;
                    }
                }
                else
                { 
                    this.dataGridView1.Rows.Clear();
                    MessageBox.Show("未查询到点料记录");
                }
            }catch(Exception ex)
            {
                LogUtil.error("查询数据出错:" + ex.ToString());
            }
        }
        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.UTF8);
                        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,string dataStr)
        {
            this.Code = code;
            this.Height = h;
            this.Width = w;
            this.Count = c;
            this.FileName = fname;
            this.DataStr = dataStr;
        }

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

        public string Height = "";

        public string Width = "";

        public string Count = "";

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