遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

带进度条任务显示

   private void btnDo_Click(object sender, EventArgs e)
        {
            try
            {
                var dt = bindingSource1.DataSource as DataTable;
                if (dt == null) return;
                if (dt.Rows.Count <= 0) throw new Exception("表没有记录!");
                btnDo.Enabled = false;
                var task = new SFG_ImportTask(dt);
                task.Do();

                #region 显示提示窗体
                using (var frm = new frmLongInvokeMonitor())
                {
                    ThreadPool.QueueUserWorkItem(o =>
                    {

                        while (task._Status == ImportTaskStatus.Running)
                        {
                            #region 设置显示属性
                            SyncContext.Send(obj =>
                            {
                                frm.progressBar1.Value =(int) task._Percent;
                                frm.txtTips.Text = task._Tips;
                            }, null);
                            #endregion
                            Thread.Sleep(500);
                        }
                        #region 关闭窗口
                        SyncContext.Send(obj =>
                        {
                            if (task._Status == ImportTaskStatus.Fail)
                            {
                                ErrMsg(task._Tips);
                            }
                            else
                            {
                                try
                                {
                                    sFGBOMBindingSource1.Clear();
                                    sFGBOMBindingSource1.DataSource = task._TreeBomList;
                                    bindingSource2.Clear();
                                    bindingSource2.DataSource = task._BomList;
                                    tabbedControlGroup1.SelectedTabPage = layoutControlGroup4;
                                    InfoMsg(string.Format("任务完成,上传了{0}个编码!", task._FGList.Count));
                                }catch(Exception ex)
                                {
                                    ErrMsg(ex.Message);
                                }
                            }
                            btnDo.Enabled = true;
                            frm.Close();
                        }, null);
                        #endregion
                    }, null);
                    frm.ShowDialog();
                }
                
                #endregion
            }
            catch (Exception ex)
            {
                ErrMsg(ex.Message);
            }
        }
View Code

任务类实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading;
using com.geelyhd.MFG.EFModel;
using F.Studio.Infrastructure;
using com.geelyhd.MFG.IService;

namespace com.geelyhd.MFG.WR.UI.M3
{
    public enum ImportTaskStatus{Init=0,Running=3, Fail=5,Sucess=10}
    public class SFG_ImportTask
    {
        public String _Tips = "";
        public   double _Percent = 0;
        
        public ImportTaskStatus _Status = ImportTaskStatus.Init;


        public List<SFG_MM> _MMList = new List<SFG_MM>();
        public List<SFG_BOM> _BomList = new List<SFG_BOM>();
        public List<SFG_MM> _FGList = new List<SFG_MM>();
        public List<SFG_BOMForTree> _TreeBomList = new List<SFG_BOMForTree>();

        private DataTable _DT;


        public SFG_ImportTask(DataTable dt)
        {
            _DT = dt;
        }
        public void Do()
        {

            
            _Status = ImportTaskStatus.Running;
            
            ThreadPool.QueueUserWorkItem(o =>
            {
                DoAction();
            }, null);


        }
        private void DoAction()
        {
            #region 执行分解
            var dt = _DT;
            try
            {
                #region 解析BOM

                Rpt("提取成品编码...", 1);
                ExtractFGList(dt);
                Rpt(string.Format("发现{0}个成品编码", _MMList.Count), 5);

                Rpt("校验成品管理权...", 6);
                VerifyFGAuth();
                Rpt("校验完成", 8);


                Rpt("BOM结构解析...", 10);
                ExtractBOM();
                Rpt("完成BOM结构解析", 50);
                #endregion

                #region 存档
                Rpt("保存产品BOM...", 51);
                SaveBOM();
                Rpt("完成保存产品BOM", 95);
                #endregion

                #region 构建显示结构
                Rpt("构建显示结构...", 95);
                BuildShowTree();
                Rpt("完成构建显示结构.", 99);
                #endregion

                Rpt("完成", 100);
                Thread.Sleep(1000);
                _Status = ImportTaskStatus.Sucess;
            }
            catch (Exception ex)
            {
                Rpt("错误:" + ex.Message);
                _Status = ImportTaskStatus.Fail;
                
            }
            finally
            {

            }
            #endregion
        }
        private void VerifyFGAuth()
        {
            var fgList = _FGList.Aggregate("", (s, e) => s += string.Format("'{0}',", e.MNo)).TrimEnd(",".ToArray());
            var where = "it.MNo in{" + fgList + "}";
            var list = ServiceLocator.Fetch<ISFG_MMService>().GetList(where, "it.RecId");
            var exceptList= _FGList.Select(ent => ent.MNo).Except(list.Select(dbIt => dbIt.MNo)).ToList();
            var exceptMsg = exceptList.Aggregate("", (s, e) => s += e + ",").TrimEnd(",".ToArray());
            if (exceptList.Count > 0) throw new Exception(string.Format("物料主数据未创建:\r\n{0}" , exceptMsg));
            foreach (var it in list)
            {
                if (string.Compare(it.AddEmpNo, UserSetting.UserName, true) != 0)
                {
                    throw new Exception("成品:" + it.MNo + "属于用户:" + it.AddEmpNo);
                }
            }
        }


        private void CopyItem(SFG_BOM from, SFG_BOMForTree to)
        {
            to.AddEmpNo = from.AddEmpNo; to.AddTime = from.AddTime;
            to.Amount = from.Amount; to.BTime = from.BTime;
            to.ETime = from.ETime; to.Harness = from.Harness;
            to.IsScan = from.IsScan; to.MNo = from.MNo;
            to.PMNo = from.PMNo; 
        }

        private void ExtractBOM()
        {
            var dt = _DT;
            var dic = new Dictionary<String, int>();
            for (int row = 0; row < dt.Rows.Count; row++)
            {
                var dRow = dt.Rows[row];
                var cMMStr = V(dRow.Field<String>("L1"));

                //是否包含复2层结构
                bool hasCombination = !string.IsNullOrWhiteSpace(cMMStr);
                var sMMStr = V(dRow.Field<String>("L2"));
                if (string.IsNullOrWhiteSpace(sMMStr)) throw new Exception("L2列不能为空!");

                foreach (var fgMM in _FGList)
                {
                    #region 解析BOM
                    var v = V(dRow.Field<String>(fgMM.MNo));
                    if (v == "1")
                    {
                        var bom = new SFG_BOM() { Amount = 1, Harness = fgMM.MNo, MNo = sMMStr, IsScan = true,AddEmpNo=UserSetting.UserName,AddTime=DateTime.Now };
                        if (hasCombination) //复合材料
                        {
                            bom.PMNo = cMMStr;
                            bom.IsScan = false;
                            if (!dic.ContainsKey(bom.PMNo + "$" + bom.Harness))
                            {
                                var cBom = new SFG_BOM() { Amount = 1, Harness = fgMM.MNo, MNo = bom.PMNo, IsScan = false, PMNo = fgMM.MNo, AddEmpNo = UserSetting.UserName, AddTime = DateTime.Now };
                                dic.Add(bom.PMNo + "$" + bom.Harness, 1);
                                _BomList.Add(cBom);

                            }
                        }
                        else
                        {
                            bom.PMNo = fgMM.MNo;
                        }
                        _BomList.Add(bom);
                    }
                    #endregion
                }

                //计算子进度
                var subPercent = 40 / dt.Rows.Count;
                AddPercent( subPercent);
            }
        }

        private void SaveBOM()
        {
            var row = 0;
            var rowCount = _FGList.Count;
            foreach (var fgMM in _FGList)
            {
                //删除原有数据
                ServiceLocator.Fetch<ISFG_MMService>().ExeSql(string.Format("delete from SFG_BOM where Harness='{0}'", fgMM.MNo));
                ServiceLocator.Fetch<ISFG_BOMService>().AddList(_BomList.Where(ent=>ent.Harness==fgMM.MNo).ToList());
                //计算子进度
                row++;
                var subPercent = 45/ rowCount;
                AddPercent( subPercent);
            }

        }

        private void BuildShowTree()
        {
            foreach (var bom in _BomList)
            {
                var newEnt = new SFG_BOMForTree();
                #region
                CopyItem(bom, newEnt);
                #endregion
                newEnt.MNo = newEnt.MNo + "_" + newEnt.Harness;

                if (newEnt.PMNo != newEnt.Harness && newEnt.IsScan == false)
                {
                    newEnt.PMNo = newEnt.PMNo + "_" + newEnt.Harness;
                }
                _TreeBomList.Add(newEnt);

            }
            foreach (var fg in _FGList)
            {
                var newEnt = new SFG_BOMForTree();
                newEnt.MNo = fg.MNo;
                newEnt.PMNo = "";
                newEnt.Harness = fg.MNo;
                newEnt.IsScan = false;
                _TreeBomList.Add(newEnt);
            }
        }
        private void ExtractFGList(DataTable dt)
        {
           
            foreach (DataColumn c in dt.Columns)
            {
                if (c.Caption == "L1" || c.Caption == "L2") continue;
                var mno = c.Caption.Trim();
                var ent = new SFG_MM();
                ent.MNo = mno;
                ent.MType ="成品" ;
                ent.MUnit = "PC";
                ent.Harness = mno;
                _MMList.Add(ent);
            }
           
            _FGList = _MMList.Where(ent => ent.MType == "成品").ToList();
            if (_FGList.Count <= 0) throw new Exception("数据异常未提取到成品编号!");
        }

        #region Help
        private void Rtp(int percent)
        {
            _Percent = percent;
        }
        private void Rpt(string tip)
        {
            _Tips = tip;
        }
        private void AddPercent(double addV)
        {
            _Percent += addV;
        }
        private void Rpt(string tip, int percent)
        {
            _Tips = tip;
            _Percent = percent;
        }
        private String V(string v)
        {
            if (string.IsNullOrWhiteSpace(v)) return string.Empty;
            return v.Trim();
        }

        #endregion
    }
}
View Code

 参考https://www.cnblogs.com/wdfrog/p/9875314.html

posted on 2019-12-26 11:16  遗忘海岸  阅读(294)  评论(0编辑  收藏  举报