Infrastructure/OpenAuth.App/ServiceApp/DataAnalysisApp.cs

312 lines
14 KiB
C#
Raw Normal View History

using Infrastructure;
using Microsoft.Extensions.Configuration;
using OpenAuth.App.BaseApp.Base;
2024-11-14 15:01:46 +08:00
using OpenAuth.App.Interface;
using OpenAuth.App.ServiceApp.Response;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
2024-11-14 15:01:46 +08:00
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenAuth.App.ServiceApp
{
/// <summary>
/// 统计分析
/// </summary>
public class DataAnalysisApp : SqlSugarBaseApp<InsTask, SugarDbContext>
2024-11-14 15:01:46 +08:00
{
private readonly IConfiguration _configuration;
private readonly ISqlSugarClient client;
private readonly IAuth _auth;
public DataAnalysisApp(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<InsTask> repository, IAuth auth, ISqlSugarClient sqlSugarClient, IConfiguration configuration) : base(unitWork, repository, auth)
2024-11-14 15:01:46 +08:00
{
_configuration = configuration;
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);
2024-11-14 15:01:46 +08:00
}
#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
2024-11-14 15:01:46 +08:00
}
}