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.

369 lines
15 KiB

4 years ago
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CK.SCP.Models;
using CK.SCP.Models.AppBoxEntity;
using CK.SCP.Models.ScpEntity;
using CK.SCP.Utils;
using System.Data.Entity.Core;
using CK.SCP.Models.Enums;
namespace CK.SCP.Controller
{
public class SCP_TA_FACTORY_CONTROLLER
{
public static ResultObject<List<TA_FACTORY>> Get_TA_FACTORY_List(TA_FACTORY p_entity)
{
ResultObject<List<TA_FACTORY>> _ret = new ResultObject<List<TA_FACTORY>>();
try
{
using (var db = EntitiesFactory.CreateAppBoxInstance())
{
IQueryable<TA_FACTORY> q = db.TA_FACTORY;
if (p_entity.ID != 0)
{
q = q.Where(p => p.ID == p_entity.ID);
}
if (!string.IsNullOrEmpty(p_entity.FactoryId))
{
q = q.Where(p => p.FactoryId.Contains(p_entity.FactoryId));
}
if (!string.IsNullOrEmpty(p_entity.FactoryName))
{
q = q.Where(p => p.FactoryName.Contains(p_entity.FactoryName));
}
if (!string.IsNullOrEmpty(p_entity.ErpSite))
{
q = q.Where(p => p.ErpSite.Contains(p_entity.ErpSite));
}
if (!string.IsNullOrEmpty(p_entity.ErpDomain))
{
q = q.Where(p => p.ErpDomain.Contains(p_entity.ErpDomain));
}
if (!string.IsNullOrEmpty(p_entity.Address))
{
q = q.Where(p => p.Address.Contains(p_entity.Address));
}
if (!string.IsNullOrEmpty(p_entity.ZipCode))
{
q = q.Where(p => p.ZipCode.Contains(p_entity.ZipCode));
}
if (p_entity.State != 0)
{
q = q.Where(p => p.State == p_entity.State);
}
if (!string.IsNullOrEmpty(p_entity.Remark))
{
q = q.Where(p => p.Remark.Contains(p_entity.Remark));
}
if (!string.IsNullOrEmpty(p_entity.CreateUser))
{
q = q.Where(p => p.CreateUser.Contains(p_entity.CreateUser));
}
if (!string.IsNullOrEmpty(p_entity.UpdateUser))
{
q = q.Where(p => p.UpdateUser.Contains(p_entity.UpdateUser));
}
if (!string.IsNullOrEmpty(p_entity.UpdateInfo))
{
q = q.Where(p => p.UpdateInfo.Contains(p_entity.UpdateInfo));
}
q = q.Where(p => p.IsDeleted == p_entity.IsDeleted);
_ret.State = ReturnStatus.Succeed;
_ret.Result = q.ToList();
}
}
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_TA_FACTORY_CONTROLLER), "Get_TA_FACTORY_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_TA_FACTORY_CONTROLLER), "Get_TA_FACTORY_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_TA_FACTORY_CONTROLLER), "Get_TA_FACTORY_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_TA_FACTORY_CONTROLLER), "Get_TA_FACTORY_List", e.Message);
throw e;
}
return _ret;
}
public static ResultObject<bool> Save_TA_FACTORY(TA_FACTORY p_entity,List<TA_FACTORY_CONFIG> p_list)
{
ResultObject<bool> _ret = new ResultObject<bool>();
try
{
using (var db = EntitiesFactory.CreateAppBoxInstance())
{
var factorylist=db.TA_FACTORY.Where(p => p.FactoryId == p_entity.FactoryId).ToList();
//db.TA_FACTORY.RemoveRange(factorylist);
//p_entity.GUID = Guid.NewGuid();
db.TA_FACTORY.AddOrUpdate(p=>p.ID,p_entity);
if (db.SaveChanges() != -1)
{
var scpdb = EntitiesFactory.CreateScpInstance();
{
var _factoryList =scpdb.TA_FACTORY_CONFIG.Where(p => p.FactoryId == p_entity.FactoryId).ToList();
scpdb.TA_FACTORY_CONFIG.RemoveRange(_factoryList);
scpdb.TA_FACTORY_CONFIG.AddRange(p_list);
scpdb.TB_FACTORY.RemoveRange(scpdb.TB_FACTORY.ToArray());
var _ls = new List<TB_FACTORY>();
foreach (var itm in db.TA_FACTORY.ToList())
{
var _tb = new TB_FACTORY();
_tb.GUID = itm.GUID;
_tb.IsDeleted = itm.IsDeleted;
_tb.State = itm.State;
_tb.UpdateInfo = itm.UpdateInfo;
_tb.Remark = itm.Remark;
_tb.FactoryId = itm.FactoryId;
_tb.FactoryName = itm.FactoryName;
_tb.ErpDomain = itm.ErpDomain;
_tb.CreateTime = itm.CreateTime;
_tb.CreateUser = itm.CreateUser;
_tb.ZipCode = itm.ZipCode;
scpdb.TB_FACTORY.Add(_tb);
}
scpdb.SaveChanges();
}
_ret.State = ReturnStatus.Succeed;
_ret.Result = true;
}
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_TA_FACTORY_CONTROLLER), "Save_TA_FACTORY", 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_TA_FACTORY_CONTROLLER), "Save_TA_FACTORY", 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_TA_FACTORY_CONTROLLER), "Save_TA_FACTORY", 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_TA_FACTORY_CONTROLLER), "Save_TA_FACTORY", e.Message);
_ret.Result = false;
_ret.ErrorList.Add(e);
throw e;
}
return _ret;
}
public static ResultObject<bool> Del_TA_FACTORY(TA_FACTORY p_entity)
{
ResultObject<bool> _ret = new ResultObject<bool>();
try
{
using (var db = EntitiesFactory.CreateAppBoxInstance())
{
var _entity=db.TA_FACTORY.Where(itm=>itm.ID== p_entity.ID).FirstOrDefault();
_entity.IsDeleted = true;
db.TA_FACTORY.AddOrUpdate(p=>p.ID,_entity);
if (db.SaveChanges() != -1)
{
var scpdb = EntitiesFactory.CreateScpInstance();
{
scpdb.TB_FACTORY.RemoveRange(scpdb.TB_FACTORY.ToArray());
var _ls = new List<TB_FACTORY>();
foreach (var itm in db.TA_FACTORY.ToList())
{
var _tb = new TB_FACTORY();
_tb.GUID = itm.GUID;
_tb.IsDeleted = itm.IsDeleted;
_tb.State = itm.State;
_tb.UpdateInfo = itm.UpdateInfo;
_tb.Remark = itm.Remark;
_tb.FactoryId = itm.FactoryId;
_tb.FactoryName = itm.FactoryName;
_tb.ErpDomain = itm.ErpDomain;
_tb.CreateTime = itm.CreateTime;
_tb.CreateUser = itm.CreateUser;
_tb.ZipCode = itm.ZipCode;
scpdb.TB_FACTORY.Add(_tb);
}
scpdb.SaveChanges();
}
_ret.State = ReturnStatus.Succeed;
_ret.Result = true;
}
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_TA_FACTORY_CONTROLLER), "Del_TA_FACTORY", 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_TA_FACTORY_CONTROLLER), "Del_TA_FACTORY", 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_TA_FACTORY_CONTROLLER), "Del_TA_FACTORY", 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.Result = false;
_ret.ErrorList.Add(e);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TA_FACTORY_CONTROLLER), "Del_TA_FACTORY", e.Message); throw e;
}
return _ret;
}
}
}