123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565 |
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- 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]
- [Authorize]
- public class AppealController : ControllerBase
- {
- spDbContext Context;
- public AppealController(spDbContext context)
- {
- Context = context;
- }
- public List<AppealType> GetAppealTypes()
- {
- return Context.AppealTypes.ToList();
- }
- public List<InputField> GetInputField(int appealTypeId, int state)
- {
- return Context.InputFields
- .Where<InputField>(ip => ip.AppealTypeId == appealTypeId && ip.AppealState == state)
- .Include(p => p.SelectValues)
- .ToList();
- }
- /// <summary>
- /// 创建申诉流程
- /// </summary>
- /// <param name="ItemId"></param>
- /// <param name="typeid"></param>
- /// <param name="reviewerId"></param>
- /// <param name="appealObject"></param>
- /// <returns></returns>
- public ApiSaveResponse CreateAppeal(int ItemId, int typeid, int reviewerId, AppealObject appealObject)
- {
- ApiSaveResponse response = new ApiSaveResponse();
- response.Success = true;
-
-
- AppealRecord appealRecord = new AppealRecord();
- if(ItemId > 0)
- {
- appealRecord.ItemId = ItemId;
- }
- else
- {
- appealRecord.ItemId = null;
- }
-
- appealRecord.TypeId = typeid;
- appealRecord.ReviewerId = reviewerId;
- appealRecord.CreaterId = Context.Staffs.Where<Staff>(s => s.Name == User.Identity.Name).FirstOrDefault().Id;
- appealRecord.CreateTime = DateTime.Now;
- var t = Context.Database.BeginTransaction();
- try
- {
- Context.AppealRecords.Add(appealRecord);
- Context.SaveChanges();
- foreach (var fieldValue in appealObject.inputFieldValues)
- {
- fieldValue.InputField = null;
- fieldValue.AppealRecordId = appealRecord.Id;
- }
- Context.InputFieldValues.AddRange(appealObject.inputFieldValues);
- foreach (var file in appealObject.attachFiles)
- {
- var temFile = Context.AttachFiles.Where<AttachFile>(f => f.Id == file.Id).FirstOrDefault();
- temFile.AppealRecordId = appealRecord.Id;
- temFile.UploadUserId = appealRecord.CreaterId;
- }
- Context.SaveChanges();
- var temType = Context.AppealTypes.FirstOrDefault(c=>c.Id == appealRecord.TypeId);
- if (!string.IsNullOrEmpty(temType.ApplealObject))
- {
- IDoAppealObject doAppeal = (IDoAppealObject)Activator.CreateInstance(Type.GetType(appealRecord.Type.ApplealObject));
- doAppeal.DoAppeal(appealObject, appealRecord.Id, Context);
- }
- t.Commit();
- var Reviewer = Context.Staffs.Where<Staff>(s => s.Id == appealRecord.ReviewerId).FirstOrDefault();
- if(appealRecord.Type == null)
- {
- appealRecord.Type = Context.AppealTypes.FirstOrDefault(p=>p.Id == appealRecord.TypeId);
- }
- string strSubject = appealRecord.Type.Name;
- string strBody = $"<div style=\"position:absolute;width:800;height:300;margin-top:50px;margin-left:200px;margin-right:200px;box-shadow:0px 0px 3px 3px #ccc \"><div style= \"margin-top: 20px; margin-left:20px;;font-size:14px; \">{Reviewer.Name},您好!</div><div style= \"margin-top: 25px; margin-left:20px;font-size:14px; \"><div>{User.Identity.Name} 向您提交了{appealRecord.Type.Name}申诉,请使用<a href= \"https://47.106.221.167:8080/ \">绩效系统</a>在<B>{utility.ConfigHelper.GetSectionValue("Lastest_feedback_date")}日</B>前完成审核!</div><div style= \"margin-top: 100px;margin-right:15px; padding-bottom: 20px; font-size:14px;color:#888888;float:right; \">小美集团绩效管理系统</div></div>";
- string strTo = Reviewer.Mail;
- if (!string.IsNullOrEmpty(strTo))
- {
- _ = QuartzUtil.AddMailJob(strSubject, strBody, Reviewer.Name, strTo);
- }
- strBody = $"<div style=\"position:absolute;width:800;height:300;margin-top:50px;margin-left:200px;margin-right:200px;box-shadow:0px 0px 3px 3px #ccc \"><div style= \"margin-top: 20px; margin-left:20px;;font-size:14px; \">{Reviewer.Name},您好!</div><div style= \"margin-top: 25px; margin-left:20px;font-size:14px; \"><div>{User.Identity.Name} 向您提交了{appealRecord.Type.Name}申诉,请使用<a href= \"https://47.106.221.167:8080/ \">绩效系统</a>查看详情!</div><div style= \"margin-top: 100px;margin-right:15px; padding-bottom: 20px; font-size:14px;color:#888888;float:right; \">小美集团绩效管理系统</div></div>";
- _ = QuartzUtil.AddMailJob(strSubject, strBody, "钟子敏", "zhongzimin@china-wispro.com");
- _ = QuartzUtil.AddMailJob(strSubject, strBody, "夏敏", "xiamin@china-wispro.com");
- _ = QuartzUtil.AddMailJob(strSubject, strBody, "吴芳", "wufang@china-wispro.com");
- return response;
- }
- catch (Exception ex)
- {
- t.Rollback();
- response.Success = true;
- response.ErrorMessage = ex.Message;
- return response;
- }
- }
- public ApiSaveResponse ReviewerAppeal(int appealRecordId,AppealObject appealObject)
- {
- ApiSaveResponse response = new ApiSaveResponse();
- response.Success = true;
- var appealRecord = Context.AppealRecords.Where<AppealRecord>(p => p.Id == appealRecordId).FirstOrDefault();
-
- if(appealRecord != null)
- {
- if (appealRecord.ItemId.HasValue)
- {
- var item = Context.PerformanceItems.Include(p=>p.CalMonth).FirstOrDefault(p => p.Id == appealRecord.ItemId.Value);
- if(item == null)
- {
- response.Success = false;
- response.ErrorMessage = "无效的ItemId!";
- return response;
- }
- else
- {
- if(item.CalMonth.Status != 0)
- {
- response.Success = false;
- response.ErrorMessage = "指定的绩效已经归档!";
- return response;
- }
- }
- }
- var t = Context.Database.BeginTransaction();
- try
- {
- appealRecord.ReviewerId = Context.Staffs.Where<Staff>(s => s.Name == User.Identity.Name).FirstOrDefault().Id;
- appealRecord.State = 1;
- appealRecord.ReviewTime = DateTime.Now;
- Context.SaveChanges();
- foreach (var fieldValue in appealObject.inputFieldValues)
- {
- fieldValue.InputField = null;
- fieldValue.AppealRecordId = appealRecord.Id;
- }
- Context.InputFieldValues.AddRange(appealObject.inputFieldValues);
- Context.SaveChanges();
- if (appealObject.attachFiles != null)
- {
- foreach (var file in appealObject.attachFiles)
- {
- var temFile = Context.AttachFiles.Where<AttachFile>(f => f.Id == file.Id).FirstOrDefault();
- temFile.AppealRecordId = appealRecord.Id;
- temFile.UploadUserId = appealRecord.ReviewerId;
- }
- }
- List<InputFieldValue> inputFieldValues = Context.InputFieldValues
- .Where<InputFieldValue>(p => p.AppealRecordId == appealRecordId
- && p.InputField.MapObjectField != null)
- .Include(i=>i.InputField)
- .ToList();
- foreach(InputFieldValue inputFieldValue in inputFieldValues)
- {
-
- SaveValueToMapObject(appealRecord, inputFieldValue);
-
- }
- Context.SaveChanges();
- if(appealRecord.Type == null)
- {
- appealRecord.Type = Context.AppealTypes.FirstOrDefault(s=>s.Id == appealRecord.TypeId);
- }
- if (!string.IsNullOrEmpty(appealRecord.Type.ReviewObject))
- {
- IDoAppealObject doAppeal = (IDoAppealObject)Activator.CreateInstance(Type.GetType(appealRecord.Type.ReviewObject));
- doAppeal.DoAppeal(appealObject, appealRecord.Id,Context);
- }
- Context.SaveChanges();
- new PerformanceItemController(Context, new Services.FileTaskCacheService()).RefreshBasePoint();
- t.Commit();
- return response;
- }
- catch (Exception ex)
- {
- t.Rollback();
- response.Success = false;
- response.ErrorMessage = ex.Message;
- return response;
- }
- }
- else
- {
- response.Success = true;
- response.ErrorMessage = "申诉不存在!";
- return response;
- }
-
- }
- private object ConvertSimpleType(object value, Type destinationType)
- {
- object returnValue;
- if ((value == null) || destinationType.IsInstanceOfType(value))
- {
- return value;
- }
- string str = value as string;
- if ((str != null) && (str.Length == 0))
- {
- return null;
- }
- TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
- bool flag = converter.CanConvertFrom(value.GetType());
- if (!flag)
- {
- converter = TypeDescriptor.GetConverter(value.GetType());
- }
- if (!flag && !converter.CanConvertTo(destinationType))
- {
- throw new InvalidOperationException("无法转换成类型:" + value.ToString() + "==>" + destinationType);
- }
- try
- {
- returnValue = flag ? converter.ConvertFrom(null, null, value) : converter.ConvertTo(null, null, value, destinationType);
- }
- catch (Exception e)
- {
- throw new InvalidOperationException("类型转换出错:" + value.ToString() + "==>" + destinationType, e);
- }
- return returnValue;
- }
- private void SaveValueToMapObject(AppealRecord appealRecord, InputFieldValue inputFieldValue)
- {
- if (!string.IsNullOrEmpty(inputFieldValue.InputField.MapObjectField))
- {
- bool isSave = false;
- if (!string.IsNullOrEmpty(inputFieldValue.InputField.MapSaveCondition))
- {
- string[] conditions = inputFieldValue.InputField.MapSaveCondition.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
- if (conditions.Length == 1)
- {
- if (appealRecord.State == int.Parse(conditions[0]))
- {
- isSave = true;
- }
- }
- else
- {
- if (appealRecord.State == int.Parse(conditions[0]))
- {
- InputFieldValue temValue =
- Context.InputFieldValues.Where<InputFieldValue>(v =>
- v.AppealRecordId == appealRecord.Id &&
- v.InputField.Id == int.Parse(conditions[1]) &&
- v.Value == conditions[2])
- .FirstOrDefault();
- if (temValue != null)
- {
- isSave = true;
- }
- }
- }
- }
- else
- {
- isSave = true;
- }
- if (isSave)
- {
- InputField field = inputFieldValue.InputField;
- PerformanceItem Item = Context.PerformanceItems.Where<PerformanceItem>(p =>
- p.Id == appealRecord.ItemId)
- .Include(p => p.ItemStaffs).ThenInclude(it=>it.DoPerson)
- .Include(p => p.Reviewer)
- .FirstOrDefault();
- bool ItemIsNull = false;
- if(Item == null)
- {
- ItemIsNull = true;
- Item = new PerformanceItem();
- }
- if (!string.IsNullOrEmpty(field.MapObjectField) && !string.IsNullOrEmpty(field.MapObjectFieldLabel))
- {
- List<InputFieldValue> temValues = new List<InputFieldValue>();
- string[] pList = field.MapObjectField.Split(new char[] { '.' });
- string[] plList = field.MapObjectFieldLabel.Split(new char[] { '.' });
- var pInfo = Item.GetType().GetProperty(pList[0]);
- var o = pInfo.GetValue(Item);
- if (Array.IndexOf(o.GetType().GetInterfaces(), typeof(IEnumerable)) > -1)
- {
- List<string> Values = new List<string>();
- List<string> Labels = new List<string>();
- var objList = o as IEnumerable;
- foreach (var obj in objList)
- {
- var objLabel = obj;
- for (int i = 1; i < plList.Length; i++)
- {
- objLabel = objLabel.GetType().GetProperty(plList[i]).GetValue(objLabel);
- }
- if (objLabel.ToString() == inputFieldValue.Label)
- {
- var objValue = obj;
- for (int i = 1; i < pList.Length - 1; i++)
- {
- objValue = objValue.GetType().GetProperty(pList[i]).GetValue(objValue);
- }
- objValue.GetType().GetProperty(pList[pList.Length - 1])
- .SetValue(objValue, ConvertSimpleType(inputFieldValue.Value,Type.GetType(inputFieldValue.InputField.FieldType)));
- }
- }
- }
-
- }
- else
- {
- Item.GetType().GetProperty(field.MapObjectField).SetValue(Item, ConvertSimpleType(inputFieldValue.Value, Type.GetType(inputFieldValue.InputField.FieldType)));
- }
- }
- }
- }
- public List<AppealRecord> GetAppealRecords(int userId)
- {
- var data = Context.AppealRecords.Where<AppealRecord>(p=>p.Id >0);
- if (userId > 0)
- {
- data = Context.AppealRecords.Where<AppealRecord>(ar => (ar.CreaterId == userId || (ar.ReviewerId == userId && ar.State != 1)) && ar.Type.NeedReview == true);
- }
- var retList = data.Include(p => p.Reviewer)
- .Include(p => p.Creater)
- .Include(p => p.Item)
- .Include(p => p.Type).ToList();
- foreach(var record in retList)
- {
- record.Creater.ItemStaffs = null;
- record.Creater.ReviewerItems = null;
- record.Creater.Customers = null;
- if (record.Item != null)
- {
- record.Item.ItemStaffs = null;
- record.Item.PreOastaff = null;
- record.Item.Reviewer = null;
- }
- }
- return retList;
-
- }
- [HttpPost]
- public List<AppealRecord> QueryByFilter(AppealRecordFilter filter)
- {
- var data = Context.AppealRecords.Where<AppealRecord>(s => true);
- if(filter.AppealTypeId > 0)
- {
- data = Context.AppealRecords.Where(f=>f.TypeId == filter.AppealTypeId);
- }
- if(!string.IsNullOrEmpty(filter.CaseNo))
- {
- data = data.Where(s=>s.Item.CaseNo.Contains(filter.CaseNo));
- }
- if (!string.IsNullOrEmpty(filter.CreateUser))
- {
- data = data.Where(s => s.Creater.Name.Contains(filter.CreateUser));
- }
- if (!string.IsNullOrEmpty(filter.Reviewer))
- {
- data = data.Where(s => s.Reviewer.Name.Contains(filter.Reviewer));
- }
- if (filter.beginCreateTime.HasValue)
- {
- data = data.Where(s => s.CreateTime>=filter.beginCreateTime.Value );
- }
- if (filter.endCreateTime.HasValue)
- {
- data = data.Where(s => s.CreateTime <= filter.endCreateTime.Value);
- }
- if (filter.beginReviewTime.HasValue)
- {
- data = data.Where(s => s.ReviewTime >= filter.beginReviewTime.Value);
- }
- if (filter.endReviewTime.HasValue)
- {
- data = data.Where(s => s.ReviewTime <= filter.endReviewTime.Value);
- }
- var retList = data
- .Include(p => p.Reviewer)
- .Include(p => p.Creater)
- .Include(p => p.Item)
- .Include(p => p.Type)
- .ToList();
- foreach (var record in retList)
- {
- record.Creater.ItemStaffs = null;
- record.Creater.ReviewerItems = null;
- record.Creater.Customers = null;
- if (record.Item != null)
- {
- record.Item.ItemStaffs = null;
- record.Item.PreOastaff = null;
- record.Item.Reviewer = null;
- }
- }
- return retList;
- }
- public AppealRecord GetAppealRecord(int Id)
- {
- var data = Context.AppealRecords.Where<AppealRecord>(ar => ar.Id == Id);
- return data.Include(p => p.Reviewer)
- .Include(p => p.Creater)
- .Include(p => p.Item)
- .Include(p => p.Type).FirstOrDefault();
- }
- public List<InputFieldValue> GetInputFieldValues(int id, int state)
- {
- var result = Context.InputFieldValues.Where<InputFieldValue>(f => f.AppealRecordId == id && f.InputField.AppealState == state);
- return result.Include(i => i.InputField).ToList();
- }
- public List<InputFieldValue> GetAllInputFieldValue(int id)
- {
- var result = Context.InputFieldValues.Where<InputFieldValue>(f => f.AppealRecordId == id);
- return result.Include(i => i.InputField).ToList();
- }
- public List<AttachFile> GetAppealRecordAttachFiles(int appealRecordId)
- {
- var result = Context.AttachFiles.Where<AttachFile>(at => at.AppealRecordId == appealRecordId).Include(f => f.UploadUser);
- return result.ToList();
- }
- public ApiSaveResponse ChangeRecordReviewer(int RecordId,int ReviewerId)
- {
- var data = Context.AppealRecords.FirstOrDefault<AppealRecord>(ar => ar.Id == RecordId);
- if(data != null)
- {
- var reviewer = Context.Staffs.FirstOrDefault(s => s.Id == ReviewerId);
- var CurrentUser = Context.Staffs.FirstOrDefault(s=>s.Name == User.Identity.Name);
- if ((CurrentUser.Id == data.CreaterId || CurrentUser.Id == data.ReviewerId) && data.State ==0)
- {
- if (reviewer != null)
- {
- data.ReviewerId = ReviewerId;
- Context.SaveChanges();
- return new ApiSaveResponse()
- {
- Success = true
- };
- }
- else
- {
- return new ApiSaveResponse()
- {
- Success = false,
- ErrorMessage = "指定的审核人不存在"
- };
- }
- }
- else
- {
- return new ApiSaveResponse()
- {
- Success = false,
- ErrorMessage = "只有申诉人和审核人在未审核状态时才能变更审核人"
- };
- }
- }
- else
- {
- return new ApiSaveResponse()
- {
- Success = false,
- ErrorMessage = "指定的申诉记录不存在"
- };
- }
- }
-
- }
- }
|