using AutoMapper; using Cksoft.Data; using Cksoft.Data.Repository; using Cksoft.Unity; using Cksoft.Unity.Log4NetConfig; using DllEapCommon.NPOI; using DllUfpDal; using DllUfpEntity; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading.Tasks; using System.Linq; namespace DllUfpBll { [Route("Ufp/api/[controller]/[action]")] [Authorize] public class StaffController : ControllerBase { private readonly string conn = "ufp"; private IConfiguration configuration; private IMapper _mapper; public StaffController(IConfiguration configuration,IMapper mapper) { this.configuration = configuration; _mapper = mapper; } /// /// 员工管理列表 /// /// /// /// /// /// /// [HttpGet] public LayuiModel Get(int pageIndex = 1, int pageSize = 10, string filter = "", string sort = "FCode", string order = "ASC") { if (string.IsNullOrEmpty(sort) || sort.ToString().ToLower() == "null") sort = "FCode"; if (order == "descend") order = "desc"; else { order = "asc"; } IEnumerable staffs = null; filter = filter ?? " "; using (IDatabase db = DbFactory.Base(conn)) { var dal = new StaffDal(db); int start = (pageIndex - 1) * pageSize + 1; string errorinfo = string.Empty; staffs = dal.Get(start, pageSize, filter, sort, order, ref errorinfo); var count = dal.GetCount(filter); var responseData = new LayuiModel() { code = 0, count = count, data = staffs, msg = "" }; return responseData; } } /// /// 员工数据导出 /// /// /// [HttpPost] public async Task Export(IDictionary filterInfo) { var filter = string.Empty; if (filterInfo.ContainsKey("filter")) filter = filterInfo["filter"]; using (IDatabase db = DbFactory.Base(conn)) { int pageIndex = 1, pageSize = 10000; var dal = new StaffDal(db); int start = (pageIndex - 1) * pageSize + 1; string errorinfo = string.Empty; var staffs = dal.Get(start, pageSize, filter, "FCode", "asc", ref errorinfo).ToList(); List list = new List() { "工号", "姓名", "录入时间", "角色","账号状态","备注" }; //var list = _mapper.Map>(staffs); var book = DataExportHelper.EntityToExcel(staffs,list); MemoryStream ms = new MemoryStream(); ms.Position = 0; book.Write(ms); ms.Dispose(); ms.Close(); await Task.CompletedTask; return File(ms.ToArray(), "application/octet-stream"); } } [HttpGet] public Staff GetSingle(string id) { using (IDatabase db = DbFactory.Base(conn)) { var dal = new StaffDal(db); Staff staff = null; staff = dal.Get(id); return staff; } } [HttpPost] public string Add([FromBody] Staff staff) { var id = staff.ID; string errorinfo = string.Empty; var usercode = Request.Headers["usercode"]; staff.Password = new Md5Helper().EnCrypt(string.IsNullOrEmpty(staff.Password) ? "123456" : staff.Password); if (!string.IsNullOrEmpty(staff.FCode)) { staff.FCode = staff.FCode.Trim(); } IDatabase db = DbFactory.Base(conn); try { db.BeginTrans(); var dal = new StaffDal(db); if (id == "0") { staff.ID = Guid.NewGuid().ToString(); staff.RecCode = usercode; staff.RecTime = DateTime.Now; staff.ModCode = usercode; staff.ModTime = DateTime.Now; staff.IsSA = -1; var res = dal.Insert(staff, usercode, ref errorinfo); if (res) { using (IDatabase eapDb = DbFactory.Base("eap")) { eapDb.BeginTrans(); var staffManager = new StaffManager(eapDb); if (staffManager.InsertToEap(staff) < 0) { eapDb.Rollback(); db.Rollback(); errorinfo = "插入EAP数据库失败"; return "-1"; } eapDb.Commit(); } db.Commit(); LogHelper.LogFatal("新增Staff-->" + Json.ToJson(staff), "用户操作", usercode); return JsonConvert.SerializeObject(new { id = staff.ID, msg = string.Empty }); } db.Rollback(); return JsonConvert.SerializeObject(new { id = "0", msg = errorinfo }); ; } var old = dal.Get(id); if (old == null) return "0"; old.FCode = staff.FCode; old.FName = staff.FName; old.FStatus = staff.FStatus; old.ModTime = DateTime.Now; old.Remark = staff.Remark; old.RoleIds = staff.RoleIds; if (dal.Update(old, usercode, ref errorinfo)) { using (IDatabase eapDb = DbFactory.Base("eap")) { eapDb.BeginTrans(); var staffManager = new StaffManager(eapDb); if (staffManager.UpdateToEap(staff) < 0) { eapDb.Rollback(); db.Rollback(); errorinfo = "更新EAP数据库失败"; return "-1"; } eapDb.Commit(); } db.Commit(); LogHelper.LogFatal("修改Staff-->" + Json.ToJson(staff), "用户操作", usercode); return JsonConvert.SerializeObject(new { id = old.ID }); } db.Rollback(); return JsonConvert.SerializeObject(new { id = "0", msg = errorinfo }); ; } catch (Exception e) { db.Rollback(); return JsonConvert.SerializeObject(new { id = "0", msg = e.Message }); } finally { if (db != null) db.Close(); } } [HttpPost] public bool Retired([FromBody] string ids) { if (string.IsNullOrEmpty(ids)) return false; string[] idArray = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); using (IDatabase db = DbFactory.Base(conn)) { var dal = new StaffDal(db); if (dal.Retired(idArray)) { return true; } return false; } } [HttpPost] public string Delete([FromBody] RequestModel model) { if (model == null) return "-1"; if (string.IsNullOrEmpty(model.Ids)) return "-1"; var ids = model.Ids; string[] idArray = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); IDatabase db = DbFactory.Base(conn); try { db.BeginTrans(); var dal = new StaffDal(db); string errorinfo = string.Empty; var modelfir = dal.getStaff(idArray); var modelsec = dal.getStaffRole(idArray); if (dal.Delete(idArray, ref errorinfo)) { using (IDatabase eapdb = DbFactory.Base("eap")) { eapdb.BeginTrans(); var staffManager = new StaffManager(eapdb); if (staffManager.DeleteToEap(idArray) < 0) { eapdb.Rollback(); db.Rollback(); errorinfo = "同步EAP数据库失败"; return "-1"; } eapdb.Commit(); } db.Commit(); LogHelper.LogFatal("删除staff-->:" + Json.ToJson(modelfir) + ";StaffRole" + Json.ToJson(modelsec), "用户操作", Request.Headers["usercode"]); return "1"; } db.Rollback(); return "-1"; } catch (Exception) { db.Rollback(); return "-1"; } finally { if (db != null) db.Close(); } } [HttpPost] [AllowAnonymous] public string Login([FromBody] Staff staff) { using (IDatabase db = DbFactory.Base("ufp")) { var dal = new StaffDal(db); string errorinfo = string.Empty; staff = dal.Login(staff, ref errorinfo); if (staff == null) return JsonConvert.SerializeObject(new { code = -1, msg = errorinfo, data = new Staff() }); return JsonConvert.SerializeObject(new { code = 1, msg = "", data = new Staff() { FCode = staff.FCode, FName = staff.FName, FStatus = staff.FStatus, Remark = staff.Remark, ModTime = staff.ModTime, RecTime = staff.RecTime, IsSA = staff.IsSA } }); } } [HttpGet] public IActionResult Logout() { try { return SignOut("Bearer"); } catch (Exception e) { return Content(e.Message); } } [HttpPost] public EapResponse ChangePassword([FromBody] Dictionary valuePairs) { var oldPass = valuePairs["oldPass"]; var newPass = valuePairs["newPass"]; var confirmPass = valuePairs["confirmPass"]; var userCode = Request.Headers["usercode"]; var res = new EapResponse() { Code = 1, Msg = string.Empty }; if (newPass != confirmPass) { res.Code = -1; res.Msg = "两次输入的密码不一致"; } string errorinfo = string.Empty; using (IDatabase db = DbFactory.Base("ufp")) { var dal = new StaffDal(db); var t = dal.ChangePassword(userCode, oldPass, newPass, ref errorinfo); if (t < 0) { res.Code = -1; res.Msg = errorinfo; } else { res.Code = 1; res.Msg = string.Empty; } return res; } } [HttpPost] public EapResponse ResetPwd([FromBody] Dictionary dic) { var userCode = Request.Headers["usercode"]; string errorinfo = string.Empty; var id = dic["id"]; var res = new EapResponse { Code = 1, Msg = string.Empty }; using (IDatabase db = DbFactory.Base("ufp")) { var dal = new StaffDal(db); if (dal.ResetPwd(id, userCode, ref errorinfo) < 0) { res.Code = -1; res.Msg = errorinfo; } return res; } } [HttpGet] public object GetToken() { var dal = new StaffDal(null, configuration); return dal.GetToken(); } [HttpGet] public IEnumerable> GetAllStaffSelect() { using (IDatabase db = DbFactory.Base("ufp")) { var dal = new StaffDal(db); return dal.GetAllStaffSelect(); } } } }