demo20230512/Areas/admin/Controllers/LCAController.cs
2023-05-12 10:20:28 +08:00

122 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Weee.DAL;
using Weee.Models;
using Weee.Supports;
using PagedList;
namespace Weee.Areas.Admin.Controllers
{
public class LCAController : AdminControllerBase
{
public LCAController(WeeeDataContext db) : base(db)
{
}
public ActionResult Index(int page = 1, string companyName = "")
{
var lcas = new List<LCA>();
lcas.AddRange(db.ProductLCAs.Include(l => l.VerifyBy));
lcas.AddRange(db.OrganizationLCAs.Include(l => l.VerifyBy));
lcas.OrderByDescending(x => x.CreatedDate);
var dataFilter = new DataFilter();
var filteredLCAs = dataFilter.FilterLca(lcas, companyName);
var onePageOfLcas = filteredLCAs.ToPagedList(page, 10);
ViewBag.CompanyName = companyName;
return View(onePageOfLcas);
}
public ActionResult Details(int? id)
{
if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); }
LCA lca = db.LCAs.Include(x=>x.StatusHistory).Include(x=>x.Comments).Where(x=>x.ID==id).Single();
if (lca == null) { return HttpNotFound(); }
ViewBag.id = id;
return View(lca);
}
public ActionResult Create()
{
ViewBag.ReceiverCompanyID = new SelectList(db.Companies, "ID", "Name");
ViewBag.SenderCompanyID = new SelectList(db.Companies, "ID", "Name");
ViewBag.VerifierCompanyID = new SelectList(db.Companies, "ID", "Name");
return View();
}
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var lca = db.LCAs.Find(id);
if (lca == null)
{
return HttpNotFound();
}
ViewBag.id = id;
return View(lca);
}
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
LCA lca = db.LCAs.Find(id);
if (lca is OrganizationLCA)
{
OrganizationLCA olca = lca as OrganizationLCA;
var qry4 = (from a in db.OrganizationOtherCompound
where a.LCAID == olca.ID
select a);
db.OrganizationOtherCompound.RemoveRange(qry4);
olca = db.OrganizationLCAs.Find(id);
db.OrganizationLCAs.Remove(olca);
}
else if (lca is ProductLCA)
{
ProductLCA plca =lca as ProductLCA;
var qry1 = db.ProductLCAFabSurveyResults.Find(id);
if (qry1 != null)
db.ProductLCAFabSurveyResults.Remove(qry1);
var qry3 = (from a in db.ProductLCAReplyRequests
join b in db.ProductLCAProductSurveyForm_Material on a.ID equals b.ID
where b.LCAID == id
select a);
db.ProductLCAReplyRequests.RemoveRange(qry3);
var qry2 = db.ProductLCAProductSurveyForm_Material.Where(x=>x.LCAID==id);
db.ProductLCAProductSurveyForm_Material.RemoveRange(qry2);
plca = db.ProductLCAs.Find(id);
db.ProductLCAs.Remove(plca);
}
db.LCAs.Remove(lca);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}