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 Dapper;
using System.Data;
using AMESCoreStudio.WebApi.Extensions;
namespace AMESCoreStudio.WebApi.Controllers.AMES
{
///
/// 錫膏使用紀錄資料檔
///
[Route("api/[controller]")]
[ApiController]
public class SolderPasteRecordController : ControllerBase
{
private readonly AMESContext _context;
public SolderPasteRecordController(AMESContext context)
{
_context = context;
}
// GET: api/SolderPasteRecord
[HttpGet]
public async Task>> GetSolderPasteRecords()
{
return await _context.SolderPasteRecords.ToListAsync();
}
// GET: api/SolderPasteRecord/5
[HttpGet("{id}")]
public async Task> GetSolderPasteRecord(int id)
{
var solderPasteRecord = await _context.SolderPasteRecords.FindAsync(id);
if (solderPasteRecord == null)
{
return NotFound();
}
return solderPasteRecord;
}
///
/// 更新錫膏使用紀錄資料檔
///
///
///
[HttpPut]
public async Task> PutSolderPasteRecord(SolderPasteRecord solderPasteRecord)
{
ResultModel result = new ResultModel();
_context.Entry(solderPasteRecord).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
}
catch (Exception ex)
{
result.Success = false;
result.Msg = ex.InnerException.Message;
}
return result;
}
///
/// 新增錫膏使用紀錄資料檔
///
/// models
///
[HttpPost]
public async Task> PostSolderPasteRecord(List solderPasteRecords)
{
ResultModel result = new ResultModel();
try
{
_context.SolderPasteRecords.AddRange(solderPasteRecords);
await _context.SaveChangesAsync();
// 更新基本檔的狀態
List solderPasteInfos = solderPasteRecords.Select(s => new SolderPasteInfo
{
SolderPasteID = s.SolderPasteID,
Status = s.Status,
UpdateUserID =s.CreateUserID
}).ToList();
var query = @" UPDATE JHAMES.SOLDER_PASTE_INFO SET STATUS = :Status ,
UPDATE_USERID = :UpdateUserID ,
UPDATE_DATE = sysdate
WHERE SOLDER_PASTE_ID = :SolderPasteID ";
_context.Database.DapperExecute(query, solderPasteInfos);
result.Success = true;
result.Msg = "OK";
}
catch (Exception ex)
{
result.Success = false;
result.Msg = ex.InnerException.Message;
}
return result;
}
// DELETE: api/SolderPasteRecord/5
[HttpDelete("{id}")]
public async Task> DeleteSolderPasteRecord(int id)
{
var solderPasteRecord = await _context.SolderPasteRecords.FindAsync(id);
if (solderPasteRecord == null)
{
return NotFound();
}
_context.SolderPasteRecords.Remove(solderPasteRecord);
await _context.SaveChangesAsync();
return solderPasteRecord;
}
}
}