Files
tnb.server/ProductionMgr/Tnb.ProductionMgr/ProductionReportRecordService.cs
2023-11-06 19:35:59 +08:00

161 lines
7.6 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.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 var value))
{
moTaskCode = value;
}
}
var db = _repository.AsSugarClient();
var result = await db.Queryable<PrdReportRecord>()
.LeftJoin<DictionaryDataEntity>((a, b) => a.mo_task_type == b.Id)
.LeftJoin<DictionaryDataEntity>((a, b, c) => a.status == c.EnCode)
.LeftJoin<PrdMoTask>((a, b, c, d) => a.mo_task_id == d.id)
.WhereIF(!string.IsNullOrEmpty(moTaskCode), (a, b, c) => a.mo_task_code.Contains(moTaskCode))
.Select((a, b, c, d) => new ReportRecordListOutput
{
id = a.id,
masterial_code = a.masterial_code,
masterial_name = a.masterial_name,
mo_task_code = a.mo_task_code,
mo_task_id = a.mo_task_code,
mo_task_id_id = a.mo_task_id,
mo_task_type = b.FullName,
plan_end_date = a.plan_end_date == null ? "" : a.plan_end_date.Value.ToString("yyyy-mm-dd"),
plan_start_date = a.plan_start_date == null ? "" : a.plan_start_date.Value.ToString("yyyy-mm-dd"),
plan_qty = d.scheduled_qty,
reported_work_qty = a.reported_work_qty,
completed_qty = SqlFunc.IsNull(d.reported_work_qty, 0) + SqlFunc.IsNull(d.scrap_qty, 0),
status = c.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 == null ? "" : x.create_time.Value.ToString(DbTimeFormat.MM),
batch = x.barcode
})
}).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult<ReportRecordListOutput>.SqlSugarPageResult(result);
}
private async Task<dynamic> AddRecord(string id, VisualDevModelDataUpInput input)
{
var db = _repository.AsSugarClient();
var result = await db.Ado.UseTranAsync(async () =>
{
List<PrdReport> prdReports = new List<PrdReport>();
foreach (var 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;
}
}
}