using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Aspose.Cells.Drawing;
using JNPF.Common.Dtos.VisualDev;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.VisualDev;
using JNPF.VisualDev.Entitys.Dto.VisualDevModelData;
using JNPF.VisualDev.Entitys;
using JNPF.VisualDev.Interfaces;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Tnb.BasicData.Entities;
using Tnb.ProductionMgr.Entities;
using Tnb.ProductionMgr.Interfaces;
using JNPF.Systems.Interfaces.System;
using Tnb.BasicData;
using JNPF.Common.Extension;
using JNPF.Common.Filter;
using JNPF.Systems.Entitys.Permission;
using JNPF.Systems.Entitys.System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Tnb.ProductionMgr.Entities.Dto;
namespace Tnb.ProductionMgr
{
///
/// 生产提报记录
///
[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 _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 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 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 GetList(VisualDevModelListQueryInput input)
{
string moTaskCode = "";
if (!string.IsNullOrEmpty(input.queryJson))
{
Dictionary queryJson = JsonConvert.DeserializeObject>(input.queryJson);
if (queryJson.TryGetValue("mo_task_code", out var value))
{
moTaskCode = value;
}
}
var db = _repository.AsSugarClient();
var result = await db.Queryable()
.LeftJoin((a,b)=>a.mo_task_type==b.Id)
.LeftJoin((a,b,c)=>a.status==c.EnCode)
.LeftJoin((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()
.LeftJoin((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.SqlSugarPageResult(result);
}
private async Task AddRecord(string id,VisualDevModelDataUpInput input)
{
var db = _repository.AsSugarClient();
var result = await db.Ado.UseTranAsync(async () =>
{
List prdReports = new List();
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(prdReports).ExecuteCommandAsync();
int? sum = prdReports.Sum(x => x.reported_qty);
await db.Updateable()
.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;
}
}
}