You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

438 lines
19 KiB

1 year ago
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
{
/// <summary>
/// 获取模具分摊列表
/// </summary>
/// <param name="p_entity"></param>
/// <param name="p_action"></param>
public static void Get_V_TA_MoldSharing_List(V_TA_MOLDSHARING p_entity, Action<ResultObject<IQueryable<V_TA_MOLDSHARING>>> p_action)
{
ResultObject<IQueryable<V_TA_MOLDSHARING>> _ret = new ResultObject<IQueryable<V_TA_MOLDSHARING>>();
try
{
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
IQueryable<V_TA_MOLDSHARING> 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;
}
}
/// <summary>
/// 模具分摊导入
/// </summary>
/// <param name="p_order_list"></param>
/// <param name="site"></param>
/// <param name="p_creator"></param>
/// <returns></returns>
public static ResultObject<bool> EXCEL_MOLDSHARING_MOD(List<SCP_MOLDSHARING_EXPORT> p_order_list, string site, string p_creator)
{
ResultObject<bool> _ret = new ResultObject<bool>();
try
{
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
int number = 1;
List<string> lineError = new List<string>();
List<string> ErrorList = new List<string>();
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("<br>", _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<string> CheckMode_MoldSharing(ScpEntities db, SCP_MOLDSHARING_EXPORT p_excel, string site)
{
List<string> ErrorList = new List<string>();
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;
}
/// <summary>
/// 数据检验
/// </summary>
/// <param name="db"></param>
/// <param name="p_excel"></param>
/// <param name="site"></param>
/// <returns></returns>
public static List<string> CheckExcelMode_MoldSharing(ScpEntities db, SCP_MOLDSHARING_EXPORT p_excel, string site)
{
List<string> ErrorList = new List<string>();
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;
}
/// <summary>
/// 获取模具分摊价格(沈阳模具分摊)
/// </summary>
/// <param name="p_list"></param>
/// <returns></returns>
public static List<V_TB_RECEIVE_LIST> GET_MoldSharing_LIST_SYJB(List<V_TB_RECEIVE_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;
}
/// <summary>
/// 获取模具分摊价格
/// </summary>
/// <param name="p_list"></param>
/// <returns></returns>
public static List<V_TB_RECEIVE_LIST> GET_MoldSharing_LIST(List<V_TB_RECEIVE_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<bool> Save_TA_MOLDSHARING_STATE (List<long> p_list, string p_user, bool p_isdelete)
{
ResultObject<bool> _ret = new ResultObject<bool>();
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;
}
}
}