using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinformGeneralDeveloperFrame;
using WinformGeneralDeveloperFrame.Commons;
using DevExpress.XtraLayout;
using MES.Entity;
using System.Data.Entity.Migrations;
using System.Data.Entity;
namespace MES.Form
{
	public partial class FrmproductBOMdetail : FrmBaseForm
	{
        private Dictionary<string, string> fieldDictionary = new Dictionary<string, string>();
		public FrmproductBOMdetail()
		{
			InitializeComponent();
		}
		private void FrmproductBOMdetail_Load(object sender, EventArgs e)
        {
			InitFrom(xtraTabControl1,grdList,grdListView,new LayoutControlGroup[]{layoutControlGroup1},new productBOMdetailInfo());
            InitSearchDicData();
        }
        /// <summary>
		/// 数据源初始化
		/// </summary>
		/// <returns></returns>
		private void Init()
		{
				txtpid.Properties.DataSource = GetDataTableUtils.SqlTable("物料tree");     
				repositoryItemTreeListtxtpid.DataSource= GetDataTableUtils.SqlTable("物料tree");  
				txtmaterialname.Properties.DataSource = GetDataTableUtils.SqlTable("物料tree");     
				repositoryItemTreeListtxtmaterialname.DataSource= GetDataTableUtils.SqlTable("物料tree");  
				txtmaterialtype.Properties.DataSource = GetDataTableUtils.SqlTable("物料类别");     
				repositoryItemtxtmaterialtype.DataSource= GetDataTableUtils.SqlTable("物料类别");  
				txtunit.Properties.DataSource = GetDataTableUtils.SqlTable("计量单位");     
				repositoryItemtxtunit.DataSource= GetDataTableUtils.SqlTable("计量单位");  
				txtwarehouse.Properties.DataSource = GetDataTableUtils.SqlTable("仓库");     
				repositoryItemtxtwarehouse.DataSource= GetDataTableUtils.SqlTable("仓库");  
		}
        /// <summary>
		/// 搜索字段
		/// </summary>
		/// <returns></returns>
        private void InitSearchDicData()
        {
				fieldDictionary.Add("id","id");     
				fieldDictionary.Add("父项","pid");     
				fieldDictionary.Add("BOM主表","productBOMid");     
				fieldDictionary.Add("BOM编号","productBOMcode");     
				fieldDictionary.Add("产品编号","productcode");     
				fieldDictionary.Add("物料id","materialid");     
				fieldDictionary.Add("物料名称","materialname");     
				fieldDictionary.Add("物料编号","materialcode");     
				fieldDictionary.Add("规格型号","materialspec");     
				fieldDictionary.Add("物料类型","materialtype");     
				fieldDictionary.Add("单位用量","unitusenumber");     
				fieldDictionary.Add("计量单位","unit");     
				fieldDictionary.Add("仓库","warehouse");     
				fieldDictionary.Add("单价","unitprice");     
				fieldDictionary.Add("金额","money");     
				fieldDictionary.Add("备注","remark");     
        }

		public override void InitgrdListDataSource()
        {
			 using (var con=new MESDB())///
            {
				grdList.DataSource=con.productBOMdetailInfo.ToList();
            }
            Init();
		}
	    /// <summary>
		/// 字段为空校验
		/// </summary>
		/// <returns></returns>
        public override bool CheckInput()
        {
			if(string.IsNullOrEmpty(txtpid.EditValue.ToString()))
			{
				"父项不能为空".ShowWarning();
				txtpid.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtproductBOMid.EditValue.ToString()))
			{
				"BOM主表不能为空".ShowWarning();
				txtproductBOMid.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtproductBOMcode.EditValue.ToString()))
			{
				"BOM编号不能为空".ShowWarning();
				txtproductBOMcode.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtproductcode.EditValue.ToString()))
			{
				"产品编号不能为空".ShowWarning();
				txtproductcode.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtmaterialid.EditValue.ToString()))
			{
				"物料id不能为空".ShowWarning();
				txtmaterialid.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtmaterialname.EditValue.ToString()))
			{
				"物料名称不能为空".ShowWarning();
				txtmaterialname.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtmaterialcode.EditValue.ToString()))
			{
				"物料编号不能为空".ShowWarning();
				txtmaterialcode.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtmaterialspec.EditValue.ToString()))
			{
				"规格型号不能为空".ShowWarning();
				txtmaterialspec.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtmaterialtype.EditValue.ToString()))
			{
				"物料类型不能为空".ShowWarning();
				txtmaterialtype.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtunitusenumber.EditValue.ToString()))
			{
				"单位用量不能为空".ShowWarning();
				txtunitusenumber.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtunit.EditValue.ToString()))
			{
				"计量单位不能为空".ShowWarning();
				txtunit.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtwarehouse.EditValue.ToString()))
			{
				"仓库不能为空".ShowWarning();
				txtwarehouse.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtunitprice.EditValue.ToString()))
			{
				"单价不能为空".ShowWarning();
				txtunitprice.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtmoney.EditValue.ToString()))
			{
				"金额不能为空".ShowWarning();
				txtmoney.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtremark.EditValue.ToString()))
			{
				"备注不能为空".ShowWarning();
				txtremark.Focus();
				return false;
			}
			return true;
        }
        /// <summary>
		/// 保存
		/// </summary>
		/// <returns></returns>
		public override bool SaveFunction()
        {
            try
            {
                productBOMdetailInfo info= (productBOMdetailInfo)this.ControlDataToModel(new productBOMdetailInfo());
                using (var db = new MESDB())
                {
                    db.productBOMdetailInfo.AddOrUpdate(info);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                ex.Message.ShowError();
                return false;
            }
            return true;
        }
        /// <summary>
		/// 删除
		/// </summary>
		/// <returns></returns>
		public override bool DelFunction()
        {
            try
            {
                productBOMdetailInfo info = (productBOMdetailInfo)this.ControlDataToModel(new productBOMdetailInfo());
                using (var db = new MESDB())
                {
                    db.Entry(info).State=EntityState.Deleted;
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                ex.Message.ShowError();
                return false;
            }
            return true;
        }
        /// <summary>
		/// 搜索
		/// </summary>
		/// <returns></returns>
        public override void SearchFunction()
        {
            FrmSearch frm = new FrmSearch(fieldDictionary);
            if (frm.ShowDialog()==DialogResult.OK)
            {
                string sql = frm.sql;
                using (var db = new MESDB())
                {
                    if (string.IsNullOrEmpty(sql))
                    {
                        grdList.DataSource = db.productBOMdetailInfo.SqlQuery("select * from productBOMdetail").ToList();
                    }
                    else
                    {
                        grdList.DataSource = db.productBOMdetailInfo.SqlQuery($"select * from productBOMdetail where {sql}").ToList();
                    }
                }
            }
        }
	}
}