CustomerSOList

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    private CustomerSOList bgCustomerSOListEntity { get; set; }
    private CustomerSOList dbCustomerSOListEntity { get; set; }
    private bool enableStyle { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        GenerateDemoData();
        SortCustomerSOListEntityProperty(dbCustomerSOListEntity);
        SortCustomerSOListEntityProperty(bgCustomerSOListEntity);

        var dbNode = new TreeNode("CustomerSOListEntity");
        var bgNode = new TreeNode("CustomerSOListEntity");
        this.TreeView1.Nodes.Add(dbNode);
        this.TreeView2.Nodes.Add(bgNode);

        enableStyle = true;
        CompareCustomerSOList(dbNode, dbCustomerSOListEntity, bgCustomerSOListEntity);
        enableStyle = false;
        CompareCustomerSOList(bgNode, bgCustomerSOListEntity, dbCustomerSOListEntity);
    }

    public void GenerateDemoData()
    {
        var soTran1 = new SOTransactionBasic()
        {
            ItemNumber = "s-001",
            Description = "sd-001"
        };

        var soTran2 = new SOTransactionBasic()
        {
            ItemNumber = "s-002",
            Description = "sd-002"
        };

        var soTran3 = new SOTransactionBasic()
        {
            ItemNumber = "s-003",
            Description = "sd-003"
        };

        var soBasic1 = new SOBasic()
        {
            SONumber = 10000,
            SODate = DateTime.Now,
            SOTransactionBasicList = new List<SOTransactionBasic>() { soTran1 }
        };

        var soBasic2 = new SOBasic()
        {
            SONumber = 10002,
            SODate = DateTime.Now,
            SOTransactionBasicList = new List<SOTransactionBasic>() { soTran1, soTran2 }
        };

        var soBasic3 = new SOBasic()
        {
            SONumber = 10002,
            SODate = DateTime.Now,
            SOTransactionBasicList = new List<SOTransactionBasic>() { soTran1, soTran3 }
        };

        var soBasic4 = new SOBasic()
        {
            SONumber = 10004,
            SODate = DateTime.Now,
            SOTransactionBasicList = new List<SOTransactionBasic>() { soTran1, soTran3 }
        };

        bgCustomerSOListEntity = new CustomerSOList()
        {
            RowKey = "10|60000",
            SOBasicList = new List<SOBasic>() { soBasic1, soBasic2 }
        };

        dbCustomerSOListEntity = new CustomerSOList()
        {
            RowKey = "10|60000",
            SOBasicList = new List<SOBasic>() { soBasic3, soBasic4 }
        };

        bgCustomerSOListEntity = null;
    }

    public void SortCustomerSOListEntityProperty(CustomerSOList customerSOListEntity)
    {
        if (customerSOListEntity == null) return;

        if (customerSOListEntity.SOBasicList != null && customerSOListEntity.SOBasicList.Any())
        {
            customerSOListEntity.SOBasicList = customerSOListEntity.SOBasicList.OrderBy(p => p.SONumber).ToList();
            customerSOListEntity.SOBasicList.ForEach(p =>
            {
                if (p.SOTransactionBasicList != null && p.SOTransactionBasicList.Any())
                {
                    p.SOTransactionBasicList = p.SOTransactionBasicList.OrderBy(m => m.ItemNumber).ToList();
                }
            });
        }
    }

    public void CompareCustomerSOList2(TreeNode dbPNode, TreeNode bgPNode, object obj1, object obj2)
    {
        CustomerSOList dbCustomerSOs = null;
        CustomerSOList bgCustomerSOs = null;
        if (obj1.GetType() == typeof(CustomerSOList))
        {
            dbCustomerSOs = obj1 as CustomerSOList;
            bgCustomerSOs = obj2 as CustomerSOList;
        }

        SOBasic dbSOBasic = null;
        SOBasic bgSOBasic = null;
        if (obj1.GetType() == typeof(SOBasic))
        {
            dbSOBasic = obj1 as SOBasic;
            bgSOBasic = obj2 as SOBasic;
        }

        var dbProperties = obj1.GetType().GetProperties();
        var bgProperties = obj2 == null ? null : obj2.GetType().GetProperties();

        foreach (var pi in dbProperties)
        {
            if (pi.PropertyType == typeof(List<SOBasic>))
            {
                var dbNodeList = new TreeNode("SOBasicList");
                var bgNodeList = new TreeNode("SOBasicList");
                dbPNode.ChildNodes.Add(dbNodeList);
                bgPNode.ChildNodes.Add(bgNodeList);

                dbCustomerSOs.SOBasicList.ForEach(p1 =>
                {
                    var dbNode = new TreeNode(string.Format("SOBasic-{0}", dbCustomerSOs.SOBasicList.IndexOf(p1)));
                    var bgNode = new TreeNode(string.Format("SOBasic-{0}", dbCustomerSOs.SOBasicList.IndexOf(p1)));
                    dbNodeList.ChildNodes.Add(dbNode);
                    bgNodeList.ChildNodes.Add(bgNode);

                    var p2 = bgCustomerSOs.SOBasicList.Find(m => m.SONumber == p1.SONumber);

                    if (p2 == null)
                    {
                        SetPerentNodeStyle(dbNode);
                        SetChildNodeStyle(dbNode);
                    }

                    CompareCustomerSOList2(dbNode, bgNode, p1, p2);
                });
            }
            else if (pi.PropertyType == typeof(List<SOTransactionBasic>))
            {
                var dbNodeList = new TreeNode("SOTransactionBasicList");
                var bgNodeList = new TreeNode("SOTransactionBasicList");
                dbPNode.ChildNodes.Add(dbNodeList);
                bgPNode.ChildNodes.Add(bgNodeList);

                dbSOBasic.SOTransactionBasicList.ForEach(p1 =>
                {
                    var dbNode = new TreeNode(string.Format("SOTransactionBasic-{0}", dbSOBasic.SOTransactionBasicList.IndexOf(p1)));
                    var bgNode = new TreeNode(string.Format("SOTransactionBasic-{0}", dbSOBasic.SOTransactionBasicList.IndexOf(p1)));
                    dbNodeList.ChildNodes.Add(dbNode);
                    bgNodeList.ChildNodes.Add(bgNode);

                    var p2 = bgSOBasic == null || bgSOBasic.SOTransactionBasicList == null ? null : bgSOBasic.SOTransactionBasicList.Find(m => m.ItemNumber == p1.ItemNumber);

                    if (p2 == null)
                    {
                        SetPerentNodeStyle(dbNode);
                        SetChildNodeStyle(dbNode);
                    }

                    CompareCustomerSOList2(dbNode, bgNode, p1, p2);
                });
            }
            else
            {
                var dbPropertyValue = pi.GetValue(obj1).ToString();
                var dbNode = new TreeNode(string.Format("{0}:{1}", pi.Name, dbPropertyValue));
                dbPNode.ChildNodes.Add(dbNode);

                string bgPropertyValue = null;

                if (bgProperties != null)
                {
                    bgPropertyValue = bgProperties.ToList().Find(p => p.Name == pi.Name).GetValue(obj2).ToString();
                    var bgNode = new TreeNode(string.Format("{0}:{1}", pi.Name, bgPropertyValue));
                    bgPNode.ChildNodes.Add(bgNode);
                }

                if (dbPropertyValue == bgPropertyValue) continue;

                SetPerentNodeStyle(dbNode);

            }

        }
    }

    public void CompareCustomerSOList(TreeNode dbPNode, object obj1, object obj2)
    {
        if (obj1 == null) return;

        CustomerSOList dbCustomerSOs = null;
        CustomerSOList bgCustomerSOs = null;
        if (obj1.GetType() == typeof(CustomerSOList))
        {
            dbCustomerSOs = obj1 as CustomerSOList;
            bgCustomerSOs = obj2 as CustomerSOList;
        }

        SOBasic dbSOBasic = null;
        SOBasic bgSOBasic = null;
        if (obj1.GetType() == typeof(SOBasic))
        {
            dbSOBasic = obj1 as SOBasic;
            bgSOBasic = obj2 as SOBasic;
        }

        var dbProperties = obj1.GetType().GetProperties();
        var bgProperties = obj2 == null ? null : obj2.GetType().GetProperties();

        foreach (var pi in dbProperties)
        {
            if (pi.PropertyType == typeof(List<SOBasic>))
            {
                var dbNodeList = new TreeNode("SOBasicList");
                dbPNode.ChildNodes.Add(dbNodeList);

                dbCustomerSOs.SOBasicList.ForEach(p1 =>
                {
                    var dbNode = new TreeNode(string.Format("SOBasic-{0}", dbCustomerSOs.SOBasicList.IndexOf(p1)));
                    dbNodeList.ChildNodes.Add(dbNode);

                    var p2 = bgCustomerSOs == null ? null : bgCustomerSOs.SOBasicList.Find(m => m.SONumber == p1.SONumber);

                    if (enableStyle && p2 == null)
                    {
                        SetPerentNodeStyle(dbNode);
                        SetChildNodeStyle(dbNode);
                    }

                    CompareCustomerSOList(dbNode, p1, p2);
                });
            }
            else if (pi.PropertyType == typeof(List<SOTransactionBasic>))
            {
                var dbNodeList = new TreeNode("SOTransactionBasicList");
                dbPNode.ChildNodes.Add(dbNodeList);

                dbSOBasic.SOTransactionBasicList.ForEach(p1 =>
                {
                    var dbNode = new TreeNode(string.Format("SOTransactionBasic-{0}", dbSOBasic.SOTransactionBasicList.IndexOf(p1)));
                    dbNodeList.ChildNodes.Add(dbNode);

                    var p2 = bgSOBasic == null || bgSOBasic.SOTransactionBasicList == null ? null : bgSOBasic.SOTransactionBasicList.Find(m => m.ItemNumber == p1.ItemNumber);

                    if (enableStyle && p2 == null)
                    {
                        SetPerentNodeStyle(dbNode);
                        SetChildNodeStyle(dbNode);
                    }

                    CompareCustomerSOList(dbNode, p1, p2);
                });
            }
            else
            {
                var dbPropertyValue = pi.GetValue(obj1).ToString();
                var dbNode = new TreeNode(string.Format("{0}:{1}", pi.Name, dbPropertyValue));
                dbPNode.ChildNodes.Add(dbNode);

                string bgPropertyValue = null;

                if (bgProperties != null)
                {
                    bgPropertyValue = bgProperties.ToList().Find(p => p.Name == pi.Name).GetValue(obj2).ToString();
                }

                if (dbPropertyValue == bgPropertyValue) continue;

                if (enableStyle)
                {
                    SetPerentNodeStyle(dbNode);
                }

            }

        }
    }

    public void SetPerentNodeStyle(TreeNode node)
    {
        if (node == null) return;

        node.Text = node.Text.IndexOf("yellowBack") > 0 ? node.Text : string.Format("<span class='yellowBack'>{0}</span>", node.Text);

        SetPerentNodeStyle(node.Parent);
    }

    public void SetChildNodeStyle(TreeNode node)
    {
        if (node == null) return;

        node.Text = node.Text.IndexOf("yellowBack") > 0 ? node.Text : string.Format("<span class='yellowBack'>{0}</span>", node.Text);

        if (node.ChildNodes != null && node.ChildNodes.Count > 0)
        {
            foreach (var item in node.ChildNodes)
            {
                SetChildNodeStyle(node);
            }
        }

    }
}
原文地址:https://www.cnblogs.com/skydau/p/3174731.html