StaffController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. using AutoMapper;
  2. using Cksoft.Data;
  3. using Cksoft.Data.Repository;
  4. using Cksoft.Unity;
  5. using Cksoft.Unity.Log4NetConfig;
  6. using DllEapCommon.NPOI;
  7. using DllUfpDal;
  8. using DllUfpEntity;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.Extensions.Configuration;
  12. using Newtonsoft.Json;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Linq;
  19. namespace DllUfpBll
  20. {
  21. [Route("Ufp/api/[controller]/[action]")]
  22. [Authorize]
  23. public class StaffController : ControllerBase
  24. {
  25. private readonly string conn = "ufp";
  26. private IConfiguration configuration;
  27. private IMapper _mapper;
  28. public StaffController(IConfiguration configuration,IMapper mapper)
  29. {
  30. this.configuration = configuration;
  31. _mapper = mapper;
  32. }
  33. /// <summary>
  34. /// 员工管理列表
  35. /// </summary>
  36. /// <param name="pageIndex"></param>
  37. /// <param name="pageSize"></param>
  38. /// <param name="filter"></param>
  39. /// <param name="sort"></param>
  40. /// <param name="order"></param>
  41. /// <returns></returns>
  42. [HttpGet]
  43. public LayuiModel<Staff> Get(int pageIndex = 1, int pageSize = 10, string filter = "", string sort = "FCode", string order = "ASC")
  44. {
  45. if (string.IsNullOrEmpty(sort) || sort.ToString().ToLower() == "null")
  46. sort = "FCode";
  47. if (order == "descend")
  48. order = "desc";
  49. else
  50. {
  51. order = "asc";
  52. }
  53. IEnumerable<Staff> staffs = null;
  54. filter = filter ?? " ";
  55. using (IDatabase db = DbFactory.Base(conn))
  56. {
  57. var dal = new StaffDal(db);
  58. int start = (pageIndex - 1) * pageSize + 1;
  59. string errorinfo = string.Empty;
  60. staffs = dal.Get(start, pageSize, filter, sort, order, ref errorinfo);
  61. var count = dal.GetCount(filter);
  62. var responseData = new LayuiModel<Staff>()
  63. {
  64. code = 0,
  65. count = count,
  66. data = staffs,
  67. msg = ""
  68. };
  69. return responseData;
  70. }
  71. }
  72. /// <summary>
  73. /// 员工数据导出
  74. /// </summary>
  75. /// <param name="filterInfo"></param>
  76. /// <returns></returns>
  77. [HttpPost]
  78. public async Task<IActionResult> Export(IDictionary<string, string> filterInfo)
  79. {
  80. var filter = string.Empty;
  81. if (filterInfo.ContainsKey("filter"))
  82. filter = filterInfo["filter"];
  83. using (IDatabase db = DbFactory.Base(conn))
  84. {
  85. int pageIndex = 1, pageSize = 10000;
  86. var dal = new StaffDal(db);
  87. int start = (pageIndex - 1) * pageSize + 1;
  88. string errorinfo = string.Empty;
  89. var staffs = dal.Get(start, pageSize, filter, "FCode", "asc", ref errorinfo).ToList();
  90. List<string> list = new List<string>() { "工号", "姓名", "录入时间", "角色","账号状态","备注" };
  91. //var list = _mapper.Map<List<ExportStaff>>(staffs);
  92. var book = DataExportHelper.EntityToExcel(staffs,list);
  93. MemoryStream ms = new MemoryStream();
  94. ms.Position = 0;
  95. book.Write(ms);
  96. ms.Dispose();
  97. ms.Close();
  98. await Task.CompletedTask;
  99. return File(ms.ToArray(), "application/octet-stream");
  100. }
  101. }
  102. [HttpGet]
  103. public Staff GetSingle(string id)
  104. {
  105. using (IDatabase db = DbFactory.Base(conn))
  106. {
  107. var dal = new StaffDal(db);
  108. Staff staff = null;
  109. staff = dal.Get(id);
  110. return staff;
  111. }
  112. }
  113. [HttpPost]
  114. public string Add([FromBody] Staff staff)
  115. {
  116. var id = staff.ID;
  117. string errorinfo = string.Empty;
  118. var usercode = Request.Headers["usercode"];
  119. staff.Password = new Md5Helper().EnCrypt(string.IsNullOrEmpty(staff.Password) ? "123456" : staff.Password);
  120. if (!string.IsNullOrEmpty(staff.FCode))
  121. {
  122. staff.FCode = staff.FCode.Trim();
  123. }
  124. IDatabase db = DbFactory.Base(conn);
  125. try
  126. {
  127. db.BeginTrans();
  128. var dal = new StaffDal(db);
  129. if (id == "0")
  130. {
  131. staff.ID = Guid.NewGuid().ToString();
  132. staff.RecCode = usercode;
  133. staff.RecTime = DateTime.Now;
  134. staff.ModCode = usercode;
  135. staff.ModTime = DateTime.Now;
  136. staff.IsSA = -1;
  137. var res = dal.Insert(staff, usercode, ref errorinfo);
  138. if (res)
  139. {
  140. using (IDatabase eapDb = DbFactory.Base("eap"))
  141. {
  142. eapDb.BeginTrans();
  143. var staffManager = new StaffManager(eapDb);
  144. if (staffManager.InsertToEap(staff) < 0)
  145. {
  146. eapDb.Rollback();
  147. db.Rollback();
  148. errorinfo = "插入EAP数据库失败";
  149. return "-1";
  150. }
  151. eapDb.Commit();
  152. }
  153. db.Commit();
  154. LogHelper<Staff>.LogFatal("新增Staff-->" + Json.ToJson(staff), "用户操作", usercode);
  155. return JsonConvert.SerializeObject(new { id = staff.ID, msg = string.Empty });
  156. }
  157. db.Rollback();
  158. return JsonConvert.SerializeObject(new { id = "0", msg = errorinfo }); ;
  159. }
  160. var old = dal.Get(id);
  161. if (old == null)
  162. return "0";
  163. old.FCode = staff.FCode;
  164. old.FName = staff.FName;
  165. old.FStatus = staff.FStatus;
  166. old.ModTime = DateTime.Now;
  167. old.Remark = staff.Remark;
  168. old.RoleIds = staff.RoleIds;
  169. if (dal.Update(old, usercode, ref errorinfo))
  170. {
  171. using (IDatabase eapDb = DbFactory.Base("eap"))
  172. {
  173. eapDb.BeginTrans();
  174. var staffManager = new StaffManager(eapDb);
  175. if (staffManager.UpdateToEap(staff) < 0)
  176. {
  177. eapDb.Rollback();
  178. db.Rollback();
  179. errorinfo = "更新EAP数据库失败";
  180. return "-1";
  181. }
  182. eapDb.Commit();
  183. }
  184. db.Commit();
  185. LogHelper<Staff>.LogFatal("修改Staff-->" + Json.ToJson(staff), "用户操作", usercode);
  186. return JsonConvert.SerializeObject(new { id = old.ID });
  187. }
  188. db.Rollback();
  189. return JsonConvert.SerializeObject(new { id = "0", msg = errorinfo }); ;
  190. }
  191. catch (Exception e)
  192. {
  193. db.Rollback();
  194. return JsonConvert.SerializeObject(new { id = "0", msg = e.Message });
  195. }
  196. finally
  197. {
  198. if (db != null)
  199. db.Close();
  200. }
  201. }
  202. [HttpPost]
  203. public bool Retired([FromBody] string ids)
  204. {
  205. if (string.IsNullOrEmpty(ids))
  206. return false;
  207. string[] idArray = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  208. using (IDatabase db = DbFactory.Base(conn))
  209. {
  210. var dal = new StaffDal(db);
  211. if (dal.Retired(idArray))
  212. {
  213. return true;
  214. }
  215. return false;
  216. }
  217. }
  218. [HttpPost]
  219. public string Delete([FromBody] RequestModel model)
  220. {
  221. if (model == null)
  222. return "-1";
  223. if (string.IsNullOrEmpty(model.Ids))
  224. return "-1";
  225. var ids = model.Ids;
  226. string[] idArray = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  227. IDatabase db = DbFactory.Base(conn);
  228. try
  229. {
  230. db.BeginTrans();
  231. var dal = new StaffDal(db);
  232. string errorinfo = string.Empty;
  233. var modelfir = dal.getStaff(idArray);
  234. var modelsec = dal.getStaffRole(idArray);
  235. if (dal.Delete(idArray, ref errorinfo))
  236. {
  237. using (IDatabase eapdb = DbFactory.Base("eap"))
  238. {
  239. eapdb.BeginTrans();
  240. var staffManager = new StaffManager(eapdb);
  241. if (staffManager.DeleteToEap(idArray) < 0)
  242. {
  243. eapdb.Rollback();
  244. db.Rollback();
  245. errorinfo = "同步EAP数据库失败";
  246. return "-1";
  247. }
  248. eapdb.Commit();
  249. }
  250. db.Commit();
  251. LogHelper<Staff>.LogFatal("删除staff-->:" + Json.ToJson(modelfir) + ";StaffRole" + Json.ToJson(modelsec), "用户操作", Request.Headers["usercode"]);
  252. return "1";
  253. }
  254. db.Rollback();
  255. return "-1";
  256. }
  257. catch (Exception)
  258. {
  259. db.Rollback();
  260. return "-1";
  261. }
  262. finally
  263. {
  264. if (db != null)
  265. db.Close();
  266. }
  267. }
  268. [HttpPost]
  269. [AllowAnonymous]
  270. public string Login([FromBody] Staff staff)
  271. {
  272. using (IDatabase db = DbFactory.Base("ufp"))
  273. {
  274. var dal = new StaffDal(db);
  275. string errorinfo = string.Empty;
  276. staff = dal.Login(staff, ref errorinfo);
  277. if (staff == null)
  278. return JsonConvert.SerializeObject(new
  279. {
  280. code = -1,
  281. msg = errorinfo,
  282. data = new Staff()
  283. });
  284. return JsonConvert.SerializeObject(new
  285. {
  286. code = 1,
  287. msg = "",
  288. data = new Staff()
  289. {
  290. FCode = staff.FCode,
  291. FName = staff.FName,
  292. FStatus = staff.FStatus,
  293. Remark = staff.Remark,
  294. ModTime = staff.ModTime,
  295. RecTime = staff.RecTime,
  296. IsSA = staff.IsSA
  297. }
  298. });
  299. }
  300. }
  301. [HttpGet]
  302. public IActionResult Logout()
  303. {
  304. try
  305. {
  306. return SignOut("Bearer");
  307. }
  308. catch (Exception e)
  309. {
  310. return Content(e.Message);
  311. }
  312. }
  313. [HttpPost]
  314. public EapResponse ChangePassword([FromBody] Dictionary<string, string> valuePairs)
  315. {
  316. var oldPass = valuePairs["oldPass"];
  317. var newPass = valuePairs["newPass"];
  318. var confirmPass = valuePairs["confirmPass"];
  319. var userCode = Request.Headers["usercode"];
  320. var res = new EapResponse() { Code = 1, Msg = string.Empty };
  321. if (newPass != confirmPass)
  322. {
  323. res.Code = -1;
  324. res.Msg = "两次输入的密码不一致";
  325. }
  326. string errorinfo = string.Empty;
  327. using (IDatabase db = DbFactory.Base("ufp"))
  328. {
  329. var dal = new StaffDal(db);
  330. var t = dal.ChangePassword(userCode, oldPass, newPass, ref errorinfo);
  331. if (t < 0)
  332. {
  333. res.Code = -1;
  334. res.Msg = errorinfo;
  335. }
  336. else
  337. {
  338. res.Code = 1;
  339. res.Msg = string.Empty;
  340. }
  341. return res;
  342. }
  343. }
  344. [HttpPost]
  345. public EapResponse ResetPwd([FromBody] Dictionary<string, string> dic)
  346. {
  347. var userCode = Request.Headers["usercode"];
  348. string errorinfo = string.Empty;
  349. var id = dic["id"];
  350. var res = new EapResponse { Code = 1, Msg = string.Empty };
  351. using (IDatabase db = DbFactory.Base("ufp"))
  352. {
  353. var dal = new StaffDal(db);
  354. if (dal.ResetPwd(id, userCode, ref errorinfo) < 0)
  355. {
  356. res.Code = -1;
  357. res.Msg = errorinfo;
  358. }
  359. return res;
  360. }
  361. }
  362. [HttpGet]
  363. public object GetToken()
  364. {
  365. var dal = new StaffDal(null, configuration);
  366. return dal.GetToken();
  367. }
  368. [HttpGet]
  369. public IEnumerable<DllEapEntity.Dtos.SelectDto<string>> GetAllStaffSelect()
  370. {
  371. using (IDatabase db = DbFactory.Base("ufp"))
  372. {
  373. var dal = new StaffDal(db);
  374. return dal.GetAllStaffSelect();
  375. }
  376. }
  377. }
  378. }