using Microsoft.AspNetCore.Authorization; 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.entity; using wispro.sp.share; namespace wispro.sp.api.Controllers { [Route("api/[controller]/[action]")] [ApiController] //[Authorize] public class PerformanceItemController : ControllerBase { spDbContext Context; public PerformanceItemController(spDbContext context) { Context = context; } public ApiSaveResponse New(PerformanceItem item) { ApiSaveResponse ret = new ApiSaveResponse(); ret.Success = true; using (Context.Database.BeginTransaction()) { try { var results = Context.PerformanceItems.Where(x => x.CaseNo == item.CaseNo && x.DoItem == item.DoItem && x.DoItem != "提出报告" && x.CaseStage == item.CaseStage); var items = results.Include(pi => pi.CalMonth).FirstOrDefault(); if (items != null) { item.AgentFeedbackMemo = "已算绩效"; item.DoItemMemo = $"{items.DoItemMemo}\r\n{items.CalMonth.Year}-{items.CalMonth.Month}已计算!"; item.BasePoint = 0; } if (item.CalMonth != null) { var calMonth = Context.CalMonths.Where(c => c.Year == item.CalMonth.Year && c.Month == item.CalMonth.Month).FirstOrDefault(); if(calMonth == null) { Context.CalMonths.Add(item.CalMonth); Context.SaveChanges(); } else { item.CalMonth = calMonth; } item.CalMonthId = item.CalMonth.Id; item.CalMonth = null; } if (!string.IsNullOrEmpty(item.Customer.Name)) { var temCustomer = Context.Customers.Where(c => c.Name == item.Customer.Name).FirstOrDefault(); if (temCustomer == null) { Context.Customers.Add(item.Customer); Context.SaveChanges(); } else { item.Customer = temCustomer; } item.CustomerId = item.Customer.Id; item.Customer = null; } else { item.Customer = null; } var ItemStaffs = item.ItemStaffs; item.ItemStaffs = null; Context.PerformanceItems.Add(item); Context.SaveChanges(); foreach (ItemStaff itemStaff in ItemStaffs) { itemStaff.ItemId = item.Id; itemStaff.Item = null; if (itemStaff.DoPersonId == 0 && itemStaff.DoPerson != null) { var temStaff = Context.Staffs.FirstOrDefault(s => s.Name == itemStaff.DoPerson.Name); if (temStaff != null) { itemStaff.DoPersonId = temStaff.Id; itemStaff.DoPerson = null; } else { Context.Staffs.Add(itemStaff.DoPerson); Context.SaveChanges(); itemStaff.DoPersonId = itemStaff.DoPerson.Id; itemStaff.DoPerson = null; } } } Context.ItemStaffs.AddRange(ItemStaffs); Context.SaveChanges(); Context.Database.CommitTransaction(); } catch (Exception ex) { ret.Success = false; ret.ErrorMessage = ex.Message; Context.Database.RollbackTransaction(); } } return ret; } /// /// 更新绩效记录信息 /// /// 绩效记录编号 /// 栏位:0:代理人反馈信息;1:案件系数;2:处理事项系数;3:翻译字数;4:撤回的案号 /// 栏位值 /// public ApiSaveResponse UpdateFieldValue(int id,string field,string value) { ApiSaveResponse ret = new ApiSaveResponse(); var item = Context.PerformanceItems.FirstOrDefault(p => p.Id == id); if(item!= null) { switch (field) { case "AgentFeedbackMemo": item.AgentFeedbackMemo = value; break; case "CaseCoefficient": item.CaseCoefficient = value; //此处添加保存到流程系统的代码 break; case "DoItemCoefficient": item.DoItemCoefficient = value; //此处添加保存到流程系统的代码 break; case "WordCount": int wordCount; if(int.TryParse(value,out wordCount)) { item.WordCount = wordCount; } else { ret.Success = false; ret.ErrorMessage = "所给的栏位值不能转换成数字!"; return ret; } break; case "ReturnCasseNo": item.ReturnCasseNo = value; break; } Context.SaveChanges(); } else { ret.Success = false; ret.ErrorMessage = $"不存在的{id}"; } return ret; } public ListApiResponse Query(int pageIndex,int pageSize) { ListApiResponse ret = new ListApiResponse(); var results = Context.PerformanceItems .Where(s => (s.ItemStaffs.Where(iStaff => iStaff.DoPerson.Name == User.Identity.Name).Count() > 0 || s.Reviewer.Name == User.Identity.Name) && s.CalMonth.Status != 4); ret.TotalCount = results.Count(); List retList = results .Include(pi=>pi.ItemStaffs).ThenInclude(iStaff=>iStaff.DoPerson) .Include(pi=>pi.Reviewer) .Include(pi=>pi.Customer) .Include(pi=>pi.CalMonth) .OrderByDescending(o=>o.Id) .Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); #region 将某些属性设为null,避免循环取值造成返回json过大 foreach (PerformanceItem item in retList) { foreach (ItemStaff itemStaff in item.ItemStaffs) { itemStaff.DoPerson.ItemStaffs = null; itemStaff.DoPerson.ReviewerItems = null; itemStaff.Item = null; } item.Reviewer.ReviewerItems = null; item.Reviewer.Customers = null; item.Reviewer.ItemStaffs = null; item.Customer.PerformanceItems = null; item.CalMonth.PerformanceItems = null; } #endregion ret.Results = retList; return ret; } /// /// 获取给定用户的绩效清单 /// /// 用户id /// 获取类型;0:处理中;1:所有;4:已归档 /// public ListApiResponse GetMyList(int userid, int type,int pageIndex=1,int pageSize = 5) { ListApiResponse ret = new ListApiResponse(); var results = Context.PerformanceItems .Where(s => (s.ItemStaffs.Where(iStaff => iStaff.DoPerson.Id == userid ).Count() > 0 || s.Reviewer.Id == userid ) && s.CalMonth.Status == type); ret.TotalCount = results.Count(); List retList = results .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson) .Include(pi => pi.Reviewer) .Include(pi => pi.Customer) .Include(pi => pi.CalMonth) .OrderByDescending(o => o.Id) .Skip((pageIndex - 1) * pageSize).Take(pageSize) .ToList(); #region 将某些属性设为null,避免循环取值造成返回json过大 foreach (PerformanceItem item in retList) { foreach(ItemStaff itemStaff in item.ItemStaffs) { itemStaff.DoPerson.ItemStaffs = null; itemStaff.DoPerson.ReviewerItems = null; itemStaff.Item = null; } item.Reviewer.ReviewerItems = null; item.Reviewer.Customers = null; item.Reviewer.ItemStaffs = null; item.Customer.PerformanceItems = null; item.CalMonth.PerformanceItems = null; } #endregion ret.Results = retList; return ret; } public ListApiResponse QueryFilter(QueryFilter queryFilter) { ListApiResponse ret = new ListApiResponse(); ret.TotalCount = Context.PerformanceItems.Count(); List retList = Context.PerformanceItems .Where(s => (s.ItemStaffs .Where(iStaff => iStaff.DoPerson.Name == queryFilter.PersonName) .Count() > 0 || s.Reviewer.Name == queryFilter.PersonName) && s.CalMonth.Month == queryFilter.Month && s.CalMonth.Year == queryFilter.Year) .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson) .Include(pi => pi.Reviewer) .Include(pi => pi.Customer) .OrderByDescending(o => o.Id) .Skip((queryFilter.pageIndex - 1) * queryFilter.pageSize).Take(queryFilter.pageSize).ToList(); //Context.PerformanceItems.Where(p=>p.ItemStaffs.Where) ret.Results = retList; return ret; } } }