FrmAddCom.cs 6.4 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 string getListMsg(List<string> list)
        {
            string msg = "";
            foreach (string str in list)
            {
                msg += str + ",";
            }
            return msg.Substring(0, msg.Length - 1);
        }
        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 = CSVBomManager.ReadFile(fileName);


                if (comList.Count <= 0)
                {
                    MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SelectRightFile, "请选择正确的文件(文件格式可以通过下载模板获得)"));
                }
                Dictionary<string, List<string>> PnPositonMap = new Dictionary<string, List<string>>();
                Dictionary<string, List<string>> positionPNMap = new Dictionary<string, List<string>>();

                foreach (ComponetInfo com in comList)
                {
                    if (!PnPositonMap.ContainsKey(com.PN))
                    {
                        PnPositonMap.Add(com.PN, new List<string>());
                    }
                    if (!PnPositonMap[com.PN].Contains(com.PositionNum))
                    {
                        PnPositonMap[com.PN].Add(com.PositionNum);
                    }


                    if (!positionPNMap.ContainsKey(com.PositionNum))
                    {
                        positionPNMap.Add(com.PositionNum, new List<string>());
                    }
                    if (!positionPNMap[com.PositionNum].Contains(com.PN))
                    {
                        positionPNMap[com.PositionNum].Add(com.PN);
                    }
                }

                foreach (string pnKey in PnPositonMap.Keys)
                {
                    List<string> positions = new List<string>(PnPositonMap[pnKey]);
                    if (positions.Count > 1)
                    {
                        result = MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SureImportPosition,
                            "元器件【{0}】配置多个位置【{1}】,是否确定导入?", pnKey, getListMsg(positions), "",
                           MessageBoxButtons.OKCancel)); ;
                        if (!result.Equals(DialogResult.OK))
                        {
                            comList = new List<ComponetInfo>();
                            return;
                        }
                    }
                }

                foreach (string key in positionPNMap.Keys)
                {
                    List<string> pnList = new List<string>(positionPNMap[key]);
                    if (pnList.Count > 1)
                    {
                        result = MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SureImportPn,
                            "位置【{0}】配置多个元器件【{1}】,是否确定导入?", key, getListMsg(pnList), "",
                           MessageBoxButtons.OKCancel)); ;
                        if (!result.Equals(DialogResult.OK))
                        {
                            comList = new List<ComponetInfo>();
                            return;
                        }
                    }

                }
            }
        }

        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 (CSVBomManager.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 (CSVBomManager.AddBom(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();
        }
    }
}