郑州市第一人民医院

UserAccountController.cs 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Security.Claims;
  6. using System.Threading.Tasks;
  7. using MadRunFabric.Common;
  8. using MadRunFabric.Model;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.Extensions.Configuration;
  12. using Microsoft.Extensions.Logging;
  13. using MongoDB.Driver;
  14. using NPOI.SS.Formula.Functions;
  15. using SignTokenApi.IRepositories;
  16. using SignTokenApi.Model.Dto;
  17. using SignTokenApi.Model.Input;
  18. namespace SignTokenApi.Controllers
  19. {
  20. [Authorize]
  21. [ApiVersion("6.0")]
  22. [Produces("application/json")]
  23. [Route("api/[controller]")]
  24. public class UserAccountController : BaseController
  25. {
  26. private readonly IConfiguration _configuration;
  27. private readonly ILogger<UserAccountController> _logger;
  28. private readonly ISys_User_AccountRepository _sys_user_accountRepository;
  29. public UserAccountController(IConfiguration configuration, ILogger<UserAccountController> logger, ISys_User_AccountRepository sys_user_accountRepository)
  30. {
  31. _configuration = configuration;
  32. _logger = logger;
  33. _sys_user_accountRepository = sys_user_accountRepository;
  34. }
  35. /// <summary>
  36. /// 用户信息列表 by page
  37. /// </summary>
  38. /// <param name="pageindex">当前页</param>
  39. /// <param name="pagesize">每页条数</param>
  40. /// <returns></returns>
  41. [HttpGet("getlistbypage")]
  42. public async Task<IActionResult> GetListByPageAsync(string keyword, string deptid, string roleid, string projectid, int type=-1, int pageindex = 1, int pagesize = 10)
  43. {
  44. #region 条件信息
  45. ////排序字段
  46. var sort = Builders<Sys_User_Account>.Sort.Descending("create_time");
  47. //根据条件查询集合
  48. var listFilter = new List<FilterDefinition<Sys_User_Account>>();
  49. listFilter.Add(Builders<Sys_User_Account>.Filter.Eq("delete_flag", false));
  50. listFilter.Add(Builders<Sys_User_Account>.Filter.Where(s => s.type !=2));
  51. //查询
  52. if (type!=-1)
  53. listFilter.Add(Builders<Sys_User_Account>.Filter.Where(s => s.type.Equals(type)));
  54. if (!string.IsNullOrEmpty(deptid))
  55. listFilter.Add(Builders<Sys_User_Account>.Filter.Where(s => s.dept_id.Equals(deptid)));
  56. if (!string.IsNullOrEmpty(roleid))
  57. listFilter.Add(Builders<Sys_User_Account>.Filter.Where(s => s.role_id.Equals(roleid)));
  58. if (!string.IsNullOrEmpty(projectid))
  59. listFilter.Add(Builders<Sys_User_Account>.Filter.Where(s => s.projectlist.Contains(projectid)));
  60. if (!string.IsNullOrEmpty(keyword))
  61. listFilter.Add(Builders<Sys_User_Account>.Filter.Where(s => s.usercode.Contains(keyword) || s.username.Contains(keyword) || s.mobile.Contains(keyword) || s.telephone.Contains(keyword)));
  62. #endregion
  63. var filter = Builders<Sys_User_Account>.Filter.And(listFilter);
  64. var list = await _sys_user_accountRepository.GetByPage(filter, pageindex, pagesize, sort);
  65. var redCount = await _sys_user_accountRepository.CountAsync(filter); //获取总数
  66. var obj = new
  67. {
  68. rows = list,
  69. total = redCount,
  70. };
  71. return Success("获取成功", obj);
  72. }
  73. /// <summary>
  74. /// 设备用户列表 by page - 关联查询
  75. /// </summary>
  76. /// <param name="pageindex">当前页</param>
  77. /// <param name="pagesize">每页条数</param>
  78. /// <returns></returns>
  79. [HttpGet("getlistsbypage")]
  80. public IActionResult GetListsByPage(string keyword, string deptid, string roleid, string projectid, int type = -1, int pageindex = 1, int pagesize = 10)
  81. {
  82. string allprojectrole = _configuration["allprojectrole"];
  83. int recordCount = 0;
  84. var result = _sys_user_accountRepository.GetListsByPage(keyword, deptid, roleid, projectid, type, allprojectrole, pageindex, pagesize, out recordCount);
  85. var obj = new
  86. {
  87. rows = result,
  88. total = recordCount,
  89. };
  90. return Success("获取成功", obj);
  91. }
  92. /// <summary>
  93. /// 获取用户列表 - 关联查询
  94. /// </summary>
  95. /// <returns></returns>
  96. [HttpGet("getapplists")]
  97. public IActionResult GetAppLists(string keyword, string deptid, string roleid, string projectid, int type = -1)
  98. {
  99. string allprojectrole = _configuration["allprojectrole"];
  100. var result = _sys_user_accountRepository.GetAppLists(keyword, deptid, roleid, projectid, type, allprojectrole);
  101. return Success("获取成功", result);
  102. }
  103. /// <summary>
  104. /// 获取用户详情 by id
  105. /// </summary>
  106. /// <param name="id">设备id</param>
  107. /// <returns></returns>
  108. [HttpGet("getdetailes")]
  109. public async Task<IActionResult> GetDetailsAsync(string id)
  110. {
  111. if (string.IsNullOrEmpty(id))
  112. return Error("参数错误");
  113. var model = await _sys_user_accountRepository.GetSingle(id);
  114. if (model != null)
  115. {
  116. return Success("获取成功!", model);
  117. }
  118. return Error("获取失败");
  119. }
  120. /// <summary>
  121. /// 获取用户详情 by id - 关联查询
  122. /// </summary>
  123. /// <param name="id"></param>
  124. /// <returns></returns>
  125. [HttpGet("getdetails")]
  126. public IActionResult GetDetails(string id)
  127. {
  128. if (string.IsNullOrEmpty(id))
  129. return Error("参数错误");
  130. var model = _sys_user_accountRepository.GetDetails(id);
  131. if (model != null)
  132. {
  133. return Success("获取成功!", model);
  134. }
  135. return Error("获取失败");
  136. }
  137. /// <summary>
  138. /// 添加用户信息
  139. /// </summary>
  140. /// <param name="input">用户信息参数</param>
  141. /// <returns></returns>
  142. [HttpPost("add")]
  143. public async Task<IActionResult> Add(UserAccountInput input)
  144. {
  145. #region 验证判断
  146. if (string.IsNullOrEmpty(input.usercode))
  147. return Error("账号不能为空");
  148. if (string.IsNullOrEmpty(input.password))
  149. return Error("密码不能为空");
  150. if (string.IsNullOrEmpty(input.mobile))
  151. return Error("手机号码不能为空");
  152. var modelold = new Sys_User_Account();
  153. modelold = await _sys_user_accountRepository.GetSingle(p => p.usercode == input.usercode && p.delete_flag == false);
  154. if (modelold != null)
  155. {
  156. return Error("账号已经存在");
  157. }
  158. #endregion
  159. var model = new Sys_User_Account();
  160. model.usercode = input.usercode;
  161. model.username = input.username;
  162. model.type = input.type;
  163. model.password = input.password;
  164. model.sex = input.sex;
  165. model.birthday = input.birthday;
  166. model.call_type = input.call_type;
  167. model.delete_flag = false;
  168. model.dept_id = input.dept_id;
  169. model.extensionnumber = input.extensionnumber;
  170. model.group = input.group;
  171. model.groupid = input.groupid;
  172. model.head_img = input.head_img;
  173. model.head_small_img = input.head_small_img;
  174. model.homephone = input.homephone;
  175. model.lock_flag = false;
  176. model.mail = input.mail;
  177. model.mobile = input.mobile;
  178. model.qq = input.qq;
  179. model.remark = input.remark;
  180. model.role_id = input.role_id;
  181. model.seat_flag = input.seat_flag;
  182. model.seat_level = input.seat_level;
  183. model.seat_right = input.seat_right;
  184. model.see_flag = input.see_flag;
  185. model.sina = input.sina;
  186. model.team_id = input.team_id;
  187. model.telephone = input.telephone;
  188. model.idcardno = input.idcardno;
  189. model.entrytime = input.entrytime;
  190. model.transfertime = input.transfertime;
  191. model.quittime = input.quittime;
  192. if (input.idcard != null)
  193. {
  194. model.idcard = input.idcard;
  195. }
  196. else
  197. {
  198. model.idcard = new List<FileBaseModel>();
  199. }
  200. if (input.certificate != null)
  201. {
  202. model.certificate = input.certificate;
  203. }
  204. else
  205. {
  206. model.certificate = new List<FileBaseModel>();
  207. }
  208. if (input.postlist != null)
  209. {
  210. model.postlist = input.postlist;
  211. }
  212. else
  213. {
  214. model.postlist = new List<string>();
  215. }
  216. if (input.projectlist != null)
  217. {
  218. model.projectlist = input.projectlist;
  219. }
  220. else
  221. {
  222. model.projectlist = new List<string>();
  223. }
  224. model.create_user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  225. bool b = await _sys_user_accountRepository.Add(model);
  226. if (b)
  227. return Success("添加成功");
  228. //日志
  229. _logger.LogError($"账号:{input.usercode}添加失败");
  230. return Error("添加失败");
  231. }
  232. /// <summary>
  233. /// 修改用户信息
  234. /// </summary>
  235. /// <param name="input"></param>
  236. /// <returns></returns>
  237. [HttpPost("update")]
  238. public async Task<IActionResult> Update(UserAccountInput input)
  239. {
  240. #region 验证判断
  241. if (string.IsNullOrEmpty(input.usercode))
  242. return Error("账号不能为空");
  243. //if (string.IsNullOrEmpty(input.password))
  244. // return Error("密码不能为空");
  245. var modelold = new Sys_User_Account();
  246. modelold = await _sys_user_accountRepository.GetSingle(p => p.usercode == input.usercode && p.id!= input.id && p.delete_flag == false);
  247. if (modelold != null)
  248. {
  249. return Error("账号已经存在");
  250. }
  251. #endregion
  252. var model = new Sys_User_Account();
  253. model = await _sys_user_accountRepository.GetSingle(input.id);
  254. if (model == null)
  255. return Error("操作失败");
  256. model.usercode = input.usercode;
  257. model.username = input.username;
  258. model.type = input.type;
  259. //model.password = input.password;
  260. model.sex = input.sex;
  261. model.birthday = input.birthday;
  262. model.call_type = input.call_type;
  263. //model.dept_id = input.dept_id;
  264. model.extensionnumber = input.extensionnumber;
  265. model.group = input.group;
  266. model.groupid = input.groupid;
  267. model.head_img = input.head_img;
  268. model.head_small_img = input.head_small_img;
  269. model.homephone = input.homephone;
  270. model.mail = input.mail;
  271. model.mobile = input.mobile;
  272. model.qq = input.qq;
  273. model.remark = input.remark;
  274. model.role_id = input.role_id;
  275. model.seat_flag = input.seat_flag;
  276. model.seat_level = input.seat_level;
  277. model.seat_right = input.seat_right;
  278. model.see_flag = input.see_flag;
  279. model.sina = input.sina;
  280. model.team_id = input.team_id;
  281. model.telephone = input.telephone;
  282. model.idcardno = input.idcardno;
  283. model.entrytime = input.entrytime;
  284. model.transfertime = input.transfertime;
  285. model.quittime = input.quittime;
  286. if (input.idcard != null)
  287. {
  288. model.idcard = input.idcard;
  289. }
  290. else
  291. {
  292. model.idcard = new List<FileBaseModel>();
  293. }
  294. if (input.certificate != null)
  295. {
  296. model.certificate = input.certificate;
  297. }
  298. else
  299. {
  300. model.certificate = new List<FileBaseModel>();
  301. }
  302. if (input.postlist != null)
  303. {
  304. model.postlist = input.postlist;
  305. }
  306. //else
  307. //{
  308. // model.postlist = new List<string>();
  309. //}
  310. if (input.projectlist != null)
  311. {
  312. model.projectlist = input.projectlist;
  313. }
  314. else
  315. {
  316. model.projectlist = new List<string>();
  317. }
  318. bool b = await _sys_user_accountRepository.UpdateOne(model);
  319. if (b)
  320. return Success("保存成功");
  321. //日志
  322. _logger.LogError($"账号:{input.usercode}修改失败");
  323. return Error("保存失败");
  324. }
  325. /// <summary>
  326. /// 逻辑删除用户
  327. /// </summary>
  328. /// <param name="ids"></param>
  329. /// <returns></returns>
  330. [HttpPost("delete")]
  331. public async Task<IActionResult> Delete(string[] ids)
  332. {
  333. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  334. //使用逻辑删除
  335. //物理删除的数据无法恢复
  336. if (ids != null && ids.Length > 0)
  337. {
  338. foreach (var item in ids)
  339. {
  340. var result = _sys_user_accountRepository.Remove(item);
  341. //var eq = await _sys_user_accountRepository.GetSingle(item);
  342. //if (eq != null)
  343. //{
  344. // eq.delete_flag = true;
  345. // eq.delete_user = usercode;
  346. // eq.delete_time = DateTime.Now.ToLocalTime();
  347. // bool bl = await _sys_user_accountRepository.UpdateOne(eq);
  348. // //if (bl)
  349. // //{
  350. // // var data = await _sys_user_dataRepository.GetSingle(p => p.usercode == eq.usercode);
  351. // // if (data != null)
  352. // // {
  353. // // await _sys_user_dataRepository.UpdateUserPost(data.usercode, "", usercode);
  354. // // await _sys_user_dataRepository.UpdateUserProject(data.usercode, "", usercode);
  355. // // data.isdelete = 1;
  356. // // data.deleteby = usercode;
  357. // // data.deletetime = DateTime.Now.ToLocalTime();
  358. // // await _sys_user_dataRepository.Update(data);
  359. // // }
  360. // //}
  361. //}
  362. }
  363. return Success("删除成功");
  364. }
  365. else
  366. return Error("请选择要删除的记录");
  367. }
  368. /// <summary>
  369. /// 修改用户密码
  370. /// </summary>
  371. /// <param name="input"></param>
  372. /// <returns></returns>
  373. [HttpPost("updatepassword")]
  374. public async Task<IActionResult> UpdatePassword(string usercode, string password, string surepassword)
  375. {
  376. #region 验证判断
  377. if (string.IsNullOrEmpty(usercode))
  378. return Error("请选择账号");
  379. if (string.IsNullOrEmpty(password))
  380. return Error("密码不能为空");
  381. if (string.IsNullOrEmpty(surepassword))
  382. return Error("确认密码不能为空");
  383. if (password!=surepassword)
  384. return Error("两次输入的密码不一致");
  385. #endregion
  386. var model = await _sys_user_accountRepository.GetSingle(p => p.usercode == usercode);
  387. if (model == null)
  388. return Error("操作失败");
  389. model.password = password;
  390. bool b = await _sys_user_accountRepository.UpdateOne(model);
  391. if (b)
  392. return Success("保存成功");
  393. return Error("保存失败");
  394. }
  395. /// <summary>
  396. /// 修改我的密码
  397. /// </summary>
  398. /// <param name="input"></param>
  399. /// <returns></returns>
  400. [HttpPost("updatemypassword")]
  401. public async Task<IActionResult> UpdateMyPassword(string oldpassword,string password, string surepassword)
  402. {
  403. #region 验证判断
  404. if (string.IsNullOrEmpty(oldpassword))
  405. return Error("原始密码不能为空");
  406. if (string.IsNullOrEmpty(surepassword))
  407. return Error("确认密码不能为空");
  408. if (password != surepassword)
  409. return Error("两次输入的密码不一致");
  410. #endregion
  411. var usercode= User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  412. var model = await _sys_user_accountRepository.GetSingle(p => p.usercode == usercode && p.password==oldpassword);
  413. if (model == null)
  414. return Error("原始密码输入错误");
  415. model.password = password;
  416. bool b = await _sys_user_accountRepository.UpdateOne(model);
  417. if (b)
  418. return Success("更新成功");
  419. return Error("更新失败");
  420. }
  421. /// <summary>
  422. /// 获取当前用户信息
  423. /// </summary>
  424. /// <returns></returns>
  425. [HttpGet("getnowuser")]
  426. public IActionResult GetUserInfo()
  427. {
  428. string allprojectrole = _configuration["allprojectrole"];
  429. string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  430. var model = _sys_user_accountRepository.GetUserInfo(nowusercode, allprojectrole);
  431. if (model != null)
  432. {
  433. return Success("获取成功!", model);
  434. }
  435. return NoToken("获取失败");
  436. }
  437. /// <summary>
  438. /// 获取用户详情 by id - 关联查询
  439. /// </summary>
  440. /// <param name="id"></param>
  441. /// <returns></returns>
  442. [AllowAnonymous]
  443. [HttpGet("getuserinfo")]
  444. public IActionResult GetUserInfo(string usercode)
  445. {
  446. string allprojectrole = _configuration["allprojectrole"];
  447. var model = _sys_user_accountRepository.GetUserInfo(usercode, allprojectrole);
  448. if (model != null)
  449. {
  450. return Success("获取成功!", model);
  451. }
  452. return Error("获取失败");
  453. }
  454. //2018-7-21获取坐席实时状态使用
  455. [HttpGet("getlist")]
  456. public async Task<IActionResult> GetListsAsync(string groupid, string agentid)
  457. {
  458. //根据条件查询集合
  459. var list = new List<FilterDefinition<Sys_User_Account>>();
  460. list.Add(Builders<Sys_User_Account>.Filter.Eq("seat_flag", true));
  461. list.Add(Builders<Sys_User_Account>.Filter.Eq("delete_flag", false));
  462. if (groupid != null)
  463. list.Add(Builders<Sys_User_Account>.Filter.Where(s => s.group.Contains(groupid)));
  464. if (agentid != null)
  465. list.Add(Builders<Sys_User_Account>.Filter.Where(s => s.usercode.Contains(agentid)));
  466. var filter = Builders<Sys_User_Account>.Filter.And(list);
  467. var agentlist = await _sys_user_accountRepository.Get(filter, null, null);
  468. #region
  469. System.Data.DataTable dt = new System.Data.DataTable();
  470. dt.Columns.Add("groupid");
  471. dt.Columns.Add("agentid");
  472. //dt.Columns.Add("group");
  473. foreach (Object obj in agentlist)
  474. {
  475. if (obj is Sys_User_Account)//这个是类型的判断,类或结构
  476. {
  477. Sys_User_Account s = (Sys_User_Account)obj;
  478. System.Data.DataRow dr = dt.NewRow();
  479. dr["groupid"] = s.groupid;
  480. dr["agentid"] = s.usercode;
  481. //dr["group"] = s.group;
  482. dt.Rows.Add(dr);
  483. }
  484. }
  485. //var dtlist = new
  486. //{
  487. // rows=dt
  488. //};
  489. #endregion
  490. return Success("根据条件获取坐席id数据成功", dt);
  491. }
  492. /// <summary>
  493. /// 获取某项目某角色人员
  494. /// </summary>
  495. /// <returns></returns>
  496. [HttpGet("getprojectuser")]
  497. public IActionResult GetProjectUser(string projectid, string rolecode = "YWYG")
  498. {
  499. var result = _sys_user_accountRepository.GetProjectUser(projectid, rolecode);
  500. return Success("获取成功", result);
  501. }
  502. /// <summary>
  503. /// 获取坐席列表
  504. /// </summary>
  505. /// <param name="key"></param>
  506. /// <returns></returns>
  507. [HttpGet("getseatlist")]
  508. public async Task<IActionResult> GetSeatListsAsync(string key)
  509. {
  510. //根据条件查询集合
  511. var list = new List<FilterDefinition<Sys_User_Account>>();
  512. list.Add(Builders<Sys_User_Account>.Filter.Eq("seat_flag", true));
  513. list.Add(Builders<Sys_User_Account>.Filter.Eq("delete_flag", false));
  514. if (!string.IsNullOrEmpty(key))
  515. list.Add(Builders<Sys_User_Account>.Filter.Where(s => s.usercode.Contains(key) || s.username.Contains(key) ));
  516. var filter = Builders<Sys_User_Account>.Filter.And(list);
  517. var agentlist = await _sys_user_accountRepository.Get(filter, null, null);
  518. return Success("根据条件获取坐席数据成功", agentlist);
  519. }
  520. /// <summary>
  521. /// 导出模板
  522. /// </summary>
  523. /// <returns></returns>
  524. [HttpGet("downtemplate")]
  525. [AllowAnonymous]
  526. public IActionResult DownTemplateAsync()
  527. {
  528. string mbname = _configuration["upload:mbname"].ToString();
  529. string mbkeys = _configuration["upload:mbkeys"].ToString();
  530. NPOIHelper npoi = new NPOIHelper();
  531. byte[] sm = npoi.ExportToExcelTemplate(mbkeys.Split(","));
  532. if (sm != null)
  533. {
  534. return File(sm, "application/vnd.ms-excel", mbname);
  535. }
  536. else
  537. {
  538. return Error("下载失败");
  539. }
  540. }
  541. /// <summary>
  542. /// 上传文件并将设备信息导入数据库
  543. /// </summary>
  544. /// <returns></returns>
  545. [HttpPost("importexcel")]
  546. public async Task<IActionResult> ImportExcel(int headrow = 0)
  547. {
  548. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  549. Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
  550. if (!_upfile.ContentType.Equals("application/vnd.ms-excel") && !_upfile.ContentType.Equals("application/x-xls") && !_upfile.ContentType.Equals("application/x-xlsx") && !_upfile.ContentType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") && !_upfile.ContentType.Equals("application/octet-stream"))
  551. return Error($"请正确上传Excel文件:file.ContentType={_upfile.ContentType}");
  552. NPOIHelper npoi = new NPOIHelper();
  553. var dtExcel = npoi.ExcelToTable1(_upfile, headrow);
  554. int num = dtExcel.Rows.Count;
  555. var cols = dtExcel.Columns;
  556. int colnum = cols.Count;
  557. string dbkeys = _configuration["upload:dbkeys"].ToString().ToLower();
  558. string[] dbcols = dbkeys.Split(",");
  559. string errmsg = string.Empty;
  560. if (num > 0)
  561. {
  562. int index = 1;
  563. foreach (System.Data.DataRow dr in dtExcel.Rows)
  564. {
  565. Sys_User_Account model = new Sys_User_Account();
  566. model.create_time = DateTime.Now;
  567. model.create_user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  568. model.lock_flag = false;//导入禁用,需要重新编辑
  569. model.dept_id = "import";
  570. model.password = "e10adc3949ba59abbe56e057f20f883e";
  571. model.role_id = "5fb22143751cef5115b5da20";
  572. List<string> plist = new List<string>();
  573. string arry = dr["postlist"].ToString();
  574. if (!string.IsNullOrEmpty(arry)) {
  575. string[] arrystr = arry.Split(',');
  576. if (arrystr.Length > 0)
  577. {
  578. foreach (string s in arrystr)
  579. {
  580. plist.Add(s);
  581. }
  582. }
  583. }
  584. model.postlist = plist;
  585. plist = new List<string>();
  586. arry = dr["projectlist"].ToString();
  587. if (!string.IsNullOrEmpty(arry))
  588. {
  589. string[] arrystr = arry.Split(',');
  590. if (arrystr.Length > 0)
  591. {
  592. foreach (string s in arrystr)
  593. {
  594. plist.Add(s);
  595. }
  596. }
  597. }
  598. model.projectlist = plist;
  599. var dbcolslist = dbcols.ToList();
  600. Type t = model.GetType();
  601. PropertyInfo[] PropertyList = t.GetProperties();
  602. foreach (PropertyInfo item in PropertyList)
  603. {
  604. if (dbcolslist.Contains(item.Name))
  605. {
  606. if (item.Name == "id")
  607. continue;
  608. object v = null;
  609. if (item.Name.ToLower() == "postlist"|| item.Name.ToLower() == "projectlist")
  610. {
  611. continue;
  612. }
  613. else
  614. {
  615. try
  616. {
  617. v = Convert.ChangeType(dr[item.Name].ToString(), item.PropertyType);
  618. }
  619. catch
  620. {
  621. continue;
  622. }
  623. }
  624. item.SetValue(model, v, null);
  625. }
  626. }
  627. if (string.IsNullOrEmpty(model.usercode))
  628. {
  629. continue;
  630. }
  631. Sys_User_Account usernmelists = await _sys_user_accountRepository.GetSingle(p=>p.usercode==model.usercode);
  632. if (usernmelists != null)
  633. {
  634. continue;
  635. }
  636. bool b = await _sys_user_accountRepository.Add(model);
  637. if (!b)
  638. {
  639. if (!string.IsNullOrEmpty(errmsg))
  640. {
  641. errmsg = errmsg + "\r\n第" + index + "行导入失败!";
  642. return Error(errmsg);
  643. }
  644. else
  645. {
  646. errmsg = "第" + index + "行导入失败!";
  647. }
  648. }
  649. index++;
  650. }
  651. }
  652. else
  653. {
  654. return Error("文件中无数据");
  655. }
  656. if (!string.IsNullOrEmpty(errmsg))
  657. {
  658. //删除已导入的部分
  659. return Error(errmsg);
  660. }
  661. return Success("导入成功");
  662. }
  663. }
  664. }