Files
tnb.server/QcMgr/Tnb.QcMgr/QcCheckTaskService.cs

690 lines
37 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using JNPF.Common.Core.Manager;
using JNPF.Common.Filter;
using JNPF.Common.Security;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.FriendlyException;
using JNPF.JsonSerialization;
using JNPF.Logging;
using JNPF.Systems.Entitys.Permission;
using JNPF.Systems.Entitys.System;
using JNPF.VisualDev;
using JNPF.VisualDev.Entitys.Dto.VisualDevModelData;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SqlSugar;
using Tnb.BasicData.Entities;
using Tnb.EquipMgr.Entities;
using Tnb.ProductionMgr.Entities.Dto.PrdManage;
using Tnb.QcMgr.Entities;
using Tnb.QcMgr.Interfaces;
using Tnb.WarehouseMgr.Entities.Consts;
using Tnb.WarehouseMgr.Entities.Dto.Inputs;
using Tnb.WarehouseMgr.Interfaces;
using Tnb.ProductionMgr.Interfaces;
using Tnb.ProductionMgr.Entities;
using Tnb.ProductionMgr.Entities.Dto;
using Tnb.WarehouseMgr.Entities.Enums;
using Tnb.ProductionMgr.Entities.Dto.PrdManage;
using Tnb.WarehouseMgr.Entities;
using JNPF.Common.Extension;
namespace Tnb.QcMgr
{
/// <summary>
/// 质检任务模块
/// </summary>
[ApiDescriptionSettings(Tag = ModuleConsts.Tag, Area = ModuleConsts.Area, Order = 800)]
[Route("api/[area]/[controller]/[action]")]
[OverideVisualDev(ModuleId)]
public class QcCheckTaskService : IQcCheckTaskService, IDynamicApiController, ITransient, IOverideVisualDevService
{
private const string ModuleId = "26745613138709";
private readonly ISqlSugarRepository<QcCheckExecH> _repository;
private readonly IUserManager _userManager;
private readonly IWmsSaleService _wmsSaleService;
private readonly IWmsPurchaseService _wmsPurchaseService;
private readonly IPrdMoTaskService _prdMoTaskService;
private static SemaphoreSlim prdreportSemaphore = new(1);
public OverideVisualDevFunc OverideFuncs { get; } = new OverideVisualDevFunc();
public QcCheckTaskService(ISqlSugarRepository<QcCheckExecH> repository,
IUserManager userManager,
IWmsSaleService wmsSaleService,
IPrdMoTaskService prdMoTaskService,
IWmsPurchaseService wmsPurchaseService)
{
_repository = repository;
_userManager = userManager;
OverideFuncs.GetListAsync = GetListAsync;
_wmsSaleService= wmsSaleService;
_prdMoTaskService = prdMoTaskService;
_wmsPurchaseService= wmsPurchaseService;
}
private async Task<dynamic> GetListAsync(VisualDevModelListQueryInput input)
{
ISqlSugarClient db = _repository.AsSugarClient();
Dictionary<string, string> dic = new()
{
{ "ok", "合格" },
{ "no", "不合格" },
{ "barelyOk", "让步接收" },
{ "await", "待检" },
{ "temporarily", "暂控" }
};
Dictionary<string, string>? queryJson = !string.IsNullOrEmpty(input.queryJson) ? JsonConvert.DeserializeObject<Dictionary<string, string>>(input.queryJson) : new Dictionary<string, string>();
string materialid = queryJson.ContainsKey("materialid") ? queryJson["materialid"].ToString() : "";
string checktype = queryJson.ContainsKey("checktype") ? queryJson["checktype"].ToString() : "";
string status = queryJson.ContainsKey("status") ? queryJson["status"].ToString() : "";
List<DictionaryDataEntity> list = await db.Queryable<DictionaryDataEntity>()
.LeftJoin<DictionaryTypeEntity>((a, b) => a.DictionaryTypeId == b.Id)
.Where((a, b) => b.FullName == "质检状态" || b.FullName == "质检类型选择").ToListAsync();
List<BasLocation> BasLocations = await db.Queryable<BasLocation>().ToListAsync();
SqlSugarPagedList<QcCheckExecHOut> result = await db.Queryable<QcCheckExecH>()
.LeftJoin<BasMaterial>((a, b) => a.materialid == b.id)
.LeftJoin<BasProcess>((a, b, c) => a.processid == c.id)
.LeftJoin<OrganizeEntity>((a, b, c, d) => a.workid == d.Id)
.LeftJoin<UserEntity>((a, b, c, d, e) => a.execuser == e.Id)
.LeftJoin<PrdReport>((a,b,c,d,e,f)=>a.report_id==f.id)
.LeftJoin<PrdMoTask>((a,b,c,d,e,f,g)=>a.mo_task_code==g.mo_task_code)
.LeftJoin<EqpEquipment>((a,b,c,d,e,f,g,h)=>g.eqp_id==h.id)
.WhereIF(!string.IsNullOrEmpty(materialid), (a, b, c, d,e) => b.name.Contains(materialid))
.WhereIF(!string.IsNullOrEmpty(checktype), (a, b, c, d, e) => a.checktype == checktype)
.WhereIF(!string.IsNullOrEmpty(status), (a, b, c, d, e) => a.status == status)
.Where((a, b, c, d, e) => a.status == list.Where(p => p.FullName == "待执行").First().Id)
.Select((a, b, c, d, e,f,g,h) => new QcCheckExecHOut
{
id = a.id,
materialid = b.name,
checktype = a.checktype,
workid = d.FullName,
processid = c.process_name,
wareid = a.wareid,
checknum = a.checknum,
status = a.status,
result = a.result,
tasktime = a.tasktime ?? "",
exectime = a.exectime ?? "",
execuser = e.RealName ?? "",
mo_task_code = a.mo_task_code,
batch = a.batch,
qty = a.qty,
rqty = a.rqty,
material_standard = b.material_standard,
material_specification = b.material_specification,
equip_name = h.name,
extras = h.name,
f_flowid = b.material_standard,
f_flowtaskid = b.material_specification,
}).OrderByDescending(a => DateTime.Parse(a.tasktime)).ToPagedListAsync(input.currentPage, input.pageSize);
foreach (QcCheckExecHOut? item in result.list)
{
item.checktype = list.Select(p => p.Id).Contains(item.checktype) ? list.Where(p => p.Id == item.checktype).First().FullName : "";
item.status = list.Select(p => p.Id).Contains(item.status) ? list.Where(p => p.Id == item.status).First().FullName : "";
item.result = dic.Where(p => p.Key == item.result).Any() ? dic.Where(p => p.Key == item.result).First().Value : "";
item.wareid = BasLocations.Where(p => p.id == item.wareid).Any() ? BasLocations.Where(p => p.id == item.wareid).First().location_code : "";
}
return PageResult<QcCheckExecHOut>.SqlSugarPageResult(result);
}
/// <summary>
/// 获取任务执行明细
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
public async Task<dynamic> GetTaskItems(string id)
{
ISqlSugarClient db = _repository.AsSugarClient();
QcCheckExecH QcCheckExecH = await db.Queryable<QcCheckExecH>().Where(p => p.id == id).FirstAsync();
List<QcCheckExecD> QcCheckExecDs = await db.Queryable<QcCheckExecD>().Where(p => p.mainid == id).ToListAsync();
List<QcCheckItem> QcCheckItems = await db.Queryable<QcCheckItem>().ToListAsync();
List<QcCheckType> QcCheckTypes = await db.Queryable<QcCheckType>().ToListAsync();
List<QcErrorCause> QcErrorCauses = await db.Queryable<QcErrorCause>().ToListAsync();
List<QcErrorLevel> QcErrorLevels = await db.Queryable<QcErrorLevel>().ToListAsync();
PrdMoTask moTask = await db.Queryable<PrdMoTask>().Where(x => x.mo_task_code == QcCheckExecH.mo_task_code).FirstAsync();
if (moTask == null)
{
throw Oops.Bah($"未找到任务单{QcCheckExecH.mo_task_code}");
}
// if (string.IsNullOrEmpty(QcCheckExecH.report_id))
// {
// throw Oops.Bah($"未找到提报记录{QcCheckExecH.report_id}");
// }
BasMaterial basMaterial = await db.Queryable<BasMaterial>().Where(x=>x.id==moTask.material_id).FirstAsync();
PrdReport prdReport = await db.Queryable<PrdReport>().SingleAsync(x => x.id == QcCheckExecH.report_id);
UserEntity userEntity = null;
if (prdReport != null)
{
userEntity = await db.Queryable<UserEntity>().SingleAsync(x => x.Id == prdReport.create_id);
}
CheckTaskOut CheckTaskOut = new()
{
mainid = id,
wareid = QcCheckExecH.wareid!,
workid = QcCheckExecH.workid!,
status = QcCheckExecH.status!,
mo_task_code = QcCheckExecH.mo_task_code,
material_id = moTask.material_id,
material_code = basMaterial.code,
material_name = basMaterial.name,
checknum = !string.IsNullOrEmpty(QcCheckExecH.checknum) ? int.Parse(QcCheckExecH.checknum) : 0,
worker_name = userEntity?.RealName ?? "",
bill_code = QcCheckExecH.bill_code,
carry_code = prdReport!=null ? prdReport.material_box_code : "",
};
if (!string.IsNullOrEmpty(CheckTaskOut.workid))
{
CheckTaskOut.workname = db.Queryable<OrganizeEntity>().Where(p => p.Id == CheckTaskOut.workid).First()?.FullName;
}
CheckTaskOut.checktypes = new List<CheckExecTypeOut>();
foreach (QcCheckExecD QcCheckExecD in QcCheckExecDs)
{
if (CheckTaskOut.checktypes.Where(p => p.checktypeid == QcCheckExecD.typeid).ToList().Count == 0)
{
CheckExecTypeOut checkType = new()
{
checktypeid = QcCheckExecD.typeid!,
checktypename = QcCheckTypes.Where(p => p.id == QcCheckExecD.typeid).First().name!,
items = new List<ExecItemOut>()
};
CheckTaskOut.checktypes.Add(checkType);
}
ExecItemOut Item = new()
{
itemid = QcCheckExecD.itemid!,
itemdid = QcCheckExecD.id!,
code = QcCheckItems.Where(p => p.id == QcCheckExecD.itemid).First().code!,
name = QcCheckItems.Where(p => p.id == QcCheckExecD.itemid).First().name!,
setData = new ExecItemData
{
extype = QcCheckExecD.extype!,
excontent = JSON.Deserialize<Excontent>(QcCheckExecD.excontent!),
check = QcCheckExecD.check!
}
};
if (!string.IsNullOrEmpty(QcCheckExecD.errorcause))
{
string[] strs = QcCheckExecD.errorcause!.Replace("[", "").Replace("]", "").Split(',', StringSplitOptions.RemoveEmptyEntries);
Item.setData.errorcause = new List<Error>();
foreach (string str in strs)
{
if (string.IsNullOrEmpty(str) || str == "null") continue;
Item.setData.errorcause.Add(new Error { id = str, name = QcErrorCauses.Where(p => p.id == str).First()?.name! });
}
}
if (!string.IsNullOrEmpty(QcCheckExecD.errorlevel))
{
string[] strs = QcCheckExecD.errorlevel!.Replace("[", "").Replace("]", "").Split(',', StringSplitOptions.RemoveEmptyEntries);
Item.setData.errorlevel = new List<Error>();
foreach (string str in strs)
{
if (string.IsNullOrEmpty(str) || str == "null") continue;
Item.setData.errorlevel.Add(new Error { id = str, name = QcErrorLevels.Where(p => p.id == str).First()?.name! });
}
}
Item.setData.remark = QcCheckExecD.remark!;
Item.setData.attachment = QcCheckExecD.attachment!;
Item.setData.customer = QcCheckExecD.custom!;
if (!string.IsNullOrEmpty(QcCheckExecD.isexec))
{
Item.setData.isexec = JSON.Deserialize<IsexecE>(QcCheckExecD.isexec!);
}
Item.setShow = new ExecItemShow
{
extype = !string.IsNullOrEmpty(Item.setData.extype),
excontent = !string.IsNullOrEmpty(QcCheckExecD.excontent),
check = !string.IsNullOrEmpty(Item.setData.check),
errorcause = Item.setData.errorcause != null && (Item.setData.errorcause?.Count) != 0,
errorlevel = Item.setData.errorlevel != null && (Item.setData.errorlevel?.Count) != 0,
remark = !string.IsNullOrEmpty(Item.setData.remark),
attachment = !string.IsNullOrEmpty(Item.setData.attachment),
customer = !string.IsNullOrEmpty(Item.setData.customer),
isexec = Item.setData.isexec != null
};
CheckTaskOut.checktypes.Where(p => p.checktypeid == QcCheckExecD.typeid).First()?.items?.Add(Item);
}
return CheckTaskOut;
}
/// <summary>
/// 执行任务
/// </summary>
/// <param name="CheckTaskInput"></param>
/// <returns></returns>
[HttpPost]
public async Task SaveData(CheckTaskInput CheckTaskInput)
{
await prdreportSemaphore.WaitAsync();
ISqlSugarClient db = _repository.AsSugarClient();
try
{
Dictionary<string, int> dic = new()
{
{ "ok", 1 },
{ "no", 2 },
{ "barelyOk", 4 },
{ "await", 8 },
{ "temporarily", 16 },
{ "checking", 32 },
};
QcCheckExecH QcCheckExecH = await db.Queryable<QcCheckExecH>().Where(p => p.id == CheckTaskInput.mainid).FirstAsync();
if (QcCheckExecH == null)
throw Oops.Bah($"未找到质检任务:{CheckTaskInput.mainid}");
DictionaryTypeEntity DictionaryType = await db.Queryable<DictionaryTypeEntity>().Where(p => p.FullName == "质检状态").FirstAsync();
DictionaryDataEntity DictionaryData = await db.Queryable<DictionaryDataEntity>().Where(p => p.DictionaryTypeId == DictionaryType.Id && p.FullName == "已完成").FirstAsync();
QcCheckExecH.checknum = CheckTaskInput.checknum;
QcCheckExecH.status = DictionaryData.Id;
QcCheckExecH.result = CheckTaskInput.result;
QcCheckExecH.execuser = _userManager.UserId;
QcCheckExecH.exectime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
QcCheckExecH.check_type = CheckTaskInput.check_type;
QcCheckExecH.remark = CheckTaskInput.remark;
QcCheckExecH.attachment = CheckTaskInput.attachment;
#region
if (CheckTaskInput.result == "no")//质检结论不合格,记录暂控时间
{
if(CheckTaskInput.startEndTime==null || CheckTaskInput.startEndTime.Length <= 1)
{
throw Oops.Bah("不合格时,请选择时间区间");
}
QcCheckExecH.control_start_time = CheckTaskInput.startEndTime[0].TimeStampToDateTime().ToString("yyyy-MM-dd HH:mm:ss");
QcCheckExecH.control_end_time = CheckTaskInput.startEndTime[1].TimeStampToDateTime().ToString("yyyy-MM-dd HH:mm:ss");
}
#endregion
List<QcCheckExecD> QcCheckExecDs = await db.Queryable<QcCheckExecD>().Where(p => p.mainid == CheckTaskInput.mainid).ToListAsync();
int rqty = 0;
List<QcCheckExecD> QcCheckExecDdel = new();
List<QcCheckExecD> QcCheckExecDinsert = new();
if (CheckTaskInput.checktypes?.Count > 0)
{
for (int i = 0; i < CheckTaskInput.checktypes.Count; i++)
{
if (CheckTaskInput.checktypes[i].Count > 0)
{
foreach (Exextype exextype in CheckTaskInput.checktypes[i])
{
if (exextype.items != null)
{
foreach (ExecItemInput item in exextype.items)
{
if (item.postItemForm == null)
{
throw Oops.Oh("执行失败");
}
QcCheckExecD? QcCheckExecD = QcCheckExecDs.Where(p => p.id == item.itemdid).FirstOrDefault();
if (QcCheckExecD == null)
{
continue;
}
if (QcCheckExecDdel.Where(p => p.id == QcCheckExecD.id).FirstOrDefault() == null)
{
QcCheckExecDdel.Add(QcCheckExecD);
}
QcCheckExecD insert = new()
{
mainid = QcCheckExecD.mainid,
extype = QcCheckExecD.extype,
excontent = QcCheckExecD.excontent,
check = QcCheckExecD.check,
errorcause = QcCheckExecD.errorcause,
errorlevel = QcCheckExecD.errorlevel,
remark = QcCheckExecD.remark,
attachment = QcCheckExecD.attachment,
isexec = QcCheckExecD.isexec,
custom = QcCheckExecD.custom,
typeid = QcCheckExecD.typeid,
itemid = QcCheckExecD.itemid,
create_id = QcCheckExecD.create_id,
create_time = QcCheckExecD.create_time,
postdata = item.postItemForm,
result = item.result,
checkindex = (i + 1).ToString(),
qty = item.qty
};
QcCheckExecDinsert.Add(insert);
rqty += item.qty;
}
}
}
}
}
}
await db.Ado.BeginTranAsync();
QcCheckExecH.qty = int.Parse(CheckTaskInput.checknum) - rqty;
QcCheckExecH.rqty = rqty;
//_ = await db.Updateable(QcCheckExecH).ExecuteCommandAsync();
await db.Updateable<QcCheckExecH>()
.SetColumns(x => x.checknum == QcCheckExecH.checknum)
.SetColumns(x => x.status == QcCheckExecH.status)
.SetColumns(x => x.result == QcCheckExecH.result)
.SetColumns(x => x.execuser == QcCheckExecH.execuser)
.SetColumns(x => x.exectime == QcCheckExecH.exectime)
.SetColumns(x => x.check_type == QcCheckExecH.check_type)
.SetColumns(x => x.remark == QcCheckExecH.remark)
.SetColumns(x => x.attachment == QcCheckExecH.attachment)
.SetColumns(x => x.qty == QcCheckExecH.qty)
.SetColumns(x => x.rqty == QcCheckExecH.rqty)
.Where(x => x.id == CheckTaskInput.mainid)
.ExecuteCommandAsync();
_ = await db.Deleteable(QcCheckExecDdel).ExecuteCommandAsync();
_ = await db.Insertable(QcCheckExecDinsert).ExecuteCommandAsync();
//出厂检
if (QcCheckExecH.checktype == "26589783783701" && !string.IsNullOrEmpty(QcCheckExecH.extras))
{
MesCheckdCallbackUpinput mesCheckdCallbackUpinput = new MesCheckdCallbackUpinput();
mesCheckdCallbackUpinput.maintableid = QcCheckExecH.extras;
mesCheckdCallbackUpinput.check_conclusion = dic.Where(p => p.Key == CheckTaskInput.result).Any() ? dic.Where(p => p.Key == CheckTaskInput.result).First().Value : 0;
await _wmsSaleService.MesCheckdSaleCallback(mesCheckdCallbackUpinput);
}
//入厂检
else if (QcCheckExecH.checktype == "26589773352981" && !string.IsNullOrEmpty(QcCheckExecH.extras))
{
MesCheckdCallbackUpinput mesCheckdCallbackUpinput = new MesCheckdCallbackUpinput();
mesCheckdCallbackUpinput.maintableid = QcCheckExecH.extras;
mesCheckdCallbackUpinput.check_conclusion = dic.Where(p => p.Key == CheckTaskInput.result).Any() ? dic.Where(p => p.Key == CheckTaskInput.result).First().Value : 0;
await _wmsPurchaseService.MesCheckdPurchaseCallback(mesCheckdCallbackUpinput);
}else if (QcCheckExecH.checktype == WmsWareHouseConst.LINGBUJIANZUIZHONGJIANYAN_ID)
{
PrdReport prdReport = await db.Queryable<PrdReport>().SingleAsync(x=>x.id==QcCheckExecH.report_id);
decimal pqty = prdReport.reported_qty.Value - rqty;
if (rqty > Decimal.Parse(QcCheckExecH.checknum))
{
throw Oops.Bah("不合格数不能大于抽样数");
}
if (CheckTaskInput.result != "await")
{
string isCheck = dic[CheckTaskInput.result].ToString();
await db.Updateable<WmsCarryH>()
.SetColumns(x => x.is_check == isCheck)
.Where(x => x.carry_code == prdReport.material_box_code)
.ExecuteCommandAsync();
await _prdMoTaskService.ReportInstock(new CheckCompleteInput()
{
report_id = QcCheckExecH.report_id,
pqty = pqty,
rqty = rqty,
check_result = ((EnumCheckConclusion)dic[CheckTaskInput.result]).ToString(),
},prdReport,db);
}
}else if (QcCheckExecH.checktype == WmsWareHouseConst.XUNJIAN_ID || QcCheckExecH.checktype == WmsWareHouseConst.SHOUJIAN_ID || QcCheckExecH.checktype == WmsWareHouseConst.MOJIAN_ID )
{
PrdMoTask prdMoTask = await db.Queryable<PrdMoTask>().Where(x=>x.mo_task_code==QcCheckExecH.mo_task_code && x.id!=null).FirstAsync();
if (CheckTaskInput.result == "no")
{
string pauseReason = QcCheckExecH.checktype == WmsWareHouseConst.XUNJIAN_ID ? "巡检不合格" : QcCheckExecH.checktype == WmsWareHouseConst.SHOUJIAN_ID ? "首检不合格" : "末检不合格";
await _prdMoTaskService.PrdTaskRelease2(new PrdTaskReleaseUpInput()
{
TaskIds = NPOI.Util.Arrays.AsList(prdMoTask.id),
Behavior = "Pause",
PauseReeson = pauseReason
},db);
await _prdMoTaskService.SelfTestScrapped2(new SelfTestScrappedInput()
{
mo_task_id = prdMoTask.id,
scrap_qty = rqty,
remark = "抽样不合格报废",
categoryItems = new List<SelfTestScrappedInputItem>()
{
new SelfTestScrappedInputItem()
{
category_id = "25574005966629",
items = new List<defectItem>()
{
new defectItem()
{
defective_item = "抽样不合格报废",
defective_item_qty = rqty
}
}
}
}
},db);
List<string> carryCodes = await db.Queryable<PrdReport>().Where(x=>x.mo_task_id==prdMoTask.id).Select(x=>x.material_box_code).Distinct().ToListAsync();
await db.Updateable<WmsCarryH>()
.SetColumns(x => x.is_check == ((int)EnumCheckConclusion.).ToString())
.Where(x => carryCodes.Contains(x.carry_code))
.ExecuteCommandAsync();
}
else
{
//末检自动完工
if (QcCheckExecH.checktype == WmsWareHouseConst.MOJIAN_ID)
{
await _prdMoTaskService.PrdTaskRelease2(new PrdTaskReleaseUpInput()
{
TaskIds = NPOI.Util.Arrays.AsList(prdMoTask.id),
Behavior = "Compled",
},db);
}
}
}
await db.Ado.CommitTranAsync();
}
catch (Exception e)
{
await db.Ado.RollbackTranAsync();
Log.Error(e.Message,e);
throw Oops.Oh("执行失败:"+e.Message);
}
finally{
prdreportSemaphore.Release();
}
}
/// <summary>
/// 获取任务结果明细
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
public async Task<dynamic> GetTaskResult(string id)
{
ISqlSugarClient db = _repository.AsSugarClient();
QcCheckExecH QcCheckExecH = await db.Queryable<QcCheckExecH>().Where(p => p.id == id).FirstAsync();
List<QcCheckExecD> QcCheckExecDs = await db.Queryable<QcCheckExecD>().Where(p => p.mainid == id).ToListAsync();
List<QcCheckItem> QcCheckItems = await db.Queryable<QcCheckItem>().ToListAsync();
List<QcCheckType> QcCheckTypes = await db.Queryable<QcCheckType>().ToListAsync();
List<QcErrorCause> QcErrorCauses = await db.Queryable<QcErrorCause>().ToListAsync();
List<QcErrorLevel> QcErrorLevels = await db.Queryable<QcErrorLevel>().ToListAsync();
Result Result = await db.Queryable<QcCheckExecH>().LeftJoin<BasMaterial>((a, b) => a.materialid == b.id)
.LeftJoin<BasProcess>((a, b, c) => a.processid == c.id)
.LeftJoin<OrganizeEntity>((a, b, c, d) => a.workid == d.Id)
.LeftJoin<UserEntity>((a, b, c, d, e) => a.execuser == e.Id)
.LeftJoin<PrdReport>((a, b, c, d, e, f) => a.report_id == f.id)
.LeftJoin<PrdMoTask>((a, b, c, d, e, f, g) => a.mo_task_code == g.mo_task_code)
.LeftJoin<EqpEquipment>((a, b, c, d, e, f, g, h) => g.eqp_id == h.id)
.Where((a, b, c, d, e) => a.id==id)
.Select((a, b, c, d, e, f, g, h) => new Result
{
mainid = a.id,
bill_code = a.bill_code,
material_name = b.name,
material_code = b.code,
material_standard = b.material_standard,
material_specification = b.material_specification,
checknum = a.checknum,
status = a.status,
result = a.result,
exectime = a.exectime ?? "",
execuser = e.RealName ?? "",
mo_task_code = a.mo_task_code,
batch = a.batch,
qty = a.qty,
equip_name = h.name,
checktypes = new List<List<Checktype>>(),
attachment = a.attachment,
}).FirstAsync();
Result.checktypes = new List<List<Checktype>>();
if (!string.IsNullOrEmpty(QcCheckExecH.carry_code))
{
WmsCarryH carryH = await db.Queryable<WmsCarryH>().FirstAsync(x => x.carry_code == QcCheckExecH.carry_code);
WmsCarryCode carryCode = await db.Queryable<WmsCarryCode>().FirstAsync(x => x.carry_id == carryH.id);
string materialId = carryCode?.id ?? "";
BasMaterial basMaterial = await db.Queryable<BasMaterial>().SingleAsync(x=>x.id==materialId);
Result.carryInfo = new CheckCarry()
{
carry_name = carryH.carry_name,
location_id = carryH.location_id,
location_code = carryH.location_code,
material_name = basMaterial?.name,
qty = carryCode?.codeqty ?? 0,
};
}
List<string?> groupkeys = QcCheckExecDs.Select(p => p.checkindex).Distinct().ToList();
foreach (string? key in groupkeys)
{
List<QcCheckExecD> QcCheckExecDsbykey = QcCheckExecDs.Where(p => p.checkindex == key).ToList();
List<Checktype> checktype = new();
foreach (QcCheckExecD? QcCheckExecD in QcCheckExecDsbykey)
{
if (checktype.Where(p => p.checktypeid == QcCheckExecD.typeid).ToList().Count == 0)
{
Checktype checkType = new()
{
checktypeid = QcCheckExecD.typeid!,
checktypename = QcCheckTypes.Where(p => p.id == QcCheckExecD.typeid).First().name!,
items = new List<ExecItem>()
};
checktype.Add(checkType);
}
ExecItem Item = new()
{
itemid = QcCheckExecD.itemid!,
itemdid = QcCheckExecD.id!,
code = QcCheckItems.Where(p => p.id == QcCheckExecD.itemid).First().code!,
name = QcCheckItems.Where(p => p.id == QcCheckExecD.itemid).First().name!,
result = QcCheckExecD.result,
setData = new ExecItemData
{
extype = QcCheckExecD.extype!,
excontent = JSON.Deserialize<Excontent>(QcCheckExecD.excontent!),
check = QcCheckExecD.check!
}
};
if (!string.IsNullOrEmpty(QcCheckExecD.errorcause))
{
string[] strs = QcCheckExecD.errorcause!.Replace("[", "").Replace("]", "").Split(',', StringSplitOptions.RemoveEmptyEntries);
Item.setData.errorcause = new List<Error>();
foreach (string str in strs)
{
Item.setData.errorcause.Add(new Error { id = str, name = QcErrorCauses.Where(p => p.id == str).First().name! });
}
}
if (!string.IsNullOrEmpty(QcCheckExecD.errorlevel))
{
string[] strs = QcCheckExecD.errorlevel!.Replace("[", "").Replace("]", "").Split(',', StringSplitOptions.RemoveEmptyEntries);
Item.setData.errorlevel = new List<Error>();
foreach (string str in strs)
{
Item.setData.errorlevel.Add(new Error { id = str, name = QcErrorLevels.Where(p => p.id == str).First().name! });
}
}
Item.setData.remark = QcCheckExecD.remark!;
Item.setData.attachment = QcCheckExecD.attachment!;
Item.setData.customer = QcCheckExecD.custom!;
if (!string.IsNullOrEmpty(QcCheckExecD.isexec))
{
Item.setData.isexec = JSON.Deserialize<IsexecE>(QcCheckExecD.isexec!);
}
Item.setShow = new ExecItemShow
{
extype = !string.IsNullOrEmpty(Item.setData.extype),
excontent = !string.IsNullOrEmpty(QcCheckExecD.excontent),
check = !string.IsNullOrEmpty(Item.setData.check),
errorcause = (Item.setData.errorcause?.Count) != 0,
errorlevel = (Item.setData.errorlevel?.Count) != 0,
remark = !string.IsNullOrEmpty(Item.setData.remark),
attachment = !string.IsNullOrEmpty(Item.setData.attachment),
customer = !string.IsNullOrEmpty(Item.setData.customer),
isexec = Item.setData.isexec != null
};
if (!string.IsNullOrEmpty(QcCheckExecD.postdata))
{
Item.postItemForm = JSON.Deserialize<PostItemForm>(QcCheckExecD.postdata!);
}
checktype.Where(p => p.checktypeid == QcCheckExecD.typeid).First()?.items?.Add(Item);
}
Result.checktypes.Add(checktype);
}
return Result;
}
[HttpPost]
public async Task CreateTaskData(CheckTaskData checkTaskData)
{
ISqlSugarClient db = _repository.AsSugarClient();
DictionaryTypeEntity DictionaryType = await db.Queryable<DictionaryTypeEntity>().Where(p => p.FullName == "质检状态").FirstAsync();
DictionaryDataEntity DictionaryData = await db.Queryable<DictionaryDataEntity>().Where(p => p.DictionaryTypeId == DictionaryType.Id && p.FullName == "待执行").FirstAsync();
QcCheckExecH QcCheckExecH = new()
{
checktype = checkTaskData.checktype,
materialid = checkTaskData.materialid,
processid = checkTaskData.processid,
workid = checkTaskData.workid,
wareid = checkTaskData.wareid,
status = DictionaryData.Id,
tasktime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
create_id = _userManager.UserId,
create_time = DateTime.Now
};
List<QcCheckExecD> QcCheckExecDs = new();
if (checkTaskData.checktypes != null)
{
foreach (CheckTaskDataInput checktype in checkTaskData.checktypes)
{
if (checktype.items != null)
{
foreach (TaskItemInput item in checktype.items)
{
QcCheckExecD QcCheckExecD = new()
{
mainid = QcCheckExecH.id,
typeid = checktype.id,
itemid = item.itemid,
extype = item.extype,
excontent = item.excontent,
check = item.check,
errorcause = item.errorcause?.Replace("\"", "").Trim(),
errorlevel = item.errorlevel?.Replace("\"", "").Trim(),
remark = item.remark,
attachment = item.attachment,
isexec = item.isexec,
custom = item.customer,
create_id = _userManager.UserId,
create_time = DateTime.Now
};
QcCheckExecDs.Add(QcCheckExecD);
}
}
}
}
await db.Ado.BeginTranAsync();
_ = await db.Insertable(QcCheckExecH).ExecuteCommandAsync();
_ = await db.Insertable(QcCheckExecDs).ExecuteCommandAsync();
await db.Ado.CommitTranAsync();
}
}
}