惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Register - Security
The Register - Security
V
Visual Studio Blog
aimingoo的专栏
aimingoo的专栏
Stack Overflow Blog
Stack Overflow Blog
IT之家
IT之家
量子位
C
Check Point Blog
博客园 - 【当耐特】
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
The Last Watchdog
The Last Watchdog
博客园 - Franky
N
Netflix TechBlog - Medium
Webroot Blog
Webroot Blog
A
About on SuperTechFans
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
D
Docker
S
Security Affairs
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
V2EX - 技术
V2EX - 技术
Jina AI
Jina AI
Help Net Security
Help Net Security
L
LangChain Blog
P
Proofpoint News Feed
The Cloudflare Blog
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
Schneier on Security
Schneier on Security
Recent Announcements
Recent Announcements
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
V
Vulnerabilities – Threatpost
Microsoft Security Blog
Microsoft Security Blog
H
Heimdal Security Blog
P
Proofpoint News Feed
O
OpenAI News
H
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
爱范儿
爱范儿
Security Archives - TechRepublic
Security Archives - TechRepublic

博客园 - Tonyyang

【XAF】如何通过前缀或自定义架构将数据库表与内置系统表分开 Power Shell 7 和5.1 批量给pdf添加页码 [XAF] Declare Conditional Appearance Rules in Code DataTableHelper C# 多任务数据同步 【原】 XAF Localization改用百度翻译 C#百度翻译--亲测试可用 SqlQueryDynamic C#上传到FTP Server FREE OFFER - .NET App Security API (Role-based Access Control) 后台管理框架 Model to Model JSON序列化和反序列化日期时间的处理 Asp.net MVC 上传文件 Asp.net MVC bootstrap 穿梭框 EXT.NET Combox下拉Grid 转 Refresh Excel Pivot Tables Automatically Using SSIS Script Task SQL Server Integration Services SSIS最佳实践 PowerBI
BOM导入
Tonyyang · 2022-07-04 · via 博客园 - Tonyyang
 #region Windchild BOM Model
    public class WindchildBomViewModel
    {
        public long BOMID { get; set; }
        /// <summary>
        /// BOM层级
        /// </summary>
        public int Level
        {
            get
            {
                int le = 0;
                int leStep = 4;
                int partLen = Part.Length;
                int partlen2 = PartTrim.Length;
                le = partLen - partlen2;
                return le / leStep;
            }
        }
        public string PartTrim { get { return Part.Trim(); } }
        public string Part { get; set; }
        public string Part_Desc { get; set; }
        public string Ver { get; set; }
        public string Status { get; set; }
        public string Qey_Per { get; set; }
        public string UM { get; set; }
        public string QAD_Suffix { get; set; }
        public string Mfgr { get; set; }
        public string PurStatus { get; set; }
        public string AltType { get; set; }
        public string AltStatus { get; set; }
        public string CompType { get; set; }
        public string CriticaPart { get; set; }
        public string StandardPart { get; set; }
        public string Assembly_Technology { get; set; }
        public string Classification { get; set; }
        public string Position { get; set; }
        public string WR_Sourcing { get; set; }
        public string TJ_Sourcing { get; set; }
        public string QX_Sourcing { get; set; }
        public string MY_Sourcing { get; set; }
        public string Base_PN { get; set; }
    }
    public class WindChild_Bom_MstrModel
    {
        public long ID { get; set; }
        public string Part { get; set; }
        public string Ver { get; set; }
        public DateTime Added { get; set; }
    }
    public class WindChild_Bom_DetModel
    {
        public long ID { get; set; }
        public string Part { get; set; }
        public string Ver { get; set; }
        public long BOMID { get; set; }
        public long ParentID { get; set; }
    }
    public class WindChild_Part_ALTModel
    {
        public long ID { get; set; }
        public string Part { get; set; }
        public string Alt_Part { get; set; }
    }
    #endregion

    #region Windchild BOM DAL
    /// <summary>
    /// Windchild BOM 导入DAL
    /// </summary>
    public class WindchildBOMDAL
    {
        #region Windchild
        public void WindchildBomImport(string fileName)
        {
            #region 数据加载
            var dt = PubUtils.ExcelToDataTable(fileName, true);
            List<WindchildBomViewModel> wbList = dt.ToList<WindchildBomViewModel>();
            #endregion

            #region 变量定义
            bool isSameBom = false;//是否同一Bom
            WindchildBomViewModel bom = new WindchildBomViewModel();
            WindchildBomViewModel part1 = new WindchildBomViewModel();
            WindchildBomViewModel part2 = new WindchildBomViewModel();
            WindchildBomViewModel part3 = new WindchildBomViewModel();
            WindchildBomViewModel part4 = new WindchildBomViewModel();
            WindchildBomViewModel part5 = new WindchildBomViewModel();
            //WindchildBomModel part6 = new WindchildBomModel();
            string compType = "制造商部件";//类型
            long currentBomID = 0;
            
            #endregion

            #region 分析处理
            foreach (var item in wbList)
            {
                isSameBom = item.Level == 0;
                if (isSameBom)
                {
                    #region Bom Mstr
                    bom = item;
                    long? bomID = GetBomMstrModel(bom)?.ID;
                    DeleteBomTree(bomID);
                    //保存 Bom Mstr
                    SaveBomMstr(bom);
                    currentBomID = GetBomMstrModel(bom).ID;
                    part1 = null; part2 = null; part3 = null; part4 = null; part5 = null;
                    #endregion
                }
                else
                {
                    #region Bom Det
                    item.BOMID = currentBomID;
                    if (item.Level == 1)
                    {
                        part1 = item;
                       SaveBomDet(null, item);
                    }
                    else if (item.Level == 2)
                    {
                        part2 = item;
                       SaveBomTree(part1, compType, item);
                    }
                    else if (item.Level == 3)
                    {
                        part3 = item;
                       SaveBomTree(part2, compType, item);
                    }
                    else if (item.Level == 4)
                    {
                        part4 = item;
                       SaveBomTree(part3, compType, item);
                    }
                    else if (item.Level == 5)
                    {
                        part5 = item;
                       SaveBomTree(part4, compType, item);
                    }
                    else if (item.Level == 6)
                    {
                       SaveBomTree(part5, compType, item);
                    }
                    #endregion
                }
            }
            #endregion
        }
        #endregion   

        #region GetModel
        /// <summary>
        /// 是否存在BOM
        /// </summary>
        /// <param name="model">WindchildBomModel</param>
        /// <returns></returns>
        public bool IsExistBomMstr(WindchildBomViewModel model)
        {
            return GetBomMstrModel(model) != null;
        }

        /// <summary>
        /// 获取BOM明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public WindChild_Bom_DetModel GetBomDetModel(WindchildBomViewModel model)
        {
            string sql = $"SELECT top 1 * FROM   WindChild_Bom_Det WHERE BOMID = '{model.BOMID}' AND Part = '{model.PartTrim}' AND Ver = '{model.Ver}' ";// AND ParentID = '{model.ParentID}'
            DataTable dt = PubUtils.GetDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            { return PubUtils.DataRowViewToModel<WindChild_Bom_DetModel>(dt.Rows[0]); }
            return null;
        }

        /// <summary>
        /// 获取Bom头
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public WindChild_Bom_MstrModel GetBomMstrModel(WindchildBomViewModel model)
        {
            string sql = $"SELECT top 1 * FROM    WindChild_Bom_Mstr WHERE Part = '{model.PartTrim}' AND Ver = '{model.Ver}' ";
            DataTable dt = PubUtils.GetDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            { return PubUtils.DataRowViewToModel<WindChild_Bom_MstrModel>(dt.Rows[0]); }
            return null;
        }
        #endregion

        #region Operation
        /// <summary>
        /// 保存BOM
        /// </summary>
        /// <param name="part1"></param>
        /// <param name="compType"></param>
        /// <param name="item"></param>
        public bool SaveBomTree(WindchildBomViewModel part1, string compType, WindchildBomViewModel item)
        {
            if (item.CompType != compType)
            { //保存Part2 
               return SaveBomDet(part1, item);
            }
            else
            { //保存Part ALT 
               return SavePartALT(part1, item);
            }
        }

        /// <summary>
        /// 保存制造商部件
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public bool SavePartALT(WindchildBomViewModel parent, WindchildBomViewModel current)
        {
            string sql = "INSERT INTO [ERPExtend].[dbo].[WindChild_Part_ALT] \n"
           + "( \n"
           + "BOMID,Part,Alt_Part "
           + ") \n"
           + "VALUES \n"
           + "( \n"
           + $" '{parent.BOMID}','{parent.PartTrim}','{current.PartTrim}' " //Parent.Part都有 Ver BomID,ParentID
           + ")";
            return PubUtils.ExeSql(sql) > 0;
        }
        /// <summary>
        /// 保存Bom明细
        /// </summary>
        /// <param name="parent">Parent</param>
        /// <param name="current"></param>
        /// <returns></returns>
        public bool SaveBomDet(WindchildBomViewModel parent, WindchildBomViewModel current)
        {
            WindChild_Bom_DetModel pmModel = new WindChild_Bom_DetModel() { BOMID = current.BOMID };
            if (parent != null)
            {
                pmModel = GetBomDetModel(parent);
            }
            if (pmModel is null)
            {
                pmModel = new WindChild_Bom_DetModel() { BOMID = current.BOMID };
            }

            string sql = "INSERT INTO [ERPExtend].[dbo].[WindChild_Bom_Det] \n"
           + "( \n"
           + "    BOMID, ParentID,Part, Ver,"
           + "    Part_Desc,     [Status],    Classification,    Qey_Per,     UM, \n" + "    Position, \n" + "    WR_Sourcing, \n" + "    TJ_Sourcing, \n"
           + "    QX_Sourcing,    MY_Sourcing,     Base_PN,     QAD_Suffix, Mfgr, PurStatus,AltType, AltStatus,     CompType,     CriticaPart,     StandardPart,    Assembly_Technology, Added \n"
           + ") \n"
           + "VALUES \n"
           + "( \n"
           + $"'{pmModel.BOMID}','{pmModel.ID}', '{current.PartTrim}','{current.Ver}', \n"
           + $"'{current.Part_Desc}','{current.Status}','{current.Classification}','{current.Qey_Per}','{current.UM}','{current.Position}','{current.WR_Sourcing}','{current.TJ_Sourcing}', \n"
           + $"'{current.QX_Sourcing}','{current.MY_Sourcing}','{current.Base_PN}','{current.QAD_Suffix}','{current.Mfgr}','{current.PurStatus}','{current.AltType}','{current.AltStatus}','{current.CompType}','{current.CriticaPart}','{current.StandardPart}','{current.Assembly_Technology}', (getdate()) \n"
           + ")";
            return PubUtils.ExeSql(sql) > 0;
        }
        /// <summary>
        /// 保存BOM头
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SaveBomMstr(WindchildBomViewModel model)
        {
            string sql = "INSERT INTO [ERPExtend].[dbo].[WindChild_Bom_Mstr] \n"
           + "( \n"
           + "    Part, \n"
           + "    Ver, \n"
           + "    Added \n"
           + ") \n"
           + "VALUES \n"
           + "( \n"
           + $"    '{model.PartTrim}', \n"
           + $"    '{model.Ver}', \n"
           + "    (getdate()) \n"
           + ")";
            return PubUtils.ExeSql(sql) > 0;
        }
        /// <summary>
        /// 删除Bom结构
        /// </summary>
        /// <param name="bomID"></param>
        /// <returns></returns>
        public bool DeleteBomTree(long? bomID)
        {
            string sql = $"DELETE from WindChild_Bom_Mstr WHERE ID = '{bomID}'  DELETE FROM WindChild_Bom_Det WHERE BOMID = '{bomID}' DELETE FROM WindChild_Part_ALT WHERE BOMID = '{bomID}' ";
            return PubUtils.ExeSql(sql) > 0;
        }
        #endregion
    }
    #endregion