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(); using (Context.Database.BeginTransaction()) { try { 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; } else { item.Customer = null; } List items = Context.PerformanceItems.Where(x => x.CaseNo == item.CaseNo && x.DoItem == item.DoItem && x.DoItem != "提出报告").ToList(); if (items.Count > 0) { ret.Success = false; ret.ErrorMessage = $"案件【{item.CaseNo}】处理事项【{item.DoItem }】已存在!"; } else { Context.PerformanceItems.Add(item); } Context.SaveChanges(); foreach(ItemStaff itemStaff in item.ItemStaffs) { itemStaff.ItemId = item.Id; 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; } } } Context.ItemStaffs.AddRange(item.ItemStaffs); Context.Database.CommitTransaction(); } catch (Exception ex) { ret.Success = false; ret.ErrorMessage = ex.Message; Context.Database.RollbackTransaction(); } } return ret; } public ListApiResponse Query(int pageIndex,int pageSize) { if (!User.Identity.IsAuthenticated) { } ListApiResponse ret = new ListApiResponse(); var results = Context.PerformanceItems .Where(s => s.ItemStaffs .Where(iStaff => iStaff.DoPerson.Name == User.Identity.Name).Count() > 0 && s.Month == 7 && s.Year == 2021); ret.TotalCount = results.Count(); List retList = results .Include(pi=>pi.ItemStaffs).ThenInclude(iStaff=>iStaff.DoPerson) .Include(pi=>pi.Reviewer) .Include(pi=>pi.Customer) .OrderByDescending(o=>o.Id) .Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); 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 == "李申").Count() > 0 && s.Month== 7 && s.Year == 2021) .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; } } }