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

137 lines
4.2 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 EmployeeCommutingController : ApiControllerBase
{
private readonly string userNm;
private readonly WeeeEmployeeCommutingService empCommutService;
private readonly WeeeSheetDataService service;
public EmployeeCommutingController(WeeeSheetDataService d, WeeeEmployeeCommutingService employeeCommutingService)
: base(d)
{
userNm = User.Identity.GetUserName();
service = d;
empCommutService = employeeCommutingService;
}
[Route("api/EmployeeCommuting/GetByLcaId/{LCAID}")]
public object GetByLcaId(int LCAID, int page = 1, string search = "", int pagesize = 15)
{
var its = empCommutService.GetByLCAID(LCAID);
var _exlNms = empCommutService.GetExcelColumnsNms();
var _commOptions = empCommutService.GetCommutingKeyValue();
object ret = new
{
tblist = its,
exlNms = _exlNms,
commOptions = _commOptions
};
return ret;
}
[Route("api/EmployeeCommuting/Save/{LCAID}/{Id}")]
[HttpPost]
public object Save(int LCAID, int Id, EmployeeCommutingViewModel toBeSave)
{
int dbId = 0;
if (!ModelState.IsValid)
return BadRequest();
if (Id != toBeSave.Id || LCAID != toBeSave.LCAID)
return BadRequest();
try
{
toBeSave.LCAID = LCAID;
toBeSave.ModifiedBy = userNm;
dbId = empCommutService.SaveEmployeeCommuting(toBeSave);
}
catch (Exception ex)
{
return Json(new { Success = false, Msg = ex.Message.ToString() });
}
if (dbId == 0)
throw new Exception("儲存失敗");
else
{
var query = empCommutService.GetByLCAID(LCAID).Where(x => x.Id == dbId);
return query.ToList().First();
}
}
[Route("api/EmployeeCommuting/SaveAll/{LCAID}")]
[HttpPost]
public object SaveAll(int LCAID, List<EmployeeCommExlImpotViewModel> toBeSave)
{
string errMsg = "";
try
{
if (!ModelState.IsValid)
throw new Exception("Model Invalid");
string exlErr = empCommutService.ExlDataCk(toBeSave);
if (!string.IsNullOrWhiteSpace(exlErr))
throw new Exception(exlErr);
var exlDatas = empCommutService.GetExlData(toBeSave, LCAID, userNm);
if (exlDatas != null && exlDatas.Count() > 0)
empCommutService.SaveToDb(exlDatas);
}
catch (Exception ex)
{
errMsg = ex.Message;
}
return new { ErrMsg = errMsg };
}
[Route("api/EmployeeCommuting/QuoteSaveAll/{LCAID}")]
[HttpPost]
public object QuoteSaveAll(int LCAID, List<EmployeeCommutingViewModel> toBeSave)
{
string errMsg = "";
try
{
if (!ModelState.IsValid)
throw new Exception("Model Invalid");
if (toBeSave != null && toBeSave.Count() > 0)
empCommutService.SaveToDb(toBeSave);
}
catch (Exception ex)
{
errMsg = ex.Message;
}
return new { ErrMsg = errMsg };
}
[Route("api/EmployeeCommuting/Delete/{ID}")]
[HttpDelete]
public IHttpActionResult Delete(int ID)
{
try
{
empCommutService.DelEmployeeCommuting(ID);
return Ok();
}
catch (Exception ex)
{
return BadRequest();
}
}
}
}