demo20230512/Service/RiskAssmt/ItemDeliveryDataService.cs

76 lines
2.8 KiB
C#
Raw Normal View History

2023-05-12 10:20:28 +08:00
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using Weee.DAL;
using Weee.Models;
namespace Weee.Service
{
public class ItemDeliveryDataService : WeeeLCADataService
{
public ItemDeliveryDataService(WeeeDataContext db)
: base(db)
{
}
public IQueryable<LCARiskAssmtSurveyForm_ItemDelivery> GetList(int LCAID)
{
if (!AuthorizedLCAs.Contains(LCAID)) throw new Exception("not authorized");
return _db.Set<LCARiskAssmtSurveyForm_ItemDelivery>().Where(x => x.LCAID == LCAID)
.OrderByDescending(x => x.ID);
}
public LCARiskAssmtSurveyForm_ItemDelivery Save(LCARiskAssmtSurveyForm_ItemDelivery toBeSave)
{
CheckMaterialNo(toBeSave);
var user = GetUserContext();
if (!AuthorizedLCAs.Contains(toBeSave.LCAID)) throw new Exception("not authorized");
var entry = _db.Entry(toBeSave);
if (toBeSave.ID == 0)
{
toBeSave.CreatedBy = user.Id;
toBeSave.CreatedDate = DateTime.Now;
entry.State = EntityState.Added;
}
else
{
toBeSave.ModifiedBy = user.Id;
toBeSave.ModifiedDate = DateTime.Now;
entry.State = EntityState.Modified;
entry.Property(x => x.LCAID).IsModified = false;
}
_db.SaveChanges();
return toBeSave;
}
private void CheckMaterialNo(LCARiskAssmtSurveyForm_ItemDelivery toBeSave)
{
if (_db.Set<LCARiskAssmtSurveyForm_ItemDelivery>().Any(i => i.LCAID == toBeSave.LCAID && i.MaterialNo == toBeSave.MaterialNo && (toBeSave.ID == 0 || toBeSave.ID != i.ID)))
{
throw new Exception($"同盤查單號不得重複:{nameof(toBeSave.MaterialNo)}");
}
if (_db.Set<LCARiskAssmtSurveyForm_ItemDelivery>().Any(i => i.LCAID == toBeSave.LCAID && i.ModelName == toBeSave.ModelName && (toBeSave.ID == 0 || toBeSave.ID != i.ID)))
{
throw new Exception($"同盤查單號不得重複:{nameof(toBeSave.ModelName)}");
}
}
public void Delete(int ID)
{
var entry = _db.Set<LCARiskAssmtSurveyForm_ItemDelivery>().Where(x => x.ID == ID).Single();
var LCA = GetLCA(entry.LCAID);
if (LCA.Status != LCAStatus.New && LCA.Status != LCAStatus.Processing) throw new Exception("Business logic error , should not delete data in this status: " + LCA.Status.ToString());
var ToBeDelete = _db.Entry(entry);
ToBeDelete.State = EntityState.Deleted;
_db.SaveChanges();
}
}
}