using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Utils.Datas;
|
|
using WeighBusiness.BO;
|
|
using WeighBusiness.Utils;
|
|
using WeighBusiness.Utils.SqlUtils;
|
|
|
|
namespace WeighBusiness.BL
|
|
{
|
|
public class SettingBL
|
|
{
|
|
private const string LoadAllSql = "select first 1 ID,A,B from {0} order by ID";
|
|
private const string LoadRrecentSql = "select first 1 ID,A from {0} order by ID";
|
|
public static SettingData Load(bool loadAll = true)//true:查询全部;false:只查询 ID与最近用户
|
|
{
|
|
var sql = loadAll ? LoadAllSql : LoadRrecentSql;
|
|
var querySql = sql.FormatWith(TableNames.配置表);
|
|
var table = SqlHelperEx.DoQuery(querySql);
|
|
if (table.Rows.Count == 0)
|
|
return new Setting() { RecentUser = "1|system" }.ToSettingData(loadAll);
|
|
|
|
return GetUseSetting(table.Rows[0], loadAll).ToSettingData(loadAll);
|
|
}
|
|
|
|
private static Setting GetUseSetting(DataRow row, bool loadAll)
|
|
{
|
|
var setting = new Setting() {
|
|
ID = DataTypeUtil.GetLongNum(row["ID"]),
|
|
RecentUser = row["A"].ToString(),
|
|
};
|
|
if (loadAll)
|
|
setting.Settings = row["B"].ToString();
|
|
return setting;
|
|
}
|
|
|
|
public static void Save(SettingData setting, bool saveAll)//插入时:全部插入;更新时:true:更新全部;false:只更新最近用户
|
|
{
|
|
var config = setting.ToSetting();
|
|
|
|
if (config.ID == 0)
|
|
Insert(config);
|
|
else
|
|
Update(config, saveAll);
|
|
}
|
|
|
|
public static void UpdateRecentUser(long recentUserID, string recentUserName)
|
|
{
|
|
var setting = Load(false);
|
|
setting.RecentUserID = recentUserID;
|
|
setting.RecentUserName = recentUserName;
|
|
var config = setting.ToSetting();
|
|
Update(config, false);
|
|
}
|
|
|
|
private static void Insert(Setting config)
|
|
{
|
|
var insertSql = InsertUtil.GetInsertSql(TableNames.配置表,
|
|
new string[] { "A", "B" },
|
|
new string[] { config.RecentUser, config.Settings });
|
|
|
|
bool success;
|
|
using (var she = new SqlHelperEx()) {
|
|
she.CreateTransaction();
|
|
she.ExecuteNonQuery(insertSql, out success);
|
|
if (!success)
|
|
she.Rollback();
|
|
else
|
|
she.Commit();
|
|
}
|
|
|
|
if (!success)//TODO:如何处理?
|
|
throw new ApplicationException("保存配置文件出错");
|
|
}
|
|
|
|
private static void Update(Setting config, bool updateAll = true)//true:更新所有数据;false;更新最近用户
|
|
{
|
|
var needUpdateFieldsValuesPairs = new Dictionary<string, string>();
|
|
needUpdateFieldsValuesPairs.Add("A", config.RecentUser);
|
|
if (updateAll)
|
|
needUpdateFieldsValuesPairs.Add("B", config.Settings);
|
|
var updateSql = UpdateUtil.GetUpdateString(TableNames.配置表, "ID", config.ID.ToString(), needUpdateFieldsValuesPairs);
|
|
|
|
bool success = true;
|
|
using (var she = new SqlHelperEx()) {
|
|
she.CreateTransaction();
|
|
she.ExecuteNonQuery(updateSql, out success);
|
|
if (success)
|
|
she.Commit();
|
|
else
|
|
she.Rollback();
|
|
}
|
|
|
|
if (!success)//TODO:如何处理?
|
|
throw new ApplicationException("保存配置文件出错");
|
|
}
|
|
|
|
}
|
|
}
|