流程设计模式

流程设计运用职责链模式,处理的流程的各个节点

public abstract class Manager
    {
        protected MSSqlAccessData wserver = new MSSqlAccessData();

        public int Companyid;
        public Manager() { }
        protected Manager superior;
        public void SetSuperior(Manager m)
        {
            this.superior = m;
        }
        public virtual string HandleRequest(Site_trans site, Site_trans_log r)
        {
            if (r.Companyid != this.Companyid)
            {
                if (superior != null)
                    return superior.HandleRequest(site, r);
            }
            else
            {
                if (r.Status == 0)
                {
                    //同一点位不能提交同一类型流程
                    var oldtrans = site.GetList().Where(t => t.Oper == site.Oper && t.Siteid == site.Siteid && t.Isend == 0);
                    if (oldtrans != null && oldtrans.Count() > 0)
                        return "同一点位不能同时提交相同流程";
                    site.Add();
                    r.Siteid = site.Id;
                }
                r.Create_time = DateTime.Now.ToString();
                r.Add();
                //转到下一个节点
                if (this.superior != null)
                {
                    site.Companyid = this.superior.Companyid;
                    site.Updatetime = DateTime.Now.ToString();
                    site.UpdateStatus();
                }
            }
            return "操作成功";
        }

        public static T CopySameFieldsObject<T>(Object source)
        {
            Type _SrcT = source.GetType();
            Type _DestT = typeof(T);

            // 构造一个要转换对象实例
            Object _Instance = _DestT.InvokeMember("", BindingFlags.CreateInstance, null, null, null);

            // 这里指定搜索所有公开和非公开的字段
            FieldInfo[] _SrcFields = _SrcT.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            // 将源头对象的每个字段的值分别赋值到转换对象里,因为假定字段都一样,这里就不做容错处理了
            foreach (FieldInfo field in _SrcFields)
            {
                if (_DestT.GetField(field.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null)
                    _DestT.GetField(field.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).SetValue(_Instance, field.GetValue(source));
            }

            return (T)_Instance;
        }
    }
    class WMBHandler : Manager
    {
        public WMBHandler() : base() { this.Companyid = 201; }

        public override string HandleRequest(Site_trans site, Site_trans_log r)
        {
            if (r.Companyid != this.Companyid)
            {
                if (superior != null)
                    return superior.HandleRequest(site, r);

            }
            else
            {

                r.Status = 2;
                r.Create_time = DateTime.Now.ToString();
                r.Add();
                //如果不是拒绝 则完成操作
                if (r.Result != 0)
                {
                    SiteNew sitenew = CopySameFieldsObject<SiteNew>(site);
                    if (site.Oper == "add")
                        sitenew.Add(); //新增 site_trans新增到sitesnew 中  
                    else
                        sitenew.Update();   // sitesnew状态修改 
                    //如果意见为待定,则完成操作后发起更新操作
                    if (r.Result == 2)
                    {
                        //当前流程结束,发起更新流程
                        Site_trans newSiteTran = (Site_trans)site.Clone();
                        newSiteTran.Oper = "update";
                        string sql = "select dps.DEPARTPOSITIONID,dps.DEPARTID,dps.DEPARTPOSITIONNAME,dep.DEPARTNAME,dep.REGIONID  from DEPARTPOSITIONS dps join DEPARTS dep on " +
                       " dps.departid=dep.DEPARTID where REGIONID = '" + site.RegionId + "' and dep.DEPARTID not like '%9%' and dep.DEPARTID not like '%11%' ";
                        DataTable dt = wserver.GetDataTable(sql);
                        if (dt != null && dt.Rows.Count == 1)
                            newSiteTran.Companyid = int.Parse(dt.Rows[0][0].ToString());//找到该点位的companyid发起更新
                        newSiteTran.Updatetime = DateTime.Now.ToString();                       

                        Site_trans_log Nr = new Site_trans_log();
                        Nr.Opinion = "同意";
                        Nr.Result = 1;
                        Nr.Siteid = newSiteTran.Id;
                        Nr.Status = 0;
                        Nr.Companyid = newSiteTran.Companyid;
                        IFactiory a = new UpdateFactory();
                        Manager m = a.GetManage(newSiteTran);
                        m.HandleRequest(newSiteTran, Nr);
                    }
                }
                //删除site_trans里的记录                   
                site.Delete();
            }
            return "操作成功";

        }
    }
    class DCHandler : Manager
    {

        public DCHandler() : base() { this.Companyid = 202; }

        public override string HandleRequest(Site_trans site, Site_trans_log r)
        {

            if (r.Companyid != this.Companyid)
            {
                if (superior != null)
                    return superior.HandleRequest(site, r);

            }
            else
            {
                if (site.Oper == "update")
                {
                    if (r.Status == 0)
                    {
                        //同一点位不能提交同一类型流程
                        var oldtrans = site.GetList().Where(t => t.Oper == site.Oper && t.Siteid == site.Siteid && t.Isend == 0);
                        if (oldtrans!=null&&oldtrans.Count() > 0)
                            return "同一点位不能同时提交相同流程";
                        site.Add();
                        r.Siteid = site.Id;
                    }
                    r.Status = 2;
                    r.Create_time = DateTime.Now.ToString();
                    r.Add();
                    //找到stie_trans替换到sitesnews里的字段 
                    SiteNew sitenew = CopySameFieldsObject<SiteNew>(site);
                    sitenew.Update();
                    //完结存档
                    site.Delete();
                }
                else
                    base.HandleRequest(site, r);
            }

            return "操作成功";

        }
    }
    class RHandler : Manager
    {
        public RHandler(Site_trans site) : base()
        {

            //this.Companyid = site.Companyid;
            string sql = "select dps.DEPARTPOSITIONID,dps.DEPARTID,dps.DEPARTPOSITIONNAME,dep.DEPARTNAME,dep.REGIONID  from DEPARTPOSITIONS dps join DEPARTS dep on " +
                " dps.departid=dep.DEPARTID where REGIONID = '" + site.RegionId + "' and dep.DEPARTID not like '%9%' and dep.DEPARTID not like '%11%' ";
            DataTable dt = wserver.GetDataTable(sql);
            if (dt != null && dt.Rows.Count == 1)
                this.Companyid = int.Parse(dt.Rows[0][0].ToString());  //通过site_new的id找到regionid  找到companyid
        }

    }

然后配置流程

    //流程
    public interface IFactiory
    {
        Manager GetManage(Site_trans site);
    }
    public class FactoryByR : IFactiory
    {
        public Manager GetManage(Site_trans site)
        {
            RHandler r = new RHandler(site);
            DCHandler dc = new DCHandler();
            WMBHandler wmb = new WMBHandler();
            r.SetSuperior(dc);
            dc.SetSuperior(wmb);
            return r;
        }
    }
    public class FactoryByDC : IFactiory
    {
        public Manager GetManage(Site_trans site)
        {
            DCHandler dc = new DCHandler();
            RHandler r = new RHandler(site);
            WMBHandler wmb = new WMBHandler();
            dc.SetSuperior(r);
            r.SetSuperior(wmb);
            return dc;
        }
    }
    public class UpdateFactory : IFactiory
    {
        public Manager GetManage(Site_trans site)
        {
            RHandler r = new RHandler(site);
            DCHandler dc = new DCHandler();
            r.SetSuperior(dc);
            return r;
        }
    }

 

 

发表评论