using System;
using System.Collections.Generic;
using ZJ_BYD.DB;
using ZJ_BYD.Model;
using ZJ_BYD.Untils;

namespace ZJ_BYD.Common
{
    public class InitSysData
    {
        /// <summary>
        /// 初始化工位数据
        /// </summary>
        public static void InitStation()
        {            
            var haveStation = StationHelper.QueryStations().Where(m => m.IpcId == Program.CurrentIpcId).ToList().Count > 0;
            if (!haveStation)
            {
                var station = new T_Station
                {
                    IpcId = Program.CurrentIpcId,
                    LineCode = Program.CurrentLineCode,
                    StationCode = "001",
                    StationName = "默认工位",
                    CreatedTime = DateTime.Now,
                    CreatedBy = "系统生成"
                };
               StationHelper.AddStation(station);
            }
        }

        /// <summary>
        /// 初始化PLC点位
        /// </summary>
        /// <param name="stationCode"></param>
        public static void InitPlcPoint(string stationCode = "001")
        {           
            var havePoin = PlcPointHelper.QueryPlcPoints().Where(m => m.IpcId == Program.CurrentIpcId).ToList().Count > 0;
            if (!havePoin)
            {
                var systemPoints = XmlHelper.GetSystemPoints();
                if (systemPoints.Count > 0)
                {
                    List<T_PlcPoint> plcPoints = new List<T_PlcPoint>();
                    foreach (var item in systemPoints)
                    {
                        var t_PlcPoint = new T_PlcPoint
                        {
                            IpcId = Program.CurrentIpcId,
                            LineCode = Program.CurrentLineCode,
                            StationCode = stationCode,
                            PointCode = item.PointCode,
                            PointStartAddress = item.Address,
                            PointDataType = item.DataType,
                            PointLength = item.Length,
                            PointName = item.PointName,
                            IsSystem = item.IsSystem,
                            IsSaveDb = item.IsSaveDb,
                            IsActive = item.IsActive,
                            Remark = item.Remark,
                            SortIndex=item.SortIndex,
                            CreatedTime = DateTime.Now,
                            CreatedBy = "系统生成"
                        };
                        plcPoints.Add(t_PlcPoint);
                    }
                    PlcPointHelper.AddPlcPoints(plcPoints);
                }
            }
        }
    }
}