132 lines
5.7 KiB
C#
132 lines
5.7 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>
|
||
/// babyRecList 的摘要描述
|
||
/// </summary>
|
||
public class babyRecList : 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 line_uid = (context.Request["line_uid"] == null) ? "" : context.Request["line_uid"].ToString();
|
||
string babyData_uid = (context.Request["babyData_uid"] == null) ? "" : context.Request["babyData_uid"].ToString();
|
||
string start = (context.Request["start"] == null) ? "" : context.Request["start"].ToString();
|
||
string end = (context.Request["end"] == null) ? "" : context.Request["end"].ToString();
|
||
string type = (context.Request["type"] == null) ? "day" : context.Request["type"].ToString();
|
||
|
||
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;
|
||
}
|
||
|
||
babyData babyData = conn.QueryFirstOrDefault<babyData>("select * from babyData where line_uid = @line_uid and babyData_uid = @babyData_uid", new { line_uid = line_uid, babyData_uid = babyData_uid });
|
||
|
||
if (babyData == null)
|
||
{
|
||
objRet.ret = "no";
|
||
objRet.err_code = "3004";
|
||
objRet.message = "沒有此babyData資料!";
|
||
json.WriteObject(context.Response.OutputStream, objRet);
|
||
return;
|
||
}
|
||
DateTime startDate = DateTime.Now;
|
||
DateTime endDate = DateTime.Now;
|
||
|
||
try
|
||
{
|
||
startDate = DateTime.Parse(start + "/1");
|
||
endDate = DateTime.Parse(end + "/1");
|
||
endDate = endDate.AddMonths(1).AddDays(-1);
|
||
}
|
||
catch (Exception ex) {
|
||
objRet.ret = "no";
|
||
objRet.err_code = "4004";
|
||
objRet.message = "start 或 end資料格式錯誤,請使用 2025/3 此種格式代表2025年3月,不需要日期! " + ex.Message;
|
||
json.WriteObject(context.Response.OutputStream, objRet);
|
||
return;
|
||
}
|
||
|
||
if (type != "day" && type != "month") {
|
||
objRet.ret = "no";
|
||
objRet.err_code = "4005";
|
||
objRet.message = "type資料錯誤,只能為day或month";
|
||
json.WriteObject(context.Response.OutputStream, objRet);
|
||
return;
|
||
}
|
||
|
||
string sqlString = "";
|
||
List<babyRec> babyRecs = new List<babyRec>();
|
||
if (type == "day") {
|
||
babyRecs = conn.Query<babyRec>("select * from babyRec where babyRec_dayLastRec = 'Y' and line_uid = @line_uid and babyData_uid = @babyData_uid and babyRec_recdate >= @rec_start and babyRec_recdate <= @rec_end ", new { line_uid = line_uid, babyData_uid = babyData_uid, rec_start = startDate.ToString("yyyy/MM/dd"), rec_end = endDate.ToString("yyyy/MM/dd") }).ToList();
|
||
}
|
||
|
||
if (type == "month")
|
||
{
|
||
babyRecs = conn.Query<babyRec>("select * from babyRec where babyRec_dayLastRec = 'Y' and babyRec_monthLastRec = 'Y' and line_uid = @line_uid and babyData_uid = @babyData_uid and babyRec_recdate >= @rec_start and babyRec_recdate <= @rec_end ", new { line_uid = line_uid, babyData_uid = babyData_uid, rec_start = startDate.ToString("yyyy/MM/dd"), rec_end = endDate.ToString("yyyy/MM/dd") }).ToList();
|
||
}
|
||
|
||
foreach (babyRec rec in babyRecs)
|
||
{
|
||
height_data height_Data = new height_data();
|
||
height_Data.babyRec_height = rec.babyRec_height;
|
||
height_Data.babyRec_months = rec.babyRec_months;
|
||
height_Data.babyRec_yearMonthStr = rec.babyRec_yearMonthStr;
|
||
height_Data.babyRec_middleHeight = rec.babyRec_middleHeight;
|
||
height_Data.babyRec_inpercent = rec.babyRec_inpercent;
|
||
height_Data.babyRec_testdate = rec.babyRec_recdate.ToString("yyyy/MM/dd");
|
||
|
||
objRet.datas.Add(height_Data);
|
||
}
|
||
|
||
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 List<height_data> datas = new List<height_data>();
|
||
}
|
||
|
||
public class height_data
|
||
{
|
||
public double babyRec_height = 0;
|
||
public double babyRec_middleHeight = 0;
|
||
public string babyRec_yearMonthStr = "";
|
||
public int babyRec_months = 0;
|
||
public double babyRec_inpercent = 0;
|
||
public string babyRec_testdate = "";
|
||
}
|
||
|
||
public bool IsReusable
|
||
{
|
||
get
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
} |