123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- 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
- {
- if(item.CalMonth != null)
- {
- var calMonth = Context.CalMonths.Where<CalMonth>(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<Customer>(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;
- }
- List<PerformanceItem> items = Context.PerformanceItems.Where<PerformanceItem>(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
- {
- 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<Staff>(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;
- }
- public ListApiResponse<PerformanceItem> Query(int pageIndex,int pageSize)
- {
- ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
- var results = Context.PerformanceItems
- .Where<PerformanceItem>(s =>
- (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Name == User.Identity.Name).Count() > 0 || s.Reviewer.Name == User.Identity.Name)
- && s.CalMonth.Month == 7 && s.CalMonth.Year == 2021);
- ret.TotalCount = results.Count();
- List<PerformanceItem> retList = results
- .Include(pi=>pi.ItemStaffs).ThenInclude(iStaff=>iStaff.DoPerson)
- .Include(pi=>pi.Reviewer)
- .Include(pi=>pi.Customer)
- .OrderByDescending(o=>o.Id)
- .Skip<PerformanceItem>((pageIndex - 1) * pageSize).Take(pageSize).ToList<PerformanceItem>();
- ret.Results = retList;
- return ret;
- }
- public ListApiResponse<PerformanceItem> QueryFilter(QueryFilter queryFilter)
- {
- ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
- ret.TotalCount = Context.PerformanceItems.Count<PerformanceItem>();
- List<PerformanceItem> retList = Context.PerformanceItems
- .Where<PerformanceItem>(s => (s.ItemStaffs
- .Where<ItemStaff>(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<PerformanceItem>((queryFilter.pageIndex - 1) * queryFilter.pageSize).Take(queryFilter.pageSize).ToList<PerformanceItem>();
- //Context.PerformanceItems.Where<PerformanceItem>(p=>p.ItemStaffs.Where<ItemStaff>)
- ret.Results = retList;
- return ret;
- }
- }
- }
|