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

193 lines
7.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using System.Threading;
using Microsoft.AspNet.Identity;
using Weee.DAL;
using Weee.ViewModels;
using Weee.Models.ExtensionMethods;
using Weee.Models;
using Microsoft.Owin.Security;
using CScommon;
using Weee.Service;
namespace Weee.Controllers
{
/// <summary>
/// this controller is for accept Requst , and Anonymous reply ,
/// 其他的request action 在 LCA api controller 中的 'region For reply request'
/// </summary>
//[Filter.MvcMultilanguage]disable obsolete warning, not sure OK or not
public class LCARequestController : QcarbonControllerBase
{
//private WeeeDataContext _db;
private readonly UserManager<User> usermanager;
protected WeeeSiteInfoService _siteInfoService;
public LCARequestController(WeeeDataContext db, UserManager<User> u, WeeeSiteInfoService siteInfoService)
:base(db)
{
//_db = db;
usermanager = u;
_siteInfoService = siteInfoService;
}
[Route("LCARequest/AnonymousReply/{Uid}/{Type}")]
[AllowAnonymous]
public ActionResult AnonymousReply(Guid uid, int type)
{
var vm = db.ProductLCAReplyRequests
.Where(x => x.Uid == uid)
.Where(x=>x.AcceptReplyDate==null)
.Select(x => new LCARequestAnonymousReplyViewModel()
{
SenderCompanyName = x.SenderCompanyName,
SenderName = x.SentByWhichMaterial.OwnerLCA.Owner.CEOName,
MaterialName = x.SentByWhichMaterial.Name,
Distribute = x.Distribute,
SendTime = x.CreatedDate,
})
.SingleOrDefault();
if (vm == null)
vm = new LCARequestAnonymousReplyViewModel();
var siteInfo = _siteInfoService.GetWebSiteInfo();
if (siteInfo != null)
{
vm.SiteInfo = siteInfo;
HttpContext.Application["customLoginImg"] = siteInfo.loginImagePath;
}
else
HttpContext.Application["customLoginImg"] = null;
vm.replyMode = type;
return View(vm);
}
[Route("LCARequest/AnonymousReply/{Uid}")]
[HttpPost]
[AllowAnonymous]
public ActionResult AnonymousReply(Guid uid, LCARequestAnonymousReplyViewModel vm)
{
int keepReplyMode = vm.replyMode;
if (!ModelState.IsValid)
{
vm = db.ProductLCAReplyRequests
.Where(x => x.Uid == uid)
.Select(x => new LCARequestAnonymousReplyViewModel()
{
SenderCompanyName = x.SenderCompanyName,
SenderName = x.SentByWhichMaterial.OwnerLCA.Owner.CEOName,
MaterialName = x.SentByWhichMaterial.Name,
Distribute = x.Distribute,
SendTime = x.CreatedDate,
replyMode= keepReplyMode
})
.SingleOrDefault();
return View(vm);
}
var request = db.ProductLCAReplyRequests.Include(x => x.SentByWhichMaterial).Where(x => x.Uid == uid).SingleOrDefault();
if (request == null)
{
ViewBag.ErrorMessage = "This Request is no longer avaliable";
return View("Fail");
}
else
{
try
{
request.AcceptDate = DateTime.Now;
request.ReplyDate = DateTime.Now;
request.replyMode = vm.replyMode;
request.KgCO2e = vm.KgCO2e;
request.RepliedValue = request.KgCO2e;
request.KgCO2eUnit = vm.KgCO2eUnit;
request.FunctionUnit = vm.FunctionUnit;
request.WeightPerFunctionUnit = vm.WeightPerFunctionUnit;
var storage = DependencyResolver.Current.GetService<Storage.AzureStorage>();
if (vm.uploadReportFile != null)
{
vm.ReportFile = vm.uploadReportFile.FileName;
request.ReportFile = storage.SaveToAzure(
vm.uploadReportFile.InputStream, vm.ReportFile, baseUrl
, uid.ToString()).ToString();
}
if (vm.uploadReferenceFile != null)
{
vm.ReferenceFile=vm.uploadReferenceFile.FileName;
request.ReferenceFile = storage.SaveToAzure(
vm.uploadReferenceFile.InputStream, vm.ReferenceFile, baseUrl
, uid.ToString()).ToString();
}
request.tCO2e = vm.tCO2e;
request.YearlyRevenue = vm.YearlyRevenue;
request.TotalAmount = vm.TotalAmount;
request.TotalQuantity = vm.TotalQuantity;
db.SaveChanges();
return View("SuccessReply");
}
catch
{
return View("Fail");
}
}
}
[Route("LCARequest/Accept/{Uid}")]
[HttpGet]
[AllowAnonymous]
public ActionResult Accept(Guid uid)
{
var ViewModel = new LoginViewModel();
return View("../account/Login", ViewModel);
}
[Route("LCARequest/Accept/{Uid}")]
[HttpPost]
public ActionResult Accept(Guid uid, LoginViewModel ViewModel)
{
if (ModelState.IsValid)
{
var user = usermanager.Find(ViewModel.UserName, ViewModel.Password);
if (user != null &&
user.Company != null &&
user.Company.CompanyType == typeof(NormalCompany) &&
usermanager.IsInRole(user.Id, ProgramConstants.normalcompany))
{
HttpContext.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = usermanager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties() { IsPersistent = ViewModel.RememberMe }, identity);
var request = db.ProductLCAReplyRequests.Where(x => x.Uid == uid).SingleOrDefault();
if (request != null)
{
request.ReceiverCompanyID = user.CompanyID;
request.AcceptDate = DateTime.Now;
db.SaveChanges();
}
return RedirectToAction("WeeeCarbonFootprint", "Home", new { area = "" });
}
if (user == null)
ModelState.AddModelError("", "The user name or password provided is incorrect.");
else if (user.Company == null || user.Company.CompanyType != typeof(NormalCompany))
ModelState.AddModelError("", "只有供應商可以接受盤查指令");
else if (usermanager.IsInRole(user.Id, ProgramConstants.normalcompany))
ModelState.AddModelError("", Resources.Resource.StaticLabelaccountpending);
return View("../account/Login", ViewModel);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The account / password is required");
return View("../account/Login",ViewModel);
}
}
}