using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using System.Data;
namespace ICSSoft.FromERP
{
///
/// 用户
///
public class ICSUser : IJob
{
private static object key = new object();
private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public void Execute(IJobExecutionContext context)
{
try
{
lock (key)
{
log.Info("开始……………………………………………………………………");
Execute();
log.Info("结束……………………………………………………………………");
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
public void Execute()
{
try
{
//string conERPStr = ICSHelper.GetERPConnectString();
string conStr = ICSHelper.GetConnectString();
string Namespace = this.GetType().Namespace;
//string Class = this.GetType().Name;
DataTable dt = ICSHelper.GetERPDB(conStr);
foreach (DataRow dr in dt.Rows)
{
string erpName = ICSHelper.GetConfigString()["ERPDB"];
string WorkPoint = dr["WorkPointCode"].ToString();
string Class = this.GetType().Name + WorkPoint;
erpName = string.Format(erpName, WorkPoint);
#region SQL
string sql = @"select aa.code AS UserCode,bb.name AS UserName,cc.Code AS DepCode,dd.Name AS DepName,
NEWID() AS ID,CASE WHEN aa.Effective_IsEffective='1' THEN '1' ELSE '0' END AS StartFlag,
aa.CreatedOn AS CreateDate,
aa.CreatedBy AS CreateUserCode,
aa.ModifiedBy AS ModifyUserCode,
aa.ModifiedOn AS ModifyDate,
GETDATE() AS MTIME
,ee.Code as F_Location
INTO #TempUser
from {1}.DBO.CBO_Operators aa with (nolock)
left join {1}.DBO.CBO_Operators_Trl bb with (nolock) on aa.id=bb.id AND bb.SysMLFlag='zh-CN'
left join {1}.DBO.cbo_department cc with (nolock) on aa.dept=cc.id
left join {1}.DBO.CBO_Department_Trl dd with (nolock) on cc.id=dd.id AND dd.SysMLFlag='zh-CN'
left join {1}.DBO.Base_Organization ee with (nolock) on aa.org=ee.id
left join {1}.DBO.Base_Organization_trl ff with (nolock) on ee.id=ff.ID AND ff.SysMLFlag='zh-CN'
where aa.ModifiedOn>=@LastTime AND ee.Code='{0}'
ORDER BY aa.code";
sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempUser");
sql += @"--删除数据
--DELETE Sys_SRM_User where F_Account NOT IN (SELECT cPsn_Num FROM {1}.dbo.[hr_hi_person])and Sys_SRM_User.F_Account<>'admin'
SELECT a.F_Account INTO #tableSys_SRM_User
FROM Sys_SRM_User a With(NoLock)
LEFT JOIN {1}.dbo.[CBO_Operators] b With(NoLock) ON a.F_Account=b.Code
WHERE a.F_Location ='{0}' and b.Code IS NULL and a.F_Account<>'admin'
DELETE Sys_SRM_User
WHERE F_Location='{0}' and F_Account IN ( SELECT F_Account from #tableSys_SRM_User)
";
Dictionary values = new Dictionary();
values.Add("F_RealName", "a.UserName");
values.Add("F_CreatorTime", "a.CreateDate");
values.Add("F_CreatorUserId", "a.CreateUserCode");
values.Add("F_LastModifyUserId", "a.ModifyUserCode");
values.Add("F_LastModifyTime", "a.ModifyDate");
values.Add("F_DepartmentId", "a.DepCode");
//values.Add("DepName", "a.DepName");
values.Add("F_EnabledMark", "a.StartFlag");
//更新存在数据
sql += ICSHelper.UpdateSQL("b", values)
+ @" #TempUser a
INNER JOIN Sys_SRM_User b ON a.UserCode=b.F_Account and a.F_Location=b.F_Location
WHERE a.F_Location='" + WorkPoint + "' ";
values.Add("F_ID", "a.ID");
values.Add("F_Account", "a.UserCode");
//values.Add("UserPwd", "'NjMDK5c7vIs='");
values.Add("F_Location", "'" + WorkPoint + "'");
//插入新增数据
sql += ICSHelper.InsertSQL("Sys_SRM_User", values)
+ @" #TempUser a
LEFT JOIN Sys_SRM_User b ON a.UserCode=b.F_Account and a.F_Location=b.F_Location
WHERE b.F_ID IS NULL and a.F_Location='" + WorkPoint + @"'
";
Dictionary value = new Dictionary();
value.Add("F_Id", "a.F_Id");
value.Add("F_UserId", "a.F_Id");
string UserSecretkey = SRMUser.Md5.md5(Common.CreateNo(), 16).ToLower();
string pwd = SRMUser.Md5.md5(ICSSoft.FromERP.SRMUser.DESEncrypt.Encrypt(SRMUser.Md5.md5("123456", 32).ToLower(), UserSecretkey).ToLower(), 32).ToLower();
value.Add("F_UserPassword", "'" + pwd + "'");
value.Add("F_UserSecretkey", "'" + UserSecretkey + "'");
//插入新增数据->Sys_SRM_UserLogOn表
sql += ICSHelper.InsertSQL("Sys_SRM_UserLogOn", value)
+ @"Sys_SRM_User a
LEFT JOIN Sys_SRM_UserLogOn b ON a.F_Id=b.F_Id and a.F_Id=b.F_UserId
WHERE b.F_ID IS NULL and a.F_Location='" + WorkPoint + @"'";
sql += " DROP TABLE #TempUser" +
" DROP TABLE #tableSys_SRM_User";
sql = string.Format(sql, WorkPoint, erpName);
ICSHelper.ExecuteDate(conStr, sql);
SRMUser.User();
#endregion
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
}
}