123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- 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;
- }
- /// <summary>
- /// 员工管理列表
- /// </summary>
- /// <param name="pageIndex"></param>
- /// <param name="pageSize"></param>
- /// <param name="filter"></param>
- /// <param name="sort"></param>
- /// <param name="order"></param>
- /// <returns></returns>
- [HttpGet]
- public LayuiModel<Staff> 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<Staff> 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<Staff>()
- {
- code = 0,
- count = count,
- data = staffs,
- msg = ""
- };
- return responseData;
- }
- }
- /// <summary>
- /// 员工数据导出
- /// </summary>
- /// <param name="filterInfo"></param>
- /// <returns></returns>
- [HttpPost]
- public async Task<IActionResult> Export(IDictionary<string, string> 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<string> list = new List<string>() { "工号", "姓名", "录入时间", "角色","账号状态","备注" };
- //var list = _mapper.Map<List<ExportStaff>>(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<Staff>.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<Staff>.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<Staff>.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<string, string> 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<string, string> 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<DllEapEntity.Dtos.SelectDto<string>> GetAllStaffSelect()
- {
- using (IDatabase db = DbFactory.Base("ufp"))
- {
- var dal = new StaffDal(db);
- return dal.GetAllStaffSelect();
- }
- }
- }
- }
|