demo20230512/Controllers/Api/ProductLCA/MaterialController.cs
2023-05-12 10:20:28 +08:00

274 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using Weee.Filter;
using Weee.Models;
using Weee.Service;
using PagedList;
using Weee.DAL;
using Weee.Models.Paramemter;
namespace Weee.Controllers
{
//[ApiMultilanguage]disable obsolete warning, not sure OK or not
public class MaterialController : ApiControllerBase
{
private readonly WeeeParameterDataService parameterservice;
private readonly WeeeSheetDataService service;
private readonly WeeeDataContext _db;
public MaterialController(WeeeSheetDataService s, WeeeParameterDataService n, WeeeDataContext db)
: base(s)
{
service = s;
parameterservice = n;
_db = db;
}
//this api is for high level Analyze
[Route("api/Material/GetAllMaterials/{LCAID}")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object GetAllMaterials(int LCAID)
{
var query = service.GetSheet<ProductLCAProductSurveyForm_Materials>(LCAID)
.Include(x => x.ChildMaterials)
.Include(x => x.RequestSent)
.Include(x => x.MaterialComposites)
.Where(x => x.ParentMaterialID == null)
.OrderBy(x => x.orderNo);
var result = query.ToList();
object ret= new
{
materials = result,
};
return ret;
}
[HttpPost]
[Route("api/Material/ReNumber/{LCAID}/{materialtype:int}")]
public string ReNumber(int LCAID, int materialtype)
{
string ret="";
try {
var qry = (from a in _db.ProductLCAProductSurveyForm_Material
where a.LCAID == LCAID && a.MaterialType == (MaterialType)materialtype
orderby a.orderNo
select a).AsQueryable();
if (qry != null && qry.Any()) {
int i = 0;
foreach (var rec in qry.ToList()) {
i++;
rec.orderNo = i ;
}
}
_db.SaveChanges();
}
catch(Exception ex) {
ret = CScommon.Exceptions.inner(ex).Message;
}
return ret;
}
//this api is for bom and 3 materials
[Route("api/Material/GetMaterials/{LCAID}/{page:int=1}/{materialtype:int=3}/{search?}/{pagesize:min(4):int=15}")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object GetMaterials(int LCAID, int page = 1, int materialtype = 3, string search = "", int pagesize = 15)
{
var query = service.GetSheet<ProductLCAProductSurveyForm_Materials>(LCAID)
.Include(x => x.ChildMaterials)
.Where(x => x.ParentMaterialID == null);
if (search != "")
{
query = query.Where(x => x.Name.Contains(search) ||
//x.LocalName.Contains(search) ||
x.Unit.Contains(search) ||
x.PartNumber.Contains(search) ||
x.Composite.Contains(search));
}
if (materialtype == 0 ||
materialtype == 1 ||
materialtype == 2)
{
query = query.Where(x => x.MaterialType == (MaterialType)materialtype);
}
var result = query.ToPagedList(page, pagesize);
object ret = new
{
materials = result,
optiontypes = parameterservice.GetSimaproCategories(),
optionunits = parameterservice.GetSimaproUnits(),
totalpages = result.PageCount,
SheetHeader = service.GetSheetHeader(LCAID, Models.Paramemter.Categories.DirectMaterial),
currentPage = page
};
return ret;
}
[Route("api/DirectMaterial/GetSheetHeader/{LCAID}")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object GetDirectMaterialHeader(int LCAID)
{
return new
{
SheetHeader = service.GetSheetHeader(LCAID, Models.Paramemter.Categories.DirectMaterial)
};
}
[Route("api/IndirectMaterial/GetSheetHeader/{LCAID}")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object GetIndirectMaterialHeader(int LCAID)
{
return new
{
SheetHeader = service.GetSheetHeader(LCAID, Models.Paramemter.Categories.IndirectMaterial)
};
}
[Route("api/WrapMaterial/GetSheetHeader/{LCAID}")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object GetWrapMaterialHeader(int LCAID)
{
return new
{
SheetHeader = service.GetSheetHeader(LCAID, Models.Paramemter.Categories.WrapMaterial)
};
}
[Route("api/BOM/GetSheetHeader/{LCAID}")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object GetBOMHeader(int LCAID)
{
return new
{
SheetHeader = service.GetSheetHeader(LCAID, Models.Paramemter.Categories.BOM)
};
}
//[Route("api/Material/SaveMaterial/{LCAID}/{id}")]
//[HttpPost]
//public object SaveMaterial(int LCAID, int id, Material material)
//{
// if (!ModelState.IsValid) { return BadRequest(); }
// if (id != material.ID || LCAID != material.LCAID) { BadRequest(); }
// var result = service.SaveSheetItem(material);
// return material;
//}
[Route("api/Material/SaveMaterials")]
[HttpPost]
[ApiEditableAttribute("materials", true)]
public object SaveMaterials(List<ProductLCAProductSurveyForm_Materials> materials)
{
if (materials.Any(x => x.VendorCode != null))//special case for high level analyze file import
{
var companyId = service.GetUserContext().CompanyID;
var joinLeft = from x in materials
where x.VendorCode != null
where x.MaterialType == MaterialType.DirectMaterial
select x;
var joinRight = from x in _db.Suppliers
where x.VendorCode != null
where x.CompanyID == companyId
select x;
var joinResults = (from x in joinLeft
join y in joinRight
on x.VendorCode.ToLower() equals y.VendorCode.ToLower()
select new { x, y }).ToList();
foreach (var joinResult in joinResults)
{
joinResult.x.SupplierCompanyName = joinResult.y.UserName;//.Name;
joinResult.x.SupplierCompanyEmail = joinResult.y.ContactEmail;
}
}
//var venderCodes = temp.Select(x => x.VendorCode.ToLower()).Distinct();
//var selectedSupplier = _db.Suppliers
// .Where(x => x.CompanyID == service.GetUserContext().CompanyID)
// .Where(x => venderCodes.Contains(x.VendorCode.ToLower()))
// .Select(x => new { email = x.ContactEmail, name = x.Name });
//foreach (var material in temp)
//{
//}
var materialComposites = materials.Select(x => x.MaterialComposites).ToList();
foreach(var material in materials)
material.MaterialComposites = new List<ProductLCASurveyForm_MaterialComposite>();
var result = service.SaveSheet(materials).ToList();
for(var i = 0; i < materials.Count(); i++)
{
if (materialComposites != null && materialComposites.Count > i
&& materialComposites[i]!=null)
{
foreach (var composite in materialComposites[i])
composite.materialID = result[i].ID;
}
materials[i].MaterialComposites = materialComposites[i];
service.UpdateMaterialComposites(result[i].ID, materials[i].MaterialComposites);
}
return new { result = result };
}
[Route("api/DirectMaterial/SaveSheetHeader")]
[HttpPost]
public HttpResponseMessage SaveDirectMaterialHeader(SheetHeader header)
{
return this.SaveSheetHeader(service, Categories.DirectMaterial, header);
}
[Route("api/IndirectMaterial/SaveSheetHeader")]
[HttpPost]
public HttpResponseMessage SaveIndirectMaterialHeader(SheetHeader header)
{
return this.SaveSheetHeader(service, Categories.IndirectMaterial, header);
}
[Route("api/WrapMaterial/SaveSheetHeader")]
[HttpPost]
public HttpResponseMessage SaveWrapMaterialHeader(SheetHeader header)
{
return this.SaveSheetHeader(service, Categories.WrapMaterial, header);
}
[Route("api/BOM/SaveSheetHeader")]
[HttpPost]
public HttpResponseMessage SaveBOMHeader(SheetHeader header)
{
return this.SaveSheetHeader(service, Categories.BOM, header);
}
[Route("api/Material/DeleteMaterial/{LCAID}/{id}")]
[HttpDelete]
public IHttpActionResult DeleteMaterial(int LCAID, int id)
{
service.DeleteMaterials(null, id, null);
return Ok();
}
[Route("api/Material/DeleteMaterials/{LCAID}")]
[HttpDelete]
public IHttpActionResult DeleteMaterials(int LCAID)
{
service.DeleteMaterials(LCAID, null, null);
return Ok();
}
[Route("api/Material/DeleteMaterials/{LCAID}/{MType}")]
[HttpDelete]
public IHttpActionResult DeleteDirectMaterials(int LCAID, MaterialType MType)
{
service.DeleteMaterials(LCAID,null, MType);
return Ok();
}
}
}