public void Delete(AscmSupplierMaterialLink ascmSupplierMaterialLink)
 {
     try
     {
         YnDaoHelper.GetInstance().nHibernateHelper.Delete<AscmSupplierMaterialLink>(ascmSupplierMaterialLink);
     }
     catch (Exception ex)
     {
         YnBaseClass2.Helper.LogHelper.GetLog().Error("删除失败(Delete AscmSupplierMaterialLink)", ex);
         throw ex;
     }
 }
Example #2
0
        public ActionResult SupplierMaterialImport(HttpPostedFileBase fileImport, int? supplierId)
        {
            JsonObjectResult jsonObjectResult = new JsonObjectResult();
            string sError = "";
            try
            {
                if (!supplierId.HasValue)
                    throw new Exception("供应商ID传值错误!");

                if (fileImport != null)
                {
                    string userName = string.Empty;
                    if (User.Identity.IsAuthenticated)
                        userName = User.Identity.Name;

                    //定义索引
                    int documentIndex = 1;
                    using (Stream stream = fileImport.InputStream)
                    {
                        List<AscmMaterialItem> listAscmMaterialItem = null;
                        List<AscmSupplierMaterialLink> listAscmSupplierMaterialLink = new List<AscmSupplierMaterialLink>();
                        List<AscmSupplierMaterialLink> listLink = AscmSupplierMaterialLinkService.GetInstance().GetList(supplierId.Value);
                        int maxId = YnDaoHelper.GetInstance().nHibernateHelper.GetMaxId("select max(id) from AscmSupplierMaterialLink");

                        IWorkbook wb = WorkbookFactory.Create(stream);
                        ISheet sheet = wb.GetSheet("Sheet1");
                        System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
                        string docNumbers = string.Empty;//对物料进行匹配,不需单引号
                        string docNumbersIn = string.Empty;//查询物料,需要对每个字符串加单引号
                        while (rows.MoveNext())
                        {
                            sError = "【未成功更新数据】<br>";
                            IRow row = (IRow)rows.Current;
                            if (row.RowNum != 0)
                            {
                                ICell buildingCell = row.GetCell(documentIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK);
                                if (buildingCell != null)
                                {
                                    if (!string.IsNullOrEmpty(docNumbers))
                                        docNumbers += ",";
                                    docNumbers += buildingCell.ToString().Trim();
                                    if (!string.IsNullOrEmpty(docNumbersIn))
                                        docNumbersIn += ",";
                                    docNumbersIn += "'" + buildingCell.ToString().Trim() + "'";
                                }
                                else
                                {
                                    sError += "&nbsp;&nbsp;[" + row.RowNum + 1 + "]行" + "无法识别;<br>";
                                }
                            }
                        }
                        if (!string.IsNullOrEmpty(docNumbersIn))
                        {
                            string sql = " from AscmMaterialItem where ";
                            string sSql = string.Empty;
                            var sDocNumbersIn = docNumbersIn.Split(',').Distinct();
                            var iCount = sDocNumbersIn.Count();
                            if (iCount > 900)
                            {
                                string ids = string.Empty;
                                for (int i = 0; i < iCount; i++)
                                {
                                    if (!string.IsNullOrEmpty(ids))
                                        ids += ",";
                                    ids += sDocNumbersIn.ElementAt(i);
                                    if ((i + 1) % 900 == 0 || (i + 1) == iCount)
                                    {
                                        if (!string.IsNullOrEmpty(sSql))
                                            sSql += " or ";
                                        sSql += "docNumber in (" + ids + ")";
                                        ids = string.Empty;
                                    }
                                }
                            }
                            else
                                sSql = "docNumber in (" + docNumbersIn + ")";
                            sql = sql + sSql;
                            listAscmMaterialItem = AscmMaterialItemService.GetInstance().GetList(sql, true);
                        }
                        if (listAscmMaterialItem != null && listAscmMaterialItem.Count() > 0)
                        {
                            var sDocNumbers = docNumbers.Split(',').Distinct();
                            AscmMaterialItem ascmMaterialItem = null;
                            int iRow = 0;
                            foreach (string docNumber in sDocNumbers)
                            {
                                iRow++;
                                ascmMaterialItem = listAscmMaterialItem.Find(item => item.docNumber == docNumber);
                                if (ascmMaterialItem != null)
                                {
                                    AscmSupplierMaterialLink ascmSupplierMaterialLink = null;
                                    if (listLink != null && listLink.Count() > 0)
                                        ascmSupplierMaterialLink = listLink.Find(item => item.materialId == ascmMaterialItem.id);
                                    if (ascmSupplierMaterialLink == null)
                                    {
                                        AscmSupplierMaterialLink ascmLink = new AscmSupplierMaterialLink();
                                        ascmLink.id = ++maxId;
                                        ascmLink.materialId = ascmMaterialItem.id;
                                        ascmLink.modifyTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                        ascmLink.modifyUser = userName;
                                        ascmLink.supplierId = supplierId.Value;
                                        listAscmSupplierMaterialLink.Add(ascmLink);
                                    }
                                }
                                else
                                {
                                    sError += "&nbsp;&nbsp;[" + docNumber + "]" + "未找到匹配物料;<br>";
                                }
                            }
                        }
                        else
                        {
                            sError += "&nbsp;&nbsp;" + "未找到匹配物料;<br>";
                        }
                        if (listAscmSupplierMaterialLink != null && listAscmSupplierMaterialLink.Count() > 0)
                        {
                            AscmSupplierMaterialLinkService.GetInstance().Save(listAscmSupplierMaterialLink);
                            sError += "【成功更新" + listAscmSupplierMaterialLink.Count() + "条】";
                        }
                        jsonObjectResult.message = sError;
                        jsonObjectResult.result = true;
                    }
                    #region
                    //List<AscmSupplierMaterialLink> listAscmSupplierMaterialLink = null;
                    //List<AscmSupplierMaterialLink> listLink=AscmSupplierMaterialLinkService.GetInstance().GetList(supplierId.Value);
                    //List<AscmMaterialItem> listAscmMaterialItem=AscmMaterialItemService.GetInstance().GetList(" from AscmMaterialItem");
                    //int maxId = YnDaoHelper.GetInstance().nHibernateHelper.GetMaxId("select max(id) from AscmSupplierMaterialLink");
                    //using (Stream stream = fileImport.InputStream)
                    //{
                    //    listAscmSupplierMaterialLink = new List<AscmSupplierMaterialLink>();

                    //    IWorkbook wb = WorkbookFactory.Create(stream);
                    //    ISheet sheet = wb.GetSheet("Sheet1");
                    //    System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
                    //    while (rows.MoveNext())
                    //    {
                    //        sError = "【未成功更新数据】<br>";
                    //        IRow row = (IRow)rows.Current;
                    //        if (row.RowNum != 0)
                    //        {
                    //            ICell buildingCell = row.GetCell(documentIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK);
                    //            string docNumber=string.Empty;
                    //            if (buildingCell != null)
                    //            {
                    //                docNumber = buildingCell.ToString().Trim();
                    //                AscmMaterialItem ascmMaterialItem = listAscmMaterialItem.Find(item => item.docNumber == docNumber);
                    //                if (ascmMaterialItem != null)
                    //                {
                    //                    AscmSupplierMaterialLink ascmSupplierMaterialLink = null;
                    //                    if(listLink!=null&&listLink.Count()>0)
                    //                        ascmSupplierMaterialLink = listLink.Find(item => item.materialId == ascmMaterialItem.id);
                    //                    if (ascmSupplierMaterialLink == null)
                    //                    {
                    //                        AscmSupplierMaterialLink ascmLink = new AscmSupplierMaterialLink();
                    //                        ascmLink.id = ++maxId;
                    //                        ascmLink.materialId = ascmMaterialItem.id;
                    //                        ascmLink.modifyTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    //                        ascmLink.modifyUser = userName;
                    //                        ascmLink.supplierId = supplierId.Value;
                    //                        listAscmSupplierMaterialLink.Add(ascmLink);
                    //                    }
                    //                }
                    //                else
                    //                {
                    //                    sError += "&nbsp;&nbsp;[" + row.RowNum + 1 + "]行" + "未匹配到相应物料;<br>";
                    //                }
                    //            }
                    //            else
                    //            {
                    //                sError += "&nbsp;&nbsp;[" + row.RowNum + 1 + "]行" + "未识别到物料编码;<br>";
                    //            }
                    //        }
                    //    }
                    //    if (listAscmSupplierMaterialLink != null && listAscmSupplierMaterialLink.Count() > 0)
                    //    {
                    //        AscmSupplierMaterialLinkService.GetInstance().Save(listAscmSupplierMaterialLink);
                    //        sError += "【成功更新" + listAscmSupplierMaterialLink.Count() + "条】";
                    //        jsonObjectResult.message = sError;
                    //        jsonObjectResult.result = true;
                    //    }
                    //}
                    #endregion
                }
            }
            catch (Exception ex)
            {
                jsonObjectResult.message += ex.Message;
            }
            string sReturn = JsonConvert.SerializeObject(jsonObjectResult);
            return Content(sReturn);
        }
 public void Update(AscmSupplierMaterialLink ascmSupplierMaterialLink)
 {
     using (ITransaction tx = YnDaoHelper.GetInstance().nHibernateHelper.GetCurrentSession().BeginTransaction())
     {
         try
         {
             YnDaoHelper.GetInstance().nHibernateHelper.Update<AscmSupplierMaterialLink>(ascmSupplierMaterialLink);
             tx.Commit();//正确执行提交
         }
         catch (Exception ex)
         {
             tx.Rollback();//回滚
             YnBaseClass2.Helper.LogHelper.GetLog().Error("修改失败(Update AscmSupplierMaterialLink)", ex);
             throw ex;
         }
     }
 }