HistoryDb.cs 2.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

using FLY.OBJComponents.Server;
using FLY.OBJComponents.Server.Model;

using FObjBase;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.Weight2.Server.Model
{
    /// <summary>
    /// 数据库 写操作
    /// </summary>
    public class HistoryDb
    {
        DbModel dbModel;
        public BufferSQLite<Lc_Flow, Db_Flow> FlowBuffer;
        public BufferError ErrorBuffer;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dBModel"></param>
        public void Init(DbModel dBModel, int weighterCnt)
        {
            this.dbModel = dBModel;
            FlowBuffer = new BufferSQLite<Lc_Flow, Db_Flow>();
            FlowBuffer.Init(dBModel.TbFlow,
                (lc) => Lc_AutoMapperProfile.Mapper.Map<Db_Flow>(lc),
                (db) => Lc_AutoMapperProfile.Mapper.Map<Lc_Flow>(db)
                );

            ErrorBuffer = new OBJComponents.Server.BufferError();
            ErrorBuffer.Init(dBModel.TbError);
        }

        /// <summary>
        /// 按时间删除数据库
        /// </summary>
        /// <param name="month"></param>
        public void KeepDBSize(int month)
        {
            if (month <= 3)
                month = 3;

            DateTime del_time = DateTime.Now - TimeSpan.FromDays(month * 30);
            string det_time_str = del_time.ToStringOfSQLiteFieldType();
            List<string> sqls = new List<string>();
            sqls.Add(
                $"DELETE FROM {dbModel.TbFlow.TableName}" +
                $" WHERE Time < {det_time_str}");

            sqls.Add(
                $"DELETE FROM {dbModel.TbError.TableName}" +
                $" WHERE Time < {det_time_str}");

            dbModel.sqliteHelper.QueryTran(sqls);

        }

        public void AddFlow(
            Lc_Flow flow
            )
        {
            FlowBuffer.Add(flow);
        }
        public void AddFlowRange(IEnumerable<Lc_Flow> flows) 
        {
            FlowBuffer.AddRange(flows);
        }

    }
}