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_ToleranceProportion_Controller { /// /// 获取容差控制列表 /// /// /// public static void Get_TA_ToleranceProportion_List(TA_ToleranceProportion p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TA_ToleranceProportion; if (!string.IsNullOrEmpty(p_entity.PartCode)) { q = q.Where(p => p.PartCode == p_entity.PartCode); } _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_ToleranceProportion_Controller), "Get_TA_ToleranceProportion_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_ToleranceProportion_Controller), "Get_TA_ToleranceProportion_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_ToleranceProportion_Controller), "Get_TA_ToleranceProportion_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_ToleranceProportion_Controller), "Get_TA_ToleranceProportion_List", e.Message); } } /// /// 模具分摊导入 /// /// /// /// /// public static ResultObject EXCEL_MOLDSHARING_MOD(List p_order_list, string site, string p_creator) { ResultObject _ret = new ResultObject(); List list = new List(); _ret.State = ReturnStatus.Succeed; _ret.Result = true; try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { List ErrorList = new List(); var _lst = p_order_list; _lst.ForEach(p => { var _ls = CheckExcelMode_MoldSharing(db, p, site); if (_ls.Count > 0) { ErrorList.Add(string.Join("
", _ls.ToArray())); } }); if (ErrorList.Count > 0) { _ret.State = ReturnStatus.Failed; _ret.MessageList.AddRange(ErrorList); _ret.Result = false; } else { _lst.ForEach(p => { var mold = db.TA_ToleranceProportion.FirstOrDefault(t => t.PartCode.ToUpper() == p.零件号.ToUpper() && t.Site == p.工厂编码.Trim()); if (mold == null) { mold = new TA_ToleranceProportion { PartCode = p.零件号.ToUpper(), CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), CreateUser = p_creator, Site = p.工厂编码.Trim(), ToleranceProportion = decimal.Parse(p.容差), }; } else { mold.ToleranceProportion = decimal.Parse(p.容差); mold.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); mold.UpdateUser = p_creator; } list.Add(mold); }); if (_ret.MessageList.Count == 0) { db.TA_ToleranceProportion.AddOrUpdate(q => q.UID, list.ToArray()); 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_ToleranceProportion_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_ToleranceProportion_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_ToleranceProportion_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_ToleranceProportion_Controller), "EXCEL_MOLDSHARING_MOD", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); _ret.MessageList.Add("保存失败" + e.Message); } return _ret; } /// /// 数据检验 /// /// /// /// /// public static List CheckExcelMode_MoldSharing(ScpEntities db, SCP_PART_ToleranceProportion p_excel, string site) { List ErrorList = new List(); if (string.IsNullOrEmpty(p_excel.零件号) || string.IsNullOrEmpty(p_excel.工厂编码) || string.IsNullOrEmpty(p_excel.容差)) { ErrorList.Add(string.Format("零件编号【{0}】有填写为空!", p_excel.零件号)); } else { int count = db.TA_PART.Count(p => p.PartCode == p_excel.零件号 && p.Site == site); if (count <= 0) { ErrorList.Add(string.Format("零件编号{0}不存在!", p_excel.零件号)); } decimal ty = 0.00m; if (!decimal.TryParse(p_excel.容差, out ty)) { ErrorList.Add(string.Format("零件编号{0},容差填写不正确,不是数字类型!", p_excel.零件号)); } else { if (ty > 1 || ty < 0) { ErrorList.Add(string.Format("零件编号{0},容差填写不正确,只能填写0到1之间小数!", p_excel.零件号)); } } } return ErrorList; } /// /// 检查是否超出容差数据 /// /// /// /// public static List CheckToleranceProportion(V_TB_ASK_DETAIL entity, decimal QtyAll) { List ErrorList = new List(); var canQty = entity.AskQty - entity.ShippedQty - entity.ReduceQty; // WS 和 WG 类型物料 填写的 发货数 不能大于 (要货数量-已发数量) if (entity.DockCode == "WS" || entity.DockCode == "WG") { if (canQty > 0) { if (QtyAll > canQty) { ErrorList.Add(entity.PartCode + "发货数量>可发数量"); } } } // YC 和 WT 填写的 发货数 能大于 (要货数 - 已发数量) 但是 不能 大于容差范围 并且 要货数 - 已发数量 不能小于等于0 else if (entity.DockCode == "YC" || entity.DockCode == "WT") { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var toleranceProportion = db.TA_ToleranceProportion.FirstOrDefault(q => q.PartCode == entity.PartCode); if (toleranceProportion == null) { ErrorList.Add(entity.PartCode + "没有容差控制,请联系管理员进行填写"); } else { decimal maxqty = entity.AskQty * (1 + toleranceProportion.ToleranceProportion); if (canQty <= 0) { ErrorList.Add(entity.PartCode + "只能超发一次哦"); } // 最大要货数量(要货数* 1+容差) - 累计发货数量 <当前填写的 要货数量 else if ((maxqty - entity.ShippedQty - entity.ReduceQty )< QtyAll) { ErrorList.Add(entity.PartCode + "容差控制为"+ (toleranceProportion.ToleranceProportion * 100)+"% ,本次填写发货数量超出"+ Math.Abs((maxqty - entity.ShippedQty - QtyAll).Value)); } } } } else if ((canQty <= 0) && (QtyAll > 0)) { ErrorList.Add(entity.PartCode + "只能超发一次哦"); } return ErrorList; } } }