测试

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml.Linq;
using DataImportLib;
using Microsoft.Win32;

namespace DataImport
{
    public partial class Form1 : Form
    {
        List<List<Item>> LstItems = new List<List<Item>>();
        IWork iWork = new Work();
        /// <summary>
        /// 是否正在处理数据(默认false)
        /// </summary>
        bool run = false;
       
        List<int> Hours;

        /// <summary>
        /// 获取每月的多少天
        /// </summary>
        int startDay = int.Parse("Day".AppSettValue());
        /// <summary>
        /// 获取每小时的多少分钟
        /// </summary>
        //int startMinute = int.Parse("Minute".AppSettValue());
        /// <summary>
        /// 获取扫描时间间隔
        /// </summary>
        int scanInterVal = int.Parse("ScanInterVal".AppSettValue());
        /// <summary>
        /// 自动运行
        /// </summary>
        bool Auto = (Convert.ToInt32("AutoRun".AppSettValue()) == 1) ? true : false;
        /// <summary>
        /// 类型
        /// </summary>
        int type = int.Parse("Type".AppSettValue());
        /// <summary>
        /// 源数据库连接字符
        /// </summary>
        string SrcCon = "SrcCon".AppSettValue();
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 时间控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < LstItems.Count; i++)
            {
                List<Item> Item = LstItems[i];
                int hour;
                bool isHour = int.TryParse(Item[0].Hour, out hour);

                if ((type == 0 || DateTime.Now.Day == startDay) && DateTime.Now.Hour == hour
                   && DateTime.Now.Minute == 0)
                {
                    try
                    {
                        Log.WriteLine("开始处理数据!");
                        run = true;
                        DateTime start = DateTime.Now.AddDays(-1);
                        DateTime end = DateTime.Now.AddDays(-1);
                        string filePath = null;
                        //foreach (List<Item> Item in LstItems)
                        //{ 

                        foreach (Item item in Item)
                        {
                           // "SrcCon".AppSettValue(item.SrcCon);
                            string XmlPath = item.XmlPath;

                            if (XmlPath != "")
                            {
                                filePath = iWork.CreateFile(item, start);
                            }

                            iWork.Save(item, start, end, filePath);
                        }
                        //}

                        run = false;
                        Log.WriteLine("处理数据完成!");

                    }
                    catch (Exception ee)
                    {
                        Log.WriteLine(ee.Message);
                    }
                }
            }

            //for (int i = 0; i < Hours.Count; i++)
            //{
            //    //if ((type == 0 || DateTime.Now.Day == startDay) && DateTime.Now.Hour == Hours[i]
            //    //   && DateTime.Now.Minute == 0)
            //    //{
            //        try
            //        {
            //            Log.WriteLine("开始处理数据!");
            //            run = true;
            //            DateTime start = DateTime.Now.AddDays(-1);
            //            DateTime end = DateTime.Now.AddDays(-1);
            //            string filePath = null;
            //            foreach (List<Item> Item in LstItems)
            //            { 
                         
            //                foreach (Item item in Item)
            //                {   
            //                    //"SrcCon".AppSettValue(item.SrcCon);
            //                    string XmlPath = item.XmlPath;

            //                    if (XmlPath != "")
            //                    {
            //                        filePath = iWork.CreateFile(item, start);     
            //                    } 

            //                    iWork.Save(item, start, end, filePath);                                
            //                }
            //            }

            //            run = false;
            //            Log.WriteLine("处理数据完成!");

            //        }
            //        catch (Exception ee)
            //        {
            //            Log.WriteLine(ee.Message);
            //        }
            //    //}
            //}
        }

        private void GetDateTime(ref DateTime start, ref DateTime end)
        {
            if (type == 0)
            {
                start = start.AddDays(-1).ToString("yyyy-MM-dd").ToDateTime();
                end = start.AddDays(1);
            }
            else
            {
                start = start.AddMonths(-1).ToString("yyyy-MM-01").ToDateTime();
                end = start.AddMonths(1);
            }
        }
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            readFloder();
            chkRun.Checked = Auto;
            timer1.Interval = scanInterVal;//设置扫描时间间隔

            try
            {
                this.notifyIcon1.Visible = false;

                if (Auto && XmlcheckList.CheckedItems.Count > 0)
                {
                    btn_OK_Click(sender, e);
                }
                else
                {
                    SetTime(false); 
                    btnStart.Enabled = false;
                }

                type = int.Parse("Type".AppSettValue());
                cmbType.SelectedIndex = type;
                cmbXml_SelectedIndexChanged(null, null);
            }
            catch (Exception ee)
            {
                Log.WriteLine(ee.Message);
            }
        }

        public string Check(List<Item> item)
        {
            string rs = CheckCon(item[0]);

            if (!rs.IsNullOrWhiteSpace())
            {
                return rs;
            }

            rs = CheckTable(item);
            return rs;

        }


        private string CheckTable(List<Item> item)
        {

            return iWork.CheckTable(item);
        }


        private void SetTime(bool flag)
        {
            if (!run)
            {
                timer1.Enabled = flag;
                btnStart.Enabled = !flag;
                btnStop.Enabled = flag;

                if (flag)
                {
                    timer1.Start();
                }
                else
                { 
                    timer1.Stop(); 
                }
            }
            else
            {
                MessageBox.Show("数据处理正在运行你不能操作!");
            }
        }

        private string CheckCon(Item item)
        {
            string checkCon = "";

            if (!iWork.TestCon(SrcCon))
            {
                checkCon += "源数据库连接不通,请检查SrcCon连接字符串!\n";
            }

            return checkCon;
        }


        //吴鑫 2013.4.8 注释 结束
        private void SetAuto()
        {
           
            try
            {
                string starupPath = Application.ExecutablePath;

                //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装.
                RegistryKey loca = Registry.LocalMachine;
                RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                string path = run.GetValue("DataImport") == null ? "" : run.GetValue("DataImport").ToString();
                
                if ("AutoRun".AppSettValue() == "1")
                {
                    if (path.IsNullOrWhiteSpace() || path != starupPath)
                    {
                        //SetValue:存储值的名称
                        run.SetValue("DataImport", starupPath);
                    }

                    Log.WriteLine("添加自动运行成功!");
                }
                else
                {
                    if (!path.IsNullOrWhiteSpace())
                    {
                        run.DeleteValue("DataImport");
                        Log.WriteLine("取消自动运行!");
                    }
                }

                loca.Close();
            }
            catch (Exception ee)
            {
                Log.WriteLine(ee.Message);
            }

        }
        /// <summary>
        /// 设置路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSetPath_Click(object sender, EventArgs e)
        {

            if (!Directory.Exists(txtXMLPath.Text.Trim()))
            {
                MessageBox.Show("此路径不存在", "设置失败");
                return;
            }

            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "XmlPath".AppSettValue();
                var xml = XElement.Load(path + cmbXml.Text.Trim());
                xml.Element("XmlPath").Value = this.txtXMLPath.Text.Trim();
                xml.Save(path + cmbXml.Text.Trim());
                MessageBox.Show("设置成功");
            }
            catch(Exception error)
            {
                Log.WriteLine(error.Message);
                MessageBox.Show("设置失败");
            }
        }
        /// <summary>
        /// XML文件路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBrowser_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtXMLPath.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            SetTime(true);
        }
        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            SetTime(false);
        }
        /// <summary>
        /// 回补
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBack_Click(object sender, EventArgs e)
        {
            try
            {
                if (!run)
                {

                    if (!cmbTables.Text.IsNullOrWhiteSpace())
                    {
                        string path = AppDomain.CurrentDomain.BaseDirectory + "XmlPath".AppSettValue();

                        List<Item> ListItem = iWork.GetXmlConfigAndSetAppConfig(path + cmbAreaType.Text.Trim());
                        if (cmbTables.Text == "全部")
                        {

                            MessageBox.Show("aswerwerwer");
 
                        }
                       

                        if (ListItem != null && ListItem.Count > 0)
                        {
                            iWork.SetCon();

                            string filePath = null;

                            string XmlPath = ListItem[0].XmlPath;

                            DateTime dtStart = dtpBackDate.Value.Date;
                            DateTime dtEnd = dtStart;

                            if (!string.IsNullOrWhiteSpace(XmlPath))
                            {
                                filePath = iWork.CreateFile(ListItem.Find(p => p.Name == cmbTables.Text.Trim()), dtStart);
                            }

                            iWork.Save(ListItem.Find(p => p.Name == cmbTables.Text.Trim()), dtStart, dtEnd, filePath);
                            MessageBox.Show("回补成功");
                        }
                        else
                        {
                            MessageBox.Show("XML文件无有效信息", "回补失败");
                            return;
                        }

                    }
                    else
                    {
                        MessageBox.Show("请选择您要回补的表", "回补失败");
                    }
                }
                else
                {
                    MessageBox.Show("数据处理正在运行你不能操作", "警告");
                }
            }
            catch (Exception ee)
            {
                Log.WriteLine(ee.Message);
                MessageBox.Show("软件发生异常请联系管理员", "错误");

            }
        }

        private void form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                NormalToMinimized();
            }
        }

        /// <summary>
        /// 单击显示窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.MinimizedToNormal();
            }
            else if (e.Button == MouseButtons.Right)
            {
                contextMenuStrip1.Show();
            }
        }

        private void MinimizedToNormal()
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            notifyIcon1.Visible = false;

        }

        private void NormalToMinimized()
        {
            this.WindowState = FormWindowState.Minimized;
            this.Visible = false;
            this.notifyIcon1.Visible = true;
        }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            NormalToMinimized();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.notifyIcon1.Visible = false;
            this.Close();
            this.Dispose();
            Application.Exit();
        }
        /// <summary>
        /// 设置每月每天
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnType_Click(object sender, EventArgs e)
        {
            type = cmbType.Text == "每天" ? 0 : 1;
            "Type".AppSettValue(type.ToString());
            "AutoRun".AppSettValue(chkRun.Checked ? "1" : "0");
            MessageBox.Show("设置成功","提示信息");
            SetAuto();
        }

        /// <summary>
        /// 清除数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("所有的数据要被清除,你确认吗?", "清楚", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                foreach (List<Item> Item in LstItems)
                {
                    iWork.Clear(Item);
                }
 
            }
        }

        /// <summary>
        /// 每天,每月
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbType_TextChanged(object sender, EventArgs e)
        {
            string Type = this.cmbType.Text.ToString();

        }

        /// <summary>、
        /// 姓名:吴鑫
        /// 功能:选中执行配置文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_OK_Click(object sender, EventArgs e)
        {
            // 开始  修改 应雷明 2013.4.8
            List<Item> ListItem = null;
            string path = AppDomain.CurrentDomain.BaseDirectory + "XmlPath".AppSettValue();

            if (XmlcheckList.CheckedItems.Count == 0)
            {
                MessageBox.Show("请至少选择一项", "设置失败");
                return;
            }

            //Hours = new List<int>();

            string xmlName = "";

            foreach (object str in XmlcheckList.CheckedItems)
            {
                ListItem = iWork.GetXmlConfigAndSetAppConfig(path + str.ToString());

                if (ListItem != null && ListItem.Count > 0)
                {
                    LstItems.Add(ListItem);

                    //int hour;
                    //bool isHour = int.TryParse(ListItem[0].Hour, out hour);
                    //Hours.Add(hour);

                    iWork.SetCon();

                    string mess = iWork.CreateDataBase();

                    if (!string.IsNullOrWhiteSpace(mess))
                    {
                        Log.WriteLine(mess);
                        MessageBox.Show(mess, "设置失败");

                        return;
                    }

                    string tableMess = iWork.CheckTable(ListItem);

                    if (!string.IsNullOrWhiteSpace(tableMess))
                    {
                        Log.WriteLine(tableMess);
                        MessageBox.Show(tableMess, "设置失败");
                        return;
                    }

                    xmlName += "," + str.ToString();
                }
                else
                {
                    MessageBox.Show("XML文件无有效信息", "设置失败");
                    return;
                }
            }

            "XmlTemp".AppSettValue(xmlName.Trim(','));

            SetTime(true);
            MessageBox.Show("设置成功");

            //结束 修改 应雷明 2013.4.8
        }

        /// <summary>
        /// 姓名:吴鑫
        /// 功能:获取文件夹下的所有XML文件
        /// 时间:2013.4.8
        /// </summary>
        /// <param name="strWriteXml"></param>
        public void readFloder()
        {
            string XmlPath = System.AppDomain.CurrentDomain.BaseDirectory + "XmlPath".AppSettValue();
            ArrayList a = new ArrayList();
            DirectoryInfo d = new DirectoryInfo(XmlPath);

            FileInfo[] fis = d.GetFiles("*.xml");
            
            foreach (FileInfo fi in fis)
            {
                a.Add(fi.Name);
                this.XmlcheckList.Items.Add(fi);
            }

            string xmlName = "XmlTemp".AppSettValue();

            for (int i = 0; i < this.XmlcheckList.Items.Count; i++)
            {

                if (xmlName.Contains(XmlcheckList.Items[i].ToString()))
                {

                    this.XmlcheckList.SetItemChecked(i, true);
                }

            }

            this.cmbAreaType.DataSource = fis;
            cmbXml.DataSource = fis;
        }


        /// <summary>
        /// 区域类型,如:阳光,区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbAreaType_TextChanged(object sender, EventArgs e)
        {
            cmbTables.Items.Clear();
            string XmlName = cmbAreaType.Text.Trim();
            string path = AppDomain.CurrentDomain.BaseDirectory + "XmlPath".AppSettValue();
            var xml = XElement.Load(path + XmlName);

            foreach (var t in xml.Elements("Item"))
            {
                string Name = t.Element("Name").Value.TrimNull();
                cmbTables.Items.Add(Name);
            }
            cmbTables.Items.Add("全部");
        }

        private void cmbXml_SelectedIndexChanged(object sender, EventArgs e)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "XmlPath".AppSettValue();
            var xml = XElement.Load(path + cmbXml.Text.Trim());

            txtXMLPath.Text = xml.Element("XmlPath").Value;
        }

    }
}
展开阅读全文
©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读