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

推荐订阅源

T
Troy Hunt's Blog
F
Fortinet All Blogs
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog
The Register - Security
The Register - Security
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
V
Vulnerabilities – Threatpost
S
Securelist
S
SegmentFault 最新的问题
T
Threat Research - Cisco Blogs
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Privacy International News Feed
S
Schneier on Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
GbyAI
GbyAI
Apple Machine Learning Research
Apple Machine Learning Research
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
美团技术团队
Cyberwarzone
Cyberwarzone
C
Cisco Blogs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google Online Security Blog
Google Online Security Blog
M
MIT News - Artificial intelligence
U
Unit 42
V
V2EX
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
B
Blog
博客园 - 叶小钗
Attack and Defense Labs
Attack and Defense Labs
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - Franky
Engineering at Meta
Engineering at Meta
Schneier on Security
Schneier on Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Blog of Author Tim Ferriss
IT之家
IT之家
W
WeLiveSecurity
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
Martin Fowler
Martin Fowler
SecWiki News
SecWiki News

博客园 - 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