307 lines
14 KiB
C#
307 lines
14 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>
|
||
/// babyData 的摘要描述
|
||
/// </summary>
|
||
public class babyDataAshx : 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";
|
||
context.Response.AddHeader("Access-Control-Allow-Origin", "*");
|
||
|
||
string name = (context.Request["name"] == null) ? "" : context.Request["name"].ToString();
|
||
string birthday = (context.Request["birthday"] == null) ? "" : context.Request["birthday"].ToString();
|
||
string gender = (context.Request["gender"] == null) ? "" : context.Request["gender"].ToString();
|
||
string height = (context.Request["height"] == null) ? "" : context.Request["height"].ToString();
|
||
string line_uid = (context.Request["line_uid"] == null) ? "" : context.Request["line_uid"].ToString();
|
||
string line_displayname = (context.Request["line_displayname"] == null) ? "" : context.Request["line_displayname"].ToString();
|
||
string babyData_uid = (context.Request["babyData_uid"] == null) ? "" : context.Request["babyData_uid"].ToString();
|
||
string utm_source = (context.Request["utm_source"] == null) ? "" : context.Request["utm_source"].ToString();
|
||
string utm_medium = (context.Request["utm_medium"] == null) ? "" : context.Request["utm_medium"].ToString();
|
||
string utm_campaign = (context.Request["utm_campaign"] == null) ? "" : context.Request["utm_campaign"].ToString();
|
||
string utm_term = (context.Request["utm_term"] == null) ? "" : context.Request["utm_term"].ToString();
|
||
string utm_content = (context.Request["utm_content"] == null) ? "" : context.Request["utm_content"].ToString();
|
||
|
||
if (line_uid == "undefined") {
|
||
line_uid = "";
|
||
}
|
||
|
||
utm_source = (utm_source.Length > 20) ? utm_source.Substring(0, 20) : utm_source;
|
||
utm_medium = (utm_medium.Length > 20) ? utm_medium.Substring(0, 20) : utm_medium;
|
||
utm_campaign = (utm_campaign.Length > 20) ? utm_campaign.Substring(0, 20) : utm_campaign;
|
||
utm_term = (utm_term.Length > 20) ? utm_term.Substring(0, 20) : utm_term;
|
||
utm_content = (utm_content.Length > 20) ? utm_content.Substring(0, 20) : utm_content;
|
||
|
||
string babyRec_key = globalClass.CreateRandomCode(32);
|
||
|
||
|
||
string delSQLDatastring = string.Format("delete babyData where line_uid = '' and babyData_createdate <= '{0}'", DateTime.Now.AddDays(-5).ToString("yyyy/MM/dd 23:59:59"));
|
||
string delSQLRecstring = string.Format("delete babyRec where line_uid = '' and babyRec_createdate <= '{0}'", DateTime.Now.AddDays(-5).ToString("yyyy/MM/dd 23:59:59"));
|
||
|
||
conn.Execute(delSQLDatastring);
|
||
conn.Execute(delSQLRecstring);
|
||
|
||
|
||
|
||
if (babyData_uid == "" && name == "") {
|
||
objRet.ret = "no";
|
||
objRet.err_code = "1001";
|
||
objRet.message = "請輸入寶貝姓名!";
|
||
|
||
json.WriteObject(context.Response.OutputStream, objRet);
|
||
return;
|
||
}
|
||
|
||
DateTime dateBirthDay;
|
||
|
||
if (babyData_uid == "") {
|
||
try
|
||
{
|
||
dateBirthDay = DateTime.Parse(birthday + " 23:59:59");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
objRet.ret = "no";
|
||
objRet.err_code = "1002";
|
||
objRet.message = "請輸入正確的寶貝生日!" + ex.Message;
|
||
|
||
json.WriteObject(context.Response.OutputStream, objRet);
|
||
return;
|
||
}
|
||
}
|
||
|
||
|
||
if (babyData_uid == "" && gender != "M" && gender != "F") {
|
||
objRet.ret = "no";
|
||
objRet.err_code = "1003";
|
||
objRet.message = "請輸入正確的寶貝性別!";
|
||
|
||
json.WriteObject(context.Response.OutputStream, objRet);
|
||
return;
|
||
}
|
||
|
||
double dblHeight;
|
||
|
||
try
|
||
{
|
||
dblHeight = Double.Parse(height);
|
||
}
|
||
catch (Exception ex) {
|
||
objRet.ret = "no";
|
||
objRet.err_code = "1004";
|
||
objRet.message = "請輸入寶貝身高!" + ex.Message;
|
||
|
||
json.WriteObject(context.Response.OutputStream, objRet);
|
||
return;
|
||
}
|
||
|
||
if (line_uid != "") {
|
||
lineUser lineUser = conn.QueryFirstOrDefault<lineUser>("select * from lineUser where line_uid = @line_uid", new { line_uid = line_uid });
|
||
|
||
if (lineUser != null)
|
||
{
|
||
if (line_displayname != lineUser.line_displayName && line_displayname != "")
|
||
{
|
||
lineUser.line_displayName = line_displayname;
|
||
lineUser.lineUser_modifydate = DateTime.Now.AddMinutes(3);
|
||
conn.Update<lineUser>(lineUser);
|
||
}
|
||
else {
|
||
lineUser.lineUser_modifydate = DateTime.Now.AddMinutes(3);
|
||
conn.Update<lineUser>(lineUser);
|
||
}
|
||
|
||
|
||
|
||
List<babyData> babyDatas = conn.Query<babyData>("select * from babyData where line_uid = @line_uid", new { line_uid = line_uid }).ToList();
|
||
|
||
if (babyDatas.Count > 0) {
|
||
objRet.more_data = "Y";
|
||
}
|
||
}
|
||
else {
|
||
lineUser = new lineUser();
|
||
|
||
if (line_displayname == null) {
|
||
objRet.ret = "no";
|
||
objRet.err_code = "1005";
|
||
objRet.message = "請輸入Line Display Name!";
|
||
|
||
json.WriteObject(context.Response.OutputStream, objRet);
|
||
}
|
||
|
||
lineUser.lineUser_uid = globalClass.CreateRandomCode(32);
|
||
lineUser.line_uid = line_uid;
|
||
lineUser.line_displayName = line_displayname;
|
||
|
||
conn.Insert<lineUser>(lineUser);
|
||
|
||
}
|
||
}
|
||
|
||
babyData newBaby;
|
||
|
||
if (babyData_uid == "")
|
||
{
|
||
babyData_uid = globalClass.CreateRandomCode(32);
|
||
|
||
newBaby = new babyData();
|
||
newBaby.babyData_uid = babyData_uid;
|
||
newBaby.babyData_birthday = DateTime.Parse(birthday + " 23:59:59");
|
||
newBaby.babyData_name = name;
|
||
newBaby.babyData_sexual = gender;
|
||
|
||
if (line_uid != "")
|
||
{
|
||
newBaby.line_uid = line_uid;
|
||
newBaby.babyData_bindedLine = "Y";
|
||
newBaby.babyData_bindeddate = DateTime.Now;
|
||
}
|
||
|
||
conn.Insert<babyData>(newBaby);
|
||
}
|
||
else {
|
||
newBaby = conn.QueryFirstOrDefault<babyData>("select * from babyData where babyData_uid = @babyData_uid", new { babyData_uid = babyData_uid });
|
||
|
||
if (newBaby == null) {
|
||
objRet.ret = "no";
|
||
objRet.err_code = "1008";
|
||
objRet.message = "無此babyData_uid的寶貝資料!";
|
||
|
||
json.WriteObject(context.Response.OutputStream, objRet);
|
||
}
|
||
|
||
babyData_uid = newBaby.babyData_uid;
|
||
}
|
||
|
||
|
||
|
||
babyRec newRec = new babyRec();
|
||
newRec.babyData_uid = babyData_uid;
|
||
newRec.line_uid = line_uid;
|
||
newRec.babyRec_key = babyRec_key;
|
||
newRec.babyRec_recdate = DateTime.Now;
|
||
newRec.babyRec_uid = "r_" + globalClass.CreateRandomCode(32);
|
||
newRec.babyRec_recYear = newRec.babyRec_recdate.Year;
|
||
newRec.babyRec_recMonth = newRec.babyRec_recdate.Month;
|
||
newRec.babyRec_recDay = newRec.babyRec_recdate.Day;
|
||
newRec.babyRec_height = double.Parse(height);
|
||
newRec.babyRec_months = globalClass.MonthDiff(newBaby.babyData_birthday, newRec.babyRec_recdate);
|
||
newRec.babyRec_monthLastRec = "Y";
|
||
newRec.babyRec_yearLastRec = "Y";
|
||
newRec.babyRec_dayLastRec = "Y";
|
||
newRec.babyRec_yearMonthStr = globalClass.YearMonthDiff(newBaby.babyData_birthday, newRec.babyRec_recdate);
|
||
newRec.utm_source = utm_source;
|
||
newRec.utm_medium = utm_medium;
|
||
newRec.utm_campaign = utm_campaign;
|
||
newRec.utm_term = utm_term;
|
||
newRec.utm_content = utm_content;
|
||
|
||
if (newRec.babyRec_months > 120) {
|
||
objRet.ret = "no";
|
||
objRet.err_code = "1006";
|
||
objRet.message = "寶貝年齡超過10歲,已過紀錄範圍!";
|
||
|
||
json.WriteObject(context.Response.OutputStream, objRet);
|
||
return;
|
||
}
|
||
|
||
|
||
|
||
lenHeiTable objLenHei = conn.QueryFirstOrDefault<lenHeiTable>("select * from lenHeiTable where lenHeiTable_sexual = @gender and lenHeiTable_month = @month and lenHeiTable_minVal <= @height1 and lenHeiTable_maxVal > @height2 ", new { gender = newBaby.babyData_sexual, month = newRec.babyRec_months, height1 = height, height2 = height});
|
||
lenHeiTable objMidHei = conn.QueryFirstOrDefault<lenHeiTable>("select * from lenHeiTable where lenHeiTable_sexual = @gender and lenHeiTable_month = @month and lenHeiTable_percent = 50", new { gender = newBaby.babyData_sexual, month=newRec.babyRec_months });
|
||
|
||
if (objLenHei == null) {
|
||
objRet.ret = "no";
|
||
objRet.err_code = "1007";
|
||
objRet.message = "無符合此身高年齡條件的數據!";
|
||
|
||
json.WriteObject(context.Response.OutputStream, objRet);
|
||
return;
|
||
}
|
||
|
||
newRec.babyRec_inpercent = objLenHei.lenHeiTable_percent;
|
||
newRec.babyRec_middleHeight = objMidHei.lenHeiTable_maxVal;
|
||
|
||
if (newRec.babyData_uid != "") {
|
||
conn.Execute("update babyRec set babyRec_yearLastRec = 'N' where babyData_uid = @babyData_uid and babyRec_recYear = @babyRec_recYear ", new { babyData_uid = babyData_uid, babyRec_recYear = newRec.babyRec_recYear });
|
||
conn.Execute("update babyRec set babyRec_monthLastRec = 'N' where babyData_uid = @babyData_uid and babyRec_recYear = @babyRec_recYear and babyRec_recMonth = @babyRec_recMonth", new { babyData_uid = babyData_uid, babyRec_recYear = newRec.babyRec_recYear, babyRec_recMonth = newRec.babyRec_recMonth });
|
||
conn.Execute("update babyRec set babyRec_dayLastRec = 'N' where babyData_uid = @babyData_uid and babyRec_recYear = @babyRec_recYear and babyRec_recMonth = @babyRec_recMonth and babyRec_recDay = @babyRec_recDay ", new { babyData_uid = babyData_uid, babyRec_recYear = newRec.babyRec_recYear, babyRec_recMonth = newRec.babyRec_recMonth, babyRec_recDay = newRec.babyRec_recDay });
|
||
}
|
||
|
||
conn.Insert<babyRec>(newRec);
|
||
|
||
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_createdate desc", new { babyData_uid = babyData_uid });
|
||
|
||
if(lastestBabyRec != null)
|
||
{
|
||
lastestBabyRec.babyRec_newestRec = "Y";
|
||
conn.Update<babyRec>(lastestBabyRec);
|
||
}
|
||
|
||
newBaby.babyData_lastTestDate = DateTime.Now;
|
||
|
||
conn.Update<babyData>(newBaby);
|
||
|
||
objRet.temp_key = babyRec_key;
|
||
objRet.baby_data.baby_birthday = newBaby.babyData_birthday.ToString("yyyy/MM/dd");
|
||
objRet.baby_data.baby_height = newRec.babyRec_height;
|
||
objRet.baby_data.baby_age = newRec.babyRec_yearMonthStr;
|
||
objRet.baby_data.average_height = newRec.babyRec_middleHeight;
|
||
objRet.baby_data.babyData_uid = newBaby.babyData_uid;
|
||
objRet.baby_data.baby_gender = newBaby.babyData_sexual;
|
||
objRet.baby_data.baby_inpercent = newRec.babyRec_inpercent;
|
||
|
||
objRet.ret = "yes";
|
||
|
||
json.WriteObject(context.Response.OutputStream, objRet);
|
||
|
||
}
|
||
|
||
public class result
|
||
{
|
||
public string ret = "no";
|
||
public string err_code = "0000";
|
||
public string message = "";
|
||
public string temp_key = "";
|
||
public string more_data = "N";
|
||
public baby baby_data = new baby();
|
||
}
|
||
|
||
public class baby
|
||
{
|
||
public string baby_birthday { get; set; } = "";
|
||
public string baby_age { get; set; } = "";
|
||
public double baby_height { get; set; } = 0.0;
|
||
public double average_height { get; set; } = 0.0;
|
||
public string babyData_uid { get; set; } = "";
|
||
|
||
public string baby_gender { get; set; } = "";
|
||
public double baby_inpercent { get; set; } = 0;
|
||
}
|
||
|
||
public bool IsReusable
|
||
{
|
||
get
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
} |