You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

284 lines
10 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using AMESCoreStudio.WebApi;
using AMESCoreStudio.WebApi.Models.AMES;
using AMESCoreStudio.CommonTools.Result;
using AMESCoreStudio.WebApi.Models.BAS;
using AMESCoreStudio.WebApi.Models.SYS;
namespace AMESCoreStudio.WebApi.Controllers.AMES
{
/// <summary>
///
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class ProductionNoticeController : ControllerBase
{
private readonly AMESContext _context;
/// <summary>
///
/// </summary>
/// <param name="context"></param>
public ProductionNoticeController(AMESContext context)
{
_context = context;
}
[HttpGet("Query")]
public async Task<ActionResult<IEnumerable<WipClass>>> GetWipClassByData(int wipID, int stationID, string ruleStatus)
{
IQueryable<WipClass> q = _context.WipClass;
if (wipID != 0)
q = q.Where(w => w.WipID == wipID);
if (stationID != 0)
q = q.Where(w => w.StationID == stationID);
if (!string.IsNullOrWhiteSpace(ruleStatus))
q = q.Where(w => w.RuleStatus == ruleStatus);
var result = await q.ToListAsync();
return result;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
// GET: api/GetProductionNotice
[HttpGet]
[Route("GetProductionNotice")]
public async Task<List<ProductionNotice>> GetProductionNotice(string ppid)
{
//ResultModel<dynamic> result = new ResultModel<dynamic>();
IQueryable<ProductionNotice> q = _context.ProductionNotice;
if (!string.IsNullOrWhiteSpace(ppid))
q = q.Where(x => x.ProductionID == ppid);
return await q.ToListAsync();
//result.Data = qq;
//result.Success = true;
//result.Msg = "OK";
//return qq;
}
/// <summary>
///
/// </summary>
/// <param name="productTypeID"></param>
/// <param name="material"></param>
///<param name="desc"></param>
/// <param name="sdate"></param>
/// <param name="edate"></param>
/// <returns></returns>
[Route("ProductionNoticeInfo")]
[HttpGet]
public async Task<ResultModel<dynamic>> GetProductionNoticeInfo(string productTypeID, string material,string desc,string sdate,string edate)
{
//IQueryable<ProductionNotice> q = _context.ProductionNotice;
ResultModel<dynamic> result = new ResultModel<dynamic>();
var q = from q1 in _context.ProductionNotice
select new
{
ProductionID = q1.ProductionID,
ProductMaterial=q1.ProductMaterial,
ProductionSID=q1.ProductionSID,
ProductTypeID=q1.ProductTypeID,
ProductFID=q1.ProductFID,
ProductDESC=q1.ProductDESC,
CreateDate=q1.CreateDate,
CreateUser= q1.CreateUser,
};
if (productTypeID != "0")
{
q = q.Where(p =>p.ProductTypeID.Equals(productTypeID));
}
if (material != null)
{
q = q.Where(p => p.ProductMaterial.Contains(material));
}
if (desc != null)
{
q = q.Where(p => p.ProductDESC.Contains(desc));
}
DateTime dateValue;
if (sdate != "*")
{
if (DateTime.TryParse(sdate, out dateValue))
{
q = q.Where(p => p.CreateDate >= DateTime.Parse(sdate));
}
}
if (edate != "*")
{
if (DateTime.TryParse(edate, out dateValue))
{
q = q.Where(p => p.CreateDate <= DateTime.Parse(edate));
}
}
q = q.OrderBy(p => p.CreateDate);
//var productionNoticeResult = q.Select(p => new
//{
// p.ProductMaterial,
// p.ProductionSID,
// p.ProductTypeID,
// p.ProductFID,
// p.ProductDESC,
// p.CreateDate
//});
var qq = await q.ToListAsync();
IQueryable<Stations> stationsQuery = _context.Stationses;
IQueryable<FactoryInfo> factoryQuery = _context.FactoryInfos;
IQueryable<UserInfo> userInfoQuery = _context.UserInfoes;
Dictionary<string, string> productTypeMapping = new Dictionary<string, string>();
productTypeMapping.Add("1", "單板");
productTypeMapping.Add("2", "系統");
productTypeMapping.Add("3", "醫療");
dynamic datas = qq.Select(data => new {
ProductFID = factoryQuery.FirstOrDefault(p => p.FactoryID.ToString() == data.ProductFID).FactoryNameCh,
ProductionID = data.ProductionID,
ProductionSID = stationsQuery.FirstOrDefault(p => p.StationID.ToString() == data.ProductionSID).StationName,
ProductMaterial = data.ProductMaterial,
ProductTypeID = productTypeMapping.FirstOrDefault(p => p.Key == data.ProductTypeID).Value,
ProductDESC = data.ProductDESC,
Images = _context.ProductionNoticeBlob.Where(w => w.APNO.Trim().ToUpper() == data.ProductionID.Trim().ToUpper()).Select(x=> x.FilePath+x.FileName).ToListAsync().Result,
CreateUser = userInfoQuery.FirstOrDefault(p => p.UserID.ToString() == data.CreateUser).UserName,
CreateDate = data.CreateDate,
});
result.Data = datas;
if (result == null)
{
result.Msg = "查無資料";
result.Success = false;
return result;
}
result.Success = true;
result.Msg = "OK";
return result;
//return ProInfo;
}
/// <summary>
/// Create
/// </summary>
/// <param name="productionNotice"></param>
/// <returns></returns>
[HttpPost]
[Route("ProductionNoticeInfo")]
public async Task<ResultModel<ProductionNotice>> PostProductionNoticeInfo(ProductionNotice productionNotice)
{
ResultModel<ProductionNotice> result = new ResultModel<ProductionNotice>();
Helper helper = new Helper(_context);
//productionNotice.WipBlobID = helper.GetIDKey("WIP_BLOB_ID").Result;
try
{
_context.ProductionNotice.Add(productionNotice);
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
}
catch (Exception ex)
{
result.Success = false;
result.Msg = ex.InnerException.Message;
}
return result;
}
/// <summary>
/// Update
/// </summary>
/// <param name="productionNotice"></param>
/// <returns></returns>
[HttpPut]
[Route("PutProductionNotice")]
public async Task<ResultModel<ProductionNotice>> PutProductionNotice(ProductionNotice productionNotice)
{
ResultModel<ProductionNotice> result = new ResultModel<ProductionNotice>();
_context.Entry(productionNotice).State = EntityState.Modified;
//設置容器空間某一個模型的某一個欄位 不提交到資料庫
//DbContent.Entry是要更新到資料庫的整個對象
_context.Entry<ProductionNotice>(productionNotice).Property("CreateDate").IsModified = false;
_context.Entry<ProductionNotice>(productionNotice).Property("CreateUser").IsModified = false;
productionNotice.UpDate = DateTime.Now;
productionNotice.UpDateUser = productionNotice.UpDateUser;
try
{
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
}
catch (Exception ex)
{
result.Success = false;
result.Msg = ex.InnerException.Message;
}
return result;
}
/// <summary>
/// 刪除
/// </summary>
/// <returns></returns>
[HttpDelete("ByPPID")]
public async Task<ResultModel<ProductionNotice>> DeleteProductionNoticeByPPID(string PPID)
{
ResultModel<ProductionNotice> result = new ResultModel<ProductionNotice>();
if (string.IsNullOrWhiteSpace(PPID))
{
result.Success = false;
result.Msg = "請輸入要刪除的資料";
}
else
{
var productionNotice = await _context.ProductionNotice.Where(w => w.ProductionID.Trim().ToUpper() == PPID.Trim().ToUpper()).ToListAsync();
if (productionNotice.Count() != 0)
{
try
{
_context.ProductionNotice.Remove(productionNotice.FirstOrDefault());
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
}
catch (Exception ex)
{
result.Success = false;
result.Msg = ex.InnerException.Message;
}
}
}
return result;
}
private bool ProductionIndexExists(int id)
{
return _context.ProductionIndexes.Any(e => e.ProductionID == id);
}
}
}