Files
number_zj/20220330_Vote/Vote.Services/ApiController/UserScore20250801Service.cs
路 范 d88e4c0bcf feat(userscore): 新增用户评分功能
- 添加 UserScore20250801 相关实体、DTO 和 API 控制器
- 实现用户评分列表、专家列表和提交评分的功能- 添加前端页面以展示和提交评分数据
- 优化评分计算逻辑,处理最高分和最低分
2025-08-02 23:41:25 +08:00

289 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Ewide.Core.Util;
using Furion.DatabaseAccessor;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Furion;
using Newtonsoft.Json;
using SqlSugar;
using Vote.Services.Dto;
namespace Vote.Services.ApiController
{
/// <summary>
/// 项目
/// </summary>
[ApiDescriptionSettings("userscore", Order = 0)]
[Route("/gb/yjb/api/userscore20250801")]
public class UserScore20250801Service : IDynamicApiController
{
private readonly IRepository<Entities.userscores20250801> repuserscore20250801;
private readonly Ewide.Core.SqlSugarRepository<Entities.userscores20250801> repuserscore;
public UserScore20250801Service(IRepository<Entities.userscores20250801> _repuserscore20250801
, Ewide.Core.SqlSugarRepository<Entities.userscores20250801> _repuserscore)
{
repuserscore20250801 = _repuserscore20250801;
repuserscore = _repuserscore;
}
/// <summary>
/// 列表
/// </summary>
/// <returns></returns>
[HttpPost]
[Microsoft.AspNetCore.Authorization.AllowAnonymous]
public async Task<dynamic> List()
{
var data = await repuserscore.AsQueryable()
.OrderBy(a => a.No)
.ToListAsync();
// var data = await repuserscore20250801.DetachedEntities
// .OrderBy(a => a.No)
// .ToListAsync();
return data;
}
[HttpPost]
[Microsoft.AspNetCore.Authorization.AllowAnonymous]
public async Task<dynamic> Experts()
{
var list = new List<ExpertColumn>();
var number = App.GetConfig<int>("UserScore:ExpertNumber");
for (int i = 1; i < number + 1; i++)
{
list.Add(new ExpertColumn() { prop = "expert" + i, label = "专家" + i });
}
return list;
}
/// <summary>
/// 提交
/// </summary>
/// <returns></returns>
[HttpPost]
[UnitOfWork]
[Microsoft.AspNetCore.Authorization.AllowAnonymous]
public async Task<dynamic> SubmitSubmit(List<SubmitEntity> args)
{
var list = await repuserscore.AsQueryable().ToListAsync();
foreach (var entity in list)
{
var submituser = args.FirstOrDefault(a => a.userId == entity.Id);
if (submituser == null)
continue;
entity.scoresjson = JsonConvert.SerializeObject(submituser.scores);
entity.finalscore = submituser.finalscore;
entity.UpdatedTime = DateTime.Now.GetNow();
}
var r = await repuserscore.UpdateAsync(list); //.AsUpdateable(list) //.UpdateColumns(it => new { it.scoresjson, it.finalscore, it.UpdatedTime })
// .ExecuteCommandAsync();
// args.ForEach(async submituser =>
// {
// //计算平均分
// var scores = submituser.scores;
// // // 修正排序逻辑:先按分数升序排列,再排除首尾元素
// // var sortedScores = scores.OrderBy(x => x.score).ToList();
// // if (sortedScores.Count > 2)
// // {
// // sortedScores.RemoveAt(sortedScores.Count - 1); // 移除最高分
// // sortedScores.RemoveAt(0); // 移除最低分
// // }
// //
// // //取2位小数使用处理后的分数
// // var finalscore = Math.Round(sortedScores.Sum(a => a.score) / sortedScores.Count, 2);
// //根据id更新
// var entity = list.FirstOrDefault(a => a.Id == submituser.userId);
// entity.scoresjson = JsonConvert.SerializeObject(submituser.scores);
// entity.finalscore = submituser.finalscore;
// entity.UpdatedTime = DateTime.Now.GetNow();
// });
// var r = await repuserscore.AsUpdateable(entity).UpdateColumns(it => new { it.scoresjson, it.finalscore }).ExecuteCommandAsync();
//
return r == list.Count;
}
// /// <summary>
// /// 检验提交码
// /// </summary>
// /// <returns></returns>
// [HttpPost]
// [Microsoft.AspNetCore.Authorization.AllowAnonymous]
// public async Task<dynamic> CheckSubmitCode(CheckSubmitCodeInput args)
// {
// var data = await rep_Experts.DetachedEntities.Where(p => !p.IsDeleted)
// .Where(a => a.login_code == args.code)
// .FirstOrDefaultAsync();
// return data != null;
// }
//
//
// /// <summary>
// /// 提交
// /// </summary>
// /// <returns></returns>
// [HttpPost]
// [UnitOfWork]
// [Microsoft.AspNetCore.Authorization.AllowAnonymous]
// public async Task<dynamic> SubmitSubmitVote(SubmitInput args)
// {
// var data = await rep_Experts.DetachedEntities.Where(p => !p.IsDeleted)
// .Where(a => a.login_code == args.code)
// .FirstOrDefaultAsync();
// _ = (data == null) ? throw Oops.Oh("提交码错误") : 1;
// //var list = args.projects.Adapt<List<Entities.VoteRecords>>();
// //删除这个专家上次提交的结果
// //或者提示不能再次提交
// _ = (await rep_VoteRecords.DetachedEntities.Where(a => !a.IsDeleted && a.expert_login_code == args.code).CountAsync() > 0) ? throw Oops.Oh("已提交,无需再次提交") : 1;
// var now = DateTime.Now;
// args.projects.ForEach(async a =>
// {
// var model = new Entities.VoteRecords
// {
// expert_login_code = args.code,
// project_id = a.id,
// is_agree = a.vote,
// vote_time = now
// };
// await model.InsertOrUpdate();
// });
// return true;
// }
//
// private async Task<List<ProjectsList2Output>> GetVoteData()
// {
// var query = from a in rep_Projects.DetachedEntities
// join b in rep_VoteRecords.DetachedEntities
// on a.Id equals b.project_id into grouping
// from p in grouping.DefaultIfEmpty()
// group new { a, p } by new { a.Id, a.name, a.serial_number, a.type }
// into pp
// select new ProjectsList2Output
// {
// serial_number = pp.Key.serial_number,
// id = pp.Key.Id,
// name = pp.Key.name,
// yes_count = pp.Where(a => a.p.is_agree).Count(),
// no_count = pp.Where(a => !a.p.is_agree).Count(),
// type = pp.Key.type
// };
// return await query.OrderBy(a => a.serial_number).ToListAsync();
// }
//
// /// <summary>
// /// 列表
// /// </summary>
// /// <returns></returns>
// [HttpPost]
// public async Task<dynamic> List2()
// {
// //var data = rep_Projects.DetachedEntities.Where(p => !p.IsDeleted)
// // //.Where(args.type != null, a => (int)a.type == args.type)
// // .Join(rep_VoteRecords.DetachedEntities, a => a.Id, a => a.project_id, (a, b) =>
// // new
// // {
// // //a.Id,
// // //type = (int)a.type,
// // //serial_number = a.serial_number,
// // //name = a.name,
// // //no_count = b.Where(bb => !bb.is_agree).Count(),
// // //yes_count = b.Where(bb => bb.is_agree).Count(),
// // }).ToList();
// ////.ProjectToType<ProjectsList2Output>()
// ////.OrderBy(a => a.serial_number)
// ////.ToListAsync();
// var data = await GetVoteData();
// //var data0 = data.Where(a => a.type == Entities.EnumProjectType.FangJian).ToList();
// //var data1 = data.Where(a => a.type == Entities.EnumProjectType.ShiZheng).ToList();
// //var data2 = data.Where(a => a.type == Entities.EnumProjectType.GuiDaoGongCheng).ToList();
// //var data3 = data.Where(a => a.type == Entities.EnumProjectType.DianLiGongCheng).ToList();
// //var data4 = data.Where(a => a.type == Entities.EnumProjectType.JiaoTongGongCheng).ToList();
// //var data5 = data.Where(a => a.type == Entities.EnumProjectType.ShuiLiGongCheng).ToList();
// //return new
// //{
// // data0,
// // data1,
// // data2,
// // data3,
// // data4,
// // data5
// //};
// var typeList = new List<string> { "房建工程", "市政工程", "轨道交通工程" }; //, "电力工程", "交通工程", "水利工程" };
// return new { data, typeList };
// }
//
// /// <summary>
// ///
// /// </summary>
// /// <returns></returns>
// public async Task<dynamic> Download()
// {
// var data = await GetVoteData();
// //var filepath = Tools.ExcelHelper.WriteTemplate(data, 4, "C");
// string template_name = "2024年度宁波市结构优质认定项目投票结果.xlsx";
// var filepath = Tools.ExcelHelper.WriteTemplate(template_name, data, 5, "C");
// return new FileStreamResult(new FileStream(filepath, FileMode.Open), "application/octet-stream") { FileDownloadName = filepath };
// }
//
// /// <summary>
// ///
// /// </summary>
// /// <returns></returns>
// public async Task<dynamic> ExpertVote()
// {
// var list = await rep_Experts.DetachedEntities.GroupJoin(rep_VoteRecords.DetachedEntities, a => a.login_code, a => a.expert_login_code, (a, b) => new { a, b })
// .SelectMany(a => a.b.DefaultIfEmpty(), (a, b) => new { a.a.Id, a.a.login_code, is_vote = b != null })
// .Distinct()
// .OrderBy(a => a.Id)
// .ToListAsync();
// return list;
// //var query = from a in rep_Experts.DetachedEntities
// // join b in rep_VoteRecords.DetachedEntities on a.login_code equals b.expert_login_code into temp
// // from tt in temp.DefaultIfEmpty()
// // select new
// // {
// // a.Id,
// // a.login_code,
// // is_vote = tt == null
// // };
// //return await query.ToListAsync();
// }
//
// /// <summary>
// /// 新增专家
// /// </summary>
// /// <returns></returns>
// [HttpPost]
// public async Task AddExpert(AddExpertInput args)
// {
// if (args == null || args.expertnum <= 0)
// throw Oops.Oh("参数异常");
// var list = new List<Experts>();
// for (int i = 0; i < args.expertnum; i++)
// {
// var newid = Ulid.NewUlid().ToString();
// var newidsimple = newid.ToUpper().Replace("I", "").Replace("L", "").Replace("0", "").Replace("O", "").Replace("1", "");
// list.Add(new Experts
// {
// Id = newid,
// login_code = newidsimple.Substring(newidsimple.Length - 6, 6),
// CreatedTime = DateTime.Now
// });
// }
//
// await rep_Experts.InsertAsync(list);
// }
}
}