using AutoMapper; using DTO; using IServices; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using zhengcaioa.Models; namespace Services { public class HrDeptService : IHrDeptService { private readonly zhengcaioaContext _context; private readonly IMapper _mapper; public HrDeptService(zhengcaioaContext context, IMapper mapper) { _context = context; _mapper = mapper; } public ResultEntity save(HrDeptDTO hrDeptDTO) { ResultEntity resultEntity = new ResultEntity(); try { var hrDept = _mapper.Map(hrDeptDTO); if (String.IsNullOrEmpty(hrDept.Id)) { hrDept.Id = Guid.NewGuid().ToString(); if (string.IsNullOrWhiteSpace(hrDept.Superior)) { hrDept.Levels = "1"; hrDept.Superior = ""; } else { var superiordept = _context.HrDepts.Find(hrDept.Superior); int aa = 0; int.TryParse(superiordept.Levels, out aa); hrDept.Levels = (aa+1).ToString(); } _context.HrDepts.Add(hrDept); } else { var updatepltRole = _context.HrDepts.Find(hrDept.Id); updatepltRole.DeptSn = hrDept.DeptSn; updatepltRole.DeptName = hrDept.DeptName; updatepltRole.Director = hrDept.Director; updatepltRole.DeptType = hrDept.DeptType; updatepltRole.Sort = hrDept.Sort; updatepltRole.QiyongStatus = hrDept.QiyongStatus; if (string.IsNullOrWhiteSpace(hrDept.Superior)) { hrDept.Levels = "1"; hrDept.Superior = ""; } else { var superiordept = _context.HrDepts.Find(hrDept.Superior); int aa = 0; int.TryParse(superiordept.Levels, out aa); hrDept.Levels = (aa + 1).ToString(); } updatepltRole.Levels = hrDept.Levels; updatepltRole.Superior = hrDept.Superior; updatepltRole.RecStatus = hrDept.RecStatus; // updatepltRole.Creater = pltRole.Creater; //updatepltRole.Createtime = pltRole.Createtime; updatepltRole.Modifier = hrDept.Modifier; updatepltRole.Modifytime = hrDept.Modifytime; } _context.SaveChanges(); resultEntity.ReturnID = hrDept.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; } return resultEntity; } public HrDeptDTO Get(string id) { var entity = _context.HrDepts.Find(id); if (entity==null || entity.RecStatus != "A") { entity = new HrDept(); } var hrDeptDTO = _mapper.Map(entity); return hrDeptDTO; } public ResultDataEntity SearchByPaging(HrDeptDTOSearch searchEntity) { ResultDataEntity data = new ResultDataEntity(); // List list = new List(); ////筛选 ////筛选 //StringBuilder sql = new StringBuilder(); //sql.Append(" SELECT a.[Id] Id,a.[dept_sn] DeptSn,a.[dept_name] DeptName,a.[director] Director,a.[dept_type] DeptType,a.[levels] Levels,a.[superior] Superior,a.[superior_top] SuperiorTop,a.[sort] Sort,a.[rec_status] RecStatus,a.[creater] Creater,a.[createtime] Createtime ,a.[modifier] Modifier,a.[modifytime] Modifytime,b.dept_name SuperiorName,c.[user_name] DirectorName "); //sql.Append(" from [hr_dept] a left join [hr_dept] b on a.superior= b.id and b.rec_status='A' left join [plt_user] c on a.director=c.Id and c.rec_status='A' "); //sql.Append(" where a.rec_status = 'A' "); //if (!string.IsNullOrWhiteSpace(searchEntity.DeptName)) //{ // sql.Append(" and a.dept_name like '%" + searchEntity.DeptName.Trim().Replace("'", "''") + "%'"); //} //if (!string.IsNullOrWhiteSpace(searchEntity.Director)) //{ // sql.Append(" and c.[user_name] like '%" + searchEntity.Director.Trim().Replace("'", "''") + "%'"); //} //if (!string.IsNullOrWhiteSpace(searchEntity.DeptType)) //{ // sql.Append(" and a.[dept_type] = '" + searchEntity.DeptType.Trim().Replace("'", "''") + "'"); //} //if (!string.IsNullOrWhiteSpace(searchEntity.Superior)) //{ // sql.Append(" and b.dept_name like '%" + searchEntity.Superior.Trim().Replace("'", "''") + "%'"); //} // var query = _context.HrDeptChaxuns.FromSqlRaw(sql.ToString()) // .OrderByDescending(x => x.Modifytime) //.ToList(); var listCode = (from a in _context.SysCodeDtls join b in _context.SysCodes on a.CodeId equals b.Id where a.RecStatus == "A" && b.RecStatus == "A" select new CodeDataEntity() { CodeId = b.Id, CodeTable = b.CodeTable, CodeField = b.CodeField, CodeSn = a.CodeSn, Comments = a.Comments, Contents = a.Contents, RecStatus = a.RecStatus, Sort = a.Sort } ); var query = (from a in _context.HrDepts//.Where(x => x.RecStatus == "A") join b in _context.HrDepts.Where(x => x.RecStatus == "A") on a.Superior equals b.Id into dept from d in dept.DefaultIfEmpty() join c in _context.PltUsers.Where(x => x.RecStatus == "A") on a.Director equals c.Id into user from u in user.DefaultIfEmpty() join f in listCode.Where(x => x.CodeTable == "system" && x.CodeField == "shifou") on a.QiyongStatus equals f.CodeSn where a.RecStatus == "A" && (string.IsNullOrWhiteSpace(searchEntity.DeptName) || a.DeptName.Contains(searchEntity.DeptName.Trim())) && (string.IsNullOrWhiteSpace(searchEntity.Director) || (u != null && u.UserName.Contains(searchEntity.Director.Trim()))) && (string.IsNullOrWhiteSpace(searchEntity.DeptType) || a.DeptType == searchEntity.DeptType.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Superior) || (d != null && d.DeptName.Contains(searchEntity.Superior.Trim()))) && (string.IsNullOrWhiteSpace(searchEntity.QiyongStatus) || a.QiyongStatus == searchEntity.QiyongStatus.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.DeptnullName) || a.DeptName != searchEntity.DeptnullName.Trim()) select new HrDeptDTO { Id = a.Id, DeptSn = a.DeptSn, DeptName = a.DeptName, Director = a.Director, DirectorName = u.UserName, DeptType = a.DeptType, Levels = a.Levels, Superior = a.Superior, SuperiorName = d.DeptName, SuperiorTop = a.SuperiorTop, Sort = a.Sort, RecStatus = a.RecStatus, Creater = a.Creater, Createtime = a.Createtime, Modifier = a.Modifier, Modifytime = a.Modifytime, QiyongStatus = a.QiyongStatus, QiyongStatusName = f.Comments, }).OrderByDescending(x => x.Modifytime).ToList(); //if (!string.IsNullOrWhiteSpace(searchEntity.DeptName)) //{ // query = query.Where(x => x.DeptName.Contains(searchEntity.DeptName.Trim())).ToList(); //} //if (!string.IsNullOrWhiteSpace(searchEntity.Director)) //{ // query = query.Where(x => x.DirectorName.Contains(searchEntity.Director.Trim())).ToList(); //} //if (!string.IsNullOrWhiteSpace(searchEntity.DeptType)) //{ // query = query.Where(x => x.DeptType == searchEntity.DeptType).ToList(); //} //if (!string.IsNullOrWhiteSpace(searchEntity.Superior)) //{ // query = query.Where(x => x.SuperiorName != null && x.SuperiorName.Contains(searchEntity.Superior.Trim())).ToList(); //} //query = query.OrderByDescending(x => x.Modifytime).ToList(); //if (searchEntity.totalrows == 0) searchEntity.totalrows = query.Count(); var rolelist = query.Skip((searchEntity.page - 1) * searchEntity.rows).Take(searchEntity.rows).ToList(); //list = _mapper.Map>(rolelist); data.LoadData(searchEntity, rolelist); return data; } public ResultEntity ModifyStatus(string id, string userid) { ResultEntity result = new ResultEntity(); result.Result = true; var model = _context.HrDepts.Find(id); if (model != null) { model.RecStatus = "D"; model.Modifier = userid; model.Modifytime = DateTime.Now; _context.SaveChanges(); } return result; } /// /// 获取所有有效部门 /// /// public List GetList() { //StringBuilder sql = new StringBuilder(); //sql.Append(" with t as "); //sql.Append(" ( select b.* from [hr_dept] b where b.[superior] = '' and b.rec_status='A' and b.qiyong_status='A' "); //sql.Append(" union all "); //sql.Append(" select a.* from [hr_dept] a join t b on a.[superior]=b.[Id] and a.rec_status='A' and a.qiyong_status='A' ) "); //sql.Append(" select [Id],[dept_sn], replace(space((CAST([levels] as int)-1)*2)+[dept_name],' ',' ') [dept_name],[director],[dept_type],[levels],[superior],[superior_top],[sort],[rec_status],[creater],[createtime],[modifier],[modifytime],[qiyong_status] "); //sql.Append(" from t "); //var listPosition = _context.HrDepts.FromSqlRaw(sql.ToString()).ToList(); //var listPositionss = departmentRecursion("",listPosition); var listPosition = _context.HrDepts.Where(x=>x.RecStatus =="A" && x.QiyongStatus=="A" && x.DeptName!="公司").OrderBy(x => x.DeptSn).ToList(); var list = _mapper.Map>(listPosition); return list; } private List departmentRecursion(String Superior, List obj) { List hrDepts = new List(); List temp = new List(); for (int i = 0; i < obj.Count; i++) { if (obj[i].Superior == Superior) { hrDepts.Add(obj[i]); temp = departmentRecursion(obj[i].Id, obj); if (temp.Count > 0) { for(int j=0;j< temp.Count; j++) { hrDepts.Add(temp[j]); } } } } return hrDepts.OrderBy(x=>x.DeptSn).ToList(); } } }