using EasyBL.WebApi.Message;
using Entity.Sugar;
using SqlSugar;
using SqlSugar.Base;
using System;
namespace EasyBL.WEBAPP.WSM
{
public class Organization_UpdService : ServiceBase
{
#region 組織管理(單筆查詢)
///
/// 組織管理(單筆查詢)
///
///
///
public ResponseMessage QueryOne(RequestMessage i_crm)
{
ResponseMessage rm = null;
string sMsg = null;
var db = SugarBase.GetIntance();
try
{
do
{
var sOrgID = _fetchString(i_crm, @"OrgID");
var oEntity = db.Queryable
((t1, t2, t3) =>
new object[] {
JoinType.Left, t1.ParentOrgID == t2.OrgID && t1.CreateUser == t2.MemberID,
JoinType.Left, t1.ParentOrgID == t3.OrgID && t1.ModifyUser == t3.MemberID
}
)
.Where((t1, t2, t3) => t1.ParentOrgID == i_crm.ORIGID && t1.OrgID == sOrgID)
.Select((t1, t2, t3) => new OTB_SYS_Organization
{
OrgID = SqlFunc.GetSelfAndAutoFill(t1.OrgID),
CreateUserName = t2.MemberName,
ModifyUserName = t3.MemberName
})
.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(Organization_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 sOrgID = _fetchString(i_crm, @"OrgID");
var oEntity = _fetchEntity(i_crm);
_setEntityBase(oEntity, i_crm);
oEntity.OrgID = sOrgID;
oEntity.ParentOrgID = i_crm.ORIGID;
var iRel = db.Insertable(oEntity).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(Organization_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 sOrgID = _fetchString(i_crm, @"OrgID");
var oNewEntity = _fetchEntity(i_crm);
_setEntityBase(oNewEntity, i_crm);
oNewEntity.OrgID = sOrgID;
var iRel = db.Updateable(oNewEntity)
.IgnoreColumns(x => new
{
x.CreateUser,
x.CreateDate
}).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(Organization_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 sOrgID = _fetchString(i_crm, @"OrgID");
var oOrganization = db.Queryable().Single(x => x.ParentOrgID == i_crm.ORIGID && x.OrgID == sOrgID);
var iRel = db.Deleteable().Where(x => x.ParentOrgID == i_crm.ORIGID && x.OrgID == sOrgID).ExecuteCommand();
var fileService = new CommonService();
i_crm.DATA.Add("FileID", oOrganization.LoGoId);
i_crm.DATA.Add("IDType", "parent");
fileService.DelFile(i_crm);
i_crm.DATA["FileID"] = oOrganization.BackgroundImage;
fileService.DelFile(i_crm);
i_crm.DATA["FileID"] = oOrganization.WebsiteLgoId;
fileService.DelFile(i_crm);
i_crm.DATA["FileID"] = oOrganization.PicShowId;
fileService.DelFile(i_crm);
i_crm.DATA["FileID"] = oOrganization.WebsiteLgoId_CN;
fileService.DelFile(i_crm);
i_crm.DATA["FileID"] = oOrganization.PicShowId_CN;
fileService.DelFile(i_crm);
i_crm.DATA["FileID"] = oOrganization.WebsiteLgoId_EN;
fileService.DelFile(i_crm);
i_crm.DATA["FileID"] = oOrganization.PicShowId_EN;
fileService.DelFile(i_crm);
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(Organization_UpdService), @"組織管理", @"Delete(組織管理(刪除))", @"", @"", @"");
}
finally
{
if (null != sMsg)
{
rm = new ErrorResponseMessage(sMsg, i_crm);
}
}
return rm;
}
#endregion 組織管理(刪除)
#region 組織管理(查詢筆數)
///
/// 組織管理(查詢筆數)
///
///
///
public ResponseMessage QueryCout(RequestMessage i_crm)
{
ResponseMessage rm = null;
string sMsg = null;
var db = SugarBase.GetIntance();
try
{
do
{
var sOrgID = _fetchString(i_crm, @"OrgID");
var iCout = db.Queryable()
.WhereIF(!string.IsNullOrEmpty(sOrgID), x => x.ParentOrgID == i_crm.ORIGID && x.OrgID == sOrgID)
.Count();
rm = new SuccessResponseMessage(null, i_crm);
rm.DATA.Add(BLWording.REL, iCout);
} while (false);
}
catch (Exception ex)
{
sMsg = Util.GetLastExceptionMsg(ex);
LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Organization_UpdService), "", "QueryCout(組織管理(查詢筆數))", "", "", "");
}
finally
{
if (null != sMsg)
{
rm = new ErrorResponseMessage(sMsg, i_crm);
}
}
return rm;
}
#endregion 組織管理(查詢筆數)
}
}