Journeys_WantHome/Models/kolFansTypeDetail.cs

132 lines
4.4 KiB
C#

using Dapper;
using Dapper.Contrib.Extensions;
using System.ComponentModel.DataAnnotations;
using System.Data.SqlClient;
using static DbTableClass;
public class kolFansTypeDetail : kolFansType
{
DbConn dbConn = new DbConn();
SqlConnection conn = new SqlConnection(GlobalClass.appsettings("ConnectionStrings:SQLConnectionString"));
public string option_name { get; set; } = "";
public string optionItem_name { get; set; } = "";
public string updateResult { get; set; } = "";
private kolFansType _kolFansType;
public kolFansTypeDetail() {
_kolFansType = new kolFansType();
}
public kolFansTypeDetail(string kolFansType_uid)
{
_kolFansType = conn.QueryFirstOrDefault<kolFansType>("select * from kolFansType where kolFansType_uid = @kolFansType_uid", new { kolFansType_uid = kolFansType_uid });
if (_kolFansType != null)
{
Type dataType = _kolFansType.GetType();
foreach (var prop in dataType.GetProperties())
{
string propName = prop.Name;
var valueProperty = dataType.GetProperty(propName);
object propValue = valueProperty.GetValue(_kolFansType, null);
this.GetType().GetProperty(propName).SetValue(this, propValue);
}
var result = conn.QueryFirstOrDefault("select A.option_name, B.optionItem_name from from option A, optionItem B where A.option_uid = B.option_uid and B.option_uid = @option_uid and B.optionItem_uid = @optionItem_uid ", new { option_uid = this.option_uid, optionItem_uid = this.optionItem_uid });
if (result != null)
{
this.option_name = result.option_name;
this.optionItem_name = result.optionItem_name;
}
}
else
{
_kolFansType = new kolFansType();
}
}
public kolFansTypeDetail(kolFansType fansType)
{
this._kolFansType = fansType;
Type dataType = _kolFansType.GetType();
foreach (var prop in dataType.GetProperties())
{
string propName = prop.Name;
var valueProperty = dataType.GetProperty(propName);
object propValue = valueProperty.GetValue(_kolFansType, null);
this.GetType().GetProperty(propName).SetValue(this, propValue);
}
var result = conn.QueryFirstOrDefault("select A.option_name, B.optionItem_name from from option A, optionItem B where A.option_uid = B.option_uid and B.option_uid = @option_uid and B.optionItem_uid = @optionItem_uid ", new { option_uid = this.option_uid, optionItem_uid = this.optionItem_uid });
if (result != null)
{
this.option_name = result.option_name;
this.optionItem_name = result.optionItem_name;
}
}
public bool dataUpdate()
{
if (this._kolFansType != null)
{
Type dataType = _kolFansType.GetType();
foreach (var prop in dataType.GetProperties())
{
string propName = prop.Name;
var valueProperty = dataType.GetProperty(propName);
object propValue = valueProperty.GetValue(this, null);
_kolFansType.GetType().GetProperty(propName).SetValue(_kolFansType, propValue);
}
if (this._kolFansType.kolFansType_uid == "")
{
this._kolFansType.kolFansType_uid = "KFT_" + GlobalClass.CreateRandomCode(16);
this.kolFansType_uid = this._kolFansType.kolFansType_uid;
try
{
conn.Insert(this._kolFansType);
updateResult = "success";
return true;
}
catch (Exception ex)
{
updateResult = ex.Message;
return false;
}
}
else
{
try
{
conn.Update(this._kolFansType);
updateResult = "success";
return true;
}
catch (Exception ex)
{
updateResult = ex.Message;
return false;
}
}
}
else
{
updateResult = "null 物件無法更新";
return false;
}
}
}