Files
tnb.server/ProductionMgr/Tnb.ProductionMgr/ProductionReportRecordService.cs
2024-07-29 17:12:20 +08:00

166 lines
7.9 KiB
C#

using JNPF.Common.Dtos.VisualDev;
using JNPF.Common.Filter;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.Systems.Entitys.Permission;
using JNPF.Systems.Entitys.System;
using JNPF.Systems.Interfaces.System;
using JNPF.VisualDev;
using JNPF.VisualDev.Entitys.Dto.VisualDevModelData;
using JNPF.VisualDev.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using Tnb.BasicData;
using Tnb.BasicData.Entities;
using Tnb.ProductionMgr.Entities;
using Tnb.ProductionMgr.Entities.Dto;
using Tnb.ProductionMgr.Interfaces;
namespace Tnb.ProductionMgr
{
/// <summary>
/// 生产提报记录
/// </summary>
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)]
[Route("api/[area]/[controller]/[action]")]
[OverideVisualDev(ModuleId)]
public class ProductionReportRecordService : IOverideVisualDevService, IProductionReportRecordService, IDynamicApiController, ITransient
{
private const string ModuleId = "25568191969061";
private readonly ISqlSugarRepository<PrdMoTask> _repository;
private readonly ISqlSugarClient _db;
private readonly IRunService _runService;
private readonly IVisualDevService _visualDevService;
private readonly IDictionaryDataService _dictionaryDataService;
public OverideVisualDevFunc OverideFuncs { get; } = new OverideVisualDevFunc();
public ProductionReportRecordService(ISqlSugarRepository<PrdMoTask> repository, IRunService runService, IVisualDevService visualDevService, IDictionaryDataService dictionaryDataService)
{
_db = repository.AsSugarClient();
_runService = runService;
_visualDevService = visualDevService;
OverideFuncs.GetListAsync = GetList;
OverideFuncs.UpdateAsync = AddRecord;
_dictionaryDataService = dictionaryDataService;
_repository = repository;
}
// private async Task<dynamic> GetList(VisualDevModelListQueryInput input)
// {
//
// VisualDevEntity? templateEntity = await _visualDevService.GetInfoById(ModuleId, true);
// var data = await _runService.GetListResult(templateEntity, input);
// //if (data?.list?.Count > 0)
// //{
// // var dicMoTaskStatus = await _dictionaryDataService.GetDicByTypeId(DictConst.PrdTaskStatusTypeId);
// // foreach (var row in data.list)
// // {
// // var dic = row.ToDictionary(x => x.Key, x => x.Value);
// // //if (dic.ContainsKey(nameof(PrdReportRecord.status)))
// // //{
// // // var statusCode = dic[nameof(PrdReportRecord.status)].ToString();
// // // row[nameof(PrdReportRecord.status)] = dicMoTaskStatus[statusCode];
// // //}
// // if (dic.ContainsKey(nameof(PrdReportRecord.mo_task_type)))
// // {
// // var moTypeId = dic[nameof(PrdReportRecord.mo_task_type)].ToString();
// // if (moTypeId.IsNotEmptyOrNull())
// // {
// // row[nameof(PrdReportRecord.mo_task_type)] = (await _dictionaryDataService.GetInfo(moTypeId)).FullName;
// // }
//
// // }
// // }
// //}
// return data!;
// }
private async Task<dynamic> GetList(VisualDevModelListQueryInput input)
{
string moTaskCode = "";
if (!string.IsNullOrEmpty(input.queryJson))
{
Dictionary<string, string> queryJson = JsonConvert.DeserializeObject<Dictionary<string, string>>(input.queryJson);
if (queryJson.TryGetValue("mo_task_code", out string? value))
{
moTaskCode = value;
}
}
ISqlSugarClient db = _repository.AsSugarClient();
SqlSugarPagedList<ReportRecordListOutput> result = await db.Queryable<PrdMoTask>()
.LeftJoin<PrdMo>((a,b)=>a.mo_id==b.id)
.LeftJoin<DictionaryDataEntity>((a, b,c) => b.mo_type == c.Id)
.LeftJoin<DictionaryDataEntity>((a, b, c,d) => a.mo_task_status == d.EnCode && d.DictionaryTypeId==DictConst.PrdTaskStatusTypeId)
.LeftJoin<BasMaterial>((a,b,c,d,e)=>a.material_id==e.id)
.WhereIF(!string.IsNullOrEmpty(moTaskCode), (a, b, c) => a.mo_task_code.Contains(moTaskCode))
.Select((a, b, c, d,e) => new ReportRecordListOutput
{
id = a.id,
masterial_code = e.code,
mo_code = b.mo_code,
masterial_name = e.name,
mo_task_code = a.mo_task_code,
mo_task_id = a.mo_task_code,
mo_task_id_id = a.id,
mo_task_type = c.FullName,
plan_end_date = a.estimated_end_date == null ? "" : a.estimated_end_date.Value.ToString("yyyy-mm-dd"),
plan_start_date = a.estimated_start_date == null ? "" : a.estimated_start_date.Value.ToString("yyyy-mm-dd"),
plan_qty = a.scheduled_qty,
reported_work_qty = a.reported_work_qty,
completed_qty = SqlFunc.IsNull(a.reported_work_qty, 0) + SqlFunc.IsNull(a.scrap_qty, 0),
status = d.FullName,
tablefield107 = SqlFunc.Subqueryable<PrdReport>()
.LeftJoin<UserEntity>((x, y) => x.create_id == y.Id)
.Where(x => x.mo_task_code == a.mo_task_code).ToList((x, y) => new ReportRecordListChildrenOutput
{
reported_qty = x.reported_qty,
create_id = y.RealName,
create_id_id = x.create_id,
create_time = x.create_time.ToString(DbTimeFormat.MM),
batch = x.batch,
material_box_code = x.material_box_code,
weight = x.weight
})
}).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult<ReportRecordListOutput>.SqlSugarPageResult(result);
}
private async Task<dynamic> AddRecord(string id, VisualDevModelDataUpInput input)
{
ISqlSugarClient db = _repository.AsSugarClient();
DbResult<bool> result = await db.Ado.UseTranAsync(async () =>
{
List<PrdReport> prdReports = new();
foreach (JToken item in (JArray)input.data["tablefield107"])
{
if (item["id"] == null)
{
prdReports.Add(new PrdReport()
{
mo_task_id = input.data["mo_task_id"]?.ToString(),
mo_task_code = input.data["mo_task_code"]?.ToString(),
reported_qty = item["reported_qty"] != null ? (int)item["reported_qty"] : 0,
create_id = item["create_id"]?.ToString(),
create_time = DateTime.Now,
});
}
}
_ = await db.Insertable<PrdReport>(prdReports).ExecuteCommandAsync();
// int? sum = prdReports.Sum(x => x.reported_qty);
// _ = await db.Updateable<PrdReportRecord>()
// .SetColumns(x => x.completed_qty == x.completed_qty + sum)
// .SetColumns(x => x.reported_work_qty == x.reported_work_qty + sum)
// .Where(x => x.id == input.data["id"].ToString()).ExecuteCommandAsync();
});
return result.IsSuccess;
}
}
}