AuthController.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. 
  2. using Microsoft.AspNetCore.SignalR;
  3. using Microsoft.EntityFrameworkCore.Metadata.Internal;
  4. using NPOI.SS.Formula.Functions;
  5. using OASystem.API.OAMethodLib;
  6. using OASystem.API.OAMethodLib.Hub.HubClients;
  7. using OASystem.API.OAMethodLib.Hub.Hubs;
  8. using OASystem.API.OAMethodLib.QiYeWeChatAPI;
  9. using OASystem.Domain.AesEncryption;
  10. using OASystem.Domain.Attributes;
  11. using OASystem.Domain.Dtos.UserDto;
  12. using OASystem.Domain.Entities.Customer;
  13. using OASystem.Domain.Entities.Groups;
  14. using OASystem.Infrastructure.Repositories.Login;
  15. using System.IdentityModel.Tokens.Jwt;
  16. using static OASystem.API.OAMethodLib.JWTHelper;
  17. namespace OASystem.API.Controllers
  18. {
  19. /// <summary>
  20. /// 鉴权相关
  21. /// </summary>
  22. [Route("api/")]
  23. public class AuthController : ControllerBase
  24. {
  25. private readonly IMapper _mapper;
  26. private readonly IConfiguration _config;
  27. private readonly LoginRepository _loginRep;
  28. private readonly MessageRepository _message;
  29. private readonly SystemMenuPermissionRepository _sysMenuPermRep;
  30. private readonly MessageRepository _messageRep;
  31. private readonly IQiYeWeChatApiService _qiYeWeChatApiServic;
  32. private readonly IHubContext<ChatHub, IChatClient> _hubContext;
  33. private readonly DeviceTokenRepository _deviceTokenRep;
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. /// <param name="config"></param>
  38. /// <param name="loginRep"></param>
  39. /// <param name="mapper"></param>
  40. /// <param name="message"></param>
  41. /// <param name="sysMenuPermRep"></param>
  42. /// <param name="qiYeWeChatApiService"></param>
  43. /// <param name="messageRep"></param>
  44. /// <param name="deviceRep"></param>
  45. /// <param name="hubContext"></param>
  46. public AuthController(
  47. IConfiguration config,
  48. LoginRepository loginRep,
  49. IMapper mapper,
  50. MessageRepository message,
  51. SystemMenuPermissionRepository sysMenuPermRep,
  52. IQiYeWeChatApiService qiYeWeChatApiService,
  53. MessageRepository messageRep,
  54. DeviceTokenRepository deviceRep,
  55. IHubContext<ChatHub,
  56. IChatClient> hubContext)
  57. {
  58. _config = config;
  59. _loginRep = loginRep;
  60. _mapper = mapper;
  61. _message = message;
  62. _sysMenuPermRep = sysMenuPermRep;
  63. _qiYeWeChatApiServic = qiYeWeChatApiService;
  64. _messageRep = messageRep;
  65. _deviceTokenRep = deviceRep;
  66. _hubContext = hubContext;
  67. }
  68. /// <summary>
  69. /// 用户登录
  70. /// </summary>
  71. /// <param name="dto"></param>
  72. /// <returns></returns>
  73. [Route("login")]
  74. [HttpPost]
  75. [ApiLog("Login", OperationEnum.Login)]
  76. [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
  77. public async Task<IActionResult> LoginAsync(LoginDto dto)
  78. {
  79. if (string.IsNullOrWhiteSpace(dto.Number) || string.IsNullOrWhiteSpace(dto.Password))
  80. {
  81. return Ok(JsonView(false, "账号或密码不能为空!!"));
  82. }
  83. #region 校验用户信息
  84. var userData = _loginRep.Login(dto).Result;
  85. if (userData.Code != 0) return Ok(JsonView(false, userData.Msg));
  86. #endregion
  87. #region 限制销售部门 除gyy外可登录
  88. var userInfo = userData.Data as UserLoginInfoView;
  89. if (userInfo == null) return Ok(JsonView(false, userData.Msg));
  90. if (userInfo.DepName.Contains("市场部"))
  91. {
  92. var noLoginAuth = _config.GetSection("NoLoginAuth").Get<List<string>>();
  93. if (noLoginAuth.Any())
  94. {
  95. if (noLoginAuth.Contains(userInfo.CnName)) return Ok(JsonView(false, "NO ACCESS!!"));
  96. }
  97. //其他市场部人员 限制登录时间段
  98. var currentDateTime = DateTime.Now;
  99. var startTime = DateTime.Parse(_config["ApiAccessTime:StartTime"]);
  100. var endTime = DateTime.Parse(_config["ApiAccessTime:EndTime"]);
  101. if (currentDateTime < startTime && currentDateTime > endTime) return Ok(JsonView(false, "NO ACCESS!!"));
  102. }
  103. #endregion
  104. Result authData = null;
  105. string uName = string.Empty,
  106. role = string.Empty,
  107. depName = string.Empty;
  108. int uId = 0;
  109. int unReadCount = 0;
  110. int announcementUnReadCount = 0;
  111. if (userData.Data != null)
  112. {
  113. uId = (userData.Data as UserLoginInfoView).UserId;
  114. uName = (userData.Data as UserLoginInfoView).CnName;
  115. depName = (userData.Data as UserLoginInfoView).DepName;
  116. role = (userData.Data as UserLoginInfoView).JobName;
  117. authData = _sysMenuPermRep.QueryMenuLoad(uId, dto.PortType);
  118. unReadCount = await _messageRep.GetUnReadCount(uId);
  119. announcementUnReadCount = await _messageRep.GetAnnouncementUnReadCount(uId);
  120. }
  121. //_hubContext.Login(uId, uName);
  122. var view = new LoginView
  123. {
  124. UserInfo = userData?.Data,
  125. AuthData = authData?.Data,
  126. UnReadCount = unReadCount,
  127. AnnouncementUnReadCount = announcementUnReadCount
  128. };
  129. DateTime createZebraTime = DateTime.Now;
  130. string authorId = dto.Number + "Token";
  131. string authorToken = await RedisRepository.RedisFactory.CreateRedisRepository().StringGetAsync<string>(authorId);//string 取
  132. if (authorToken != null)
  133. {
  134. #region 解析出过期时间
  135. var jwtHandler = new JwtSecurityTokenHandler();
  136. JwtSecurityToken securityToken = jwtHandler.ReadJwtToken(authorToken);
  137. DateTime expDt = (securityToken.Payload[JwtRegisteredClaimNames.Exp] ?? 0).GetInt().GetTimeSpmpToDate();
  138. #endregion
  139. if (expDt >= createZebraTime) //超时重新获取token
  140. {
  141. authorToken = await JwtHelper.IssueJwtAsync(new TokenModelJwt() { UserId = uId, UserName = uName, Department = depName, Role = role }); //
  142. }
  143. view.Expires = expDt;
  144. view.Token = authorToken;
  145. }
  146. else
  147. {
  148. view.Expires = createZebraTime.AddMinutes(30);
  149. //view.Token = await GeneralMethod.GetToken(_config, dto.Number, uId, uName, createZebraTime); //JwtHelper
  150. view.Token = await JwtHelper.IssueJwtAsync(new TokenModelJwt() { UserId = uId, UserName = uName, Department = depName, Role = role }); //
  151. TimeSpan ts = view.Expires.AddMinutes(-1) - createZebraTime; //设置redis 过期时间 比 jwt 时间 快一分钟
  152. await RedisRepository.RedisFactory.CreateRedisRepository().StringSetAsync<string>(authorId, view.Token, ts);//string 存
  153. }
  154. //#region 添加登录用户上线信息
  155. //_hubContext.SignalRLogin(uId);
  156. //#endregion
  157. #region 测试添加系统消息
  158. //await _message.AddMsg(new MessageDto()
  159. //{
  160. // Type = 1,
  161. // IssuerId = 208,
  162. // Title = "测试添加消息标题",
  163. // Content = "消息体测试",
  164. // ReleaseTime = DateTime.Now,
  165. // UIdList = new List<int> {
  166. // 5,
  167. // 208,
  168. // 219
  169. // }
  170. //});
  171. #endregion
  172. return Ok(JsonView(view));
  173. }
  174. /// <summary>
  175. /// 移动端用户登录
  176. /// </summary>
  177. /// <param name="dto"></param>
  178. /// <returns></returns>
  179. [Route("MobileLogin")]
  180. [HttpPost]
  181. [ApiLog("Login", OperationEnum.Login)]
  182. [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
  183. public async Task<IActionResult> MobileLoginAsync(LoginDto dto)
  184. {
  185. if (string.IsNullOrWhiteSpace(dto.Number) || string.IsNullOrWhiteSpace(dto.Password))
  186. {
  187. return Ok(JsonView(false, "账号或密码不能为空!!"));
  188. }
  189. #region 校验用户信息
  190. var userData = _loginRep.Login(dto).Result;
  191. if (userData.Code != 0) return Ok(JsonView(false, userData.Msg));
  192. #endregion
  193. Result authData = null;
  194. string uName = string.Empty;
  195. string role = string.Empty;
  196. int uId = 0;
  197. int unReadCount = 0;
  198. int announcementUnReadCount = 0;
  199. if (userData.Data != null)
  200. {
  201. uId = (userData.Data as UserLoginInfoView).UserId;
  202. uName = (userData.Data as UserLoginInfoView).CnName;
  203. role = (userData.Data as UserLoginInfoView).JobName;
  204. authData = _sysMenuPermRep.MobileMenuLoad(uId, dto.PortType);
  205. unReadCount = await _messageRep.GetUnReadCount(uId);
  206. announcementUnReadCount = await _messageRep.GetAnnouncementUnReadCount(uId);
  207. }
  208. //_hubContext.Login(uId, uName);
  209. var view = new LoginView
  210. {
  211. UserInfo = userData?.Data,
  212. AuthData = authData?.Data,
  213. UnReadCount = unReadCount,
  214. AnnouncementUnReadCount = announcementUnReadCount
  215. };
  216. DateTime createZebraTime = DateTime.Now;
  217. string authorId = dto.Number + "Token";
  218. string authorToken = await RedisRepository.RedisFactory.CreateRedisRepository().StringGetAsync<string>(authorId);//string 取
  219. if (authorToken != null)
  220. {
  221. #region 解析出过期时间
  222. var jwtHandler = new JwtSecurityTokenHandler();
  223. JwtSecurityToken securityToken = jwtHandler.ReadJwtToken(authorToken);
  224. DateTime expDt = (securityToken.Payload[JwtRegisteredClaimNames.Exp] ?? 0).GetInt().GetTimeSpmpToDate();
  225. #endregion
  226. if (expDt >= createZebraTime) //超时重新获取token
  227. {
  228. authorToken = await JwtHelper.IssueJwtAsync(new TokenModelJwt() { UserId = uId, UserName = uName, Role = role }); //
  229. }
  230. view.Expires = expDt;
  231. view.Token = authorToken;
  232. }
  233. else
  234. {
  235. view.Expires = createZebraTime.AddMinutes(30);
  236. //view.Token = await GeneralMethod.GetToken(_config, dto.Number, uId, uName, createZebraTime); //JwtHelper
  237. view.Token = await JwtHelper.IssueJwtAsync(new TokenModelJwt() { UserId = uId, UserName = uName, Role = role }); //
  238. TimeSpan ts = view.Expires.AddMinutes(-1) - createZebraTime; //设置redis 过期时间 比 jwt 时间 快一分钟
  239. await RedisRepository.RedisFactory.CreateRedisRepository().StringSetAsync<string>(authorId, view.Token, ts);//string 存
  240. }
  241. //#region 添加登录用户上线信息
  242. //_hubContext.SignalRLogin(uId);
  243. //#endregion
  244. #region 测试添加系统消息
  245. //await _message.AddMsg(new MessageDto()
  246. //{
  247. // Type = 1,
  248. // IssuerId = 208,
  249. // Title = "测试添加消息标题",
  250. // Content = "消息体测试",
  251. // ReleaseTime = DateTime.Now,
  252. // UIdList = new List<int> {
  253. // 5,
  254. // 208,
  255. // 219
  256. // }
  257. //});
  258. #endregion
  259. return Ok(JsonView(view));
  260. }
  261. /// <summary>
  262. /// 申请注册 数据Data
  263. /// </summary>
  264. /// <returns></returns>
  265. //[Authorize]
  266. [HttpPost]
  267. [Route("register/daraSource")]
  268. public async Task<IActionResult> RegisterDataSource()
  269. {
  270. string sql = string.Format(@"Select sc.Id CompanyId,sc.CompanyName,sd.Id DepId,sd.DepName,sjp.Id JobId,sjp.JobName From Sys_Company sc
  271. Left Join Sys_Department sd On sd.IsDel = 0 And sc.Id = sd.CompanyId
  272. Left Join Sys_JobPost sjp On sjp.IsDel = 0 And sjp.DepId = sd.Id
  273. Where sc.IsDel = 0");
  274. var companyDetails = _loginRep._sqlSugar.SqlQueryable<CompanyDetailsView>(sql).ToList();
  275. var detailsView1 = new List<CompanyDetailsView1>();
  276. if (companyDetails.Count > 0)
  277. {
  278. var companyDetails1 = companyDetails.GroupBy(it => it.CompanyId).Select(it => it.First()).ToList();
  279. detailsView1 = companyDetails1.Select(it =>
  280. {
  281. var itemCompany = new CompanyDetailsView1();
  282. var depDetailsView = new List<DepDetailsView>();
  283. var companyDetails2 = companyDetails.GroupBy(it => it.DepId).Select(it => it.First()).ToList();
  284. //部门
  285. depDetailsView = companyDetails2.Where(depIt => depIt.CompanyId == it.CompanyId).Select(depIt =>
  286. {
  287. var depDetails = new DepDetailsView();
  288. var jobDetails = new List<JobDetailsView>();
  289. //岗位
  290. jobDetails = companyDetails.Where(jobIt => jobIt.DepId == depIt.DepId).Select(jobIt =>
  291. {
  292. var jobDetail = new JobDetailsView()
  293. {
  294. JobId = jobIt.JobId,
  295. JobName = jobIt.JobName,
  296. };
  297. return jobDetail;
  298. }).ToList();
  299. depDetails.DepId = depIt.DepId;
  300. depDetails.DepName = depIt.DepName;
  301. depDetails.SubJob = jobDetails;
  302. return depDetails;
  303. }).ToList();
  304. itemCompany.CompanyId = it.CompanyId;
  305. itemCompany.CompanyName = it.CompanyName;
  306. itemCompany.SubDep = depDetailsView;
  307. return itemCompany;
  308. }).ToList();
  309. }
  310. return Ok(new { Code = 200, Msg = "查询成功!", Data = detailsView1 });
  311. }
  312. /// <summary>
  313. /// 申请注册
  314. /// </summary>
  315. /// <param name="dto"></param>
  316. /// <returns></returns>
  317. //[Authorize]
  318. [HttpPost]
  319. [Route("register")]
  320. public async Task<IActionResult> Register(RegisterDto dto)
  321. {
  322. #region 企业微信添加员工
  323. //string lastName = dto.CnName.Substring(0, 1);
  324. //string lastNamePy = string.Empty;
  325. //if (PinyinHelper.IsChinese(Convert.ToChar(lastName)))
  326. //{
  327. // lastNamePy = PinyinHelper.GetPinyin(lastName);
  328. //}
  329. //string userId = string.Format("{0}.{1}", dto.EnName, lastNamePy.ToLower());
  330. //Create_Request request = new Create_Request()
  331. //{
  332. // userid = userId,
  333. // name = dto.CnName,
  334. // mobile = dto.Phone,
  335. // department = new List<long>() { dto.DepId },
  336. // position = dto.JobPostId.ToString(),
  337. // gender = dto.Sex == 0 ? 1 : dto.Sex == 1 ? 2 : 1,
  338. // biz_mail = dto.Email
  339. //};
  340. //var qiYeWeChatCreateData = await _qiYeWeChatApiServic.CreateAsync(request);
  341. #endregion
  342. var userData = _loginRep.Register(dto);
  343. if (userData.Result.Code != 0)
  344. {
  345. return Ok(JsonView(false, userData.Result.Msg));
  346. }
  347. return Ok(JsonView(true, userData.Result.Msg));
  348. }
  349. /// <summary>
  350. /// 修改密码
  351. /// </summary>
  352. /// <param name="dto"></param>
  353. /// <returns></returns>
  354. [Authorize]
  355. [HttpPost]
  356. [Route("UpdPassword")]
  357. public async Task<IActionResult> UpdateUserPassword(UpdateDto dto)
  358. {
  359. //Result result = new Result();
  360. //var httpContext = HttpContext.User.Claims.FirstOrDefault(it => it.Type == ClaimTypes.Name)?.Value;
  361. //Sys_Users sys_Users = _mapper.Map<Sys_Users>(dto);
  362. var _view = await _loginRep.ChangePassword(dto.UserId, dto.Password);
  363. if (_view.Code == 0) return Ok(JsonView(true, "操作成功!"));
  364. return Ok(JsonView(false, _view.Msg));
  365. }
  366. /// <summary>
  367. /// 保存deviceToken
  368. /// </summary>
  369. /// <param name="dto"></param>
  370. /// <returns></returns>
  371. [HttpPost("SaveDeviceToken")]
  372. [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
  373. public async Task<IActionResult> SaveDeviceToken(SaveDeviceToken dto)
  374. {
  375. var view = await _deviceTokenRep.SaveToken(dto);
  376. if (view.Code == 0) return Ok(JsonView(true, "操作成功!"));
  377. return Ok(JsonView(false, view.Msg));
  378. }
  379. /// <summary>
  380. /// 获取deviceToken
  381. /// </summary>
  382. /// <param name="dto"></param>
  383. /// <returns></returns>
  384. [HttpPost("GetDeviceToken")]
  385. [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
  386. public async Task<IActionResult> GetDeviceToken(GetDeviceToken dto)
  387. {
  388. var view = await _deviceTokenRep.GetToken(dto.account);
  389. if (view.Code == 0) return Ok(JsonView(true, "操作成功!", view.Data));
  390. return Ok(JsonView(false, view.Msg));
  391. }
  392. /// <summary>
  393. /// 测试auth
  394. /// </summary>
  395. /// <param name="dto"></param>
  396. /// <returns></returns>
  397. [OASystemAuthentication]
  398. [HttpPost("TestToken")]
  399. [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
  400. public async Task<IActionResult> TestToken(LoginDto dto)
  401. {
  402. string authorId = dto.Number + "Token";
  403. // 从Redis里面取数据
  404. //string userToken = _redis.StringGet(authorId);
  405. string userToken = "";
  406. var view = new LoginView
  407. {
  408. Token = authorId + ":" + userToken
  409. };
  410. return Ok(JsonView(view));
  411. }
  412. /// <summary>
  413. /// 限流测试
  414. /// </summary>
  415. /// <returns></returns>
  416. [HttpGet("rate-test")]
  417. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  418. public IActionResult RateTest()
  419. {
  420. return Ok(JsonView(true, $"限流测试 - IP:{HttpContext.Connection.RemoteIpAddress?.ToString()}", null, GetRequestCount()));
  421. }
  422. private static int _requestCount = 0;
  423. private int GetRequestCount()
  424. {
  425. return ++_requestCount;
  426. }
  427. ///// <summary>
  428. ///// 员工信息 迁移
  429. ///// Old OA To New OA
  430. ///// </summary>
  431. ///// <returns></returns>
  432. //[HttpPost("UpdateUserDataOldOAToNewOA")]
  433. //[ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
  434. //public async Task<IActionResult> UpdateUserDataOldOAToNewOA()
  435. //{
  436. // dynamic view = null;
  437. // try
  438. // {
  439. // var _sqlSuar = _loginRep._sqlSugar;
  440. // var oldOaUsersData = await _sqlSuar.Queryable<OA2014UsersView>().AS("OA2014.dbo.Users").ToListAsync();
  441. // var newOaCompanyData = await _sqlSuar.Queryable<Sys_Company>().ToListAsync();
  442. // var newOaDepartmentData = await _sqlSuar.Queryable<Sys_Department>().ToListAsync();
  443. // var newOaJobPostData = await _sqlSuar.Queryable<Sys_JobPost>().ToListAsync();
  444. // List<Sys_Users> newOaUserDatas = new List<Sys_Users>();
  445. // foreach (var oldUser in oldOaUsersData)
  446. // {
  447. // int depId = 0, postId = 0;
  448. // #region 处理部门岗位
  449. // int did = oldUser.Did;
  450. // string post = oldUser.Post;
  451. // switch (did)
  452. // {
  453. // case 1 : //信息部
  454. // depId = 2;
  455. // if (post.Equals("信息部经理")) postId = 4;
  456. // else if (post.Equals("美工")) { depId = 5; postId = 18; }
  457. // else if(post.Equals("网络推广")) postId = 46;
  458. // else if (post.Equals("软件开发")) postId = 5;
  459. // else if (post.Equals("平面设计师")) { depId = 5; postId = 18; }
  460. // else if (post.Equals("平面设计")) { depId = 5; postId = 18; }
  461. // else if (post.Equals("平面设计师")) { depId = 5; postId = 18; }
  462. // else if (post.Equals("软件工程师")) postId = 5;
  463. // else if (post.Equals("OP操作")) { depId = 7; postId = 28; }
  464. // else if (post.Equals("软件工程师.")) postId = 5;
  465. // else if (post.Equals(".net工程师")) postId = 5;
  466. // else if (post.Equals("安卓开发工程师")) postId = 7;
  467. // else if (post.Equals("web前端")) postId = 6;
  468. // else if (post.Equals("Web后端开发")) postId = 5;
  469. // break;
  470. // case 2 : //财务部
  471. // depId=3;
  472. // if (post.Equals("主管")) postId = 47;
  473. // else if (post.Equals("财务总监")) { postId = 9; }
  474. // else if (post.Equals("会计")) { postId = 10; }
  475. // else if (post.Equals("财务经理")) { postId = 47; }
  476. // else if (post.Equals("财务助理")) { postId = 50; }
  477. // else if (post.Equals("出纳")) { postId = 48; }
  478. // else { postId = 10; }
  479. // break;
  480. // case 3: //人事部
  481. // depId = 4;
  482. // if (post.Equals("主管")) postId = 51;
  483. // else if (post.Equals("人事部主管")) { postId = 51; }
  484. // else if (post.Equals("人事行政主管")) { postId = 51; }
  485. // else if (post.Equals("行政人事助理")) { postId = 52; }
  486. // else if (post.Equals("人事助理")) { postId = 52; }
  487. // else if (post.Equals("人事主管")) { postId = 51; }
  488. // else if (post.Equals("行政人事专员")) { postId = 12; }
  489. // else if (post.Equals("行政司机")) { postId = 14; }
  490. // else if (post.Equals("司机")) { postId = 14; }
  491. // else if (post.Equals("统筹执行")) { postId = 12; }
  492. // else if (post.Equals("培训专员")) { postId = 13; }
  493. // else if (post.Equals("人事经理")) { postId = 11; }
  494. // else if (post.Equals("前台")) { postId = 33; }
  495. // else if (post.Equals("人事行政经理")) { postId = 11; }
  496. // else if (post.Equals("人事部经理")) { postId = 11; }
  497. // else if (post.Equals("人事专员")) { postId = 12; }
  498. // else if (post.Equals("人事经理")) { postId = 11; }
  499. // else postId = 12;
  500. // break;
  501. // case 4: //国交部
  502. // //22 7 主管
  503. // //23 7 计调
  504. // //24 7 机票
  505. // //25 7 酒店
  506. // //26 7 签证
  507. // //27 7 商邀
  508. // //28 7 OP
  509. // //32 7 经理
  510. // depId = 7;
  511. // if (post.Equals("酒店")) postId = 25;
  512. // else if (post.Equals("经理")) { postId = 32; }
  513. // else if (post.Equals("OP专员")) { postId = 28; }
  514. // else if (post.Equals("酒店预订")) { postId = 25; }
  515. // else if (post.Equals("商务邀请")) { postId = 27; }
  516. // else if (post.Equals("-")) { postId = 0; }
  517. // else if (post.Equals("签证专员")) { postId = 26; }
  518. // else if (post.Equals("OP操作")) { postId = 28; }
  519. // else if (post.Equals("司机")) { postId = 14; }
  520. // else if (post.Equals("国际交流部经理")) { postId = 32; }
  521. // else if (post.Equals("机票酒店")) { postId = 24; }
  522. // else if (post.Equals("签证")) { postId = 26; }
  523. // else if (post.Equals("票房")) { postId = 24; }
  524. // else if (post.Equals("票务专员")) { postId = 24; }
  525. // else if (post.Equals("酒店/机票")) { postId = 24; }
  526. // else if (post.Equals("OP")) { postId = 28; }
  527. // else if (post.Equals("主管")) { postId = 22; }
  528. // else if (post.Equals("订票专员")) { postId = 24; }
  529. // else if (post.Equals("机票")) { postId = 24; }
  530. // else if (post.Equals("国交部经理")) { postId = 32; }
  531. // else if (post.Equals("计调")) { postId = 23; }
  532. // else if (post.Equals("票务")) { postId = 24; }
  533. // else if (post.Equals("国交部主管")) { postId = 22; }
  534. // else if (post.Equals("暂无")) { postId = 22; }
  535. // else if (post.Equals("初级OP")) { postId = 28; }
  536. // else if (post.Equals("计调")) { postId = 23; }
  537. // else { postId = 0; }
  538. // break;
  539. // case 5: //会展部
  540. // //15 5 经理
  541. // //16 5 文案策划
  542. // //17 5 活动执行
  543. // //18 5 平面设计师
  544. // //19 5 3D设计师
  545. // depId = 5;
  546. // if (post.Equals("-")) postId = 16;
  547. // break;
  548. // case 6: //市场销售部
  549. // //20 6 经理
  550. // //21 6 市场专员
  551. // //53 6 主管
  552. // depId = 6;
  553. // if (post.Equals("主管")) postId = 53;
  554. // else if (post.Equals("-")) postId = 21;
  555. // else if (post.Equals("销售总监")) postId = 53;
  556. // else if (post.Equals("市场专员")) postId = 21;
  557. // else if (post.Equals("销售专员")) postId = 54;
  558. // else if (post.Equals("市场助理")) postId = 55;
  559. // else if (post.Equals("销售")) postId = 54;
  560. // break;
  561. // case 99: //总经办
  562. // //1 1 总经理
  563. // //2 1 副总经理
  564. // //3 1 总经理助理
  565. // depId = 1;
  566. // if (post.Equals("总经理")) postId = 1;
  567. // else if (post.Equals("副总")) postId = 2;
  568. // break;
  569. // case 107: //会议会展策划部
  570. // //15 5 经理
  571. // //16 5 文案策划
  572. // //17 5 活动执行
  573. // //18 5 平面设计师
  574. // //19 5 3D设计师
  575. // //56 5 销售
  576. // //46 5 网络推广
  577. // //57 5 市场推广
  578. // depId = 5;
  579. // if (post.Equals("销售")) postId = 56;
  580. // else if (post.Equals("策划执行")) postId = 16;
  581. // else if (post.Equals("策活动划")) postId = 16;
  582. // else if (post.Equals("活动执行")) postId = 17;
  583. // else if (post.Equals("网络媒介推广")) postId = 46;
  584. // else if (post.Equals("媒介主任")) postId = 46;
  585. // else if (post.Equals("公关部经理")) postId = 15;
  586. // else if (post.Equals("项目执行")) postId = 17;
  587. // else if (post.Equals("市场推广")) postId = 57;
  588. // else if (post.Equals("策划")) postId = 16;
  589. // else if (post.Equals("3D设计师")) postId = 19;
  590. // else if (post.Equals("平面设计")) postId = 18;
  591. // else if (post.Equals("设计")) postId = 18;
  592. // else if (post.Equals("活动策划")) postId = 16;
  593. // else if (post.Equals("活动策划执行")) postId = 17;
  594. // else if (post.Equals("高级活动策划")) postId = 16;
  595. // else postId = 0;
  596. // break;
  597. // case 115:
  598. // if (post.Equals("系统管理员")) { depId = 9; postId = 31; }
  599. // else if (post.Equals("后勤专员")) { depId = 5; postId = 58; }
  600. // break;
  601. // case 287: //会展部
  602. // //59 2 17 经理
  603. // //60 2 17 主管
  604. // //61 2 17 会展专员
  605. // //62 2 17 会展销售
  606. // //63 2 17 会展策划
  607. // //64 2 17 招商专员
  608. // //65 2 17 媒介专员
  609. // depId = 17;
  610. // if (post.Equals("会展部经理")) postId = 59;
  611. // else if (post.Equals("会展专员")) postId = 61;
  612. // else if (post.Equals("会展销售")) postId = 62;
  613. // else if (post.Equals("招商招展")) postId = 63;
  614. // else if (post.Equals("会展部主管")) postId = 60;
  615. // else if (post.Equals("媒介专员")) postId = 65;
  616. // else if (post.Equals("会展策划")) postId = 63;
  617. // else if (post.Equals("招商专员")) postId = 64;
  618. // else postId = 61;
  619. // break;
  620. // case 304: //总经理助理
  621. // //1 1 总经理
  622. // //2 1 副总经理
  623. // //3 1 总经理助理
  624. // depId = 1;
  625. // postId = 3;
  626. // break;
  627. // case 323: //海外游学部
  628. // //66 3 19 游学顾问
  629. // depId = 19;
  630. // postId = 66;
  631. // break;
  632. // case 335: //会议会展策划部
  633. // //15 5 经理
  634. // //16 5 文案策划
  635. // //17 5 活动执行
  636. // //18 5 平面设计师
  637. // //19 5 3D设计师
  638. // //56 5 销售
  639. // //46 5 网络推广
  640. // //57 5 市场推广
  641. // //67 5 策划主管
  642. // depId = 5;
  643. // if (post.Equals("会展专员")) { depId = 17; postId = 61; }
  644. // else if (post.Equals("策划执行")) postId = 16;
  645. // else if (post.Equals("策划主管")) postId = 67;
  646. // else if (post.Equals("策划")) postId = 16;
  647. // else if (post.Equals("文案")) postId = 16;
  648. // else if (post.Equals("策划执行")) postId = 17;
  649. // else if (post.Equals("执行专员 ")) postId = 17;
  650. // break;
  651. // case 761://项目部
  652. // //20 6 经理
  653. // //21 6 市场专员
  654. // //53 6 主管
  655. // if (post.Equals("销售主管")) { depId = 6; postId = 20; }
  656. // else if (post.Equals("场站经理")) { depId = 6; postId = 53; }
  657. // else if (post.Equals("暂无")) { depId = 5; postId = 58; }
  658. // else
  659. // {
  660. // if (oldUser.CnName.Equals("许婷"))
  661. // {
  662. // depId = 5; postId = 16;
  663. // }
  664. // else if (oldUser.CnName.Equals("陈雪"))
  665. // {
  666. // depId = 5; postId = 17;
  667. // }
  668. // }
  669. // break;
  670. // default:
  671. // break;
  672. // }
  673. // #endregion
  674. // string idCrad = string.Empty;
  675. // string idCradNumber = string.Empty;
  676. // DateTime? birthday = null;
  677. // if (!string.IsNullOrEmpty(oldUser.IDCard))
  678. // {
  679. // idCrad = oldUser.IDCard.Trim();
  680. // #region 处理身份证Number 出生日期
  681. // if (idCrad.ValidateIdNumber())
  682. // {
  683. // idCradNumber = idCrad.ToString();
  684. // string birthDate = idCrad.Substring(6, 8); // 提取从第6位开始的8个字符,即出生日期部分
  685. // birthday = new DateTime(int.Parse(birthDate.Substring(0, 4)), int.Parse(birthDate.Substring(4, 2)), int.Parse(birthDate.Substring(6, 2)));
  686. // }
  687. // #endregion
  688. // }
  689. // DateTime? startWorkDate = null;
  690. // #region 判断是否是日期格式的字符串
  691. // string format = "yyyy-MM-dd"; // 日期格式
  692. // DateTime date;
  693. // bool isParsed = DateTime.TryParseExact(oldUser.StartWorkDate, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
  694. // if (isParsed)
  695. // {
  696. // startWorkDate = date;
  697. // }
  698. // #endregion
  699. // int education = 0;
  700. // #region 处理学历
  701. // if (!string.IsNullOrEmpty(oldUser.Education))
  702. // {
  703. // //0 未设置 1 小学、2 初中、3 高中、4 专科、5 本科、6 研究生
  704. // if (oldUser.Education.Equals("本科")) education = 5;
  705. // else if (oldUser.Education.Equals("大学专科")) education = 4;
  706. // else if (oldUser.Education.Equals("大专")) education = 4;
  707. // else if (oldUser.Education.Equals("全日制本科")) education = 5;
  708. // else if (oldUser.Education.Equals("硕士")) education = 6;
  709. // else if (oldUser.Education.Equals("硕士研究生")) education = 6;
  710. // else if (oldUser.Education.Equals("学士")) education = 6;
  711. // else if (oldUser.Education.Equals("研究生")) education = 6;
  712. // else if (oldUser.Education.Equals("专科")) education = 4;
  713. // }
  714. // #endregion
  715. // int theOrAdultEducation = 0;
  716. // #region 处理统招/成人
  717. // if (!string.IsNullOrEmpty(oldUser.TheOrAdultEducation))
  718. // {
  719. // //0 未设置 1 成教 2 统招 3 留学
  720. // if (oldUser.TheOrAdultEducation.Equals("成教")) theOrAdultEducation = 1;
  721. // if (oldUser.TheOrAdultEducation.Equals("自考")) theOrAdultEducation = 1;
  722. // else if (oldUser.TheOrAdultEducation.Equals("统招")) theOrAdultEducation = 2;
  723. // else if (oldUser.TheOrAdultEducation.Equals("留学")) theOrAdultEducation = 3;
  724. // }
  725. // #endregion
  726. // Sys_Users user = new Sys_Users()
  727. // {
  728. // Id = oldUser.Id,
  729. // CnName = oldUser.CnName,
  730. // EnName = oldUser.EnName,
  731. // Number = oldUser.Number,
  732. // CompanyId = 2,
  733. // DepId = depId,
  734. // JobPostId = postId,
  735. // Password = oldUser.Password,
  736. // Sex = oldUser.Sex,
  737. // Ext = oldUser.Ext,
  738. // Phone = oldUser.Phone,
  739. // UrgentPhone = oldUser.UrgentPhone,
  740. // Email = oldUser.Email,
  741. // Address = oldUser.Address,
  742. // Edate = oldUser.Edate,
  743. // Rdate = oldUser.Rdate,
  744. // Seniority = oldUser.Seniority,
  745. // Birthday = birthday,
  746. // IDCard = idCradNumber,
  747. // StartWorkDate = startWorkDate,
  748. // GraduateInstitutions = oldUser.GraduateInstitutions,
  749. // Professional = oldUser.Professional,
  750. // Education = education,
  751. // TheOrAdultEducation = theOrAdultEducation,
  752. // MaritalStatus = oldUser.MaritalStatus,
  753. // HomeAddress = oldUser.HomeAddress,
  754. // UsePeriod = oldUser.UsePeriod,
  755. // WorkExperience = oldUser.WorkExperience,
  756. // Certificate = oldUser.Certificate,
  757. // HrAudit = 1,
  758. // CreateUserId = 208,
  759. // CreateTime = DateTime.Now,
  760. // DeleteUserId = null,
  761. // DeleteTime = string.Empty,
  762. // Remark = oldUser.Remark,
  763. // IsDel = oldUser.IsDel,
  764. // };
  765. // newOaUserDatas.Add(user);
  766. // }
  767. // if (newOaUserDatas.Count > 0)
  768. // {
  769. // //执行删除
  770. // bool resetStatus = _sqlSuar.DbMaintenance.TruncateTable<Sys_Users>();
  771. // //执行批量添加
  772. // int addTotal = await _sqlSuar.Insertable(newOaUserDatas).IgnoreColumns(it => it.Id).ExecuteCommandAsync();
  773. // }
  774. // view = new
  775. // {
  776. // Code = 200,
  777. // Msg = "操作成功!",
  778. // Data = newOaUserDatas
  779. // };
  780. // }
  781. // catch (Exception ex)
  782. // {
  783. // view = new
  784. // {
  785. // Code = 400,
  786. // Msg = ex.Message
  787. // };
  788. // }
  789. // return Ok(JsonView(view));
  790. //}
  791. /// <summary>
  792. /// 测试
  793. /// 创建员工号
  794. /// </summary>
  795. /// <param name="depId">部门Id</param>
  796. /// <returns></returns>
  797. [HttpPost("TestCreateUserNumber")]
  798. [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
  799. public async Task<IActionResult> TestCreateUserNumber(int depId)
  800. {
  801. try
  802. {
  803. var number = await _loginRep.CreateNumber(depId);
  804. return Ok(JsonView(true, "操作成功!", number));
  805. }
  806. catch (Exception ex)
  807. {
  808. return Ok(JsonView(false, "操作失败!", ex.Message));
  809. }
  810. }
  811. /// <summary>
  812. /// ClientTest
  813. /// </summary>
  814. /// <returns></returns>
  815. [HttpPost("ClientTest")]
  816. [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
  817. public async Task<IActionResult> ClientTest()
  818. {
  819. var _sqlsugar = _loginRep._sqlSugar;
  820. var groups = await _sqlsugar.Queryable<Grp_DelegationInfo>()
  821. .Where(x => x.IsDel == 0 && x.VisitDate >= Convert.ToDateTime("2024-01-01") && x.VisitDate <= Convert.ToDateTime("2024-12-31"))
  822. .Select(x => new { x.Id, x.TeamName, x.ClientUnit, x.ClientName, x.VisitDate })
  823. .ToListAsync();
  824. var newClients = await _sqlsugar.Queryable<Crm_NewClientData>()
  825. .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.Contact))
  826. .Select(x => new Crm_NewClientData() { Contact = x.Contact, Telephone = x.Telephone, Phone = x.Phone })
  827. .ToListAsync();
  828. foreach (var item in newClients) EncryptionProcessor.DecryptProperties(item);
  829. var datas = new List<NewClientInfo>();
  830. foreach (var group in groups)
  831. {
  832. var clientName = group.ClientName;
  833. var clientInfo = newClients.Find(x => !string.IsNullOrEmpty(clientName) && !string.IsNullOrEmpty(x.Contact) && clientName.Contains(x.Contact));
  834. if (clientInfo != null)
  835. {
  836. datas.Add(new NewClientInfo()
  837. {
  838. TeamName = group.TeamName,
  839. ClientUnit = group.ClientUnit,
  840. ClientName = group.ClientName,
  841. VisitDate = group.VisitDate,
  842. NewClientContact = clientInfo?.Contact ?? "",
  843. Telephone = clientInfo?.Telephone ?? "",
  844. Phone = clientInfo?.Phone ?? "",
  845. });
  846. }
  847. }
  848. datas = datas.OrderBy(x => x.VisitDate).ToList();
  849. return Ok(JsonView(datas));
  850. }
  851. public class NewClientInfo
  852. {
  853. public string TeamName { get; set; }
  854. public string ClientUnit { get; set; }
  855. public string ClientName { get; set; }
  856. public DateTime VisitDate { get; set; }
  857. public string NewClientContact { get; set; }
  858. public string Telephone { get; set; }
  859. public string Phone { get; set; }
  860. }
  861. }
  862. }