Merge remote-tracking branch 'origin/Insight' into Insight

Insight
陈伟 2024-11-18 15:25:10 +08:00
commit 240c8a2d3b
8 changed files with 416 additions and 13 deletions

View File

@ -129,7 +129,7 @@ namespace OpenAuth.App.ServiceApp
var content = new var content = new
{ {
queryid = insTaskInfo.Id, queryid = insTaskInfo.Id,
message="有新创建的任务" message = "有新创建的任务"
}; };
var contents = Json.ToJson(content); var contents = Json.ToJson(content);
try try
@ -161,18 +161,25 @@ namespace OpenAuth.App.ServiceApp
/// 任务列表 /// 任务列表
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public async Task<Response<PageInfo<List<InsTask>>>> GteTaskListForAdmin(string taskName, int page, int limit) public async Task<Response<PageInfo<List<InsTaskForAdminResp>>>> GteTaskListForAdmin(string taskName, int page, int limit)
{ {
using (var db = base.UnitWork.CreateContext()) using (var db = base.UnitWork.CreateContext())
{ {
RefAsync<int> totalCount = 0; RefAsync<int> totalCount = 0;
var tsakList = await db.InsTask.AsQueryable() var tsakList = await db.InsTask.AsQueryable()
.LeftJoin<InsAishp>((a, b) => a.ShpId == b.Id)
.WhereIF(!string.IsNullOrEmpty(taskName), r => r.TaskName.Contains(taskName)) .WhereIF(!string.IsNullOrEmpty(taskName), r => r.TaskName.Contains(taskName))
.ToPageListAsync(page, limit, totalCount); .Select((a, b) => new InsTaskForAdminResp()
{
Id = a.Id.SelectAll(),
ShpCount = b.ShpCount,
ReceiveShpCount = SqlFunc.Subqueryable<InsTaskgroup>().Where(r => r.TaskId == a.Id && !string.IsNullOrEmpty(r.ReciveUserId)).Sum(r=>(r.EndNum-r.BeginNum)),
NoReceiveShpCount = SqlFunc.Subqueryable<InsTaskgroup>().Where(r => r.TaskId == a.Id && string.IsNullOrEmpty(r.ReciveUserId)).Sum(r => (r.EndNum - r.BeginNum)),
}).ToPageListAsync(page, limit, totalCount);
return new Response<PageInfo<List<InsTask>>> return new Response<PageInfo<List<InsTaskForAdminResp>>>
{ {
Result = new PageInfo<List<InsTask>> Result = new PageInfo<List<InsTaskForAdminResp>>
{ {
Items = tsakList, Items = tsakList,
Total = totalCount Total = totalCount

View File

@ -1,5 +1,10 @@
using Microsoft.Extensions.Configuration; using Infrastructure;
using Microsoft.Extensions.Configuration;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.Interface; using OpenAuth.App.Interface;
using OpenAuth.App.ServiceApp.Response;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using SqlSugar; using SqlSugar;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -12,16 +17,295 @@ namespace OpenAuth.App.ServiceApp
/// <summary> /// <summary>
/// 统计分析 /// 统计分析
/// </summary> /// </summary>
public class DataAnalysisApp public class DataAnalysisApp : SqlSugarBaseApp<InsTask, SugarDbContext>
{ {
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly ISqlSugarClient client; private readonly ISqlSugarClient client;
public DataAnalysisApp(IAuth auth, ISqlSugarClient sqlSugarClient, IConfiguration configuration) private readonly IAuth _auth;
public DataAnalysisApp(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<InsTask> repository, IAuth auth, ISqlSugarClient sqlSugarClient, IConfiguration configuration) : base(unitWork, repository, auth)
{ {
_configuration = configuration; _configuration = configuration;
this.client = sqlSugarClient; this.client = sqlSugarClient;
_auth = auth;
}
/// <summary>
/// 时相统计
/// </summary>
/// <returns></returns>
public async Task<Response<PageInfo<List<InsTask>>>> GteTaskListForAdmin(int month, int week, DateTime beginTime, DateTime endTime, string taskName, int page, int limit)
{
using (var db = base.UnitWork.CreateContext())
{
RefAsync<int> totalCount = 0;
var tsakList = await db.InsTask.AsQueryable()
.WhereIF(string.IsNullOrEmpty(taskName), r => r.TaskName.Contains(taskName))
.ToPageListAsync(page, limit, totalCount);
if (beginTime.Year == 0001)
{
beginTime = GetMonthStartAndEndDate(month).Start;
endTime = GetMonthStartAndEndDate(month).End;
}
var tifList = await db.InsTif.AsQueryable()
.WhereIF(beginTime.Year != 0001 || month != 0, r => r.CreateTime >= beginTime && r.CreateTime <= endTime)
.ToListAsync();
//总面积
var totalTifArea = tifList.Sum(r => r.AreaNum);
//个数
var totalTifCount = tifList.Count();
return new Response<PageInfo<List<InsTask>>>
{
Result = new PageInfo<List<InsTask>>
{
Items = tsakList,
Total = totalCount
}
};
}
}
/// <summary>
/// 每个县区的时相统计
/// </summary>
/// <returns></returns>
public async Task<Response<List<dynamic>>> GetShiXiangForArea(int month, int week, DateTime beginTime, DateTime endTime)
{
using (var db = base.UnitWork.CreateContext())
{
if (beginTime.Year == 0001)
{
beginTime = GetMonthStartAndEndDate(month).Start;
endTime = GetMonthStartAndEndDate(month).End;
}
var tifList = db.InsTif.AsQueryable()
.WhereIF(beginTime.Year != 0001 || month != 0, r => r.CreateTime >= beginTime && r.CreateTime <= endTime);
//总面积
var totalTifArea = tifList.Sum(r => r.AreaNum);
//个数
var totalTifCount = tifList.Count();
var endData = await tifList.GroupBy(r => r.AreaNum).Select<dynamic>(r => new
{
r.AreaName,
sumArea = SqlFunc.AggregateSum(r.AreaNum),
}).ToListAsync();
return new Response<List<dynamic>>
{
Result = endData,
};
}
}
/// <summary>
/// 每天时相统计
/// </summary>
/// <returns></returns>
public async Task<Response<List<dynamic>>> GetShiXiangForTime(int month, int week, DateTime beginTime, DateTime endTime)
{
using (var db = base.UnitWork.CreateContext())
{
if (beginTime.Year == 0001)
{
beginTime = GetMonthStartAndEndDate(month).Start;
endTime = GetMonthStartAndEndDate(month).End;
}
var tifList = db.InsTif.AsQueryable()
.WhereIF(beginTime.Year != 0001 || month != 0, r => r.CreateTime >= beginTime && r.CreateTime <= endTime);
//总面积
var totalTifArea = tifList.Sum(r => r.AreaNum);
//个数
var totalTifCount = tifList.Count();
var endData = await tifList.GroupBy(r => r.CreateTime).Select<dynamic>(r => new
{
r.CreateTime,
sumArea = SqlFunc.AggregateSum(r.AreaNum),
}).ToListAsync();
return new Response<List<dynamic>>
{
Result = endData,
};
}
}
static (DateTime Start, DateTime End) GetMonthStartAndEndDate(int month)
{
int year = DateTime.Now.Year; // 获取当前年份
DateTime startDate = new DateTime(year, month, 1, 0, 0, 0); // 当月起始日期
DateTime endDate = startDate.AddMonths(1).AddSeconds(-1); // 当月结束日期
return (startDate, endDate);
}
// 获取本月的起始时间和结束时间
public static (DateTime startOfMonth, DateTime endOfMonth) GetStartEndOfMonth()
{
DateTime now = DateTime.Now;
// 本月的第一天
DateTime startOfMonth = new DateTime(now.Year, now.Month, 1, 0, 0, 0);
// 本月的最后一天
DateTime endOfMonth = startOfMonth.AddMonths(1).AddSeconds(-1);
return (startOfMonth, endOfMonth);
}
public static (DateTime startOfWeek, DateTime endOfWeek) GetStartEndOfWeek()
{
DateTime now = DateTime.Now;
// 计算本周的第一天(假设一周从周一开始)
DateTime startOfWeek = now.AddDays(-((int)now.DayOfWeek - 1));
startOfWeek = startOfWeek.Date;
// 计算本周的最后一天(假设一周从周一开始,周日为最后一天)
DateTime endOfWeek = startOfWeek.AddDays(7).AddSeconds(-1);
return (startOfWeek, endOfWeek);
} }
#region 统计数据
/// <summary>
/// 小组 人员 的任务数和图斑数
/// </summary>
/// <param name="type"></param>
/// <param name="dateType"></param>
/// <returns></returns>
public async Task<Response<List<UserOrGroupTaskResp>>> GetGroupOrUserTaskAndTuBan(int type, int dateType)
{
var user = _auth.GetCurrentUser().User;
DateTime beginTime, endTime;
if (dateType == 1)
{
beginTime = GetStartEndOfMonth().startOfMonth;
endTime = GetStartEndOfMonth().endOfMonth;
}
else if (dateType == 2)
{
beginTime = GetStartEndOfWeek().startOfWeek;
endTime = GetStartEndOfWeek().endOfWeek;
}
else
{
beginTime = DateTime.Today;
endTime = DateTime.Now;
}
using (var db = base.UnitWork.CreateContext())
{
// 获取当前用户相关的所有小组用户
var groupIds = await db.SysGroup.AsQueryable()
.Where(r => r.GroupLeaderId == user.Id.ToString())
.Select(r => r.Id)
.ToListAsync();
var groupUser = await db.SysGroupuser.AsQueryable()
.Where(r => groupIds.Contains(r.GroupId) || r.UserId == user.Id.ToString())
.Select(r => r.UserId)
.ToListAsync();
// 如果是小组统计
if (type == 1)
{
var result = await db.InsTaskgroup.AsQueryable()
.LeftJoin<SysUser>((a, b) => a.ReciveUserId == b.Id.ToString())
.WhereIF(beginTime.Year != 0001, a => a.CreateTime >= beginTime && a.CreateTime <= endTime)
.WhereIF(groupUser.Count > 0, a => groupUser.Contains(a.ReciveUserId))
.GroupBy((a, b) => b.Name)
.Select((a, b) => new UserOrGroupTaskResp
{
UserName = b.Name,
TaskCount = SqlFunc.AggregateCount(a.Id),
TuBanCount = SqlFunc.AggregateSum(a.EndNum - a.BeginNum),
}).ToListAsync();
return new Response<List<UserOrGroupTaskResp>>
{
Result = result,
};
}
// 如果是人员统计
else if (type == 2)
{
var result = await db.InsTaskgroup.AsQueryable()
.LeftJoin<SysGroupuser>((a, b) => a.ReciveUserId == b.UserId)
.LeftJoin<SysGroup>((a, b, c) => b.GroupId == c.Id)
.WhereIF(beginTime.Year != 0001, a => a.CreateTime >= beginTime && a.CreateTime <= endTime)
.WhereIF(groupUser.Count > 0, a => groupUser.Contains(a.ReciveUserId))
.GroupBy((a, b, c) => c.Name)
.Select((a, b, c) => new UserOrGroupTaskResp
{
UserName = c.Name,
TaskCount = SqlFunc.AggregateCount(a.Id),
TuBanCount = SqlFunc.AggregateSum(a.EndNum - a.BeginNum),
}).ToListAsync();
return new Response<List<UserOrGroupTaskResp>>
{
Result = result,
};
}
}
return new Response<List<UserOrGroupTaskResp>>(); // 默认返回空列表
}
public async Task<Response<List<AiShpDataExpenseAccountingTableResp>>> GetAiShpDataExpenseAccountingTableOld(DateTime beginTime, DateTime endTime)
{
using (var db = base.UnitWork.CreateContext())
{
var aiShpList = db.InsAishp.AsQueryable()
.WhereIF(beginTime.Year != 0001, r => r.CreateTime >= beginTime && r.CreateTime <= endTime);
//总面积
var totalTifArea = aiShpList.Sum(r => r.AreaNum);
//个数
var totalTifCount = aiShpList.Count();
var endData = await aiShpList.GroupBy(r => r.ShpDate.Date).Select(r => new AiShpDataExpenseAccountingTableResp
{
ShapDate = r.ShpDate.Date.ToString("yyyyMMdd"),
AreaNum = SqlFunc.AggregateSum(r.AreaNum),
ShpCount = SqlFunc.AggregateSum(r.ShpCount),
}).ToListAsync();
return new Response<List<AiShpDataExpenseAccountingTableResp>>()
{
Result = endData
};
}
}
public async Task<Response<List<AiShpDataExpenseAccountingTableResp>>> GetAiShpDataExpenseAccountingTable(DateTime beginTime, DateTime endTime)
{
using (var db = base.UnitWork.CreateContext())
{
List<DateTime> dateList = new List<DateTime>();
List<AiShpDataExpenseAccountingTableResp> dataList = new List<AiShpDataExpenseAccountingTableResp>();
if (beginTime > endTime)
{
throw new ArgumentException("开始时间需要小于结束时间");
}
// 从开始日期遍历到结束日期
for (DateTime date = beginTime.Date; date <= endTime.Date; date = date.AddDays(1))
{
dateList.Add(date);
}
var aiShpList = db.InsAishp.AsQueryable().WhereIF(beginTime.Year != 0001, r => r.CreateTime >= beginTime && r.CreateTime <= endTime);
var taskList = db.InsTask.AsQueryable().WhereIF(beginTime.Year != 0001, t => t.CreateTime >= beginTime && t.CreateTime <= endTime);
for (int i = 0; i < dateList.Count; i++)
{
AiShpDataExpenseAccountingTableResp aiShpDataExpenseAccountingTableResp = new AiShpDataExpenseAccountingTableResp();
var aiShpListDay = db.InsAishp.AsQueryable().Where(r => r.ShpDate >= dateList[i]);
aiShpDataExpenseAccountingTableResp.ShapDate = dateList[i].ToString("yyyyMMdd");
//提取个数
aiShpDataExpenseAccountingTableResp.AreaNum = await aiShpListDay.SumAsync(r => r.AreaNum);
aiShpDataExpenseAccountingTableResp.ShpCount = await aiShpListDay.SumAsync(r => r.ShpCount);
//下发数量
aiShpDataExpenseAccountingTableResp.issuedCount = await taskList.Where(t => aiShpListDay.Select(r => r.Id).ToList().Contains(t.ShpId)).CountAsync();
}
//总面积
var totalTifArea = aiShpList.Sum(r => r.AreaNum);
//个数
var totalTifCount = aiShpList.Count();
return new Response<List<AiShpDataExpenseAccountingTableResp>>()
{
Result = dataList
};
}
}
#endregion
} }
} }

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenAuth.App.ServiceApp.Response
{
public class AiShpDataExpenseAccountingTableResp
{
public string ShapDate { get; set; }
public decimal AreaNum { get; set; }
public int? ShpCount { get; set; }
public int issuedCount { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using OpenAuth.Repository.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenAuth.App.ServiceApp.Response
{
public class InsTaskForAdminResp: InsTask
{
public int? ShpCount { get; set; }
public int ReceiveShpCount { get; set; }
public int NoReceiveShpCount { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using OpenAuth.Repository.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenAuth.App.ServiceApp.Response
{
public class UserOrGroupTaskResp
{
public string UserName { get; set; }
public int TaskCount { get; set; }
public int TuBanCount { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using OpenAuth.Repository.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenAuth.App.ServiceApp.Response
{
public class UserTaskResp: InsTaskgroup
{
public string UserName { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using Infrastructure; using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.ServiceApp; using OpenAuth.App.ServiceApp;
using OpenAuth.App.ServiceApp.Request; using OpenAuth.App.ServiceApp.Request;
@ -27,9 +28,9 @@ namespace OpenAuth.WebApi.Controllers.ServiceControllers
/// <param name="limit"></param> /// <param name="limit"></param>
/// <returns></returns> /// <returns></returns>
[HttpGet] [HttpGet]
public async Task<Response<PageInfo<List<InsTask>>>> GteTaskListForAdmin(string taskName, int page, int limit) public async Task<Response<PageInfo<List<InsTaskForAdminResp>>>> GteTaskListForAdmin(string taskName, int page, int limit)
{ {
Response<PageInfo<List<InsTask>>> response = new Response<PageInfo<List<InsTask>>>(); Response<PageInfo<List<InsTaskForAdminResp>>> response = new Response<PageInfo<List<InsTaskForAdminResp>>>();
try try
{ {
return await _app.GteTaskListForAdmin(taskName, page, limit); return await _app.GteTaskListForAdmin(taskName, page, limit);

View File

@ -1,4 +1,9 @@
using Microsoft.AspNetCore.Mvc; using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.ServiceApp;
using OpenAuth.App.ServiceApp.Response;
using OpenAuth.WebApi.Model.CustomAttribute;
namespace OpenAuth.WebApi.Controllers.ServiceControllers namespace OpenAuth.WebApi.Controllers.ServiceControllers
{ {
@ -9,9 +14,53 @@ namespace OpenAuth.WebApi.Controllers.ServiceControllers
[ApiController] [ApiController]
public class DataAnalysisController : ControllerBase public class DataAnalysisController : ControllerBase
{ {
public DataAnalysisController() readonly DataAnalysisApp _app;
public DataAnalysisController(DataAnalysisApp app)
{ {
_app = app;
}
/// <summary>
/// 小组或人员统计
/// </summary>
/// <param name="type">1-小组2-人员</param>
/// <param name="dateType">1-本月2-本周3-本日</param>
/// <returns></returns>
[HttpGet]
public async Task<Response<List<UserOrGroupTaskResp>>> GetGroupOrUserTaskAndTuBan(int type, int dateType)
{
Response<List<UserOrGroupTaskResp>> response = new Response<List<UserOrGroupTaskResp>>();
try
{
return await _app.GetGroupOrUserTaskAndTuBan(type, dateType);
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.InnerException?.Message ?? ex.Message;
}
return response;
}
/// <summary>
/// 解译分析数据交付与费用记账表
/// </summary>
/// <param name="beginTime"></param>
/// <param name="endTime"></param>
/// <returns></returns>
[HttpGet]
[AllowAnonymous]
public async Task<Response<List<AiShpDataExpenseAccountingTableResp>>> GetAiShpDataExpenseAccountingTable(DateTime beginTime, DateTime endTime)
{
Response<List<AiShpDataExpenseAccountingTableResp>> response = new Response<List<AiShpDataExpenseAccountingTableResp>>();
try
{
return await _app.GetAiShpDataExpenseAccountingTable(beginTime, endTime);
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.InnerException?.Message ?? ex.Message;
}
return response;
} }
} }
} }