WPF物料替换源码参考(返回多个数据表)

using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Midea.MES.Plugin.OnlineRepair.ViewModel
{
    class material_sn_replaceVM : ViewModelPlugin
    {
        public material_sn_replaceVM()
        {
            this.MATERIAL_SN_HEAD = new VM_SFC_DC_MATERIAL_SN_HEAD();
            this.MATERIAL_SN_BODY = new ObservableCollection<VM_SFC_DC_MATERIAL_SN_BODY>();              
            this.MATERIAL_SN_RECORD = new ObservableCollection<VM_SFC_DC_MATERIAL_SN_RECORD>();
            this.MATERIAL_SN_INFO = new MATERIAL_SN_INFO();          //所有信息

            //QueryCommand = new RelayCommand(QueryEvent);
            SaveCommand = new RelayCommand<bool>(SaveEvent);
            //SaveCommand = new RelayCommand(SaveEvent);
        }

        #region 全局变量

        //是否检查物料条码规则
        private string g_p_check_material_sn = "Y";
        //定义当前的操作哪一步 
        //* 0 扫入成品SN 
        //* 1 扫入旧物料 
        //* 2 扫入新物料
        private int g_curroper = 0;

        #endregion

        #region 命令事件-方法
        public RelayCommand QueryCommand { set; get; }
        public RelayCommand<bool> SaveCommand { set; get; }
        //public RelayCommand SaveCommand { set; get; }
        
        //子过程,获取成品SN信息
        private bool GETDATA(string Bardcode) 
        {
            bool retval=false;
            var contract = new MessageContract();
            contract.InvokeMethod = "MATERIAL_SN_HEAD_GETDATA";
            contract.MessageToken = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
            contract.Context["p_inv_org_id"] = this.Framework.FrameworkContext[Midea.MES.Platform.Common.FrameworkSymbol.SYS_INFO_KEY_INV_ORG_ID];
            contract.Context["p_sn"] = this.temp_CurrentBardcode;  //"D110001103415626831501";                   ;
            var msgContract = this.Framework.DataService.DoRequest(contract);
            if (msgContract.IsSucess)
            {
                //MATERIAL_SN_INFO = msgContract.GetMessageDataModel<MATERIAL_SN_INFO>();
                MATERIAL_SN_INFO = msgContract.GetMessageDataModelClass<MATERIAL_SN_INFO>();
                if (MATERIAL_SN_INFO != null)
                {
                    //基础信息 
                    this.MATERIAL_SN_HEAD = MATERIAL_SN_INFO.MATERIAL_SN_HEAD;

                    //物料明细信息
                    if (MATERIAL_SN_INFO.MATERIAL_SN_BODY != null)
                    {
                        foreach (var BODY in MATERIAL_SN_INFO.MATERIAL_SN_BODY)
                        {
                            this.MATERIAL_SN_BODY.Add(BODY);
                        }
                    }
                    //物料替换记录
                    if (MATERIAL_SN_INFO.MATERIAL_SN_RECORD != null)
                    {
                        foreach (var RECORD in MATERIAL_SN_INFO.MATERIAL_SN_RECORD)
                        {
                            this.MATERIAL_SN_RECORD.Add(RECORD);
                        }
                    }  
                }

                retval = true;                      
                
             }
            else
            {
                MessageInvoker.ShowError(string.Format("{0}", msgContract.ErrorMessage));
            }

            return retval;
        }

        //设置当前行
        private void SET_CURR_MATERIAL_SN_BODY(string Bardcode) 
        {
            CURR_MATERIAL_SN_BODY = MATERIAL_SN_BODY.FirstOrDefault(p => p.material_sn == Bardcode);
            if (CURR_MATERIAL_SN_BODY != null)
            {
                CURR_MATERIAL_SN_BODY.IsModelSelected = true;
                MessageInvoker.ShowSuccess(string.Format("被替换物料【{0}】匹配成功,请扫描替换后的新物料编码!匹配旧物料【{0}】成功!", Bardcode));
                this.g_curroper = 2;
            }
            else
            {
                MessageInvoker.ShowError(string.Format("扫描物料SN“{0}”与此次替换的成品SN对应的成品SN“{1}”不一致,不允许替换!", Bardcode, this.MATERIAL_SN_HEAD.sn));
                return;
            }
        }

        //条码回车事件
        //* 0 获取成品SN 信息
        //* 1 定位旧物料信息
        //* 2 替换保存物料信息
        private void QueryEvent(string Bardcode) 
        {
            try
            {
                
                if (string.IsNullOrWhiteSpace(Bardcode)) 
                {
                    MessageInvoker.ShowNormal("提示:请输入条码!");
                    return;
                }

                if (g_curroper == 0)          //获取成品SN信息
                {
                    if (GETDATA(Bardcode))
                    {
                        if (this.MATERIAL_SN_HEAD != null)
                        { 
                            MessageInvoker.ShowSuccess(string.Format("加载成品条码【{0}】资料成功!请扫入需要替换的物料条码!", Bardcode));
                            this.g_curroper = 1;
                        }
                    }
                    else
                    {
                        //MessageInvoker.ShowError(string.Format("加载资料出现错误,请重新扫入成品条码进行加载!"));
                        return;
                    }
                    
                }
                else if (g_curroper == 1)      //定位当前旧物料
                {
                    if (this.MATERIAL_SN_BODY != null)
                    {
                        SET_CURR_MATERIAL_SN_BODY(Bardcode);
                        
                    }
                }
                else if (g_curroper == 2)      //扫入新物料 新增一条记录(仅显示)
                {                                   

                    SaveEvent(false);          //扫描提交
                    
                }
                
            }
            catch (Exception ex)
            {
                MessageInvoker.ShowError(ex.Message + Environment.NewLine + ex.StackTrace);
            }
 
        }

        //替换日记增加一条记录
        private void AddRecord(string oldmaterial_sn)
        {

            try
            {
                VM_SFC_DC_MATERIAL_SN_RECORD record = new VM_SFC_DC_MATERIAL_SN_RECORD();
                record.seq = this.MATERIAL_SN_RECORD.Count + 1;
                record.datetime_preview = CURR_MATERIAL_SN_BODY.datetime_modified;
                record.datetime_created = DateTime.Now;
                record.old_material_sn = oldmaterial_sn;
                record.new_material_sn = temp_CurrentBardcode;
                record.mitem_code = CURR_MATERIAL_SN_BODY.mitem_code;
                this.MATERIAL_SN_RECORD.Add(record);                  
            }
            catch
            {
                MessageInvoker.ShowNormal(string.Format("添加替换记录失败!"));
            }

            
        }
        /// <summary>
        /// 保存事件
        /// false - 扫描提交(增加记录)
        /// true  - 按钮提交
        /// </summary>
        /// <param name="Mode"></param>
        private void SaveEvent(bool Mode)
        {
            try
            {
                if (this.MATERIAL_SN_BODY.Count == 0 || this.MATERIAL_SN_BODY == null 
                                                     || (this.g_curroper == 1 && Mode == false))
                {
                    MessageInvoker.ShowNormal("没有需要保存的数据!");
                    return;
                }

                //检查扫描过程中,进行按钮提交
                if (Mode==true)
                {
                    if (g_curroper == 2)      //当前-待扫入替换的物料
                    {
                        MessageInvoker.ShowNormal("请扫入替换的物料后提交!");
                        return;
                    }

                    if (this.g_curroper == 1 && CURR_MATERIAL_SN_BODY==null) //当前-待扫入旧物料
                    {
                        MessageInvoker.ShowNormal("没有数据可以提交!");
                        return;
                    }
                }

                if (CURR_MATERIAL_SN_BODY == null) 
                {
                    MessageInvoker.ShowNormal("请先扫入待替换的物料!");
                    return;
                }

                string oldmaterial_sn = CURR_MATERIAL_SN_BODY.material_sn;
                foreach (var p in MATERIAL_SN_BODY)
                {                                 
                    if (p.material_sn == oldmaterial_sn)
                    {
                        p.material_sn = this.temp_CurrentBardcode;
                        p.IsUpdate = true;  //需更新行
                    }
                }

                //按钮提交
                string strcommit;
                if (Mode)
                {
                    strcommit = "Y";
                }
                else
                {
                    strcommit = this.aotocommit ? "Y" : "N"; 
                }
                
                var contract = new MessageContract();
                contract.InvokeMethod = "MATERIAL_SN_HEAD_SAVEDATA";
                contract.MessageToken = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
                contract.Context["p_inv_org_id"] = this.Framework.FrameworkContext[Midea.MES.Platform.Common.FrameworkSymbol.SYS_INFO_KEY_INV_ORG_ID];
                contract.Context["p_commit"] = strcommit;
                contract.Context["p_sn"] = this.MATERIAL_SN_HEAD.sn;
                contract.Context["p_check_material_sn"] = this.g_p_check_material_sn;    //检查条码规则
                contract.Context["p_reason"] = this.reason;

                contract.Context["MATERIAL_SN_BODY"] = Newtonsoft.Json.JsonConvert.SerializeObject(this.MATERIAL_SN_BODY.Where(m => m.IsUpdate == true), 
                                                                                                   Newtonsoft.Json.Formatting.None);
                

                contract.Context["p_user_modified"] = this.Framework.FrameworkContext[FrameworkSymbol.SYS_INFO_KEY_USER_CODE];  //this.framework.FrameworkContext[Midea.MES.Platform.Common.FrameworkSymbol.SYS_INFO_KEY_USER_CODE]
                
                                            
                var msgContract = this.Framework.DataService.DoRequest(contract);
                if (msgContract.IsSucess)
                {

                    //按钮提交不重复增加记录
                    if (!Mode) 
                    {
                        AddRecord(oldmaterial_sn);
                    }

                    //自动提交数据 或 提交按钮提交数据
                    if (this.aotocommit || Mode==true)
                    {
                        ClearMonitor();
                        this.g_curroper = 0;
                        if (Mode)
                        {
                            MessageInvoker.ShowSuccess(string.Format("数据提交成功!"));
                        }
                        else
                        {
                            MessageInvoker.ShowSuccess(string.Format("操作成功!请扫入下一成品条码!"));
                        }
                        
                    }
                    else
                    {
                        this.g_curroper = 1;
                        MessageInvoker.ShowSuccess(string.Format("操作成功,请继续扫描下一个被替换的新物料编码!"));
                        this.aotocommit = false;
                    }       
                     
                }
                else
                {                               
                    MessageInvoker.ShowError(string.Format("{0}", msgContract.ErrorMessage));
                    //如果替换不成功,需要改回物料代码
                    foreach (var p in MATERIAL_SN_BODY)
                    {
                        if (p.material_sn == this.temp_CurrentBardcode)
                        {
                            p.material_sn = oldmaterial_sn;
                            p.IsUpdate = false;  //需更新行
                        }
                    }
                }

 
            }
            catch (Exception ex)
            {
                MessageInvoker.ShowError(ex.Message);
            }
        }

        /// <summary>
        /// 重置页面
        /// </summary>
        public ICommand CmdReset
        {
            get
            {
                return new RelayCommand(() =>
                {
                    try
                    {
                        if (MsgBoxHelper.ShowQuestion(string.Format("即将重置页面!{0}确定要放弃正在操作的数据吗?", Environment.NewLine), System.Windows.MessageBoxButton.OKCancel) == System.Windows.MessageBoxResult.OK)
                        {
                            ClearMonitor();
                            g_curroper = 0;
                            MessageInvoker.ShowNormal("页面被重置!");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageInvoker.ShowError(ex.Message);
                    }
                }, () =>
                {
                    return true;
                });
            }
        }

        /// <summary>
        /// 清空所有
        /// </summary>
        public void ClearMonitor()
        {
            try
            {
                this.MATERIAL_SN_HEAD = null;
                this.MATERIAL_SN_BODY.Clear();            
                this.MATERIAL_SN_RECORD.Clear();
                this.MATERIAL_SN_INFO = null;
                this.reason = "";
            }
            catch (Exception ex)
            {
                MessageInvoker.ShowError(ex.Message);
            }
        }

        #endregion       

 
        /// <summary>
        /// 所有信息
        /// </summary>
        private MATERIAL_SN_INFO _MATERIAL_SN_INFO;
        public MATERIAL_SN_INFO MATERIAL_SN_INFO
        {
            get { return _MATERIAL_SN_INFO; }
            set
            {
                if (_MATERIAL_SN_INFO != value)
                {
                    _MATERIAL_SN_INFO = value;
                    NotifyPropertyChanged("MATERIAL_SN_INFO");
                }
            }
        }

        /// <summary>
        /// 基础信息
        /// </summary>
        private VM_SFC_DC_MATERIAL_SN_HEAD _MATERIAL_SN_HEAD;
        public VM_SFC_DC_MATERIAL_SN_HEAD MATERIAL_SN_HEAD
        {
            get
            {
                return _MATERIAL_SN_HEAD;
            }
            set
            {
                if (_MATERIAL_SN_HEAD != value)
                {
                    _MATERIAL_SN_HEAD = value;
                    NotifyPropertyChanged("MATERIAL_SN_HEAD");
                }
            }
        }

        /// <summary>
        /// 当前选定的物料采集信息
        /// </summary>
        private VM_SFC_DC_MATERIAL_SN_BODY _CURR_MATERIAL_SN_BODY;
        public VM_SFC_DC_MATERIAL_SN_BODY CURR_MATERIAL_SN_BODY
        {
            get
            {
                return _CURR_MATERIAL_SN_BODY;
            }
            set
            {
                if (_CURR_MATERIAL_SN_BODY != value)
                {
                    _CURR_MATERIAL_SN_BODY = value;
                    NotifyPropertyChanged("CURR_MATERIAL_SN_BODY");
                }
            }
        }     
    
        /// <summary>
        /// 物料采集信息
        /// </summary>
        private ObservableCollection<VM_SFC_DC_MATERIAL_SN_BODY> _MATERIAL_SN_BODY;
        public ObservableCollection<VM_SFC_DC_MATERIAL_SN_BODY> MATERIAL_SN_BODY
        {
            get
            {
                return _MATERIAL_SN_BODY;
            }
            set
            {
                if (_MATERIAL_SN_BODY != value)
                {
                    _MATERIAL_SN_BODY = value;
                    NotifyPropertyChanged("MATERIAL_SN_BODY");
                }
            }
        }      
        /// <summary>
        /// 物料替换记录
        /// </summary>
        private ObservableCollection<VM_SFC_DC_MATERIAL_SN_RECORD> _MATERIAL_SN_RECORD;
        public ObservableCollection<VM_SFC_DC_MATERIAL_SN_RECORD> MATERIAL_SN_RECORD
        {
            get
            {
                return _MATERIAL_SN_RECORD;
            }
            set
            {
                if (_MATERIAL_SN_RECORD != value)
                {
                    _MATERIAL_SN_RECORD = value;
                    NotifyPropertyChanged("MATERIAL_SN_RECORD");
                }
            }
        }
        /// <summary>
        /// 是否自动提交
        /// </summary>
        private bool _aotocommit;            
        public bool aotocommit
        {
            get
            {
                return _aotocommit;
            }
            set
            {
                _aotocommit = value;
                NotifyPropertyChanged("aotocommit");
            }
        }

        //当前条码的中间变量
        private string temp_CurrentBardcode;
        /// <summary>
        /// 当前输入的成品条码
        /// </summary>
        private string _CurrentBardcode;
        public string CurrentBardcode
        {
            get
            {
                return _CurrentBardcode;
            }
            set
            {
                //_CurrentBardcode = value;
                //NotifyPropertyChanged("CurrentBardcode");
                temp_CurrentBardcode = value;
                QueryEvent(value);          
            }
        }

        /// <summary>
        /// 换料原因
        /// </summary>
        private string _reason;
        public string reason
        {
            get
            {
                return _reason;
            }
            set
            {
                _reason = value;
                NotifyPropertyChanged("reason");
            }
        }

    }
}

//========================================================================

using Midea.MES.Biz.Model;
using Midea.MES.Platform.Common;
using Midea.MES.Platform.Server.BizServiceProvider.DataAccess.Oracle;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;

namespace Midea.MES.Platform.Server.BizServiceProvider
{
    public partial class OnlineRepairDomain
    {
        //获取资料,返回多个数据集
        public MessageContract MATERIAL_SN_HEAD_GETDATA(MessageContract msg)
        {
            try
            {
                Params ps = new Params();
                ps.Add("p_inv_org_id", Convert.ToDecimal(msg.Context["p_inv_org_id"]));
                ps.Add("p_sn", msg.Context["p_sn"]);
                ps.Add("x_ret_code", ParamDirection.Output);
                ps.Add("x_ret_msg", ParamDirection.Output);
                ps.Add("x_ret_data_head", ParamDirection.Output);
                ps.Add("x_ret_data_body", ParamDirection.Output);
                ps.Add("x_ret_data_record", ParamDirection.Output);
                using (var context = new BizServiceContext())
                {
                    var ds = DBHelper.ExecProcDS(context.Database.Connection.ConnectionString, "sfc_dc_material_sn_query_pub.main", ps);
                    //var ds = DBHelper.ExecProcEntitys<ObservableCollection<MATERIAL_SN_INFO>>(context.Database.Connection.ConnectionString, "sfc_dc_material_sn_query_pub.main", ps);
                    if (string.Equals(ps["x_ret_code"].Value, "0"))
                    {    
                                                                         
                        MATERIAL_SN_INFO data = new MATERIAL_SN_INFO();

                        //基础资料
                        data.MATERIAL_SN_HEAD = ds.Tables[0].ToEntityList<VM_SFC_DC_MATERIAL_SN_HEAD>().FirstOrDefault();                         
                        

                        //物料采集明细
                        var body = ds.Tables[1].ToEntityList<VM_SFC_DC_MATERIAL_SN_BODY>().ToArray();
                        ObservableCollection<VM_SFC_DC_MATERIAL_SN_BODY> bodyCollection = new ObservableCollection<VM_SFC_DC_MATERIAL_SN_BODY>();
                        foreach (var item in body) 
                        {
                            bodyCollection.Add(item);                              
                        }
                        data.MATERIAL_SN_BODY = bodyCollection;  

                        //物料替换记录
                        var record = ds.Tables[2].ToEntityList<VM_SFC_DC_MATERIAL_SN_RECORD>().ToArray();
                        ObservableCollection<VM_SFC_DC_MATERIAL_SN_RECORD> recordCollection = new ObservableCollection<VM_SFC_DC_MATERIAL_SN_RECORD>();
                        foreach (var item in record)
                        {
                            recordCollection.Add(item);
                        }
                        data.MATERIAL_SN_RECORD = recordCollection;

                        msg.SetMessageDataModel<MATERIAL_SN_INFO>(data);
                        msg.IsSucess = true;
                    }
                    else
                    {
                        msg.ErrorCode = ps["x_ret_code"].Value;
                        msg.ErrorMessage = Convert.ToString(ps["x_ret_msg"].Value);
                        msg.IsSucess = false;
                    }
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMessage = string.Format("{0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);
                msg.IsSucess = false;
            }
            return msg;
        }


        //提交数据
        public MessageContract MATERIAL_SN_HEAD_SAVEDATA(MessageContract Msg)
        {
            try
            {
                ObservableCollection<VM_SFC_DC_MATERIAL_SN_BODY> body = Newtonsoft.Json.JsonConvert.DeserializeObject<ObservableCollection<VM_SFC_DC_MATERIAL_SN_BODY>>(Msg.Context["MATERIAL_SN_BODY"]);
                decimal InvOrgId = Convert.ToDecimal(Msg.Context["p_inv_org_id"]);
                Params ps = new Params();
                ps.Add("p_inv_org_id", InvOrgId);
                ps.Add("p_commit", Msg.Context["p_commit"]);
                ps.Add("p_sn", Msg.Context["p_sn"]);  
                ps.Add("p_material_sn", body.Select(m => m.material_sn).ToArray());
                ps.Add("p_check_material_sn", Msg.Context["p_check_material_sn"]);
                ps.Add("p_keypart_id", body.Select(m => m.ID).ToArray());
                ps.Add("p_reason", Msg.Context["p_reason"]);
                ps.Add("p_user_modified", Msg.Context["p_user_modified"]);
                ps.Add("x_ret_code", ParamDirection.Output);
                ps.Add("x_ret_msg", ParamDirection.Output);       

                using (var context = new BizServiceContext())
                {
                    var dt = DBHelper.ExecProc(context.Database.Connection.ConnectionString, "sfc_dc_material_sn_save_pub.main", ps);
                    if (string.Equals(ps["x_ret_code"].Value, "0"))
                    {                           
                        Msg.IsSucess = true;
                    }
                    else
                    {
                        Msg.ErrorCode = ps["x_ret_code"].Value;
                        Msg.ErrorMessage = Convert.ToString(ps["x_ret_msg"].Value);
                        Msg.IsSucess = false;
                    }
                }
            }
            catch (Exception ex)
            {
                Msg.ErrorMessage = string.Format("{0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);
                Msg.IsSucess = false;
            }
            return Msg;
        }
    }
}


你可能感兴趣的:(public,数据表)