namespace Tiobon.Core.Services; /// /// 问卷调查 (服务) /// public class Ghre_SurveyServices : BaseServices, IGhre_SurveyServices { private readonly IBaseRepository _dal; public Ghre_SurveyServices(ICaching caching, IBaseRepository dal) { this._dal = dal; base.BaseDal = dal; base._caching = caching; } public override async Task> QueryFilterPage(QueryBody filter, string condition, bool? IsEnable = true) { var result = await base.QueryFilterPage(filter, condition, IsEnable); return result; } public override async Task Add(InsertGhre_SurveyInput entity) { var result = await base.Add(entity); return result; } public override async Task Update(long Id, EditGhre_SurveyInput editModel) { var result = await base.Update(Id, editModel, null, ["Status"]); return result; } public override async Task> QueryForm(QueryForm body) { var result = await base.QueryForm(body); return result; } public async Task> QueryData(long id) { var entity = await base.QueryById(id); var data = Mapper.Map(entity).ToANew(); data.BeginEndTime.Add(data.BeginTime); data.BeginEndTime.Add(data.EndTime); if (entity.StaffId.IsNotEmptyOrNull()) data.StaffIds = JsonHelper.JsonToObj>(entity.StaffId); if (entity.DeptId.IsNotEmptyOrNull()) data.DeptIds = JsonHelper.JsonToObj>(entity.DeptId); var questions = await Db.Queryable().OrderBy(x => x.SortNo).Where(x => x.SurveyId == id).ToListAsync(); var options = await Db.Queryable().OrderBy(x => x.SortNo).Where(x => x.SurveyId == id).ToListAsync(); data.Questions = Mapper.Map(questions).ToANew>(); data.Questions.ForEach(x => { x.Options = Mapper.Map(options.Where(o => o.SurveyQuestionId == x.Id)).ToANew>(); }); return ServiceResult.OprateSuccess("成功", data); } public async Task> InsertByStatus(InsertGhre_SurveyInput insertModel, string status) { var data = ServiceResult.OprateSuccess("新增成功", 0); insertModel.Status = status; var id = await Add(insertModel); data.Success = id > 0; if (data.Success) data.Data = id; else return ServiceResult.OprateFailed("失败!"); return data; } public async Task UpdateStatus(InsertGhre_SurveyInput input, string status) { HttpRequest request = UserContext.Context.Request; var api = request.Path.ObjToString().TrimEnd('/').ToLower(); var ip = GetUserIp(UserContext.Context); var entities = new List(); foreach (var id in input.Ids) { if (!BaseDal.Any(id)) continue; var entity = await BaseDal.QueryById(id); entity.UpdateIP = ip; entity.UpdateProg = api; entity.Status = status; entities.Add(entity); } var result = await BaseDal.Update(entities); return ServiceResult.OprateSuccess("执行成功!"); } public async Task> InsertData(long id, InsertGhre_SurveyExtend insertModel) { if (insertModel.BeginEndTime != null && insertModel.BeginEndTime.Count == 2) { insertModel.BeginTime = insertModel.BeginEndTime[0]; insertModel.EndTime = insertModel.BeginEndTime[1]; } var data = ServiceResult.OprateSuccess("新增成功", id); if (id == 0) { var insert = Mapper.Map(insertModel).ToANew(); insert.Status = "Temporary"; if (insertModel.StaffIds != null && insertModel.StaffIds.Any()) insert.StaffId = JsonHelper.ObjToJson(insertModel.StaffIds); if (insertModel.DeptIds != null && insertModel.DeptIds.Any()) insert.DeptId = JsonHelper.ObjToJson(insertModel.DeptIds); id = await Add(insert); for (int i = 0; i < insertModel.Questions.Count; i++) { var question = Mapper.Map(insertModel.Questions[i]).ToANew(); //question.Id = SnowFlakeSingle.instance.getID(); question.SurveyId = id; question.SortNo = i; var questionId = await Db.Insertable(question).ExecuteReturnSnowflakeIdAsync(); for (int j = 0; j < insertModel.Questions[i].Options.Count; j++) { var option = Mapper.Map(insertModel.Questions[i].Options[j]).ToANew(); option.SurveyId = id; option.SurveyQuestionId = questionId; option.SortNo = j; await Db.Insertable(option).ExecuteReturnSnowflakeIdAsync(); } } data.Data = id; } else { await Db.Deleteable().Where(x => x.SurveyId == id).ExecuteCommandAsync(); await Db.Deleteable().Where(x => x.SurveyId == id).ExecuteCommandAsync(); var insert = Mapper.Map(insertModel).ToANew(); if (insertModel.StaffIds != null && insertModel.StaffIds.Any()) insert.StaffId = JsonHelper.ObjToJson(insertModel.StaffIds); if (insertModel.DeptIds != null && insertModel.DeptIds.Any()) insert.DeptId = JsonHelper.ObjToJson(insertModel.DeptIds); await Update(id, insert, null, ["Status"]); for (int i = 0; i < insertModel.Questions.Count; i++) { var question = Mapper.Map(insertModel.Questions[i]).ToANew(); //question.Id = SnowFlakeSingle.instance.getID(); question.SurveyId = id; var questionId = await Db.Insertable(question).ExecuteReturnSnowflakeIdAsync(); for (int j = 0; j < insertModel.Questions[i].Options.Count; j++) { var option = Mapper.Map(insertModel.Questions[i].Options[j]).ToANew(); option.SurveyId = id; option.SurveyQuestionId = questionId; await Db.Insertable(option).ExecuteReturnSnowflakeIdAsync(); } } data.Message = "修改成功!"; } return data; } public async Task> QueryESSData(long id) { var entity = await base.QueryById(id); var data = Mapper.Map(entity).ToANew(); data.BeginEndTime.Add(data.BeginTime); data.BeginEndTime.Add(data.EndTime); if (entity.StaffId.IsNotEmptyOrNull()) data.StaffIds = JsonHelper.JsonToObj>(entity.StaffId); if (entity.DeptId.IsNotEmptyOrNull()) data.DeptIds = JsonHelper.JsonToObj>(entity.DeptId); var questions = await Db.Queryable().OrderBy(x => x.SortNo).Where(x => x.SurveyId == id).ToListAsync(); var options = await Db.Queryable().OrderBy(x => x.SortNo).Where(x => x.SurveyId == id).ToListAsync(); data.Questions = Mapper.Map(questions).ToANew>(); data.Questions.ForEach(x => { x.Options = Mapper.Map(options.Where(o => o.SurveyQuestionId == x.Id)).ToANew>(); }); #region 是否存在已提交问卷数据 data.IsSubmit = await Db.Queryable().Where(x => x.StaffId == App.User.StaffId && x.SurveyId == id).AnyAsync(); #endregion #region 处理已提交信息 if (data.IsSubmit) { var recordId = await Db.Queryable().Where(x => x.StaffId == App.User.StaffId && x.SurveyId == id).Select(x => x.Id).FirstAsync(); var recordDetails = await Db.Queryable().Where(x => x.SurveyRecordId == recordId).ToListAsync(); var recordOptions = await Db.Queryable().Where(x => x.SurveyRecordId == recordId).ToListAsync(); data.Questions.ForEach(question => { switch (question.QuestionType) { case "Multiple": case "MultipleScore": question.Value1 = recordOptions.Where(x => x.SurveyQuestionId == question.Id).Select(x => x.OptionContent).ToList(); if (question.Options.Where(x => question.Value1.Contains(x.OptionNo) && x.IsOther == true).Any()) question.Options.ForEach(o => { if (question.Value1.Contains(o.OptionNo) && o.IsOther == true) o.OtherContent = recordOptions.Where(x => x.SurveyQuestionId == question.Id && x.SurveyQuestionOptionId == o.Id) .Select(x => x.Reverse1).FirstOrDefault() ?? ""; }); break; case "Single": case "ShortAnswer": case "SingleScore": case "Rate": case "Scale": question.Value = recordOptions.Where(x => x.SurveyQuestionId == question.Id).Select(x => x.OptionContent).FirstOrDefault() ?? ""; if (question.Options.Where(x => x.OptionNo == question.Value && x.IsOther == true).Any()) question.Options.ForEach(o => { if (o.OptionNo == question.Value && o.IsOther == true) o.OtherContent = recordOptions.Where(x => x.SurveyQuestionId == question.Id).Select(x => x.Reverse1).FirstOrDefault() ?? ""; }); break; default: break; } }); } #endregion return ServiceResult.OprateSuccess("成功", data); } public async Task SubmitESSData(long id, Ghre_SurveyExtend input) { var entity = await base.QueryById(id); await Db.Updateable() .SetColumns(it => new Ghre_SurveyRecord() { IsEnable = 0 }) .Where(x => x.StaffId == App.User.StaffId && x.SurveyId == id && x.IsEnable == 1) .ExecuteCommandAsync(); var questions = await Db.Queryable().OrderBy(x => x.SortNo).Where(x => x.SurveyId == id).ToListAsync(); var options = await Db.Queryable().OrderBy(x => x.SortNo).Where(x => x.SurveyId == id).ToListAsync(); //var recordId = SnowFlakeSingle.Instance.NextId(); var record = new Ghre_SurveyRecord() { //Id = recordId, SurveyId = id, StaffId = App.User.StaffId, Score = 0, AdjustScore = 0, SubmitDate = DateTime.Now, IsView = false }; var recordDetails = new List(); var recordId = await Db.Insertable(record).ExecuteReturnSnowflakeIdAsync(); for (int i = 0; i < input.Questions.Count; i++) { var question = input.Questions[i]; var recordDetailId = await Db.Insertable(new Ghre_SurveyRecordDetail() { SurveyId = id, SurveyRecordId = recordId, SurveyQuestionId = question.Id, StaffId = App.User.StaffId, Score = 0 }).ExecuteReturnSnowflakeIdAsync(); var recordOptions = new List(); switch (question.QuestionType) { case "Multiple": case "MultipleScore": for (int j = 0; j < question.Value1.Count; j++) { var option1 = new Ghre_SurveyRecordOption() { SurveyId = id, SurveyRecordId = recordId, SurveyRecordDetailId = recordDetailId, SurveyQuestionId = question.Id, //SurveyQuestionOptionId = x.Id, StaffId = App.User.StaffId, Score = 0 }; if (question.Value.IsNotEmptyOrNull()) { var questionOption1 = question.Options.Where(x => x.OptionNo == question.Value1[j]).FirstOrDefault(); option1.SurveyQuestionOptionId = questionOption1?.Id; option1.OptionContent = question.Value1[j]; if (questionOption1.IsOther == true) option1.Reverse1 = questionOption1.OtherContent; await Db.Insertable(option1).ExecuteReturnSnowflakeIdAsync(); } } break; case "Single": case "SingleScore": var option = new Ghre_SurveyRecordOption() { SurveyId = id, SurveyRecordId = recordId, SurveyRecordDetailId = recordDetailId, SurveyQuestionId = question.Id, //SurveyQuestionOptionId = x.Id, StaffId = App.User.StaffId, Score = 0 }; if (question.Value.IsNotEmptyOrNull()) { var questionOption = question.Options.Where(x => x.OptionNo == question.Value).FirstOrDefault(); option.SurveyQuestionOptionId = questionOption?.Id; option.OptionContent = question.Value; if (questionOption.IsOther == true) option.Reverse1 = questionOption.OtherContent; await Db.Insertable(option).ExecuteReturnSnowflakeIdAsync(); } break; case "Rate": case "Scale": case "ShortAnswer": option = new Ghre_SurveyRecordOption() { SurveyId = id, SurveyRecordId = recordId, SurveyRecordDetailId = recordDetailId, SurveyQuestionId = question.Id, //SurveyQuestionOptionId = x.Id, StaffId = App.User.StaffId, Score = 0 }; option.OptionContent = question.Value; await Db.Insertable(option).ExecuteReturnSnowflakeIdAsync(); break; default: break; } } return ServiceResult.OprateSuccess("提交成功!"); } public async Task Publish(long id) { var entity = await base.QueryById(id); if (entity.Status == "Publish") return ServiceResult.OprateSuccess("发布成功!"); await Db.Updateable() .SetColumns(it => new Ghre_Survey() { Status = "Publish" }) .Where(x => x.Id == id) .ExecuteCommandAsync(); var deptIds = new List(); var staffIds = new List(); if (entity.DeptId.IsNotEmptyOrNull()) deptIds = JsonHelper.JsonToObj>(entity.DeptId); if (entity.StaffId.IsNotEmptyOrNull()) staffIds = JsonHelper.JsonToObj>(entity.StaffId); var staffIds1 = await Db.Queryable().Where(x => x.DeptID != null && deptIds.Contains(x.DeptID.Value)).Select(x => x.StaffID).ToListAsync(); staffIds.AddRange(staffIds1); staffIds = staffIds.Distinct().ToList(); var userIds = await Db.Queryable().Where(x => x.UserStaffID != null && staffIds.Contains(x.UserStaffID.Value)).Select(x => new { x.UserId, x.UserPhotoUrl }).ToListAsync(); var ToDos = userIds.Select(x => new Ghrm_MessageToDo() { ToUserID = x.UserId, GroupType = "Survey", Tag = "问卷调查", DataID = 0, DataTable = "Ghre_Survey", WorkID = 0, Title = "问卷调查填写", Content = $"问卷名称:{entity.SurveyName}
填写时间:{DateTimeHelper.ConvertToMiniuteString(entity.BeginTime)}~{DateTimeHelper.ConvertToMiniuteString(entity.EndTime)}", PicType = "Url", JumpType = "link", WEBUrl = "/F_Ess_Nav_Survey/" + id, IsAgreeBtn = "N", IsRejectBtn = "N", SortNo = 1, IsDefault = 1 }).ToList(); await Db.Insertable(ToDos).ExecuteReturnIdentityAsync(); return ServiceResult.OprateSuccess("发布成功!"); } }