196 lines
6.8 KiB
C#
196 lines
6.8 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using static DbTableClass;
|
|
using static resultClass;
|
|
using System.Data.SqlClient;
|
|
using Dapper.Contrib.Extensions;
|
|
using Dapper;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace QuotationMaker.Controllers
|
|
{
|
|
[Route("Api")]
|
|
public class ApiController : ControllerBase
|
|
{
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
DbConn dbConn = new DbConn();
|
|
SqlConnection conn = new SqlConnection(GlobalClass.appsettings("ConnectionStrings:SQLConnectionString"));
|
|
SqlConnection elabConn = new SqlConnection(GlobalClass.appsettings("ConnectionStrings:ElabConnectionString"));
|
|
|
|
public ApiController(IHttpContextAccessor httpContextAccessor)
|
|
{
|
|
this._httpContextAccessor = httpContextAccessor;
|
|
|
|
|
|
}
|
|
|
|
[Route("addEditDelGetCompany")]
|
|
public ActionResult AddEditDelSubItem(IFormCollection obj)
|
|
{
|
|
companyListResult ret = new companyListResult();
|
|
|
|
authToken token = new authToken(this._httpContextAccessor);
|
|
if (token.user_isLogin == false)
|
|
{
|
|
HttpContext.Response.Cookies.Delete("token_key");
|
|
ret.ret = "no";
|
|
ret.err_code = "99999";
|
|
ret.message = "非登入狀態!";
|
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
|
}
|
|
|
|
|
|
|
|
string company_uid = obj["company_uid"].ToString();
|
|
string company_name = obj["company_name"].ToString();
|
|
string company_serialNo = obj["company_serialNo"].ToString();
|
|
string company_address = obj["company_address"].ToString();
|
|
string company_tel = obj["company_tel"].ToString();
|
|
string company_fax = obj["company_fax"].ToString();
|
|
|
|
string method = obj["method"].ToString();
|
|
|
|
|
|
|
|
|
|
if (method == "")
|
|
{
|
|
ret.ret = "no";
|
|
ret.err_code = "0001";
|
|
ret.message = "沒有method!";
|
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (method == "add")
|
|
{
|
|
if (company_name.Trim() == "")
|
|
{
|
|
ret.ret = "no";
|
|
ret.err_code = "0003";
|
|
ret.message = "沒有company_name!";
|
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
|
}
|
|
|
|
company_uid = GlobalClass.CreateRandomCode(24);
|
|
company newItem = new company();
|
|
newItem.company_name = company_name;
|
|
newItem.company_uid = company_uid;
|
|
newItem.company_serialNo = company_serialNo;
|
|
newItem.company_address = company_address;
|
|
newItem.company_tel = company_tel;
|
|
newItem.company_fax = company_fax;
|
|
|
|
newItem.company_lastmodify_uid = token.user_uid;
|
|
newItem.company_createdate = DateTime.Now;
|
|
newItem.company_modifydate = DateTime.Now;
|
|
|
|
conn.Insert(newItem);
|
|
ret.companys.Add(newItem);
|
|
ret.ret = "yes";
|
|
|
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
|
}
|
|
|
|
if (company_uid.Trim() == "")
|
|
{
|
|
ret.ret = "no";
|
|
ret.err_code = "0002";
|
|
ret.message = "沒有company_uid!";
|
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
|
}
|
|
|
|
|
|
|
|
company editItem = conn.QueryFirstOrDefault<company>("select * from company where company_isdel = 'N' and company_uid = @company_uid ", new { company_uid = company_uid });
|
|
|
|
if (editItem == null)
|
|
{
|
|
ret.ret = "no";
|
|
ret.err_code = "0004";
|
|
ret.message = "沒有company_uid此筆資料!";
|
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
|
}
|
|
|
|
if (method == "edit")
|
|
{
|
|
|
|
|
|
if (company_name.Trim() == "")
|
|
{
|
|
ret.ret = "no";
|
|
ret.err_code = "0002";
|
|
ret.message = "沒有company_name!";
|
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
|
}
|
|
|
|
editItem.company_name = company_name;
|
|
|
|
editItem.company_serialNo = company_serialNo;
|
|
editItem.company_address = company_address;
|
|
editItem.company_tel = company_tel;
|
|
editItem.company_fax = company_fax;
|
|
|
|
editItem.company_lastmodify_uid = token.user_uid;
|
|
editItem.company_modifydate = DateTime.Now;
|
|
|
|
conn.Update(editItem);
|
|
ret.companys.Add(editItem);
|
|
ret.ret = "yes";
|
|
|
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
|
}
|
|
|
|
if (method == "get")
|
|
{
|
|
ret.companys.Add(editItem);
|
|
ret.ret = "yes";
|
|
|
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
|
}
|
|
|
|
if (method == "del")
|
|
{
|
|
editItem.company_isdel = "Y";
|
|
editItem.company_lastmodify_uid = token.user_uid;
|
|
editItem.company_modifydate = DateTime.Now;
|
|
|
|
conn.Execute("update contactPerson set contactPerson_isdel = 'Y' where company_uid = @company_uid ", new { company_uid = company_uid });
|
|
|
|
conn.Update(editItem);
|
|
ret.ret = "yes";
|
|
|
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
|
}
|
|
|
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
|
}
|
|
|
|
[Route("companyList")]
|
|
public ActionResult AuthSubItemList(IFormCollection obj)
|
|
{
|
|
companyListResult ret = new companyListResult();
|
|
|
|
authToken token = new authToken(this._httpContextAccessor);
|
|
if (token.user_isLogin == false)
|
|
{
|
|
HttpContext.Response.Cookies.Delete("token_key");
|
|
ret.ret = "no";
|
|
ret.err_code = "99999";
|
|
ret.message = "非登入狀態!";
|
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
|
}
|
|
|
|
|
|
|
|
ret.companys = conn.Query<company>("select * from company where company_isdel = 'N' ").ToList();
|
|
ret.ret = "yes";
|
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
|
}
|
|
}
|
|
}
|