Journeys_WantHome/Models/kolTagDetail.cs

127 lines
3.6 KiB
C#

using Dapper;
using Dapper.Contrib.Extensions;
using System.Data.SqlClient;
using static DbTableClass;
public class kolTagDetail : kolTag
{
DbConn dbConn = new DbConn();
SqlConnection conn = new SqlConnection(GlobalClass.appsettings("ConnectionStrings:SQLConnectionString"));
public string tag_text { get; set; } = "";
kolTag _kolTag;
public string updateResult { get; set; } = "";
public kolTagDetail()
{
_kolTag = new kolTag();
}
public kolTagDetail(string kolTag_uid) {
_kolTag = conn.QueryFirstOrDefault<kolTag>("select * from kolTag where kolTag_uid = @kolTag_uid", new { kolTag_uid = kolTag_uid});
if (_kolTag != null)
{
Type dataType = _kolTag.GetType();
foreach (var prop in dataType.GetProperties())
{
string propName = prop.Name;
var valueProperty = dataType.GetProperty(propName);
object propValue = valueProperty.GetValue(_kolTag, null);
this.GetType().GetProperty(propName).SetValue(this, propValue);
}
var result = conn.QueryFirstOrDefault("select * from tags where tag_uid = @tag_uid ", new { tag_uid = tag_uid });
if (result != null)
{
this.tag_text = result.tag_text;
}
}
else
{
_kolTag = new kolTag();
}
}
public kolTagDetail(kolTag kolTag) {
this._kolTag = kolTag;
Type dataType = _kolTag.GetType();
foreach (var prop in dataType.GetProperties())
{
string propName = prop.Name;
var valueProperty = dataType.GetProperty(propName);
object propValue = valueProperty.GetValue(_kolTag, null);
this.GetType().GetProperty(propName).SetValue(this, propValue);
}
var result = conn.QueryFirstOrDefault("select * from tags where tag_uid = @tag_uid ", new { tag_uid = tag_uid });
if (result != null)
{
this.tag_text = result.tag_text;
}
}
public bool dataUpdate()
{
if (this._kolTag != null)
{
Type dataType = _kolTag.GetType();
foreach (var prop in dataType.GetProperties())
{
string propName = prop.Name;
var valueProperty = dataType.GetProperty(propName);
object propValue = valueProperty.GetValue(this, null);
_kolTag.GetType().GetProperty(propName).SetValue(_kolTag, propValue);
}
if (this._kolTag.kolTag_uid == "")
{
this._kolTag.kolTag_uid = "KTAG_" + GlobalClass.CreateRandomCode(16);
this.kolTag_uid = this._kolTag.kolTag_uid;
try
{
conn.Insert(this._kolTag);
updateResult = "success";
return true;
}
catch (Exception ex)
{
updateResult = ex.Message;
return false;
}
}
else
{
try
{
conn.Update(this._kolTag);
updateResult = "success";
return true;
}
catch (Exception ex)
{
updateResult = ex.Message;
return false;
}
}
}
else
{
updateResult = "null 物件無法更新";
return false;
}
}
}