192 lines
6.9 KiB
C#
192 lines
6.9 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Net;
|
|||
|
using System.Net.Http;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Web.Http;
|
|||
|
using CScommon;
|
|||
|
using Customize.Insynerger;
|
|||
|
using Customize.Insynerger.Interfaces;
|
|||
|
using Customize.ViewModels.Insynerger;
|
|||
|
using Microsoft.AspNet.Identity;
|
|||
|
using NLog;
|
|||
|
using PagedList;
|
|||
|
using Weee.DAL;
|
|||
|
using Weee.Models;
|
|||
|
using Weee.Service;
|
|||
|
using Weee.Supports;
|
|||
|
|
|||
|
namespace Weee.Controllers
|
|||
|
{
|
|||
|
public class FabController : ApiControllerBase
|
|||
|
{
|
|||
|
private Logger _log;
|
|||
|
private readonly WeeeFabDataService service;
|
|||
|
protected WeeeDataContext db;
|
|||
|
protected IinsynergerAuthenticate _insynergerAuthenticate;
|
|||
|
protected IinsynergerReportService _insynergerReportService;
|
|||
|
|
|||
|
public FabController(WeeeFabDataService s) : base(s)
|
|||
|
{
|
|||
|
_log = NLog.LogManager.GetCurrentClassLogger();
|
|||
|
service = s;
|
|||
|
db = new WeeeDataContext();
|
|||
|
_insynergerAuthenticate = new insynergerAuthenticate(db);
|
|||
|
_insynergerReportService = new insynergerReportService(db);
|
|||
|
}
|
|||
|
|
|||
|
[Route("api/Fab/GetByPage/{page?}/{searchString?}")]
|
|||
|
[HttpGet]
|
|||
|
public Object GetByPage(int page = 1, string searchString = "")
|
|||
|
{
|
|||
|
var pageSize = 10;
|
|||
|
var fabs = service.GetFabs();
|
|||
|
var dataFilter = new DataFilter();
|
|||
|
var filteredProducts = dataFilter.FilterFab(fabs, searchString);
|
|||
|
var pagedFabs = filteredProducts.ToPagedList(page, pageSize);
|
|||
|
return new
|
|||
|
{
|
|||
|
fabs = pagedFabs,
|
|||
|
totalPage = pagedFabs.PageCount
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
[Route("api/Fab/Get/{id}")]
|
|||
|
[HttpGet]
|
|||
|
public Fab Get(int id)
|
|||
|
{
|
|||
|
return service.GetFab(id);
|
|||
|
}
|
|||
|
|
|||
|
[Route("api/Fab/Get2/{id}")]
|
|||
|
[HttpGet]
|
|||
|
//public Fab Get(int id)
|
|||
|
public object Get2(int id)
|
|||
|
{
|
|||
|
object ret = new
|
|||
|
{
|
|||
|
fab = service.GetFab(id),
|
|||
|
groupId = _insynergerReportService.FabId2GroupId(id)//get groupId
|
|||
|
};
|
|||
|
return ret;
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 由groupId呼叫api取得groupId vs name pairs, 給下拉選單用
|
|||
|
/// </summary>
|
|||
|
/// <param name="groupId"></param>
|
|||
|
/// <returns></returns>
|
|||
|
[Route("api/Fab/GetGroupIdsNames/")]
|
|||
|
[HttpGet]
|
|||
|
public async Task<Dictionary<int, string>> GetGroupIdsNames()
|
|||
|
{
|
|||
|
Dictionary<int, string> ret = new Dictionary<int, string>();
|
|||
|
IinsynergerAuthenticate ia = new insynergerAuthenticate(db);
|
|||
|
var userid = User.Identity.GetUserId();
|
|||
|
var qry = (from a in db.SynergerUser
|
|||
|
where a.userId == userid
|
|||
|
select a).FirstOrDefault();
|
|||
|
if (qry == null)
|
|||
|
return ret;
|
|||
|
string access_token = qry.accessToken; // 思納捷要取得groupId的name
|
|||
|
HttpResponseRec res0 = await ia.myInfo2groupId(access_token);
|
|||
|
if (res0 == null || res0.statusCode != HttpStatusCode.OK)
|
|||
|
return ret;
|
|||
|
int groupId = 0;
|
|||
|
if (!int.TryParse(res0.result + "", out groupId))
|
|||
|
return ret;
|
|||
|
HttpResponseRec httpResRec = await ia.getListOfGroupNames(access_token, groupId);
|
|||
|
if (httpResRec == null || httpResRec.statusCode != HttpStatusCode.OK
|
|||
|
|| string.IsNullOrWhiteSpace(httpResRec.returnJson))
|
|||
|
return ret;
|
|||
|
ret = CScommon.JsonUtl.toT<Dictionary<int, string>>(httpResRec.returnJson);
|
|||
|
return ret;
|
|||
|
}
|
|||
|
[Route("api/Fab/GetAll")]
|
|||
|
[HttpGet]
|
|||
|
public IEnumerable<Fab> GetAll()
|
|||
|
{
|
|||
|
var ret= service.GetFabs().Where(x => x.enable);
|
|||
|
return ret;
|
|||
|
}
|
|||
|
[Route("api/Fab/GetAll2")]
|
|||
|
[HttpGet]
|
|||
|
//public IEnumerable<Fab> GetAll()
|
|||
|
public object GetAll2()
|
|||
|
{
|
|||
|
object ret = new
|
|||
|
{
|
|||
|
fabs = service.GetFabs(),
|
|||
|
insynerger = _insynergerAuthenticate.IsSynergerOn()
|
|||
|
};
|
|||
|
return ret;// service.GetFabs();
|
|||
|
}
|
|||
|
|
|||
|
[Route("api/Fab/Save/{id?}")]
|
|||
|
[HttpPost]
|
|||
|
public HttpResponseMessage Save(Fab toBeSave)
|
|||
|
{
|
|||
|
if (ModelState.IsValid)
|
|||
|
{
|
|||
|
var userid = User.Identity.GetUserId();
|
|||
|
if (CheckNameExist(toBeSave.Name, toBeSave.ID, userid))
|
|||
|
{
|
|||
|
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "廠區\"" + toBeSave.Name + "\"已存在");
|
|||
|
}
|
|||
|
toBeSave.UserId = userid;
|
|||
|
service.SaveFab(toBeSave);
|
|||
|
return Request.CreateResponse(HttpStatusCode.OK, toBeSave);
|
|||
|
}
|
|||
|
|
|||
|
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
|
|||
|
}
|
|||
|
[Route("api/Fab/Save2/{id?}")]
|
|||
|
[HttpPost]
|
|||
|
public HttpResponseMessage Save2(InsynergerFab toBeSave)
|
|||
|
{
|
|||
|
if (ModelState.IsValid)
|
|||
|
{
|
|||
|
var userid = User.Identity.GetUserId();
|
|||
|
if (CheckNameExist(toBeSave.Name, toBeSave.ID, userid))
|
|||
|
{
|
|||
|
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "廠區\"" + toBeSave.Name + "\"已存在");
|
|||
|
}
|
|||
|
toBeSave.UserId = userid;
|
|||
|
//var trans = service.BeginTransaction();
|
|||
|
try
|
|||
|
{
|
|||
|
string json = CScommon.JsonUtl.fromT(toBeSave);
|
|||
|
Fab toBeSave2 =CScommon.JsonUtl.toT<Fab>(json);
|
|||
|
service.SaveFab(toBeSave2);
|
|||
|
int fabId = toBeSave2.ID;
|
|||
|
// save groupId
|
|||
|
string err = _insynergerReportService.SaveFabGroupId(fabId
|
|||
|
, toBeSave.groupId);
|
|||
|
if (!string.IsNullOrWhiteSpace(err))
|
|||
|
throw new Exception(err);
|
|||
|
//trans.Commit();
|
|||
|
return Request.CreateResponse(HttpStatusCode.OK, toBeSave);
|
|||
|
}
|
|||
|
catch(Exception ex)
|
|||
|
{
|
|||
|
//trans.Rollback();
|
|||
|
log.Error(ex);
|
|||
|
log.Error(CScommon.Exceptions.inner(ex));
|
|||
|
log.Error(ex.StackTrace);
|
|||
|
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
|
|||
|
}
|
|||
|
|
|||
|
private bool CheckNameExist(string name, int id, string userid)
|
|||
|
{
|
|||
|
var exist = (from a in db.Fabs
|
|||
|
join b in db.Users on a.CompanyID equals b.CompanyID
|
|||
|
where a.Name == name && a.ID != id && b.Id==userid
|
|||
|
select a).Any();
|
|||
|
return exist;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|