using System; using System.Linq; using CK.SCP.Models.Enums; using CK.SCP.Models.ScpEntity; using CK.SCP.Utils; using CK.SCP.Models; using System.Data; using System.Data.Entity.Core; using System.Text; using CK.SCP.Models.ScpEntity.ExcelExportEnttity; using System.Collections.Generic; using System.Data.Entity.Migrations; namespace CK.SCP.Controller { public class SCP_MoldSharing_Controller { /// /// 获取模具分摊列表 /// /// /// public static void Get_V_TA_MoldSharing_List(V_TA_MOLDSHARING p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TA_MOLDSHARING; if (!string.IsNullOrEmpty(p_entity.PartCode)) { q = q.Where(p => p.PartCode == p_entity.PartCode); } if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId == p_entity.VendId); } q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); _ret.State = ReturnStatus.Succeed; _ret.Result = q; p_action(_ret); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MoldSharing_Controller), "Get_V_TA_MoldSharing_List", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MoldSharing_Controller), "Get_V_TA_MoldSharing_List", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MoldSharing_Controller), "Get_V_TA_MoldSharing_List", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(e); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MoldSharing_Controller), "Get_V_TA_MoldSharing_List", e.Message); throw e; } } /// /// 模具分摊导入 /// /// /// /// /// public static ResultObject EXCEL_MOLDSHARING_MOD(List p_order_list, string site, string p_creator) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { int number = 1; List lineError = new List(); List ErrorList = new List(); var _lst = p_order_list; _lst.ForEach(p => { var _ls = CheckExcelMode_MoldSharing(db, p, site); if (_ls.Count > 0) { lineError.Add(number.ToString()); ErrorList.Add(string.Join("
", _ls.ToArray())); } number++; }); if (lineError.Count > 0) { _ret.State = ReturnStatus.Failed; _ret.MessageList.AddRange(ErrorList); _ret.Result = false; } else { _lst.ForEach(p => { var _moldsharing = db.TA_MoldSharing.SingleOrDefault(t => t.VendId == p.供应商编码.ToUpper() && t.PartCode == p.零件编码.ToUpper()) ?? new TA_MOLDSHARING { VendId = p.供应商编码.ToUpper(), PartCode = p.零件编码.ToUpper(), CreateTime = DateTime.Now, CreateUser = p_creator, IsDeleted = false, Count = 0, }; _moldsharing.Price = decimal.Parse(p.价格); _moldsharing.Qty = decimal.Parse(p.分摊数量); _moldsharing.UpdateTime = DateTime.Now; _moldsharing.UpdateUser = p_creator; _moldsharing.Site = site; db.TA_MoldSharing.AddOrUpdate(_moldsharing); }); if (_ret.MessageList.Count == 0) { int state = db.SaveChanges(); if (state != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MoldSharing_Controller), "EXCEL_MOLDSHARING_MOD", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MoldSharing_Controller), "EXCEL_MOLDSHARING_MOD", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MoldSharing_Controller), "EXCEL_MOLDSHARING_MOD", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MoldSharing_Controller), "EXCEL_MOLDSHARING_MOD", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static List CheckMode_MoldSharing(ScpEntities db, SCP_MOLDSHARING_EXPORT p_excel, string site) { List ErrorList = new List(); int count = db.TA_MoldSharing.Count(p => p.VendId == p_excel.供应商编码 && p.Site == site&&p.PartCode==p_excel.零件编码); if (count <= 0) { ErrorList.Add(string.Format("供应商{0}零件编码{1}已存在分摊数据,不可修改分摊价格和数量!", p_excel.供应商编码, p_excel.零件编码)); } return ErrorList; } /// /// 数据检验 /// /// /// /// /// public static List CheckExcelMode_MoldSharing(ScpEntities db, SCP_MOLDSHARING_EXPORT p_excel, string site) { List ErrorList = new List(); if (!string.IsNullOrEmpty(p_excel.供应商编码)) { int count = db.TA_VENDER.Count(p => p.VendId == p_excel.供应商编码 && p.Site == site); if (count <= 0) { ErrorList.Add(string.Format("供应商{0}不存在!", p_excel.供应商编码)); } } else { ErrorList.Add(string.Format("供应商代码为空!")); } if (!string.IsNullOrEmpty(p_excel.零件编码)) { int count = db.TA_PART.Count(p => p.PartCode == p_excel.零件编码 && p.Site == site); if (count <= 0) { ErrorList.Add(string.Format("零件编号{0}不存在!", p_excel.零件编码)); } } else { ErrorList.Add(string.Format("零件号为空!")); } if ( string.IsNullOrEmpty(p_excel.供应商编码) || string.IsNullOrEmpty(p_excel.零件编码) || string.IsNullOrEmpty(p_excel.价格) || string.IsNullOrEmpty(p_excel.分摊数量)) { ErrorList.Add(string.Format("供应商代码【{0}】零件编号【{1}】有填写为空!", p_excel.供应商编码, p_excel.零件编码)); } return ErrorList; } /// /// 获取模具分摊价格(沈阳模具分摊) /// /// /// public static List GET_MoldSharing_LIST_SYJB(List p_list) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (p_list.Count > 0) { p_list.ForEach(p => { var _entity = db.TA_MoldSharing.Where(itm => itm.Qty > itm.Count && itm.PartCode == p.PartCode && itm.VendId == p.VendId).FirstOrDefault(); if (_entity != null) { p.Price = decimal.Parse(_entity.Price.ToString()); } }); } } return p_list; } /// /// 获取模具分摊价格 /// /// /// public static List GET_MoldSharing_LIST(List p_list) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (p_list.Count > 0) { p_list.ForEach(p => { var _entity = db.TA_MoldSharing.Where(itm => itm.Qty > itm.Count && itm.PartCode == p.PartCode && itm.VendId == p.VendId).FirstOrDefault(); if (_entity != null) { p.BlancePrice = decimal.Parse(_entity.Price.ToString()); } else { p.BlancePrice = 0; } }); } } return p_list; } public static ResultObject Save_TA_MOLDSHARING_STATE (List p_list, string p_user, bool p_isdelete) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (p_isdelete == true) { var _ls = db.TA_MoldSharing.Where(p => p_list.Contains(p.UID)&& p.Count != 0).ToList(); if (_ls.Count <= 0) { _ls.ForEach(p => { p.IsDeleted = true; p.UpdateTime = DateTime.Now; p.UpdateUser = p_user; } ); db.TA_MoldSharing.AddOrUpdate(p => p.UID, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,已经开始计数不能删除!"; } } if (string.IsNullOrEmpty(_ret.Message)) { if (db.SaveChanges() != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "数据更新失败!"; } } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MoldSharing_Controller), "Save_TA_MOLDSHARING_STATE", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MoldSharing_Controller), "Save_TA_MOLDSHARING_STATE", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MoldSharing_Controller), "Save_TA_MOLDSHARING_STATE", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MoldSharing_Controller), "Save_TA_MOLDSHARING_STATE", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } } }