FrmAddCom.cs 5.5 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.DeviceLibrary;
using TSA_V.LoadCSVLibrary;

namespace TSA_V
{
    public partial class FrmAddCom : FrmBase
    {
        public FrmAddCom()
        {
            InitializeComponent();
        }

        private void FrmAddCom_Shown(object sender, EventArgs e)
        {
            SetSkin(this);
            LanguageProcess();
        }
        private List<ComponetInfo> comList = new List<ComponetInfo>();
        private void btnBomImport_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); 
            DialogResult result = this.openFileDialog1.ShowDialog();
            if (result.Equals(DialogResult.OK))
            {
                string fileName = this.openFileDialog1.FileName;

                comList = CSVReaderBomManager.ReadFile(fileName);

                if (comList.Count > 0)
                {
                    Dictionary<string, string> namePositonMap = new Dictionary<string, string>();
                    foreach (ComponetInfo com in comList)
                    {
                        if (!namePositonMap.ContainsKey(com.ComponentName))
                        {
                            //验证库位是否存在过
                            List<string> positinList = new List<string>();
                            if (positinList.Contains(com.ComponentName))
                            {
                                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.CheckFile,"两个不同的元器件中都配置在库位【{0}】中,请检查文件",com.PositionNum));
                                comList = new List<ComponetInfo>();
                                return  ;
                            }
                            namePositonMap.Add(com.ComponentName, com.PositionNum);
                        }
                        else
                        {
                            string prePosition = namePositonMap[com.ComponentName];
                            if (!prePosition.Equals(com.PositionNum))
                            {
                                 result = MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SureImport,
                                     "元器件【{0}】配置了两个位置【{1}】和【{2}】,是否确定导入?",com.ComponentName,prePosition,com.PositionNum), "",
                                    MessageBoxButtons.YesNoCancel); ;
                                if (!result.Equals(DialogResult.Yes))
                                {
                                    comList = new List<ComponetInfo>();
                                    return  ;
                                }
                            }
                        }
                    }
                    this.txtBomFilePath.Text = fileName;
                }
                else
                {
                    MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SelectRightFile,"请选择正确的文件(文件格式可以通过下载模板获得)"));
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (this.txtBomName.Text.Equals("") || this.txtBomFilePath.Text.Equals(""))
            {

            }
            this.Close();
        }
        private bool SaveComList()
        {
            string bomName = txtBomName.Text;
            if (bomName.Equals(""))
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.WriteCName,"请先输入元器件库名称!"));
                txtBomName.Focus();
                return false;
            }
            if (CSVReaderBomManager.allComMap.ContainsKey(bomName))
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.CNameHasExits,"元器件库【{0}】已存在,请重新输入!",bomName));
                txtBomName.Focus();
                return false;
            }
            if (comList.Count <= 0)
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.ImportFile,"请导入元件文件!"));
                txtBomName.Focus();
                return false;
            }
           
            if (CSVReaderBomManager.AddBomList(bomName, comList))
            { 
                return true;
            } return false;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            bool result = SaveComList();
            if (result)
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SaveOk,"保存成功!"));
                this.Close();
            }
             
        }


        private void btnBeginIn_Click(object sender, EventArgs e)
        {
            bool result = SaveComList();
            if (result)
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SaveOk,"保存成功!"));
                this.Visible = false;
                FrmPutCom frm = new FrmPutCom();
                if (frm.SetOperateInfo(txtBomName.Text, comList))
                {
                    frm.ShowDialog();
                    this.Close();
                }
            }
             
       
        }

        private void FrmAddCom_Load(object sender, EventArgs e)
        {
            SetScreen();
        }
    }
}