103 lines
4.1 KiB
C#
103 lines
4.1 KiB
C#
|
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 MaterialC3EmissionDataService : WeeeLCADataService
|
|||
|
{
|
|||
|
public MaterialC3EmissionDataService(WeeeDataContext db)
|
|||
|
: base(db)
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
public IQueryable<LCARiskAssmtSurveyForm_MaterialC3Emission> GetList(int LCAID)
|
|||
|
{
|
|||
|
if (!AuthorizedLCAs.Contains(LCAID)) throw new Exception("not authorized");
|
|||
|
return _db.Set<LCARiskAssmtSurveyForm_MaterialC3Emission>().Where(x => x.LCAID == LCAID)
|
|||
|
.OrderByDescending(x => x.ID);
|
|||
|
}
|
|||
|
|
|||
|
public LCARiskAssmtSurveyForm_MaterialC3Emission GetByLcaIdMaterialNo(int LCAID, string MaterialNo)
|
|||
|
{
|
|||
|
if (!AuthorizedLCAs.Contains(LCAID)) throw new Exception("not authorized");
|
|||
|
var ret = _db.Set<LCARiskAssmtSurveyForm_MaterialC3Emission>()
|
|||
|
.Where(x => x.LCAID == LCAID && x.MaterialNo == MaterialNo)
|
|||
|
.FirstOrDefault();
|
|||
|
return ret;
|
|||
|
}
|
|||
|
|
|||
|
public Dictionary<string, string> MaterialNoLst(int LCAID)
|
|||
|
{
|
|||
|
//if (!AuthorizedLCAs.Contains(LCAID)) throw new Exception("not authorized");
|
|||
|
var qry = (from a in _db.LCARiskAssmtSurveyForm_MaterialC3Emissions
|
|||
|
where a.LCAID==LCAID && a.MaterialNo != null
|
|||
|
orderby a.MaterialNo
|
|||
|
select a).ToList()
|
|||
|
.Select(x=>new { ID=x.ID, MaterialNo=x.MaterialNo })
|
|||
|
.ToDictionary(x => x.ID.ToString(), x => x.MaterialNo);
|
|||
|
//select new
|
|||
|
//{
|
|||
|
// Key = a.MaterialNo,
|
|||
|
// Value = a.MaterialNo
|
|||
|
//}).ToDictionary(x=>x.Key, x=>x.Value);
|
|||
|
|
|||
|
//a.MaterialNo).AsQueryable();
|
|||
|
//Dictionary<string, string> ret = new Dictionary<string, string>();
|
|||
|
//foreach (var item in qry)
|
|||
|
// ret.Add(item, item);
|
|||
|
return qry;// ret;
|
|||
|
//_db.Set<LCARiskAssmtSurveyForm_MaterialC3Emission>().Where(x => x.LCAID == LCAID)
|
|||
|
// .OrderByDescending(x => x.ID);
|
|||
|
}
|
|||
|
|
|||
|
public LCARiskAssmtSurveyForm_MaterialC3Emission Save(LCARiskAssmtSurveyForm_MaterialC3Emission toBeSave)
|
|||
|
{
|
|||
|
var user = GetUserContext();
|
|||
|
|
|||
|
//if (!AuthorizedLCAs.Contains(toBeSave.LCAID))
|
|||
|
// throw new Exception("not authorized");
|
|||
|
if (string.IsNullOrWhiteSpace(toBeSave.MaterialNo))
|
|||
|
throw new Exception("料號不得為空");
|
|||
|
var entry = _db.Entry(toBeSave);
|
|||
|
if (toBeSave.ID == 0)
|
|||
|
{
|
|||
|
var qry = (from a in _db.LCARiskAssmtSurveyForm_MaterialC3Emissions
|
|||
|
where a.LCAID == toBeSave.LCAID && a.ID > 0 && a.MaterialNo == toBeSave.MaterialNo
|
|||
|
select a);
|
|||
|
if (qry.Any())
|
|||
|
{
|
|||
|
throw new Exception($"料號 {toBeSave.MaterialNo} 已存在!");
|
|||
|
}
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
public void Delete(int ID)
|
|||
|
{
|
|||
|
var entry = _db.Set<LCARiskAssmtSurveyForm_MaterialC3Emission>().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();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|