233 lines
7.1 KiB
C#
233 lines
7.1 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Net.Http;
|
||
using System.Web.Http;
|
||
using PagedList;
|
||
using Newtonsoft.Json;
|
||
using System.Collections;
|
||
using static TreeView;
|
||
using System.Web;
|
||
using iTextSharp.text.pdf.qrcode;
|
||
|
||
[ezAuthorize]
|
||
public class supplierController : BaseApiController
|
||
{
|
||
// GET api/<controller>
|
||
// TODO: REVIEW - 無參數 Get() 會載入所有供應商到內存
|
||
// 若供應商數量少(<100),可接受;若會成長,建議停用此方法
|
||
public IEnumerable<Model.supplier> Get()
|
||
{
|
||
var list = _db.suppliers.ToList();
|
||
if (list == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
||
return list;
|
||
}
|
||
|
||
// GET api/<controller>/5
|
||
public Model.supplier Get(int id)
|
||
{
|
||
var item = _db.suppliers.Where(q => q.num == id).FirstOrDefault();
|
||
if (item == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
||
return item;
|
||
}
|
||
|
||
// GET api/<controller>/5
|
||
public IEnumerable<Model.supplier> GetPage(int page)
|
||
{
|
||
var supplier = _db.suppliers.Where(q => q.num < 10).ToList();
|
||
return supplier;
|
||
}
|
||
|
||
// POST api/<controller>
|
||
public void Post([FromBody] string value)
|
||
{
|
||
}
|
||
|
||
// PUT api/<controller>/5
|
||
public void Put(int id, [FromBody] string value)
|
||
{
|
||
}
|
||
|
||
// DELETE api/<controller>/5
|
||
public void Delete(int id)
|
||
{
|
||
var prod = _db.suppliers.Where(q => q.num == id).FirstOrDefault(); //刪除該筆資料
|
||
if (prod != null)
|
||
{
|
||
if (!string.IsNullOrEmpty(prod.pic1))
|
||
{
|
||
publicFun publicFun = new publicFun();
|
||
publicFun.DeleteFile(Model.supplier.Dir + "/" + prod.pic1);
|
||
}
|
||
|
||
_db.suppliers.Remove(prod);
|
||
_db.SaveChanges(); //執行
|
||
|
||
|
||
Model.admin_log admin_log = new Model.admin_log();
|
||
MyWeb.admin admin = new MyWeb.admin();//api裡不可以用MyWeb
|
||
if (admin.isLoign())
|
||
{
|
||
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Supplier, (int)Model.admin_log.Status.Delete, prod.s_number + prod.u_name);
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
|
||
[HttpDelete]
|
||
[Route("api/supplier/Delete/{nums}")]
|
||
public void Delete(string nums)
|
||
{
|
||
if (!string.IsNullOrEmpty(nums))
|
||
{
|
||
var ids = nums.TrimEnd(',').Split(',').Select(s => int.Parse(s)).ToList();
|
||
var prod = _db.suppliers.Where(q => ids.Contains(q.num)).ToList();
|
||
if (prod.Count() > 0)
|
||
{
|
||
publicFun publicFun = new publicFun();
|
||
foreach (var row in prod)
|
||
{
|
||
if (!string.IsNullOrEmpty(row.pic1))
|
||
{
|
||
publicFun.DeleteFile(Model.supplier.Dir + "/" + row.pic1);
|
||
}
|
||
}
|
||
|
||
_db.suppliers.RemoveRange(prod);
|
||
_db.SaveChanges();
|
||
|
||
|
||
Model.admin_log admin_log = new Model.admin_log();
|
||
MyWeb.admin admin = new MyWeb.admin();//api裡不可以用MyWeb
|
||
if (admin.isLoign())
|
||
{
|
||
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Supplier, (int)Model.admin_log.Status.Delete, admin_log.LogViewBtn(prod.Select(x => x.s_number + x.u_name).ToList()));
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
}
|
||
[HttpPost]
|
||
[Route("api/supplier/GetList")]
|
||
public IHttpActionResult GetList([FromBody] Model.ViewModel.supplier q, int page, int pageSize = 10,
|
||
string sortBy = "", bool sortDesc = false)
|
||
{
|
||
var qry = _db.suppliers.AsQueryable();
|
||
|
||
if (!string.IsNullOrEmpty(q.u_name))
|
||
qry = qry.Where(o => o.u_name.Contains(q.u_name.Trim()));
|
||
if (!string.IsNullOrEmpty(q.s_number))
|
||
qry = qry.Where(o => o.s_number.Contains(q.s_number.Trim()));
|
||
|
||
if (q.kind.HasValue)//搜尋要包含其分類底下所有分類
|
||
{
|
||
var _subKinds = new TreeView().subKinds(_db.supplier_kind.Select(o => new TreeItem()
|
||
{
|
||
num = o.num,
|
||
root = o.root,
|
||
}).ToList(), q.kind.Value);
|
||
|
||
qry = qry.Where(o => o.kind == q.kind.Value || _subKinds.Any(s => s == o.kind));
|
||
}
|
||
if (!string.IsNullOrEmpty(q.kindTxt))
|
||
qry = qry.Where(o => o.kind != null && o.supplier_kind.kind.Contains(q.kindTxt));
|
||
|
||
|
||
if (sortBy.Equals("u_name"))
|
||
{
|
||
if (sortDesc)
|
||
qry = qry.OrderByDescending(o => o.u_name);
|
||
else
|
||
qry = qry.OrderBy(o => o.u_name);
|
||
}
|
||
else if(sortBy.Equals("s_number"))
|
||
{
|
||
if (sortDesc)
|
||
qry = qry.OrderByDescending(o => o.s_number);
|
||
else
|
||
qry = qry.OrderBy(o => o.s_number);
|
||
}
|
||
else if (sortBy.Equals("kind"))
|
||
{
|
||
if (sortDesc)
|
||
qry = qry.OrderByDescending(o => o.kind);
|
||
else
|
||
qry = qry.OrderBy(o => o.kind);
|
||
}
|
||
|
||
else
|
||
qry = qry.OrderByDescending(o => o.num);
|
||
|
||
var count = qry.Count();
|
||
var qryList = (pageSize > 0) ? qry.ToPagedList(page, pageSize).ToList() : qry.ToList();
|
||
|
||
var ret = new
|
||
{
|
||
list = qryList.Select(x => new
|
||
{
|
||
num = x.num,
|
||
u_name = x.u_name,
|
||
s_number = x.s_number,
|
||
kind = x.kind,
|
||
kindTxt = x.kind.HasValue ? x.supplier_kind.kind : "",
|
||
kindsTxt = x.kind.HasValue ? new TreeView().kindText(_db.supplier_kind.Select(o => new TreeItem()
|
||
{
|
||
kind = o.kind,
|
||
num = o.num,
|
||
root = o.root,
|
||
}).ToList(), x.kind) : "",
|
||
|
||
}),
|
||
count = count
|
||
|
||
};
|
||
|
||
|
||
if (ret.list == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
||
return Ok(ret);
|
||
}
|
||
|
||
[HttpPost]
|
||
[Route("api/supplier/GetKindList")]
|
||
public IHttpActionResult GetKindList([FromBody] Model.ViewModel.supplier_kind q, int page, int pageSize = 10,
|
||
string sortBy = "", bool sortDesc = false)
|
||
{
|
||
|
||
|
||
var qry = _db.supplier_kind.AsQueryable();
|
||
|
||
if (!string.IsNullOrEmpty(q.kind))
|
||
qry = qry.Where(o => o.kind.Contains(q.kind));
|
||
|
||
var qry2 = new TreeView().get_data2(qry.Select(o => new TreeItem()
|
||
{
|
||
kind = o.kind,
|
||
num = o.num,
|
||
root = o.root,
|
||
range = o.range,
|
||
}).OrderBy(x => x.root).ThenBy(x => x.kind).ToList(), 0, 0);
|
||
|
||
var count = qry.Count();
|
||
var qry2List = (pageSize > 0) ? qry2.ToPagedList(page, pageSize).ToList() : qry2.ToList();
|
||
|
||
var ret = new
|
||
{
|
||
list = qry2List.Select(x => new
|
||
{
|
||
num = x.num,
|
||
kind = new TreeView().RptDash(x.Level) + x.kind,
|
||
|
||
}),
|
||
count = count
|
||
};
|
||
|
||
|
||
if (ret.list == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
||
return Ok(ret);
|
||
}
|
||
|
||
}
|