using System; using System.Collections.Generic; using System.Linq; using System.Text; using EMIS.DataLogic.Common.TeachingMaterial; using EMIS.ViewModel.TeachingMaterial; using Bowin.Common.Linq.Entity; using EMIS.ViewModel; using EMIS.Entities; using EMIS.DataLogic.Repositories; using System.Linq.Expressions; using Bowin.Common.Linq; using EMIS.DataLogic.Common.ProcurementPlan; using EMIS.CommonLogic.SystemServices; using System.Text.RegularExpressions; namespace EMIS.CommonLogic.TeachingMaterial { public class StockInServices : BaseServices, IStockInServices { public StockInDAL StockInDAL { get; set; } public PurchasingStatisticsDAL PurchasingStatisticsDAL { get; set; } public Lazy TeachersOrderServices { get; set; } public Lazy ParameterServices { get; set; } public Lazy SerialNumberServices { get; set; } #region --0.0 定义-- public TeachersOrderRepository TeachersOrderRepository { get; set; } public StockInRepository StockInRepository { get; set; } public StockInDetailRepository StockInDetailRepository { get; set; } public SchoolyearRepository SchoolyearRepository { get; set; } public UserRepository UserRepository { get; set; } public TeachingMaterialPoolRepository TeachingMaterialPoolRepository { get; set; } public CoursematerialRepository CoursematerialRepository { get; set; } public PublishRepository PublishRepository { get; set; } public CollegeRepository CollegeRepository { get; set; } public DictionaryItemRepository DictionaryItemRepository { get; set; } public TeachingMateriaInventoryRepository TeachingMateriaInventoryRepository { get; set; } #endregion #region 1.0 查询入库信息 public Bowin.Common.Linq.Entity.IGridResultSet GetStockInViewGrid(ViewModel.ConfiguretView configuretView, Guid? schoolyearID, Guid? supplierID, DateTime? stockInDJTimeStart, DateTime? stockInDJTimeEnd, int? isLate, IList stockInTypeIDList, int pageIndex, int pageSize) { if (stockInDJTimeStart == null) { stockInDJTimeStart = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")); } var query = StockInDAL.GetStockInGridView(x => stockInTypeIDList.Contains(x.StockInType)); if (isLate.HasValue&& isLate >= 0) { query = query.Where(x => x.RecordStatus == isLate); } if (schoolyearID.HasValue) query = query.Where(x => x.SchoolyearID == schoolyearID); if (supplierID.HasValue) query = query.Where(x => x.SupplierID == supplierID); if (stockInDJTimeStart.HasValue) query = query.Where(x => x.StockInDJTime >= stockInDJTimeStart); if (stockInDJTimeEnd.HasValue) { DateTime newStockOutTimeEnd = stockInDJTimeEnd.Value.AddDays(1);//取起止日期一天、 query = query.Where(x => x.StockInDJTime <= newStockOutTimeEnd); } // query = GetQueryByDataRangeByCollege(query);//区分院系数据 if (!string.IsNullOrEmpty(configuretView.ConditionValue)) return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.StockInDJTime).ToGridResultSet(pageIndex, pageSize); return query.OrderBy(x => x.SchoolyearName).OrderBy(x => x.StockInDocumentNo).OrderByDescending(x => x.StockInDJTime).ToGridResultSet(pageIndex, pageSize); } #endregion #region 2.0 查询教材信息 public IGridResultSet GetStockInByTeachingMaterialPoolViewGrid(ViewModel.ConfiguretView configuretView, Guid? schoolyearID, Guid? teachingMaterialPoolID, Guid? teachingMaterialCodeID, decimal? discount, int pageIndex, int pageSize) { var endStatusID = TeachersOrderServices.Value.GetCorrectEndStatus(); Expression> teacherOrderExp = (x => x.ApprovalStatus == endStatusID); Expression> studentOrderExp = (x => true); if (schoolyearID.HasValue) { teacherOrderExp = teacherOrderExp.And(x => x.SchoolyearID == schoolyearID); studentOrderExp = studentOrderExp.And(x => x.EM_SpecialtyPlan.SchoolyearID == schoolyearID); } if (teachingMaterialPoolID.HasValue) { teacherOrderExp = teacherOrderExp.And(x => x.TeachingMaterialPoolID == teachingMaterialPoolID); studentOrderExp = studentOrderExp.And(x => x.TeachingMaterialPoolID == teachingMaterialPoolID); } if (teachingMaterialCodeID.HasValue) { teacherOrderExp = teacherOrderExp.And(x => x.TeachingMaterialPoolID == teachingMaterialCodeID); studentOrderExp = studentOrderExp.And(x => x.TeachingMaterialPoolID == teachingMaterialCodeID); } var query = StockInDAL.GetTeachingMaterial(x => x.IsLate == false, 1); if (discount.HasValue) { query = StockInDAL.GetTeachingMaterial(x => x.IsLate == false, discount);//获取不过期数据 } if (!string.IsNullOrEmpty(configuretView.ConditionValue)) return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.TeachingMaterialCode.Length).ThenBy(x => x.TeachingMaterialCode).ToGridResultSet(pageIndex, pageSize); return query.OrderBy(x => x.TeachingMaterialCode.Length).ThenBy(x => x.TeachingMaterialCode).ToGridResultSet(pageIndex, pageSize); } #endregion #region 3.0 保存 public void Save(StockInView stockInView, IList tmList, Guid? userId, string type) { if (this.StockInRepository.GetList(x => x.StockInDocumentNo == stockInView.StockInDocumentNo && x.StockInID != stockInView.StockInID).Count() > 0) { throw new Exception("入库单号已经存在,请重新输入!"); } int RecordStatusType = (int)CF_GeneralPurpose.IsNo; if (type == "Submit")//如果是Save 那么状态默认为0、 如果是Submit 那么状态默认1 { RecordStatusType = (int)CF_GeneralPurpose.IsYes; } ET_StockIn stockInEntity = StockInRepository.GetSingle(x => x.StockInID == stockInView.StockInID); if (stockInEntity != null) { stockInEntity.Desc = stockInView.Desc; stockInEntity.SchoolyearID = stockInView.SchoolyearID; stockInEntity.StockInTime = (DateTime)stockInView.StockInDJTime; stockInEntity.StockInDocumentNo = stockInView.StockInDocumentNo; stockInEntity.SupplierID = (Guid)stockInView.SupplierID; stockInEntity.StockInNumber = stockInView.StockInNumber; stockInEntity.RecordStatus = RecordStatusType; StockInRepository.UnitOfWork.Update(stockInEntity); // StockInRepository.UnitOfWork.Commit(); //删除明细数据、重新插入 StockInDetailRepository.UnitOfWork.Delete(x => x.StockInID == stockInView.StockInID); List stockInDetailList = new List(); foreach (var item in tmList) { ET_StockInDetail stockInDetail = new ET_StockInDetail() { CreateTime = DateTime.Now, CreateUserID = userId, Desc = stockInView.Desc, Discount = item.Discount, LibraryID = null,//stockInView.LibraryID,, ModifyTime = DateTime.Now, ModifyUserID = userId, DiscountPrice = item.Discount * item.Price,//折合价 Quantity = item.OrderQty, RecordStatus = RecordStatusType, StockInDetailID = Guid.NewGuid(), StockInID = stockInView.StockInID, TeachingMaterialPoolID = item.TeachingMaterialPoolID }; UnitOfWork.Add(stockInDetail); } } else { ET_StockIn stockIn = new ET_StockIn() { StockInID = Guid.NewGuid(), Desc = stockInView.Desc, StockInType = stockInView.StockInType, CreateTime = DateTime.Now, CreateUserID = userId, SchoolyearID = stockInView.SchoolyearID, ModifyTime = DateTime.Now, ModifyUserID = userId, HandlerUserID = userId, OrdersNo = stockInView.OrdersNo, RecordStatus = RecordStatusType, StockInSumMoney = stockInView.StockInSumMoney, StockInTime = (DateTime)stockInView.StockInDJTime, StockInNumber = stockInView.StockInNumber, StockInDocumentNo = stockInView.StockInDocumentNo, SupplierID = (Guid)stockInView.SupplierID }; UnitOfWork.Add(stockIn); List stockInDetailList = new List(); foreach (var item in tmList) { ET_StockInDetail stockInDetail = new ET_StockInDetail() { CreateTime = DateTime.Now, CreateUserID = userId, Desc = stockInView.Desc, Discount = item.Discount, LibraryID = null,//stockInView.LibraryID, ModifyTime = DateTime.Now, ModifyUserID = userId, DiscountPrice = item.Discount * item.Price,//折合价 Quantity = item.OrderQty, RecordStatus = RecordStatusType, StockInDetailID = Guid.NewGuid(), StockInID = stockIn.StockInID, TeachingMaterialPoolID = item.TeachingMaterialPoolID }; UnitOfWork.Add(stockInDetail); } } UnitOfWork.Commit(); } #endregion #region 4.0 Excel 查询 public IList GetStockInViewList(ViewModel.ConfiguretView configuretView, Guid? schoolyearID, Guid? supplierID, DateTime? stockInDJTimeStart, DateTime? stockInDJTimeEnd, int? isLate, IList stockInTypeIDList) { DateTime newStockOutTimeEnd = stockInDJTimeEnd.Value.AddDays(1);//取起止日期一天、 var query = StockInDAL.GetStockInGridView(x => stockInTypeIDList.Contains(x.StockInType)); if (isLate.HasValue && isLate > 0) { query = query.Where(x => x.RecordStatus == isLate); } if (schoolyearID.HasValue) query = query.Where(x => x.SchoolyearID == schoolyearID); if (supplierID.HasValue) query = query.Where(x => x.SupplierID == supplierID); if (stockInDJTimeStart.HasValue) query = query.Where(x => x.StockInDJTime >= stockInDJTimeStart); if (stockInDJTimeEnd.HasValue) query = query.Where(x => x.StockInDJTime <= newStockOutTimeEnd); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.StockInDJTime).ToList(); return query.OrderBy(x => x.StockInDJTime).ToList(); } #endregion #region 5.0 删除 /// /// 删除入库信息 /// /// public void Delete(List stockInIDs) { try { var stockInList = StockInRepository.GetList(x => stockInIDs.Contains(x.StockInID)).ToList(); foreach (var stockIn in stockInList) { if (stockIn.RecordStatus == (int)SYS_STATUS.USABLE) { throw new Exception("选择的信息已入库,无法删除。"); } } if (stockInIDs.Count > 0) { //删除入库数据、及该单号下挂钩的明细数据 StockInDetailRepository.UnitOfWork.Delete(x => stockInIDs.Contains(x.StockInID)); StockInRepository.UnitOfWork.Delete(x => stockInIDs.Contains(x.StockInID)); } } catch (Exception ex) { throw ex; } } #endregion #region 6.0 获取编辑信息 /// /// 获取编辑数据 /// /// /// public StockInView GetSingleStockIn(Guid stockInID) { return StockInDAL.GetSingleStockIn(stockInID); } #endregion #region 7.0 获取要入库的教材信息 public IList GetStockInByTMViewList(Guid stockInID) { return StockInDAL.GetTeachingMaterialView(stockInID).OrderBy(x => x.TeachingMaterialCode.Length).ThenBy(x => x.TeachingMaterialCode).ToList(); } #endregion #region 8.0 废弃的提交 public void Submit(List stockInIDs, Guid userId) { try { if (stockInIDs.Count > 0) { var stockInList = StockInRepository.GetList(x => stockInIDs.Contains(x.StockInID), (x => x.ET_StockInDetail)).ToList(); foreach (var stockInEnity in stockInList) { if (stockInEnity.RecordStatus == (int)CF_GeneralPurpose.IsYes) { throw new Exception("选择的信息已提交,无法重复提交。"); } } foreach (var stockInEnity in stockInList) { stockInEnity.RecordStatus = (int)CF_GeneralPurpose.IsYes; StockInRepository.UnitOfWork.Update(stockInEnity); StockInRepository.UnitOfWork.Commit(); IList stockInDetailList = stockInEnity.ET_StockInDetail.ToList(); BalanceInventory(stockInDetailList, userId);//提交平衡库存 } } } catch (Exception ex) { throw ex; } } #endregion #region 9.0 检验入库单据编号是否存在 /// /// 检验入库单据编号是否存在 /// /// /// public bool IsExistStockInDocumentNo(string stockInDocumentNo) { return StockInDAL.IsExistStockInDocumentNo(stockInDocumentNo); } #endregion #region 11.0 平衡教材库存数量(适用与出库、入库) public void BalanceInventory(IList stockInDetailList, Guid userId) { //1.0 提取当前库存量、 foreach (var stockInDetail in stockInDetailList) { ET_TeachingMateriaInventory teachingMateriaInventoryEntity = TeachingMateriaInventoryRepository.GetSingle(x => x.TeachingMaterialPoolID == stockInDetail.TeachingMaterialPoolID && x.Discount == stockInDetail.DiscountPrice); if (teachingMateriaInventoryEntity != null) { //2.0 当前库存量+入库数量 当前库存量-出库数量 teachingMateriaInventoryEntity.PresentInventory = teachingMateriaInventoryEntity.PresentInventory + stockInDetail.Quantity; teachingMateriaInventoryEntity.ModifyTime = DateTime.Now; TeachingMateriaInventoryRepository.UnitOfWork.Update(teachingMateriaInventoryEntity);//更新库存数量 } else { ET_TeachingMateriaInventory teachingMateriaInventoryEnt = new ET_TeachingMateriaInventory { PresentInventory = stockInDetail.Quantity, TeachingMateriaInventoryID = Guid.NewGuid(), CreateTime = DateTime.Now, CreateUserID = userId, Discount = stockInDetail.Discount, DiscountPrice = stockInDetail.DiscountPrice, ModifyTime = DateTime.Now, ModifyUserID = userId, TeachingMaterialPoolID = (Guid)stockInDetail.TeachingMaterialPoolID }; TeachingMateriaInventoryRepository.UnitOfWork.Add(teachingMateriaInventoryEnt); } } UnitOfWork.Commit(); } #endregion #region 12.0 自动生成入库信息 public void AutoNewStockIn(Guid? schoolyearID, Guid? userId) { var endStatusID = TeachersOrderServices.Value.GetCorrectEndStatus(); Expression> exporder = (x => x.ApprovalStatus == endStatusID); Expression> stuExporder = (x => true); Expression> poolexp = (x => true); if (schoolyearID.HasValue) { exporder = exporder.And(x => x.SchoolyearID == schoolyearID); stuExporder = stuExporder.And(x => x.EM_SpecialtyPlan.SchoolyearID == schoolyearID); } var Purchasingquery = PurchasingStatisticsDAL.GetPurchasingStatisticsGridView(exporder, stuExporder); List purchasingList = Purchasingquery.ToList();//获取一个学期下所有采购信息 var StockInquery = StockInDAL.GetStockInGridView(x => true); if (schoolyearID.HasValue) { StockInquery = StockInquery.Where(x => x.SchoolyearID == schoolyearID); } List stockInList = StockInquery.ToList(); List TeachingMaterialPoolIDList = new List(); List stockinIDList = stockInList.Select(x => x.StockInID).ToList(); var teachingMaterialPoolViewList = StockInDAL.GetDetailByIDList(stockinIDList).ToList(); List purchasingList1 = new List(); List a = new List(); foreach (PurchasingStatisticsView purchasing in purchasingList) { //判断这个学期下是否已经有入库信息,有的就排除。 if (stockInList != null) { foreach (StockInView stockIn in stockInList) { var teachingMaterialPoolView = teachingMaterialPoolViewList.Where(x => x.StockInID == stockIn.StockInID); foreach (var view in teachingMaterialPoolView) { if (view.TeachingMaterialPoolID == purchasing.TeachingMaterialPoolID) { TeachingMaterialPoolIDList.Add(purchasing.TeachingMaterialPoolID);//获取已有入库信息的教材ID } }; }; } }; List id = TeachingMaterialPoolIDList.Distinct().ToList(); List tmList = new List(); var supplier = StockInDAL.SupplierDiscountSetRepository.GetList(x => x.IsDefault == true).FirstOrDefault(); var query = StockInDAL.GetTeachersOrderByTeachingMaterial(x => x.IsLate == false, exporder, stuExporder); purchasingList1 = Purchasingquery.Where(x => id.All(y => y!=x.TeachingMaterialPoolID)).ToList(); tmList = StockInDAL.GetTeachersOrderByTeachingMaterial(poolexp, exporder, stuExporder).Where(x => id.All(y => y != x.TeachingMaterialPoolID)).ToList(); //foreach (var MateriaPoolID in id) //{ // //purchasingList = null; // //PurchasingStatisticsView view = Purchasingquery.Where(x => x.TeachingMaterialPoolID != MateriaPoolID).FirstOrDefault(); // //purchasingList1.Add(view);//排除掉已有的入库信息 // poolexp = poolexp.And(x => x.TeachingMaterialPoolID == MateriaPoolID); // TeachingMaterialPoolView pool = StockInDAL.GetTeachersOrderByTeachingMaterial(poolexp, exporder, stuExporder).FirstOrDefault();//根据教材总库ID获取教材信息 // tmList.Add(pool); //}; string datestr = DateTime.Now.ToString("yyyyMMdd"); var stockInNumber = this.GetStockInNo(CF_StockInType.PurchasingStockIn); var numStr = stockInNumber.Remove(0, stockInNumber.Length - 4); var num = int.Parse(numStr); var noStr = stockInNumber.Remove(stockInNumber.Length - 4); int count = 0; foreach (PurchasingStatisticsView pur in purchasingList1) { string stockInNumberstr = noStr + num.ToString("0000"); ET_StockIn NewstockIn = new ET_StockIn() { StockInID = Guid.NewGuid(), Desc = "",// StockInType = (int)CF_StockInType.PurchasingStockIn, CreateTime = DateTime.Now, CreateUserID = userId, SchoolyearID = schoolyearID, ModifyTime = DateTime.Now, ModifyUserID = userId, HandlerUserID = userId, OrdersNo = null,// RecordStatus = 0, StockInSumMoney = Math.Round((decimal)(pur.Price * supplier.Discount * pur.CountNumber), 2),// StockInTime = DateTime.Now,// StockInNumber = datestr + num.ToString("0000"),//入库编号 StockInDocumentNo = stockInNumberstr,//入库单据号 SupplierID = supplier == null ? null : supplier.PublishID, }; num += 1; count += 1; UnitOfWork.Add(NewstockIn); foreach (var item in tmList.Where(x => x.TeachingMaterialPoolID == pur.TeachingMaterialPoolID)) { List stockInDetailList = new List(); ET_StockInDetail stockInDetail = new ET_StockInDetail() { CreateTime = DateTime.Now, CreateUserID = userId, Desc = "",// Discount = supplier == null ? 1 : supplier.Discount, LibraryID = null,//stockInView.LibraryID, ModifyTime = DateTime.Now, ModifyUserID = userId, DiscountPrice = (supplier == null ? 1 : supplier.Discount) * pur.Price,//折合价 Quantity = pur.CountNumber, RecordStatus = 0, StockInDetailID = Guid.NewGuid(), StockInID = NewstockIn.StockInID, TeachingMaterialPoolID = item.TeachingMaterialPoolID }; UnitOfWork.Add(stockInDetail); }; }; string prefix = ""; DateTime nowTime = DateTime.Now; prefix = ParameterServices.Value.GetParameterValue(CF_ParameterType.PurchaseStockInPrifix); var matchFormatterList = Regex.Matches(prefix, @"\{[\S]*\}"); for (int i = 0; i < matchFormatterList.Count; i++) { var formatter = matchFormatterList[i].Value.TrimStart('{').TrimEnd('}'); var dateString = nowTime.ToString(formatter); prefix = prefix.Replace(matchFormatterList[i].Value, dateString); } UnitOfWork.Commit(); SerialNumberServices.Value.SkipSN(prefix, count - 1); } #endregion public string GetStockInNo(CF_StockInType stockInType) { string prefix = ""; DateTime nowTime = DateTime.Now; switch (stockInType) { case CF_StockInType.PurchasingStockIn: prefix = ParameterServices.Value.GetParameterValue(CF_ParameterType.PurchaseStockInPrifix); break; case CF_StockInType.Return: prefix = ParameterServices.Value.GetParameterValue(CF_ParameterType.ReturnStockInPrifix); break; } var matchFormatterList = Regex.Matches(prefix, @"\{[\S]*\}"); for (int i = 0; i < matchFormatterList.Count; i++) { var formatter = matchFormatterList[i].Value.TrimStart('{').TrimEnd('}'); var dateString = nowTime.ToString(formatter); prefix = prefix.Replace(matchFormatterList[i].Value, dateString); } string stockOutNo = SerialNumberServices.Value.SetSN(prefix, 4); return stockOutNo; } } }