157 lines
6.4 KiB
C#
157 lines
6.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Runtime.Serialization.Json;
|
|
using System.Web.SessionState;
|
|
using Dapper;
|
|
using Dapper.Contrib.Extensions;
|
|
using System.Data.SqlClient;
|
|
|
|
namespace abbott_2024_event.webapi
|
|
{
|
|
/// <summary>
|
|
/// bindBabyData 的摘要描述
|
|
/// </summary>
|
|
public class bindBabyData : IHttpHandler
|
|
{
|
|
SqlConnection conn = new SqlConnection(globalClass.appsettings("DBConnectionString"));
|
|
public void ProcessRequest(HttpContext context)
|
|
{
|
|
result objRet = new result();
|
|
DataContractJsonSerializer json = new DataContractJsonSerializer(objRet.GetType());
|
|
context.Response.ContentType = "application/json;charset=utf-8";
|
|
|
|
string line_uid = (context.Request["line_uid"] == null) ? "" : context.Request["line_uid"].ToString();
|
|
string bind_type = (context.Request["bind_type"] == null) ? "" : context.Request["bind_type"].ToString();
|
|
string babyData_uid = (context.Request["babyData_uid"] == null) ? "" : context.Request["babyData_uid"].ToString();
|
|
string temp_key = (context.Request["temp_key"] == null) ? "" : context.Request["temp_key"].ToString();
|
|
|
|
if (temp_key == "") {
|
|
objRet.ret = "no";
|
|
objRet.err_code = "3001";
|
|
objRet.message = "沒有temp_key!";
|
|
json.WriteObject(context.Response.OutputStream, objRet);
|
|
return;
|
|
}
|
|
|
|
if (bind_type != "new" && bind_type != "append") {
|
|
objRet.ret = "no";
|
|
objRet.err_code = "3003";
|
|
objRet.message = "bind_type值錯誤!";
|
|
json.WriteObject(context.Response.OutputStream, objRet);
|
|
return;
|
|
}
|
|
|
|
babyRec babyRec = conn.QueryFirstOrDefault<babyRec>("select * from babyRec where babyRec_key = @babyRec_key", new { babyRec_key = temp_key });
|
|
|
|
if (babyRec == null) {
|
|
objRet.ret = "no";
|
|
objRet.err_code = "3002";
|
|
objRet.message = "沒有此temp_key資料!";
|
|
json.WriteObject(context.Response.OutputStream, objRet);
|
|
return;
|
|
}
|
|
|
|
lineUser lineUser = conn.QueryFirstOrDefault<lineUser>("select * from lineUser where line_uid = @line_uid", new { line_uid = line_uid });
|
|
|
|
if (lineUser == null) {
|
|
objRet.ret = "no";
|
|
objRet.err_code = "2002";
|
|
objRet.message = "無此line_uid的資料!";
|
|
json.WriteObject(context.Response.OutputStream, objRet);
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
//新增寶寶
|
|
if (bind_type == "new") {
|
|
babyData_uid = babyRec.babyData_uid;
|
|
babyData babyData = conn.QueryFirstOrDefault<babyData>("select * from babyData where babyData_uid = @babyData_uid", new { babyData_uid = babyData_uid });
|
|
|
|
if (babyData == null) {
|
|
objRet.ret = "no";
|
|
objRet.err_code = "3004";
|
|
objRet.message = "沒有此babyData資料!";
|
|
json.WriteObject(context.Response.OutputStream, objRet);
|
|
return;
|
|
}
|
|
|
|
if (babyData.babyData_bindedLine == "Y") {
|
|
objRet.ret = "no";
|
|
objRet.err_code = "3005";
|
|
objRet.message = "此寶貝資料已經綁定過Line帳號了!";
|
|
json.WriteObject(context.Response.OutputStream, objRet);
|
|
return;
|
|
}
|
|
|
|
babyData.line_uid = line_uid;
|
|
babyData.babyData_bindedLine = "Y";
|
|
babyData.babyData_bindeddate = DateTime.Now;
|
|
babyData.babyData_lastTestDate = babyRec.babyRec_createdate;
|
|
|
|
babyRec.babyRec_monthLastRec = "Y";
|
|
babyRec.line_uid = line_uid;
|
|
|
|
conn.Update<babyData>(babyData);
|
|
conn.Update<babyRec>(babyRec);
|
|
|
|
conn.Execute("update babyRec set babyRec_newestRec = 'N' where babyData_uid = @babyData_uid", new { babyData_uid = babyData_uid });
|
|
|
|
babyRec lastestBabyRec = conn.QueryFirstOrDefault<babyRec>("select * from babyRec where babyData_uid = @babyData_uid order by babyRec_months desc, babyRec_sn desc, babyRec_monthLastRec desc", new { babyData_uid = babyData_uid });
|
|
|
|
if (lastestBabyRec != null)
|
|
{
|
|
lastestBabyRec.babyRec_newestRec = "Y";
|
|
conn.Update<babyRec>(lastestBabyRec);
|
|
}
|
|
|
|
objRet.ret = "yes";
|
|
json.WriteObject(context.Response.OutputStream, objRet);
|
|
return;
|
|
}
|
|
|
|
//綁定寶寶
|
|
if (bind_type == "append") {
|
|
babyRec.babyData_uid = babyData_uid;
|
|
babyRec.babyRec_monthLastRec = "Y";
|
|
|
|
conn.Execute("update babyRec set babyRec_monthLastRec = 'N' where babyData_uid = @babyData_uid and babyRec_recYear = @babyRec_recYear and babyRec_recMonth = @babyRec_recMonth and babyRec_months = @babyRec_months", new { babyData_uid = babyData_uid, babyRec_recYear = babyRec.babyRec_recYear, babyRec_recMonth = babyRec.babyRec_recMonth, babyRec_months = babyRec.babyRec_months });
|
|
|
|
babyRec.line_uid = line_uid;
|
|
|
|
conn.Update<babyRec>(babyRec);
|
|
|
|
conn.Execute("update babyRec set babyRec_newestRec = 'N' where babyData_uid = @babyData_uid", new { babyData_uid = babyData_uid });
|
|
|
|
babyRec lastestBabyRec = conn.QueryFirstOrDefault<babyRec>("select * from babyRec where babyData_uid = @babyData_uid order by babyRec_months desc, babyRec_sn desc, babyRec_monthLastRec desc", new { babyData_uid = babyData_uid });
|
|
|
|
if (lastestBabyRec != null)
|
|
{
|
|
lastestBabyRec.babyRec_newestRec = "Y";
|
|
conn.Update<babyRec>(lastestBabyRec);
|
|
}
|
|
|
|
objRet.ret = "yes";
|
|
json.WriteObject(context.Response.OutputStream, objRet);
|
|
return;
|
|
}
|
|
}
|
|
|
|
public class result
|
|
{
|
|
public string ret = "no";
|
|
public string err_code = "0000";
|
|
public string message = "";
|
|
}
|
|
|
|
public bool IsReusable
|
|
{
|
|
get
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
} |