using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using wispro.sp.api.Job; using wispro.sp.entity; using wispro.sp.share; namespace wispro.sp.api.Controllers { [Route("api/[controller]/[action]")] [ApiController] public class ProjectController : ControllerBase { spDbContext Context; public ProjectController(spDbContext context) { Context = context; } public List GetProjects(int state) { return Context.ProjectInfos .Include(p => p.Customer) .Include(p=>p.Reviewer) .Include(p => p.WorkflowUser) .Where(s => s.CaseState == state).ToList(); } public List GetAll() { var retList = Context.ProjectInfos .Include(p => p.Customer) .Include(p => p.Reviewer) .Include(p => p.WorkflowUser) .ToList(); return retList; } public bool SetFinished(string caseNo) { var obj = Context.ProjectInfos.FirstOrDefault(s=>s.CaseNo == caseNo ); if(obj != null) { try { obj.CaseState = 4; Context.SaveChanges(); return true; } catch(Exception ex) { return false; } } else { return false; } } public List GetMyProjects() { return Context.ProjectContentRecords .Include(p=>p.CalMonth) .Include(p => p.Reviewer) .Include(p => p.Project).ThenInclude(p => p.Customer) .Include(p=>p.ProjectWorkContents) .Where(p => p.Staff.Name == User.Identity.Name).ToList(); } public ProjectContents getProjectWorkContent(int Id) { var retObj = Context.ProjectContentRecords .Include(p => p.CalMonth) .Include(p => p.Reviewer) .Include(p => p.Project).ThenInclude(p=>p.Customer) .Include(p => p.ProjectWorkContents) .FirstOrDefault(p=>p.Id == Id); if(retObj != null) { ProjectContents ret = new ProjectContents(); ret.ProjectContentRecord = retObj; ret.ProjectWorkContents = new List(); foreach(var wContent in retObj.ProjectWorkContents) { ViewProjectWorkContent tem = new ViewProjectWorkContent(); tem.modifyState = ModifyState.UnChanged; tem.Id = wContent.Id; tem.Content = wContent.Content; tem.ActualPerformance = wContent.ActualPerformance; tem.ContentRecordId = wContent.ContentRecordId; tem.FinalPerformance = wContent.FinalPerformance; tem.TakeTime = wContent.TakeTime; tem.TimeSpan = wContent.TimeSpan; tem.WorkDate = wContent.WorkDate; ret.ProjectWorkContents.Add(tem); } ret.ProjectContentRecord.ProjectWorkContents = null; return ret; } return null; } public ApiSaveResponse SaveProjectWorkContent(ProjectContents saveObj) { ApiSaveResponse ret = new ApiSaveResponse(); ret.Success = true; using (var t = Context.Database.BeginTransaction()) { try { if (saveObj.ProjectContentRecord.Id == 0) { if(string.IsNullOrEmpty(saveObj.ProjectContentRecord.ProjectNo)) { ret.Success = false; ret.ErrorMessage = "没有选择专案!"; return ret; } else { saveObj.ProjectContentRecord.Project = null; } var currentUser = Context.Staffs.FirstOrDefault(s=>s.Name == User.Identity.Name); saveObj.ProjectContentRecord.StaffId = currentUser.Id; Context.ProjectContentRecords.Add(saveObj.ProjectContentRecord); Context.SaveChanges(); } else { var temObj = Context.ProjectContentRecords.FirstOrDefault(p => p.Id == saveObj.ProjectContentRecord.Id); temObj.CaseCoefficient = saveObj.ProjectContentRecord.CaseCoefficient; temObj.Point = saveObj.ProjectContentRecord.Point; temObj.ReviewerId = saveObj.ProjectContentRecord.ReviewerId; //temObj.StaffId = saveObj.ProjectContentRecord.StaffId; temObj.State = saveObj.ProjectContentRecord.State; Context.SaveChanges(); saveObj.ProjectContentRecord = temObj; } foreach (var temContent in saveObj.ProjectWorkContents) { switch (temContent.modifyState) { case ModifyState.Deleted: Context.Database.ExecuteSqlRaw($"Delete from ProjectWorkContent where id={temContent.Id}"); break; case ModifyState.Modified: var modifyContent = Context.ProjectWorkContents.FirstOrDefault(p => p.Id == temContent.Id); if (modifyContent != null) { modifyContent.Content = temContent.Content; modifyContent.ActualPerformance = temContent.ActualPerformance; modifyContent.DifficultFactor = temContent.DifficultFactor; modifyContent.FinalPerformance = temContent.FinalPerformance; modifyContent.TakeTime = temContent.TakeTime; modifyContent.TimeSpan = temContent.TimeSpan; modifyContent.WorkDate = temContent.WorkDate; } break; case ModifyState.New: var newContent = new ProjectWorkContent(); newContent.Content = temContent.Content; newContent.ActualPerformance = temContent.ActualPerformance; newContent.DifficultFactor = temContent.DifficultFactor; newContent.FinalPerformance = temContent.FinalPerformance; newContent.TakeTime = temContent.TakeTime; newContent.TimeSpan = temContent.TimeSpan; newContent.WorkDate = temContent.WorkDate; newContent.ContentRecordId = saveObj.ProjectContentRecord.Id; Context.ProjectWorkContents.Add(newContent); break; } Context.SaveChanges(); } t.Commit(); } catch(Exception ex) { t.Rollback(); ret.Success = false; ret.ErrorMessage = ex.Message; } } return ret; } public ApiSaveResponse SubmitToReview(int Id, int reviewerId) { ApiSaveResponse ret = new ApiSaveResponse(); ret.Success = true; using (var t = Context.Database.BeginTransaction()) { try { var temObj = Context.ProjectContentRecords.FirstOrDefault(p=>p.Id == Id); if(temObj != null) { temObj.State = 1; var temStaff = Context.Staffs.FirstOrDefault(s=>s.Id == reviewerId); if(temStaff == null) { ret.Success = false; ret.ErrorMessage = "指定审核人不存在!"; return ret; } var calMonth = Context.CalMonths.FirstOrDefault(f=>f.Status ==0); if(calMonth == null) { ret.Success = false; ret.ErrorMessage = "当前没有正在处理的绩效核算月份!"; return ret; } temObj.ReviewerId = reviewerId; temObj.CalMonthId = calMonth.Id; Context.SaveChanges(); t.Commit(); #region 邮件通知 string strSubject = $"{User.Identity.Name}{calMonth.Month}月份专案工作内容"; string strBody = $"{temStaff.Name},您好!

    
{User.Identity.Name}提交了专案{temObj.ProjectNo} {calMonth.Year}年{calMonth.Month}月工作内容,请在尽快确认完成!
"; string strTo = temStaff.Mail; _ = QuartzUtil.AddMailJob(strSubject, strBody, temStaff.Name, strTo); #endregion } else { ret.Success = false; ret.ErrorMessage = "指定项目工作内容Id不存在!"; return ret; } } catch (Exception ex) { t.Rollback(); ret.Success = false; ret.ErrorMessage = ex.Message; } } return ret; } } }