472 lines
23 KiB

using CounsellorBL.BLStructure;
using CounsellorBL.Common;
using CounsellorBL.Helper;
using MonumentDefine;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OT.COM.ArsenalDB;
using OT.COM.SignalerMessage;
using SoldierData.EnterprizeV4;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using static CounsellorBL.ORD.ShoppingListService;
using Util = OT.COM.LogisticsUtil.Util;
namespace CounsellorBL.ORD
{
public class MessageLogService : SingleDataTableTemplate<tb_ord_message_log>
{
public override string MainTable => typeof(tb_ord_message_log).Name;
public MessageLogService()
{
dgReadCommandPostDataHandler = readCommandPostDataHandler;
dgDeleteCommandGenerator = deleteCommandGenerator;
dgUpdateCommandGenerator = updateCommandGenerator;
}
private class MessageLogMaster
{
public string uid { get; set; }
public string branch_uid { get; set; }
public string branch_name { get; set; }
public int sn { get; set; }
public string message { get; set; }
public int send_count { get; set; }
public int success_count { get; set; }
public int send_flag { get; set; }
public string create_date { get; set; }
public string create_user_name { get; set; }
public string group_name { get; set; }
public string received_count { get; set; }
}
protected string readCommandPostDataHandler(CRequestMessage i_crmInput, ArsenalInterface i_aiArsenal, Command i_cCmd, JArray i_jaData, tb_sys_session i_sSessionUser, out object o_c,
[System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
[System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
[System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
{
if (i_sSourcePath is null)
{
throw new ArgumentNullException(nameof(i_sSourcePath));
}
string sMsg;
Command cRes = null;
object oResultData = null;
try
{
do
{
sMsg = ScheduleRange.ParseLogStartEnd(tb_ord_message_log.CN_CREATE_DATE, i_jaData, out ScheduleRange sr);
if (sMsg != null)
{
break;
}
Dictionary<string, string> dicCondition = GetQueryMasterFirstQJEDicwherecols(i_crmInput);
var lsBranch = ProjectHelper.GetUserGroup(i_crmInput);
List<string> lsMainColumns = typeof(tb_ord_message_log).GetProperties()
.Where(x => x.Name != "PKNames" && x.Name != "Dirty" && x.Name != "SetNull")
.Select(x => x.Name).ToList();
QueryJsonElementCollection lBlocks = new QueryJsonElementCollection();
QueryJsonElement qjeA = lBlocks.GetInst<tb_ord_message_log>();
qjeA.displaycols = new List<string>() { tb_ord_message_log.CN_UID, tb_ord_message_log.CN_BRANCH_UID, tb_ord_message_log.CN_MESSAGE, tb_ord_message_log.CN_SEND_COUNT, tb_ord_message_log.CN_SUCCESS_COUNT, tb_ord_message_log.CN_SEND_FLAG, tb_ord_message_log.CN_CREATE_DATE };
qjeA.ordercols = new List<Tuple<QueryJsonElement, string, string>>() {
new Tuple<QueryJsonElement, string, string>(qjeA, tb_ord_message_log.CN_CREATE_DATE, BLWording.ORDER_DESC) };
List<WhereNode> lwnMainInfo = new List<WhereNode>();
if (dicCondition.ContainsKey("create_date_start"))
{
bool isDateTime = DateTime.TryParse(dicCondition["create_date_start"], out DateTime create_date_start);
string create_date_start_text = isDateTime ? create_date_start.ToString("yyyy-MM-dd") : null;
lwnMainInfo.Add(new WhereNode(tb_ord_message_log.CN_CREATE_DATE, WhereNode.EColumnOperation.EOT_GTEQ, typeof(tb_ord_message_log), create_date_start_text));
dicCondition.Remove("create_date_start");
}
if (dicCondition.ContainsKey("create_date_end"))
{
bool isDateTime = DateTime.TryParse(dicCondition["create_date_end"], out DateTime create_date_end);
string create_date_end_text = isDateTime ? create_date_end.AddDays(1).ToString("yyyy-MM-dd") : null;
lwnMainInfo.Add(new WhereNode(tb_ord_message_log.CN_CREATE_DATE, WhereNode.EColumnOperation.EOT_LTEQ, typeof(tb_ord_message_log), create_date_end_text));
dicCondition.Remove("create_date_end");
}
if (dicCondition.ContainsKey(tb_ord_message_log.CN_BRANCH_UID))
{
lwnMainInfo.Add(new WhereNode(tb_ord_message_log.CN_BRANCH_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_ord_message_log), dicCondition[tb_ord_message_log.CN_BRANCH_UID]));
dicCondition.Remove(tb_ord_message_log.CN_BRANCH_UID);
}
//if (dicCondition.ContainsKey(tb_ord_message_log.CN_MESSAGE))
//{
// lwnMainInfo.Add(new WhereNode(tb_ord_message_log.CN_MESSAGE, WhereNode.EColumnOperation.EOT_LIKE, typeof(tb_ord_message_log), dicCondition[tb_ord_message_log.CN_MESSAGE]));
// dicCondition.Remove(tb_ord_message_log.CN_MESSAGE);
//}
lwnMainInfo.Add(new WhereNode(tb_ord_message_log.CN_STATUS_FLAG, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_ord_message_log), "1"));
qjeA.wherecols = lwnMainInfo.Count == 1 ? lwnMainInfo.Single() : new WhereNode(WhereNode.ENodeOperation.ENO_AND, lwnMainInfo.ToArray());
lBlocks.Add(qjeA);
QueryJsonElement qjeC = lBlocks.GetInst<tb_grp_branch>();
qjeC.aliascols = new Dictionary<string, List<string>>() {
{ tb_grp_branch.CN_BRANCH_NAME, new List<string>(){ "branch_name" } }
};
qjeC.jointype = QueryJsonElement.JOIN;
qjeC.jointable = qjeA;
qjeC.joincols = new Dictionary<string, string>() {
{tb_grp_branch.CN_UID, tb_ord_message_log.CN_BRANCH_UID }
};
lBlocks.Add(qjeC);
QueryJsonElement qjeD = lBlocks.GetInst<tb_hr_employee>();
qjeD.aliascols = new Dictionary<string, List<string>>() {
{ tb_hr_employee.CN_NAME, new List<string>(){ "create_user_name" } }
};
qjeD.jointype = QueryJsonElement.JOIN;
qjeD.jointable = qjeA;
qjeD.joincols = new Dictionary<string, string>() {
{tb_hr_employee.CN_UID, tb_ord_message_log.CN_CREATE_USER_UID }
};
lBlocks.Add(qjeD);
QueryJsonElement qjeE = lBlocks.GetInst<tb_grp_group>();
qjeE.aliascols = new Dictionary<string, List<string>>() {
{ tb_grp_group.CN_NAME, new List<string>(){ "group_name" } }
};
qjeE.jointype = QueryJsonElement.JOIN;
qjeE.jointable = qjeC;
qjeE.joincols = new Dictionary<string, string>() {
{ tb_grp_group.CN_UID, tb_grp_branch.CN_GROUP_UID }
};
lwnMainInfo = new List<WhereNode>();
lwnMainInfo.Add(new WhereNode(tb_grp_group.CN_UID, WhereNode.EColumnOperation.EOT_IN, typeof(tb_grp_group), lsBranch.ToArray()));
if (dicCondition.ContainsKey("group_uid"))
{
lwnMainInfo.Add(new WhereNode(tb_grp_group.CN_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_ord_message_log), dicCondition["group_uid"]));
}
qjeE.wherecols = lwnMainInfo.Count == 1 ? lwnMainInfo.Single() : new WhereNode(WhereNode.ENodeOperation.ENO_AND, lwnMainInfo.ToArray());
lBlocks.Add(qjeE);
sMsg = MakeSelectJoinByBlocks(lBlocks, out cRes);
ArsenalInterface ai = ArsenalDBMgr.GetInst(cRes);
QueryDataSet qds = ai.RunQueryDataSet(cRes);
var res = new List<MessageLogMaster>();
int sn = 1;
foreach (DataRow data in qds.DATA.Tables[0].Rows)
{
var model = new MessageLogMaster()
{
uid = data[tb_ord_message_log.CN_UID].ToString(),
sn = sn++,
group_name = data["group_name"].ToString() + "(" + data[tb_grp_branch.CN_BRANCH_NAME]?.ToString() + ")",
branch_uid = data[tb_ord_message_log.CN_BRANCH_UID]?.ToString(),
branch_name = data[tb_grp_branch.CN_BRANCH_NAME]?.ToString(),
create_user_name = data["create_user_name"].ToString(),
message = data[tb_ord_message_log.CN_MESSAGE].ToString(),
send_count = (int)data[tb_ord_message_log.CN_SEND_COUNT],
success_count = (int)data[tb_ord_message_log.CN_SUCCESS_COUNT],
send_flag = (int)data[tb_ord_message_log.CN_SEND_FLAG],
create_date = Convert.ToDateTime(data[tb_ord_message_log.CN_CREATE_DATE]).ToString("yyyy-MM-ddTHH:mm:ss"),
//create_date = data.Field<DateTime?>(tb_ord_message_log.CN_CREATE_DATE),
received_count = data[tb_ord_message_log.CN_SEND_COUNT].ToString() + "/" + data[tb_ord_message_log.CN_SUCCESS_COUNT].ToString()
};
res.Add(model);
}
oResultData = res;
}
while (false);
}
catch (Exception ex)
{
LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
sMsg = $"{nameof(readCommandPostDataHandler)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
#if DEBUG
System.Diagnostics.Debug.WriteLine(sMsg);
#endif
}
o_c = oResultData;
return sMsg;
}
protected string deleteCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out List<Command> o_lResult, List<string> i_saQryContainKeys,
[System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
[System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
[System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
{
string sMsg = null;
List<Command> lCmds = new List<Command>();
try
{
do
{
string message_log_ID = null;
foreach (JToken joData in i_jaData)
{
Dictionary<string, object> dicData = joData.ToObject<Dictionary<string, object>>();
//取得user_uid
if (dicData.ContainsKey(BLWording.WHEREDATA))
{
JObject wheredata = dicData[BLWording.WHEREDATA] as JObject;
Dictionary<string, object> wheredataDic = wheredata.ToObject<Dictionary<string, object>>();
if (wheredataDic.ContainsKey(BLWording.UID))
{
message_log_ID = wheredataDic[BLWording.UID].ToString();
}
}
//sMsg = valueAssignment(i_crmInput, u, dicData[BLWording.DATA] as JObject, out List<Command> lcAddInsert, i_bIsCreate: false);
//if (sMsg != null)
//{
// break;
//}
tb_ord_message_log cUpMessage = new tb_ord_message_log() { uid = message_log_ID };
tb_ord_message_log cNewMessage = new tb_ord_message_log();
cNewMessage.status_flag = 0;
lCmds.Add(Command.SetupUpdateCmd(cNewMessage, cUpMessage));
}
if (sMsg != null)
{
break;
}
}
while (false);
}
catch (Exception ex)
{
LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
sMsg = $"{nameof(deleteCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
#if DEBUG
System.Diagnostics.Debug.WriteLine(sMsg);
#endif
}
o_lResult = lCmds;
return sMsg;
}
protected string updateCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out List<Command> o_lResult, List<string> i_saQryContainKeys,
[System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
[System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
[System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
{
string sMsg = null;
string uid = null;
string create_date;
List<Command> lCmds = new List<Command>();
try
{
do
{
QueryJsonElementCollection lBlocks = new QueryJsonElementCollection();
QueryJsonElement qjeA = lBlocks.GetInst<tb_ord_message_log>();
qjeA.displaycols = new List<string>() { tb_ord_message_log.CN_UID, tb_ord_message_log.CN_BRANCH_UID, tb_ord_message_log.CN_MESSAGE, tb_ord_message_log.CN_SEND_COUNT, tb_ord_message_log.CN_SUCCESS_COUNT, tb_ord_message_log.CN_SEND_FLAG, tb_ord_message_log.CN_CREATE_DATE };
qjeA.ordercols = new List<Tuple<QueryJsonElement, string, string>>() {
new Tuple<QueryJsonElement, string, string>(qjeA, tb_ord_message_log.CN_CREATE_DATE, BLWording.ORDER_ASC) };
qjeA.wherecols = new WhereNode(tb_ord_message_log.CN_SUCCESS_COUNT, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_ord_message_log), 0);
lBlocks.Add(qjeA);
sMsg = MakeSelectJoinByBlocks(lBlocks, out Command cRes);
ArsenalInterface ai = ArsenalDBMgr.GetInst(cRes);
var qds = ai.RunQueryList<MessageLogMaster>(cRes);
create_date = qds.FirstOrDefault()?.create_date != null ? qds.FirstOrDefault()?.create_date : DateTime.Now.ToString();
List<string> data = new List<string>();
foreach (JToken jtk in i_jaData)
{
Dictionary<string, object> dicData = jtk.ToObject<Dictionary<string, object>>();
// 取得私訊UID
if (dicData.ContainsKey(BLWording.WHEREDATA))
{
JObject wheredata = dicData[BLWording.WHEREDATA] as JObject;
Dictionary<string, object> wheredataDic = wheredata.ToObject<Dictionary<string, object>>();
if (wheredataDic.ContainsKey(BLWording.UID))
{
uid = wheredataDic[BLWording.UID].ToString();
}
}
Command c = Command.SetupUpdateCmd(
new tb_ord_message_log()
{
create_date = DateTime.Parse(create_date),
},
new tb_ord_message_log() { uid = uid });
lCmds.Add(c);
}
}
while (false);
}
catch (Exception ex)
{
LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
sMsg = $"{nameof(updateCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
#if DEBUG
System.Diagnostics.Debug.WriteLine(sMsg);
#endif
}
o_lResult = lCmds;
return string.IsNullOrWhiteSpace(sMsg) ? null : sMsg;
}
[CounsellorBL.Common.Auth(false)]
public CResponseMessage QueryNoticeSchedule(CRequestMessage i_crmInput)
{
string sMsg;
Command cRes = null;
CResponseMessage crmRes = new CResponseMessage();
try
{
do
{
// 查詢條件
Dictionary<string, string> dicCondition = GetQueryMasterFirstQJEDicwherecols(i_crmInput); // 取得condition
QueryJsonElementCollection lBlocks = new QueryJsonElementCollection();
QueryJsonElement qjeA = lBlocks.GetInst();
qjeA.table = tb_ord_message_log_record.TABLENAME;
qjeA.displaycols = new List<string>() {
tb_ord_message_log_record.CN_UID,
tb_ord_message_log_record.CN_MESSAGE,
tb_ord_message_log_record.CN_IS_READ,
tb_ord_message_log_record.CN_STATUS_FLAG_APP
};
lBlocks.Add(qjeA);
QueryJsonElement qjeB = lBlocks.GetInst();
qjeB.table = tb_meb_member.TABLENAME;
qjeB.jointype = QueryJsonElement.JOIN;
qjeB.jointable = qjeA;
qjeB.displaycols = new List<string>() {
tb_meb_member.CN_MESSAGE_USER_ID,
tb_meb_member.CN_GROUP_ID
};
qjeB.joincols = new Dictionary<string, string>() {
{ tb_meb_member.CN_UID,tb_ord_message_log_record.CN_MEMBER_UID }};
qjeB.dicwherecols = FilterQueryDictionary<tb_meb_member>(dicCondition);
lBlocks.Add(qjeB);
QueryJsonElement qjeC = lBlocks.GetInst();
qjeC.table = tb_ord_message_log.TABLENAME;
qjeC.jointype = QueryJsonElement.JOIN;
qjeC.jointable = qjeA;
qjeC.joincols = new Dictionary<string, string>() {
{ tb_ord_message_log.CN_UID,tb_ord_message_log_record.CN_LOG_UID }};
qjeC.displaycols = new List<string>() { tb_ord_message_log.CN_CREATE_DATE };
qjeC.wherecols = new WhereNode(tb_ord_message_log.CN_CREATE_DATE, WhereNode.EColumnOperation.EOT_GT, typeof(tb_ord_message_log), DateTime.Now.AddMonths(-3).ToString("yyyy-MM-dd"));// 找三個月前的資料
lBlocks.Add(qjeC);
QueryJsonElement qjeD = lBlocks.GetInst();
qjeD.table = tb_grp_group.TABLENAME;
qjeD.jointype = QueryJsonElement.JOIN;
qjeD.jointable = qjeB;
qjeD.joincols = new Dictionary<string, string>() {
{ tb_grp_group.CN_FB_GROUP_ID, tb_meb_member.CN_GROUP_ID }};
//qjeD.displaycols = new List<string>() { tb_grp_group.CN_UID };
lBlocks.Add(qjeD);
sMsg = MakeSelectJoinByBlocks(lBlocks, out cRes);
if (sMsg != null)
{
break;
}
ArsenalInterface ai = ArsenalDBMgr.GetInst(cRes);
List<SendNoticeQueryEntity> qds = ai.RunQueryList<SendNoticeQueryEntity>(cRes);
if (!cRes.IsSuccess)
{
sMsg = cRes.LastErrorCode;
break;
}
crmRes = new CSuccessResponseMessage(null, i_crmInput);
crmRes.param.Add(BLWording.DATA, qds);
}
while (false);
}
catch (Exception ex)
{
Logger.Error(Util.GetLastExceptionMsg(ex));
LogHelper.DBLog(Util.GetLastExceptionMsg(ex));
sMsg = $"{nameof(QueryNoticeSchedule)} unknwon exception. ";
Logger.Error($"{nameof(QueryNoticeSchedule)} function error: {ex.ToString()}");
#if DEBUG
System.Diagnostics.Debug.WriteLine(sMsg);
#endif
}
if (null != sMsg)
{
crmRes = new CErrorResponseMessage(sMsg);
}
return crmRes;
}
public SystemColumnInfo GeCustomSystemColumnInfo()
{
string sUID;
string sOrgID = null;
SystemColumnInfo scRes = new SystemColumnInfo(
(Dictionary<string, object> i_dicInsertData, Dictionary<string, object> i_dicUpdateData) =>
{
i_dicInsertData[tb_sys_dblock.CN_UID] = Guid.NewGuid().ToString().ToUpper(CultureInfo.CurrentCulture);
i_dicInsertData[tb_sys_dblock.CN_UPDATE_DATE] =
i_dicInsertData[tb_sys_dblock.CN_CREATE_DATE] =
i_dicUpdateData[tb_sys_dblock.CN_UPDATE_DATE] = DateTime.Now;
}
);
sUID = "594e6c14-c589-435b-a69f-cca9ce2ba2af";
sOrgID = "001";
DateTime dtNow = DateTime.Now;
scRes.InsertData = new Dictionary<string, object>()
{
{ BLWording.CREATE_ORG_UID, sOrgID},
{ BLWording.CREATE_USER_UID, sUID},
{ BLWording.CREATE_DATE, dtNow},
{ BLWording.UPDATE_ORG_UID, sOrgID},
{ BLWording.UPDATE_USER_UID, sUID},
{ BLWording.UPDATE_DATE, dtNow}
};
scRes.UpdateData = new Dictionary<string, object>()
{
{ BLWording.UPDATE_ORG_UID, sOrgID},
{ BLWording.UPDATE_USER_UID, sUID},
{ BLWording.UPDATE_DATE, dtNow}
};
return scRes;
}
public class SendNoticeQueryEntity
{
public string uid { get; set; }
public string message { get; set; }
public DateTime create_date { get; set; }
public int is_read { get; set; }
}
}
}