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

221 lines
7.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Weee.Models;
using Weee.Models.Paramemter;
using Weee.Service;
using PagedList;
using Weee.Filter;
using System.Web;
using System.IO;
using Qcarbon.ViewModels.DTO;
using Microsoft.AspNet.Identity;
namespace Weee.Controllers
{
public class MaterialTransportController : ApiControllerBase
{
private readonly string userNm;
private readonly WeeeMaterialTransportService _materialTransportService;
private readonly WeeeSheetDataService service;
public MaterialTransportController(WeeeSheetDataService d, WeeeMaterialTransportService materialTransportService)
: base(d)
{
userNm = User.Identity.GetUserName();
service = d;
_materialTransportService = materialTransportService;
}
[Route("api/MaterialTransport/GetByLcaId/{LCAID}")]
public object GetByLcaId(int LCAID, int page = 1, string search = "", int pagesize = 15)
{
var itst = _materialTransportService.GetByLCAID(LCAID);
//var _exlNms = _materialTransportService.GetExcelColumnsNms();
//var _commOptions = _materialTransportService.GetCommutingKeyValue();
object ret = new
{
tblist = itst,
//exlNms = _exlNms,
//commOptions = _commOptions
};
return ret;
}
[Route("api/MaterialTransport/GetByC3EmissionId/{C3EmissionID}")]
public object GetByC3EmissionId(int C3EmissionID, int page = 1, string search = "", int pagesize = 15)
{
var itst = _materialTransportService.GetByC3EmissionId(C3EmissionID);
//var _exlNms = _materialTransportService.GetExcelColumnsNms();
//var _commOptions = _materialTransportService.GetCommutingKeyValue();
object ret = new
{
tblist = itst,
//exlNms = _exlNms,
//commOptions = _commOptions
};
return ret;
}
[Route("api/MaterialTransport/Save/{LCAID}/{Id}")]
[HttpPost]
public object Save(int LCAID, int Id, RiskMaterialC3EmissionViewModel toBeSave)
{
log.Info("save 1");
int dbId = 0;
if (!ModelState.IsValid)
return BadRequest();
var trans = service.GetDbContext().Database.BeginTransaction();
try
{
log.Info("save 2");
if (Id != toBeSave.ID || LCAID != toBeSave.LCAID)
{
trans.Rollback();
return BadRequest();
}
//try
{
toBeSave.LCAID = LCAID;
toBeSave.ModifiedBy = userNm;
dbId = _materialTransportService.SaveMaterialC3Emissions(toBeSave);
}
//catch (Exception ex)
//{
// trans.Rollback();
// return Json(new { Success = false, Msg = ex.Message.ToString() });
//}
if (dbId == 0)
throw new Exception("儲存失敗");
else
{
var query = _materialTransportService.GetByLCAID(LCAID).Where(x => x.ID == dbId);
trans.Commit();
return query.ToList().First();
}
}
catch(Exception ex)
{
log.Error(ex);
log.Error(CScommon.Exceptions.inner(ex));
log.Error(ex.StackTrace);
trans.Rollback();
return Json(new { Success = false, Msg = ex.Message.ToString() });
}
finally
{
trans = null;
}
}
[Route("api/MaterialTransport/SaveTransport/{C3EmissionID}/{Id}")]
[HttpPost]
public object SaveTransport(int C3EmissionID, int Id, RiskMaterialTransportViewModel toBeSave)
{
int dbId = 0;
if (!ModelState.IsValid)
return BadRequest();
var trans=service.GetDbContext().Database.BeginTransaction();
if (Id != toBeSave.ID || C3EmissionID != toBeSave.RiskAssmtMaterialID)
return BadRequest();
try
{
toBeSave.RiskAssmtMaterialID = C3EmissionID;
toBeSave.ModifiedBy = userNm;
dbId = _materialTransportService.SaveMaterialTransport(toBeSave);
}
catch (Exception ex)
{
trans.Rollback();
return Json(new { Success = false, Msg = ex.Message.ToString() });
}
if (dbId == 0)
{
trans.Rollback();
throw new Exception("儲存失敗");
}
else
{
var query = _materialTransportService.GetByC3EmissionId(C3EmissionID).Where(x => x.ID == dbId);
trans.Commit();
return query.ToList().First();
}
}
[Route("api/MaterialTransport/SaveAll/{LCAID}")]
[HttpPost]
public object SaveAll(int LCAID, List<RiskMaterialTransportExlImportViewModel> toBeSave)
{
string errMsg = "";
try
{
if (!ModelState.IsValid)
throw new Exception("Model Invalid");
string exlErr = _materialTransportService.ExlDataCk(toBeSave);
if (!string.IsNullOrWhiteSpace(exlErr))
throw new Exception(exlErr);
var exlDatas = _materialTransportService.GetExlData(toBeSave, LCAID, userNm);
if (exlDatas != null && exlDatas.Count() > 0)
_materialTransportService.SaveToDb(exlDatas);
}
catch (Exception ex)
{
errMsg = ex.Message;
}
return new { ErrMsg = errMsg };
}
[Route("api/MaterialTransport/QuoteSaveAll/{LCAID}/{quoteLCAID}")]
[HttpPost]
public object QuoteSaveAll(int LCAID, int quoteLCAID)
{
string errMsg = "";
try
{
_materialTransportService.QuoteAllData(LCAID, quoteLCAID, userNm);
}
catch (Exception ex)
{
errMsg = ex.Message;
}
return new { ErrMsg = errMsg };
}
[Route("api/MaterialTransport/Delete/{ID}")]
[HttpDelete]
public IHttpActionResult Delete(int ID)
{
try
{
_materialTransportService.DelMaterialC3Emissions(ID);
return Ok();
}
catch (Exception ex)
{
return BadRequest();
}
}
[Route("api/MaterialTransport/DeleteTransport/{ID}")]
[HttpDelete]
public IHttpActionResult DeleteTransport(int ID)
{
try
{
_materialTransportService.DelMaterialTransport(ID);
return Ok();
}
catch (Exception ex)
{
return BadRequest();
}
}
}
}