using EasyBL.WebApi.Message;
using Entity.Sugar;
using Newtonsoft.Json.Linq;
using SqlSugar;
using SqlSugar.Base;
using System;
using System.Collections.Generic;
namespace EasyBL.WEBAPP.WSM
{
public class RoleMaintain_UpdService : ServiceBase
{
#region 角色管理(單筆查詢)
///
/// 角色管理(單筆查詢)
///
///
///
public ResponseMessage QueryOne(RequestMessage i_crm)
{
ResponseMessage rm = null;
string sMsg = null;
var db = SugarBase.GetIntance();
try
{
do
{
var sRuleID = _fetchString(i_crm, @"RuleID");
var oEntity = db.Queryable
((t1, t2, t3) =>
new object[] {
JoinType.Left, t1.OrgID == t2.OrgID && t1.CreateUser == t2.MemberID,
JoinType.Left, t1.OrgID == t3.OrgID && t1.ModifyUser == t3.MemberID
}
)
.Where((t1, t2, t3) => t1.OrgID == i_crm.ORIGID && t1.RuleID == sRuleID)
.Select((t1, t2, t3) => new OTB_SYS_Rules
{
RuleID = SqlFunc.GetSelfAndAutoFill(t1.RuleID),
CreateUserName = t2.MemberName,
ModifyUserName = t3.MemberName,
ExFeild1 = SqlFunc.MappingColumn(t1.RuleID, "(SELECT b.MemberID+',' FROM OTB_SYS_MembersToRule b left join OTB_SYS_Members c on b.MemberID=c.MemberID and b.OrgID=c.OrgID WHERE c.Effective='Y' and c.OrgID=t1.OrgID and b.OrgID=t1.OrgID and b.RuleID=t1.RuleID FOR XML PATH(''))")
})
.Single();
rm = new SuccessResponseMessage(null, i_crm);
rm.DATA.Add(BLWording.REL, oEntity);
} while (false);
}
catch (Exception ex)
{
sMsg = Util.GetLastExceptionMsg(ex);
LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(RoleMaintain_UpdService), "", "QueryOne(角色管理(單筆查詢))", "", "", "");
}
finally
{
if (null != sMsg)
{
rm = new ErrorResponseMessage(sMsg, i_crm);
}
}
return rm;
}
#endregion 角色管理(單筆查詢)
#region 角色管理(新增)
///
/// 角色管理(新增)
///
/// todo: describe i_crm parameter on UpdImportCustomers
///
public ResponseMessage Insert(RequestMessage i_crm)
{
ResponseMessage rm = null;
string sMsg = null;
try
{
rm = SugarBase.ExecTran(db =>
{
do
{
var oEntity = _fetchEntity(i_crm);
_setEntityBase(oEntity, i_crm);
oEntity.DelStatus = "N";
var oEntity_Del = db.Queryable()
.Single(x => x.OrgID == i_crm.ORIGID && x.RuleID == oEntity.RuleID && x.DelStatus == "Y");
var iRel = oEntity_Del != null ? db.Updateable(oEntity)
.IgnoreColumns(x => new
{
x.CreateUser,
x.CreateDate
}).ExecuteCommand() : db.Insertable(oEntity).ExecuteCommand();
var saRuleInsert = new List();
//更新角色
if (i_crm.DATA["users"] is JArray saUsers && saUsers.Count > 0)
{
foreach (var userid in saUsers)
{
var oMembersToRule = new OTB_SYS_MembersToRule
{
RuleID = oEntity.RuleID,
MemberID = userid.ToString()
};
_setEntityBase(oMembersToRule, i_crm);
saRuleInsert.Add(oMembersToRule);
}
iRel += db.Insertable(saRuleInsert).ExecuteCommand();
}
rm = new SuccessResponseMessage(null, i_crm);
rm.DATA.Add(BLWording.REL, iRel);
} while (false);
return rm;
});
}
catch (Exception ex)
{
sMsg = Util.GetLastExceptionMsg(ex);
LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(RoleMaintain_UpdService), @"角色管理", @"Add(角色管理(新增))", @"", @"", @"");
}
finally
{
if (null != sMsg)
{
rm = new ErrorResponseMessage(sMsg, i_crm);
}
}
return rm;
}
#endregion 角色管理(新增)
#region 角色管理(修改)
///
/// 角色管理(修改)
///
/// todo: describe i_crm parameter on UpdImportCustomers
///
public ResponseMessage Update(RequestMessage i_crm)
{
ResponseMessage rm = null;
string sMsg = null;
try
{
rm = SugarBase.ExecTran(db =>
{
do
{
var oNewEntity = _fetchEntity(i_crm);
_setEntityBase(oNewEntity, i_crm);
var iRel = db.Updateable(oNewEntity)
.IgnoreColumns(x => new
{
x.DelStatus,
x.CreateUser,
x.CreateDate
}).ExecuteCommand();
iRel += db.Deleteable().Where(x => x.OrgID == i_crm.ORIGID && x.RuleID == oNewEntity.RuleID).ExecuteCommand();
var saRuleInsert = new List();
//更新角色
if (i_crm.DATA["users"] is JArray saUsers && saUsers.Count > 0)
{
foreach (var userid in saUsers)
{
var oMembersToRule = new OTB_SYS_MembersToRule
{
RuleID = oNewEntity.RuleID,
MemberID = userid.ToString()
};
_setEntityBase(oMembersToRule, i_crm);
saRuleInsert.Add(oMembersToRule);
}
iRel += db.Insertable(saRuleInsert).ExecuteCommand();
}
rm = new SuccessResponseMessage(null, i_crm);
rm.DATA.Add(BLWording.REL, iRel);
} while (false);
return rm;
});
}
catch (Exception ex)
{
sMsg = Util.GetLastExceptionMsg(ex);
LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(RoleMaintain_UpdService), @"角色管理", @"Update(角色管理(修改))", @"", @"", @"");
}
finally
{
if (null != sMsg)
{
rm = new ErrorResponseMessage(sMsg, i_crm);
}
}
return rm;
}
#endregion 角色管理(修改)
#region 角色管理(刪除)
///
/// 角色管理(刪除)
///
/// todo: describe i_crm parameter on UpdImportCustomers
///
public ResponseMessage Delete(RequestMessage i_crm)
{
ResponseMessage rm = null;
string sMsg = null;
try
{
rm = SugarBase.ExecTran(db =>
{
do
{
var sRuleID = _fetchString(i_crm, @"RuleID");
var iRel = db.Updateable()
.UpdateColumns(x => new OTB_SYS_Rules { DelStatus = "Y" })
.Where(x => x.OrgID == i_crm.ORIGID && x.RuleID == sRuleID).ExecuteCommand();
iRel += db.Deleteable().Where(x => x.OrgID == i_crm.ORIGID && x.RuleID == sRuleID).ExecuteCommand();
rm = new SuccessResponseMessage(null, i_crm);
rm.DATA.Add(BLWording.REL, iRel);
} while (false);
return rm;
});
}
catch (Exception ex)
{
sMsg = Util.GetLastExceptionMsg(ex);
LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(RoleMaintain_UpdService), @"角色管理", @"Delete(角色管理(刪除))", @"", @"", @"");
}
finally
{
if (null != sMsg)
{
rm = new ErrorResponseMessage(sMsg, i_crm);
}
}
return rm;
}
#endregion 角色管理(刪除)
}
}