using FLY.Thick.FilmCasting.Server.Model;
using SQLite;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class TestDb
    {
        DbModel dbModel;

        static void Main(string[] args)
        {
            Console.WriteLine("生成假数据库数据");
            TestDb p = new TestDb();


            p.Init();
            p.Debug_Db();
            Console.Read();
        }

        public void Init()
        {
            //手动先删除数据库
            //@"D:\film_casting_data\thickness.sqlite3"
            Directory.Delete(@"D:\film_casting_data", true);
           
            dbModel = new DbModel();
            dbModel.Init();
        }
        public void Debug_Db()
        {
            //创建8个月的数据
            DateTime startTime = DateTime.Now - TimeSpan.FromDays(30 * 8);
            DateTime time = startTime;
            DateTime sampleTime = DateTime.MinValue;
            int number = 0;

            int boltCnt = 120;

            Db_Profile profile = new Db_Profile()
            {
                ID = dbModel.TbProfile.FreeID,
                PName = "abc",
                Batch = "abc",
                Number = number.ToString(),
                StartTime = time,
                EndTime = time,
                ScanBegin = 10,
                ScanEnd = boltCnt - 1 - 10,
                Target = 110,
                TolerancePercent = 0.03,
                IsFinished = false
            };

            double position = 0;
            double filmVelocity = 20;
   
            Random r = new Random();
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            List<string> sqls = new List<string>();
            int cnt = 0;
            int sample0Ad = 50300;
            int leftNoUsed = 10;
            int rightNoUsed = 12;
            double filmWidth = (boltCnt - leftNoUsed - rightNoUsed) * 25.4 * (1 - 0.03);






            while (time < DateTime.Now)
            {
                //每1小时1个产品
                //每10分钟添加一条样品
                if ((sampleTime == DateTime.MinValue) || (time - sampleTime > TimeSpan.FromMinutes(10)))
                {
                    sampleTime = time;
                    sample0Ad = 50300 + r.Next(50) - 25;

                    Lc_Sample sampleData = new Lc_Sample()
                    {
                        Time = time,
                        SampleADs = new int[] { sample0Ad, -1, -1 }
                    };
                    sampleData.ID = dbModel.TbSample.FreeID;
                    var db_sampleData = Lc_AutoMapperProfile.Mapper.Map<Db_Sample>(sampleData);
                    sqls.Add(SQLiteHelper.GetInsertCommandText(db_sampleData));
                }

                //每次都添加扫描数据
                Lc_ScanData lc_ScanData;
                {
                    double[] datas = new double[boltCnt];
                    for (int i = 0; i < boltCnt; i++)
                    {
                        datas[i] = double.NaN;
                    }

                    for (int i = leftNoUsed; i < boltCnt - rightNoUsed; i++)
                    {
                        datas[i] = (r.NextDouble() - 0.5) * 0.02 * profile.Target + profile.Target;
                    }

                    lc_ScanData = new Lc_ScanData()
                    {
                        ID = dbModel.TbScanData.FreeID,
                        Time = time - TimeSpan.FromSeconds(25),
                        EndTime = time,
                        K = 1,
                        B = 0,
                        FilmPosition = Math.Round(position, 2),
                        FilmVelocity = filmVelocity,
                        FilmWidth = (int)(filmWidth + (r.NextDouble() - 0.5) * 3),
                        Thicks = datas,
                        SampleADs = new int[] { sample0Ad, -1, -1 },
                        Target = profile.Target,
                        TolerancePercent = profile.TolerancePercent
                    };
                    var db_ScanData = Lc_AutoMapperProfile.Mapper.Map<Db_ScanData>(lc_ScanData);
                    sqls.Add(SQLiteHelper.GetInsertCommandText(db_ScanData));
                }
               
                time += TimeSpan.FromSeconds(30);
                position += filmVelocity * 7 / 60;
                profile.EndTime = time;
                if (time - profile.StartTime > TimeSpan.FromMinutes(60))
                {
                    //换卷
                    var db_profile = Lc_AutoMapperProfile.Mapper.Map<Db_Profile>(profile);
                    sqls.Add(SQLiteHelper.GetInsertCommandText(db_profile));

                    //全部数据写入
                    cnt++;
                    if (cnt > 24)
                    {
                        dbModel.sqliteHelper.QueryTran(sqls);
                        sqls.Clear();

                        cnt = 0;
                        stopwatch.Stop();
                        TimeSpan ts = stopwatch.Elapsed;
                        stopwatch.Restart();
                        double sec = (DateTime.Now - time).TotalDays * ts.TotalSeconds;
                        ts = TimeSpan.FromSeconds(sec);

                        //每24小时保存一次
                        Console.WriteLine($"number={number} time={time:MM/dd}  估计剩余:{ts.TotalMinutes:F1}分钟");
                    }
                    number++;
                    position = 0;

                    profile.ID = dbModel.TbProfile.FreeID;
                    profile.Number = number.ToString();
                    profile.StartTime = time;
                    profile.EndTime = time;
                }
            }

            //保存最后一个
            {
                var db_profile = Lc_AutoMapperProfile.Mapper.Map<Db_Profile>(profile);
                sqls.Add(SQLiteHelper.GetInsertCommandText(db_profile));


                //全部数据写入
                dbModel.sqliteHelper.QueryTran(sqls);
                sqls.Clear();

                Console.WriteLine($"number={number} time={time:yyyy/MM/dd HH:mm} 完成");
            }

            //备份上一个月之前的数据到 yyyy-MM 文件夹
            Console.WriteLine($"备份上一个月之前的数据到 yyyy-MM 文件夹");
            dbModel.BackupBbInSize(12);


            //当前的数据库只保存上个月与这个月的数据
            Console.WriteLine($"当前的数据库只保存上个月与这个月的数据");
            dbModel.KeepDBSize(1);

            Console.WriteLine($"完成");
        }
    }
}
