migrate to new git
This commit is contained in:
484
web/App_Code/api/stockController.cs
Normal file
484
web/App_Code/api/stockController.cs
Normal file
@@ -0,0 +1,484 @@
|
||||
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.IO;
|
||||
using System.Web;
|
||||
using System.Threading.Tasks;
|
||||
using System.Diagnostics;
|
||||
using DocumentFormat.OpenXml.Wordprocessing;
|
||||
using DocumentFormat.OpenXml.Office2010.Excel;
|
||||
|
||||
[ezAuthorize]
|
||||
public class stockController : BaseApiController
|
||||
{
|
||||
// GET api/<controller>
|
||||
public IEnumerable<Model.stock> Get()
|
||||
{
|
||||
var list = _db.stocks.ToList();
|
||||
if (list == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
||||
return list;
|
||||
}
|
||||
|
||||
// GET api/<controller>/5
|
||||
public Model.stock Get(int id)
|
||||
{
|
||||
var item = _db.stocks.Where(q => q.num == id).FirstOrDefault();
|
||||
if (item == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
||||
return item;
|
||||
}
|
||||
|
||||
// GET api/<controller>/5
|
||||
public IEnumerable<Model.stock> GetPage(int page)
|
||||
{
|
||||
var stock = _db.stocks.Where(q => q.num < 10).ToList();
|
||||
return stock;
|
||||
}
|
||||
|
||||
// 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.stocks.AsEnumerable().Where(q => q.num == id).FirstOrDefault(); //刪除該筆資料
|
||||
if (prod != null)
|
||||
{
|
||||
var prod2 = prod.stock_files;
|
||||
if (prod2.Count > 0)
|
||||
{
|
||||
publicFun publicFun = new publicFun();
|
||||
foreach (var item in prod2)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(item.pic1))
|
||||
{
|
||||
publicFun.DeleteFile(Model.stock.Dir + "/" + item.pic1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
_db.stocks.Remove(prod);
|
||||
_db.SaveChanges(); //執行
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
[Route("api/stock/Delete/{nums}")]
|
||||
public void Delete(string nums)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(nums))
|
||||
{
|
||||
var getDelItem = nums.TrimEnd(',').Split(',').Select(s => int.Parse(s));
|
||||
var prod = _db.stocks.AsEnumerable().Where(q => getDelItem.Contains(q.num)).ToList();
|
||||
if (prod.Count() > 0)
|
||||
{
|
||||
var prod2 = _db.stock_files.AsEnumerable().Where(q => q.stock_num.HasValue && getDelItem.Contains(q.stock_num.Value)).ToList();
|
||||
if (prod2.Count() > 0)
|
||||
{
|
||||
publicFun publicFun = new publicFun();
|
||||
foreach (var item in prod2)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(item.pic1))
|
||||
{
|
||||
publicFun.DeleteFile(Model.stock.Dir + "/" + item.pic1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_db.stocks.RemoveRange(prod);
|
||||
_db.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
[HttpPost]
|
||||
[Route("api/stock/GetList")]
|
||||
public IHttpActionResult GetList([FromBody] Model.ViewModel.stock q, int page, int pageSize = 10,
|
||||
string sortBy = "", bool sortDesc = false)
|
||||
{
|
||||
var qry = _db.stocks.AsEnumerable();
|
||||
|
||||
if (q.category.HasValue)
|
||||
qry = qry.Where(o => o.category == q.category.Value);
|
||||
if (q.kind.HasValue)
|
||||
{
|
||||
var _subKinds = new TreeView().subKinds(_db.stock_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 (q.reason.HasValue)
|
||||
{
|
||||
var _subKinds = new TreeView().subKinds(_db.stock_reason.Select(o => new TreeItem()
|
||||
{
|
||||
num = o.num,
|
||||
root = o.root,
|
||||
}).ToList(), q.reason.Value);
|
||||
qry = qry.Where(o => o.reason == q.reason.Value || _subKinds.Any(s => s == o.reason));
|
||||
}
|
||||
if (q.activity_num.HasValue)
|
||||
qry = qry.Where(o => o.activity_num == q.activity_num.Value);
|
||||
if (q.actItem_num.HasValue)
|
||||
qry = qry.Where(o => o.actItem_num == q.actItem_num.Value);
|
||||
if (q.uptime1.HasValue)
|
||||
qry = qry.Where(o => o.uptime >= q.uptime1.Value);
|
||||
if (q.uptime2.HasValue)
|
||||
qry = qry.Where(o => o.uptime < Convert.ToDateTime(q.uptime2.Value).AddDays(1));
|
||||
if (!string.IsNullOrEmpty(q.activity_num_txt))
|
||||
qry = qry.Where(o => o.activity_num.HasValue && o.activity.subject.Contains(q.activity_num_txt.Trim()));
|
||||
if (!string.IsNullOrEmpty(q.mem_num_txt))
|
||||
qry = qry.Where(o => o.mem_num.HasValue && o.member.u_name.Contains(q.mem_num_txt.Trim()));
|
||||
if (!string.IsNullOrEmpty(q.debtor))
|
||||
qry = qry.Where(o => (o.debtor??"").Contains(q.debtor.Trim()));
|
||||
|
||||
|
||||
if (sortBy.Equals("category_Txt"))
|
||||
{
|
||||
if (sortDesc)
|
||||
qry = qry.OrderByDescending(o => o.category);
|
||||
else
|
||||
qry = qry.OrderBy(o => o.category);
|
||||
}
|
||||
else if (sortBy.Equals("kindsTxt"))
|
||||
{
|
||||
if (sortDesc)
|
||||
qry = qry.OrderByDescending(o => o.kind);
|
||||
else
|
||||
qry = qry.OrderBy(o => o.kind);
|
||||
}
|
||||
else if (sortBy.Equals("reasonsTxt"))
|
||||
{
|
||||
if (sortDesc)
|
||||
qry = qry.OrderByDescending(o => o.reason);
|
||||
else
|
||||
qry = qry.OrderBy(o => o.reason);
|
||||
}
|
||||
else if (sortBy.Equals("activity_Txt"))
|
||||
{
|
||||
if (sortDesc)
|
||||
qry = qry.OrderByDescending(o => o.activity_num);
|
||||
else
|
||||
qry = qry.OrderBy(o => o.activity_num);
|
||||
}
|
||||
else if (sortBy.Equals("actItem_Txt"))
|
||||
{
|
||||
if (sortDesc)
|
||||
qry = qry.OrderByDescending(o => o.actItem_num);
|
||||
else
|
||||
qry = qry.OrderBy(o => o.actItem_num);
|
||||
}
|
||||
else if (sortBy.Equals("uptime"))
|
||||
{
|
||||
if (sortDesc)
|
||||
qry = qry.OrderByDescending(o => o.uptime);
|
||||
else
|
||||
qry = qry.OrderBy(o => o.uptime);
|
||||
}
|
||||
else
|
||||
qry = qry.OrderByDescending(o => o.num);
|
||||
|
||||
var tdesc = publicFun.enum_desc<Model.stock.type>();
|
||||
|
||||
var ret = new
|
||||
{
|
||||
list = qry.ToPagedList(page, pageSize).Select(x => new
|
||||
{
|
||||
num = x.num,
|
||||
category = x.category,
|
||||
category_Txt = tdesc[x.category ?? 1],
|
||||
kind = x.kind,
|
||||
kindTxt = x.kind.HasValue? x.stock_kind.kind :"",
|
||||
kindsTxt = x.kind.HasValue ? new TreeView().kindText(_db.stock_kind.Select(o => new TreeItem()
|
||||
{
|
||||
kind = o.kind,
|
||||
num = o.num,
|
||||
root = o.root,
|
||||
}).ToList(), x.kind) : "",
|
||||
reason = x.reason,
|
||||
reasonsTxt = x.reason.HasValue ? new TreeView().kindText(_db.stock_reason.Select(o => new TreeItem()
|
||||
{
|
||||
kind = o.kind,
|
||||
num = o.num,
|
||||
root = o.root,
|
||||
}).ToList(), x.reason) : "",
|
||||
actItem_num = x.actItem_num,
|
||||
actItem_Txt = x.actItem_num.HasValue ? x.actItem.subject : "",
|
||||
activity_num = x.activity_num,
|
||||
activity_Txt = x.activity_num.HasValue? x.activity.subject :"",
|
||||
uptime = x.uptime,
|
||||
qty = x.qty,
|
||||
price = x.price,
|
||||
|
||||
}),
|
||||
count = qry.Count()
|
||||
|
||||
};
|
||||
|
||||
|
||||
if (ret.list == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
||||
return Ok(ret);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("api/stock/GetKindList")]
|
||||
public IHttpActionResult GetKindList([FromBody] Model.ViewModel.stock_kind q, int page, int pageSize = 10,
|
||||
string sortBy = "", bool sortDesc = false)
|
||||
{
|
||||
var qry = _db.stock_kind.AsEnumerable();
|
||||
|
||||
if (!string.IsNullOrEmpty(q.kind))
|
||||
qry = qry.Where(o => o.kind.Contains(q.kind));
|
||||
|
||||
|
||||
//if (sortBy.Equals("kind"))
|
||||
//{
|
||||
// if (sortDesc)
|
||||
// qry = qry.OrderByDescending(o => o.kind);
|
||||
// else
|
||||
// qry = qry.OrderBy(o => o.kind);
|
||||
//}
|
||||
|
||||
//else
|
||||
// qry = qry.OrderBy(o => o.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 ret = new
|
||||
{
|
||||
list = qry2.ToPagedList(page, pageSize).Select(x => new
|
||||
{
|
||||
num = x.num,
|
||||
kind = new TreeView().RptDash(x.Level) + x.kind,
|
||||
|
||||
}),
|
||||
count = qry.Count()
|
||||
};
|
||||
|
||||
|
||||
if (ret.list == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
||||
return Ok(ret);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("api/stock/GetReasonList")]
|
||||
public IHttpActionResult GetReasonList([FromBody] Model.ViewModel.stock_reason q, int page, int pageSize = 10,
|
||||
string sortBy = "", bool sortDesc = false)
|
||||
{
|
||||
var qry = _db.stock_reason.AsEnumerable();
|
||||
|
||||
if (!string.IsNullOrEmpty(q.kind))
|
||||
qry = qry.Where(o => o.kind.Contains(q.kind));
|
||||
|
||||
if (q.category.HasValue && Convert.ToInt32(q.category)>0)
|
||||
qry = qry.Where(o => o.category == q.category);
|
||||
|
||||
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 ret = new
|
||||
{
|
||||
list = qry2.ToPagedList(page, pageSize).Select(x => new
|
||||
{
|
||||
num = x.num,
|
||||
kind = new TreeView().RptDash(x.Level) + x.kind,
|
||||
}),
|
||||
count = qry.Count()
|
||||
};
|
||||
|
||||
|
||||
if (ret.list == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
||||
return Ok(ret);
|
||||
}
|
||||
|
||||
|
||||
|
||||
[HttpPost]
|
||||
[Route("api/stock/GetStockFiles")]
|
||||
public IHttpActionResult GetItemFiles([FromBody] Model.stock_files q, int page, int pageSize = 10,
|
||||
string sortBy = "", bool sortDesc = false)
|
||||
{
|
||||
if (q.stock_num.HasValue && q.stock_num.Value>0)
|
||||
{
|
||||
//檢查
|
||||
var qry = _db.stock_files.AsEnumerable();
|
||||
qry = qry.Where(o => o.stock_num == q.stock_num.Value);
|
||||
qry.OrderByDescending(x => x.num);
|
||||
|
||||
int i = 1;
|
||||
//已有值
|
||||
var ret = new
|
||||
{
|
||||
list = qry.ToPagedList(page, pageSize).Select(x => new
|
||||
{
|
||||
id = i++,
|
||||
num = x.num,
|
||||
stock_num = x.stock_num,
|
||||
pic1 = x.pic1,
|
||||
pic1_name = x.pic1_name,
|
||||
}),
|
||||
count = qry.Count(),
|
||||
};
|
||||
|
||||
if (ret.list == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
||||
return Ok(ret);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("api/stock/uploadFiles")]
|
||||
public async Task<IHttpActionResult> uploadFiles()
|
||||
{
|
||||
if (!Request.Content.IsMimeMultipartContent())
|
||||
{
|
||||
return BadRequest("無效的請求。");
|
||||
}
|
||||
|
||||
|
||||
string uploadFolder = Path.Combine(HttpContext.Current.Server.MapPath("~/upload"), "stock");
|
||||
Directory.CreateDirectory(uploadFolder);
|
||||
|
||||
var provider = new MultipartFormDataStreamProvider(HttpContext.Current.Server.MapPath(Model.stock.Dir));
|
||||
await Request.Content.ReadAsMultipartAsync(provider);
|
||||
|
||||
if (provider.FileData.Count == 0)
|
||||
{
|
||||
return BadRequest("缺少檔案。");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
string tempFilePath = provider.FileData[0].LocalFileName;
|
||||
string fileName = provider.FileData[0].Headers.ContentDisposition.FileName.Trim('\"');
|
||||
//string Dir = provider.FormData[0];
|
||||
|
||||
//re-name
|
||||
string[] n = Path.GetFileName(fileName).Split('.');
|
||||
fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + n[n.Length - 1];
|
||||
|
||||
bool isAllowed = false;
|
||||
string[] type = {
|
||||
"jpg",
|
||||
"jpeg",
|
||||
"png",
|
||||
"pdf"
|
||||
};
|
||||
|
||||
for (int i = 0; i <= type.Length - 1; i++)
|
||||
{
|
||||
if (n[n.Length - 1].ToLower() == type[i])
|
||||
{
|
||||
isAllowed= true;
|
||||
}
|
||||
}
|
||||
if (isAllowed)
|
||||
{
|
||||
//計算檔案大小
|
||||
long result = -1;
|
||||
System.Net.WebRequest req = System.Net.WebRequest.Create(tempFilePath);
|
||||
req.Method = "HEAD";
|
||||
using (System.Net.WebResponse resp = req.GetResponse())
|
||||
{
|
||||
if (long.TryParse(resp.Headers.Get("Content-Length"), out long ContentLength))
|
||||
{
|
||||
result = ContentLength;//位元組
|
||||
}
|
||||
}
|
||||
|
||||
//result / 1024 = kB
|
||||
|
||||
if(result/1000/1000<= 2)
|
||||
{
|
||||
|
||||
string filePath = Path.Combine(uploadFolder, fileName);
|
||||
File.Move(tempFilePath, filePath);
|
||||
|
||||
return Ok(fileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest("檔案限制 2 MB 以內");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest("格式不符。");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("api/stock/SaveFileData")]
|
||||
public IHttpActionResult SaveFileData([FromBody] Model.stock_files item)
|
||||
{
|
||||
item.reg_time = DateTime.Now;
|
||||
_db.stock_files.Add(item);
|
||||
_db.SaveChanges();
|
||||
return Ok(item.num);
|
||||
|
||||
}
|
||||
|
||||
|
||||
[HttpDelete]
|
||||
[Route("api/stock/DeleteFilesItem/{id}")]//刪除相關檔案
|
||||
public void DeleteFilesItem(int id)
|
||||
{
|
||||
var prod = _db.stock_files.AsEnumerable().Where(q => q.num == id).FirstOrDefault(); //刪除該筆資料
|
||||
if (prod != null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(prod.pic1))
|
||||
{
|
||||
publicFun publicFun = new publicFun();
|
||||
publicFun.DeleteFile(Model.stock.Dir + "/" + prod.pic1);
|
||||
}
|
||||
|
||||
_db.stock_files.Remove(prod);
|
||||
_db.SaveChanges(); //執行
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user