SyncUserService.cs 5.29 KB
using Hh.Mes.Common.config;
using Hh.Mes.POJO.ApiEntity;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Text;
using Hh.Mes.Service.Repository;
using Hh.Mes.POJO.Entity;
using Hh.Mes.Common.Http;
using Hh.Mes.POJO.EnumEntitys;
using System.Net;
using Hh.Mes.Service.Logs;
using Hh.Mes.Common.Json;
using Hh.Mes.Common;

namespace Hh.Mes.Service.QuartzJobService
{
    public class SyncUserService:RepositorySqlSugar<sys_user>
    {
        /// <summary>
        /// 同步OA用户账号到 sys_user表
        /// </summary>
        public void Execute()
        { 
            //1、调用OA接口;
            var item = new HttpItem()
            {
                URL = ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend6,
                Method = "post",
                ContentType = "application/json",//返回类型    可选项有默认值
            };
            var result = new HttpHelper().GetHtml(item);
            var oa = EnumLog.OA人员接口.ToString();
            if (result.StatusCode != HttpStatusCode.OK)
            {
                QueueInterLog.GetInstance.EnqueueInterLog(null, null, oa, result.Html, 0, user: "SyncUserJob", sysTitle: oa);
                return;
            }

            var json = DynamicJson.Parse(result.Html);
            if (json.code != 200)
            {
                QueueInterLog.GetInstance.EnqueueInterLog(null, null, oa, json.msg, 0, user: "SyncUserJob", sysTitle: oa);
                return;
            }
            //2、返回结果,解密用户信息;
            var userInfo = Encryption.AesDecryptOA(json.data, "e9c4b6be35f74dca");
            if (string.IsNullOrEmpty(userInfo))
            {
                QueueInterLog.GetInstance.EnqueueInterLog(null, null, oa, "OA用户信息解密解析失败!", 0, user: "SyncUserJob", sysTitle: oa);
                return;
            }
            List<SysncUserOaEntity> entity = JsonConvert.DeserializeObject<List<SysncUserOaEntity>>(userInfo);
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(@" begin tran 
                           begin try");

            //3、新增IoT用户表,不存在新增,存在跳过;
            foreach (var e in entity)
            {
                for (int j = 0; j < e.memberInfoList.Count; j++)
                {
                    if (string.IsNullOrEmpty(e.memberInfoList[j].code)) continue;
                    sb.AppendLine(@$"
                                        IF NOT EXISTS (SELECT 1 FROM sys_user WHERE account = '{e.memberInfoList[j].code}')
                                        INSERT INTO [dbo].[sys_user]
                                                    ([account],
                                                    [password],
                                                    [name],

                                                    [sex],
                                                    [status],
                                                    [remarks],
                                                    [createTime],
                                                    [createBy])
                                             VALUES
                                             ('{e.memberInfoList[j].code}',
                                              '6EjfZkO8o92OjsH7rK+kVg==' ,
                                              '{e.memberInfoList[j].name}',

                                              1,1,'数据库任务定时作业同步OA账号',GETDATE(),'OA') ");
                }
            }

            //4、新增用户-部门关系表,不存在新增,存在跳过;默认部门:【长沙华恒机器人制造有限公司】
            sb.AppendLine($@" insert into sys_relevance (relKey,firstId,secondId,createBy,createTime)
                            select  UserOrg='UserOrg' , 
		                            t1.id ,
		                            secondId={ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend2},

		                            createBy=t1.createBy ,
	                                [createTime] = GETDATE()
                            from  sys_user t1 with (nolock)
                            left join  (select distinct firstId from sys_relevance) t2 on t1.id=t2.firstId
                            where   t1.createBy='OA' and t2.firstId  is null");

            //5、新增用户-角色关系表,~; 默认角色:【普通员工】
            sb.AppendLine($@" insert into sys_relevance (relKey,firstId,secondId,createBy,createTime)
                             select  UserRole='UserRole', 
		                            t1.id,
		                            secondId={ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend3},

		                            createBy=t1.createBy,
	                                [createTime] = GETDATE()
                            from  sys_user t1 with (nolock)
                             WHERE t1.createBy ='OA' AND id NOT IN (SELECT firstId FROM   sys_relevance WHERE  relKey='UserRole' )");

            sb.AppendLine(@"end try
                            begin catch 
	                            rollback tran 
                            end catch
                            commit tran");//提交事务
            
            var res = Context.Ado.ExecuteCommand(sb.ToString());
            
        }
    }
}