颐和api

UserAccountController.cs 28KB

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