using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Aop.Api.Domain; using DingTalk.Api.Request; using JNPF.Common.Core.Manager; using JNPF.Common.Extension; using JNPF.Common.Filter; using JNPF.Common.Security; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.FriendlyException; using JNPF.Logging; using JNPF.Systems.Entitys.System; using JNPF.Systems.Interfaces.System; using JNPF.VisualDev; using JNPF.VisualDev.Entitys.Dto.VisualDevModelData; using Microsoft.AspNetCore.Mvc; using Microsoft.ClearScript.JavaScript; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; using SqlSugar; using Tnb.BasicData.Entities; using Tnb.EquipMgr.Entities; using Tnb.ProductionMgr.Entities; using Tnb.ProductionMgr.Entities.Entity; using Tnb.QcMgr.Entities; using Tnb.QcMgr.Entities.Dto; using Tnb.QcMgr.Entities.Entity; using Tnb.QcMgr.Entities.Enums; using Tnb.WarehouseMgr; using Tnb.WarehouseMgr.Entities; using Tnb.WarehouseMgr.Entities.Consts; using Tnb.WarehouseMgr.Entities.Dto; using Tnb.WarehouseMgr.Entities.Enums; using Tnb.WarehouseMgr.Interfaces; namespace Tnb.QcMgr { [ApiDescriptionSettings(Tag = ModuleConsts.Tag, Area = ModuleConsts.Area, Order = 800)] [Route("api/[area]/[controller]/[action]")] public class QcTempControlHService : BaseWareHouseService, IDynamicApiController, ITransient { private readonly ISqlSugarRepository _repository; private readonly IUserManager _userManager; private readonly IWareHouseService _wareHouseService; private readonly IBillRullService _billRullService; private readonly ISqlSugarClient _db; public QcTempControlHService(ISqlSugarRepository repository, IUserManager userManager, IWareHouseService wareHouseService,IBillRullService billRullService) { _db = repository.AsSugarClient(); _userManager = userManager; _wareHouseService = wareHouseService; _billRullService = billRullService; } /// /// 获取质检暂控处理单 /// /// [HttpPost] public async Task AllList(VisualDevModelListQueryInput input) { var bill_code = ""; //单据号 var pro_task_code = ""; //生产任务单号 var status = "";//状态 if (!input.queryJson.IsNullOrWhiteSpace()) { bill_code = JObject.Parse(input.queryJson).Value("bill_code"); pro_task_code = JObject.Parse(input.queryJson).Value("pro_task_code"); status = JObject.Parse(input.queryJson).Value("status"); } var result = await _db.Queryable().LeftJoin((a, b) => a.in_warehouse_id == b.id) .LeftJoin((a, b, c) => a.out_warehouse_id == c.id) .WhereIF(!string.IsNullOrEmpty(bill_code), (a, b, c) => a.bill_code.Contains(bill_code)) .WhereIF(!string.IsNullOrEmpty(pro_task_code), (a, b, c) => a.pro_task_code.Contains(pro_task_code)) .WhereIF(!string.IsNullOrEmpty(status), (a, b, c) => a.status == status) .OrderByDescending((a, b, c) => a.create_time) .Select((a, b, c) => new QcTempControlHOutput() { id = a.id, bill_code = a.bill_code, source_code = a.source_code, pro_task_code = a.pro_task_code, out_warehouse_id = a.out_warehouse_id, out_warehouse_name = c.whname, in_warehouse_id = a.in_warehouse_id, in_warehouse_name = b.whname, material_id = a.material_id, material_code = a.material_code, control_lx_qty = a.control_lx_qty, control_qty = a.control_qty, result = a.result, loger_id = a.loger_id, confirmer_id = a.confirmer_id, status = a.status, called_box_qty = a.called_box_qty, called_qty = a.called_qty, leave_call_box_qty = a.leave_call_box_qty, leave_call_qty = a.leave_call_qty, details = SqlFunc.Subqueryable().Where(r => r.bill_id == a.id).OrderByDesc(r => r.create_time).ToList((x) => new QcTempControlDsOutput { id = x.id, bill_id = x.bill_id, carry_id = x.carry_id, carry_code = x.carry_code, warehouse_code = x.warehouse_code, warehouse_id = x.warehouse_id, location_code = x.location_code, location_id = x.location_id, qty = x.qty, submiter = x.submiter, maker_id = x.maker_id, }) }).ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(result); } /// /// 确认暂控料处理单结论 /// /// /// [HttpPost] public async Task ModifyResult(Dictionary dic) { string id = dic["id"]; string qcRes = dic["result"]; try { var qcTempControlH = await _db.Queryable().Where(r => r.id == id).FirstAsync(); if(qcTempControlH==null) { throw Oops.Bah("暂控处理单数据不存在"); } await _db.Ado.BeginTranAsync(); await _db.Updateable().SetColumns(x => x.result == qcRes).SetColumns(x=>x.status==((int)EnumTempControlStatus.待下发).ToString()).Where(x => x.id == id).ExecuteCommandAsync(); await _db.Updateable().SetColumns(x => x.check_status == qcRes).Where(x => x.bill_id == qcTempControlH.bill_code).ExecuteCommandAsync(); await _db.Ado.CommitTranAsync(); } catch(Exception e) { await _db.Ado.RollbackTranAsync(); Log.Error(e.Message, e); throw Oops.Bah(e.Message); } return "修改成功"; } /// /// 下发暂控料处理单 /// /// /// [HttpPost] public async Task QcTempControlIssued(Dictionary dic) { string id = dic["id"]; try { var qcTempControlH = await _db.Queryable().Where(r => r.id == id).FirstAsync(); if (qcTempControlH == null) { throw Oops.Bah("暂控处理单数据不存在"); } //所有暂控的料箱必须都进入中储仓才能进行下发 var qcTempControlDs = await _db.Queryable().Where(r => r.bill_id == qcTempControlH.id).ToListAsync(); if (qcTempControlDs!=null && qcTempControlDs.Count > 0) { var carrys = await _db.Queryable().Where(r => qcTempControlDs.Select(r => r.carry_id).Contains(r.carry_id)).ToListAsync(); var noInStockCarrys = carrys.Where(r => r.warehouse_id != WmsWareHouseConst.WAREHOUSE_ZC_ID); if (noInStockCarrys.Any()) { var carryCodes=string.Join(",", noInStockCarrys.Distinct().Select(r=>r.carry_code)); throw Oops.Bah($"{carryCodes}等料箱还没入中储仓,不能下发"); } } await _db.Ado.BeginTranAsync(); await _db.Updateable().SetColumns(x => x.status == ((int)EnumTempControlStatus.已下发).ToString()).Where(x => x.id == id).ExecuteCommandAsync(); await _db.Ado.CommitTranAsync(); } catch (Exception e) { await _db.Ado.RollbackTranAsync(); Log.Error(e.Message, e); throw Oops.Bah(e.Message); } return "修改成功"; } /// /// 获取质检单 for APP /// /// [HttpPost] public async Task GetQcTempControlList(PageInputBase input) { Dictionary queryJson = string.IsNullOrEmpty(input.queryJson) ? new Dictionary() : input.queryJson.ToObject>(); string? bill_code = queryJson.ContainsKey("bill_code") ? queryJson["bill_code"].ToString() : ""; string? status = queryJson.ContainsKey("status") ? queryJson["status"].ToString() : ""; if (string.IsNullOrEmpty(input.sidx)) { input.sidx = "create_time"; input.sort = "desc"; } var result = await _db.Queryable().LeftJoin((a, b) => a.in_warehouse_id == b.id) .LeftJoin((a, b, c) => a.out_warehouse_id == c.id) .WhereIF(!string.IsNullOrEmpty(bill_code), (a, b, c) => a.bill_code.Contains(bill_code)) .WhereIF(!string.IsNullOrEmpty(status), (a, b, c) => a.status == status) .OrderByDescending((a, b, c) => a.create_time) .Select((a, b, c) => new QcTempControlHAppOutput() { id = a.id, bill_code = a.bill_code, source_code = a.source_code, pro_task_code = a.pro_task_code, out_warehouse_id = a.out_warehouse_id, out_warehouse_name = c.whname, in_warehouse_id = a.in_warehouse_id, in_warehouse_name = b.whname, material_id = a.material_id, material_code = a.material_code, control_lx_qty = a.control_lx_qty, control_qty = a.control_qty, result = a.result, loger_id = a.loger_id, confirmer_id = a.confirmer_id, status = a.status, called_box_qty = a.called_box_qty, called_qty = a.called_qty, leave_call_box_qty = a.leave_call_box_qty, leave_call_qty = a.leave_call_qty, }).ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(result); } /// /// PDA暂控料呼叫 /// /// [HttpPost] public async Task TempControlCall(QcTempControlCallInput input) { try { if (string.IsNullOrEmpty(input.id)) throw Oops.Bah("主键id不能为空"); if (string.IsNullOrEmpty(input.destination_location_code)) throw Oops.Bah("目标库位不能为空"); if (input.box_qty <= 0) throw Oops.Bah("呼叫箱数不能小于1"); var qcTempControlH = await _db.Queryable().Where(r => r.id == input.id).FirstAsync(); if (qcTempControlH == null) throw Oops.Bah("暂控处理单数据不存在"); var basLocation=await _db.Queryable().Where(r=>r.location_code==input.destination_location_code).FirstAsync(); if (basLocation == null) throw Oops.Bah($"目标库位{input.destination_location_code}不存在"); var qcTempControlDs = await _db.Queryable().Where(r => r.bill_id == input.id && r.call_status == "未呼叫").Take(input.box_qty).ToListAsync(); if (qcTempControlDs == null || qcTempControlDs.Count <= 0) throw Oops.Bah("不存在未下发的料箱"); await _db.Ado.BeginTranAsync(); decimal callqty = 0;//当前呼叫物料总数量 foreach(var item in qcTempControlDs) { callqty += (string.IsNullOrEmpty(item.qty) ? 0 : Convert.ToDecimal(item.qty)); // 计算路径,插入预任务申请 WmsPointH sPoint = await _db.Queryable().FirstAsync(it => it.location_id ==item.location_id); WmsPointH ePoint = await _db.Queryable().FirstAsync(it => it.location_id == basLocation.id); if (sPoint == null) throw Oops.Bah($"载具编号{item.carry_code}未找到起始点位"); if (ePoint == null) throw Oops.Bah($"载具编号{item.carry_code}未找到终点点位"); List points = new List(); if (sPoint.area_code != ePoint.area_code) { points = await _wareHouseService.PathAlgorithms(sPoint.id, ePoint.id); if (points.Count <= 2) { throw new AppFriendlyException($"sPoint {sPoint.point_code} ePoint{ePoint.point_code}该路径不存在", 500); } } else { points.Add(sPoint); points.Add(ePoint); } if (points.Count <= 0) throw Oops.Bah("点位不存在"); List preTasks = points.Where(it => !it.location_id.IsNullOrEmpty()).GroupBy(g => g.area_code).Select(it => { WmsPointH? sPoint = it.FirstOrDefault(); WmsPointH? ePoint = it.LastOrDefault(); WmsPretaskH preTask = new() { org_id = _userManager!.User.OrganizeId, startlocation_id = sPoint?.location_id!, startlocation_code = sPoint?.location_code!, endlocation_id = ePoint?.location_id!, endlocation_code = ePoint?.location_code!, start_floor = sPoint?.floor.ToString(), end_floor = ePoint?.floor.ToString(), startpoint_id = sPoint?.id!, startpoint_code = sPoint?.point_code!, endpoint_id = ePoint?.id!, endpoint_code = ePoint?.point_code!, bill_code = _billRullService!.GetBillNumber(WmsWareHouseConst.WMS_PRETASK_H_ENCODE).GetAwaiter().GetResult(), status = WmsWareHouseConst.PRETASK_BILL_STATUS_DXF_ID, biz_type = WmsWareHouseConst.BIZTYPE_WMSTRANSFERINSTOCK_ID, task_type = WmsWareHouseConst.WMS_PRETASK_TRANSFER_TYPE_ID }; preTask.area_id = sPoint?.area_id!; preTask.area_code = it.Key; preTask.require_id = qcTempControlH.id; preTask.require_code = qcTempControlH.bill_code; preTask.create_id = _userManager.UserId; preTask.create_time = DateTime.Now; return preTask; }).ToList(); bool isOk = await _wareHouseService.GenPreTask(preTasks, null!); if (!isOk) throw Oops.Bah($"载具编号{item.carry_code}生成预任务失败"); } await _db.Updateable(qcTempControlDs).SetColumns(x=>x.call_status=="已呼叫").ExecuteCommandAsync(); //已呼叫料箱数量 var called_box_qty = qcTempControlDs.Count + (string.IsNullOrEmpty(qcTempControlH.called_box_qty) ? 0 : Convert.ToDecimal(qcTempControlH.called_box_qty)); //已呼叫物料数量 var called_qty = callqty + (string.IsNullOrEmpty(qcTempControlH.called_qty) ? 0 : Convert.ToDecimal(qcTempControlH.called_qty)); //剩余呼叫料箱数量 var leave_call_box_qty = (string.IsNullOrEmpty(qcTempControlH.control_lx_qty) ? 0 : Convert.ToDecimal(qcTempControlH.control_lx_qty)) - called_box_qty; //剩余呼叫数量 var leave_call_qty = (string.IsNullOrEmpty(qcTempControlH.control_qty) ? 0 : Convert.ToDecimal(qcTempControlH.control_qty)) - called_qty; await _db.Updateable(qcTempControlH).SetColumns(x => x.called_box_qty == called_box_qty.ToString()) .SetColumns(x => x.called_qty == called_qty.ToString()) .SetColumns(x => x.leave_call_box_qty == leave_call_box_qty.ToString()) .SetColumns(x => x.leave_call_qty == leave_call_qty.ToString()) .SetColumns(x=>x.status==((int)EnumTempControlStatus.执行中).ToString()) .ExecuteCommandAsync(); await _db.Ado.CommitTranAsync(); } catch (Exception e) { await _db.Ado.RollbackTranAsync(); Log.Error(e.Message, e); throw Oops.Bah(e.Message); } return "呼叫成功"; } /// /// PDA扫描料箱获取暂控处理单详情 /// /// /// [HttpPost] public async Task GetQcTempControlDetail(Dictionary dic) { var id = dic["id"].ToString(); var carryCode = dic["carryCode"].ToString(); if (string.IsNullOrEmpty(carryCode)) throw Oops.Bah("料箱号不能为空"); var qcTempControlH = await _db.Queryable().Where(r => r.id == id).FirstAsync(); if (qcTempControlH == null) throw Oops.Bah("未找到暂控料处理单数据"); var carry = await _db.Queryable().Where(r => r.carry_code == carryCode).FirstAsync(); if (carry == null) throw Oops.Bah($"料箱号:{carryCode}不存在"); var qcTempControlD = await _db.Queryable().Where(r => r.bill_id == qcTempControlH.id && r.carry_id == carry.id).FirstAsync(); if (qcTempControlD == null) throw Oops.Bah($"料箱号:{carryCode}不在此暂控料处理单的暂控料箱列表中"); var material= await _db.Queryable().Where(r => r.id == qcTempControlH.material_id).FirstAsync(); var result = new QcTempControlScanCodeOutput(); result.id = qcTempControlD.id; result.material_code = material?.code; result.material_name = material?.name; result.container_no=material?.container_no; result.material_standard = material?.material_standard; result.material_specification = material?.material_specification; result.unit_code = material?.unit_id; result.code_qty = qcTempControlD.qty; return result; } /// /// 保存挑选合格数量 /// /// /// [HttpPost] public async Task SaveQualifiedQty(Dictionary dic) { try { var id = dic["id"].ToString(); var qualifiedQty = dic["qualifiedQty"].ToString(); if (string.IsNullOrEmpty(qualifiedQty)) throw Oops.Bah("合格数量不能为空"); //先保存不合格数量,修改状态为让步接收,改变对应载具台账的检验状态为合格,并生成对应料箱的8号线入库任务 var qcTempControlD = await _db.Queryable().Where(r => r.id == id).FirstAsync(); if (qcTempControlD == null) throw Oops.Bah($"数据不存在"); if (qcTempControlD.pick_status == "已挑选") throw Oops.Bah("已挑选,不能重复挑选"); var qctempControlH=await _db.Queryable().Where(r=>r.id==qcTempControlD.bill_id).FirstAsync(); //计算不合格数量 var unQualifiedQty = (string.IsNullOrEmpty(qcTempControlD.qty) ? 0 : Convert.ToDecimal(qcTempControlD.qty)) - (Convert.ToDecimal(qualifiedQty)); await _db.Ado.BeginTranAsync(); //修改对应暂控子表的不合格数量和挑选状态 await _db.Updateable() .SetColumns(r => r.qualified_qty == qualifiedQty) .SetColumns(r=>r.unqualified_qty== unQualifiedQty.ToString()) .SetColumns(r => r.pick_status == "已挑选") .SetColumns(r=>r.check_status=="让步接收") .Where(r => r.id == qcTempControlD.id).ExecuteCommandAsync(); //更新对应载具台账的检验状态为合格 await _db.Updateable().SetColumns(r => r.is_check == ((int)EnumCheckConclusion.合格).ToString()).Where(r => r.id == qcTempControlD.carry_id).ExecuteCommandAsync(); //更新对应载具条码表的条码数量为挑选好的合格数量 await _db.Updateable().SetColumns(r => r.codeqty == Convert.ToDecimal(qualifiedQty)).Where(r => r.carry_id == qcTempControlD.carry_id && r.material_id == qctempControlH.material_id).ExecuteCommandAsync(); #region 生成8号线入库任务 //入库取终点 InStockStrategyQuery inStockStrategyInput = new() { warehouse_id = qcTempControlD.warehouse_id, Size = 1 }; List endLocations = await _wareHouseService.InStockStrategy(inStockStrategyInput); WmsPointH? sPoint = null; WmsPointH? ePoint = null; sPoint = await _db.Queryable().FirstAsync(it => it.location_id == qcTempControlD.warehouse_id); if (sPoint == null) throw Oops.Bah($"起始库位不可用"); if (endLocations?.Count > 0) { WmsCarryH carry = await _db.Queryable().SingleAsync(it => it.id == qcTempControlD.carry_id); BasLocation loc = await _db.Queryable().SingleAsync(it => it.id == endLocations[0].id); bool isMatch = await IsCarryAndLocationMatchByCarryStd(carry, loc); if (!isMatch) { throw new AppFriendlyException("库位与载具规格不匹配", 500); } ePoint = await _db.Queryable().FirstAsync(it => it.location_id == endLocations[0].id); } if (ePoint == null) throw Oops.Bah("无可用的目标库位"); List points = new List(); if (sPoint.area_code != ePoint.area_code) { points = await _wareHouseService.PathAlgorithms(sPoint.id, ePoint.id); if (points.Count <= 2) { throw new AppFriendlyException($"sPoint {sPoint.point_code} ePoint{ePoint.point_code}该路径不存在", 500); } } else { points.Add(sPoint); points.Add(ePoint); } //根据获取的路径点生成预任务,生成顺序必须预路径算法返回的起终点的顺序一致(预任务顺序) List preTasks = points.Where(it => !it.location_id.IsNullOrEmpty()).GroupBy(g => g.area_code).Select(it => { WmsPointH? sPoint = it.FirstOrDefault(); WmsPointH? ePoint = it.LastOrDefault(); WmsPretaskH preTask = new() { org_id = _userManager.User.OrganizeId, startlocation_id = sPoint?.location_id!, startlocation_code = sPoint?.location_code!, endlocation_id = ePoint?.location_id!, endlocation_code = ePoint?.location_code!, start_floor = sPoint?.floor.ToString(), end_floor = ePoint?.floor.ToString(), startpoint_id = sPoint?.id!, startpoint_code = sPoint?.point_code!, endpoint_id = ePoint?.id!, endpoint_code = ePoint?.point_code!, bill_code = _billRullService.GetBillNumber(WmsWareHouseConst.WMS_PRETASK_H_ENCODE).GetAwaiter().GetResult(), status = WmsWareHouseConst.PRETASK_BILL_STATUS_DXF_ID, biz_type = WmsWareHouseConst.BIZTYPE_TEMPCONTROLINSTOCK_ID, task_type = WmsWareHouseConst.WMS_PRETASK_INSTOCK_TYPE_ID, carry_id = qcTempControlD.carry_id, carry_code = qcTempControlD.carry_code, area_id = sPoint?.area_id!, area_code = it.Key, require_id = qctempControlH?.id, require_code = qctempControlH?.bill_code, create_id = _userManager.UserId, create_time = DateTime.Now, }; return preTask; }).ToList(); bool isOk = await _wareHouseService.GenPreTask(preTasks, null!); if (!isOk) throw Oops.Bah($"生成暂控料入库预任务失败"); await _db.Updateable().SetColumns(r => r.pre_task_id == preTasks.FirstOrDefault().id).Where(r => r.id == id).ExecuteCommandAsync(); #endregion await _db.Ado.CommitTranAsync(); } catch(Exception e) { await _db.Ado.RollbackTranAsync(); Log.Error(e.Message, e); throw Oops.Bah(e.Message); } return "保存成功"; } /// /// 显示已挑选合格数量 /// /// /// [HttpPost] public async Task ShowPickQualifiedQty(Dictionary dic) { try { var id = dic["id"].ToString(); var qcTempControlH = await _db.Queryable().Where(r => r.id == id).FirstAsync(); if (qcTempControlH == null) throw Oops.Bah($"数据不存在"); var qcTempControlDs = await _db.Queryable().Where(r => r.bill_id == id).ToListAsync(); QcTempControlPickQualifiedQtyOutput result = new QcTempControlPickQualifiedQtyOutput(); if (qcTempControlDs != null) { result.temp_control_qty = qcTempControlH.control_qty; result.qualified_qty = qcTempControlDs.Sum(r => (string.IsNullOrEmpty(r.qualified_qty) ? 0 : Convert.ToDecimal(r.qualified_qty))).ToString(); result.unQualified_qty = ((string.IsNullOrEmpty(qcTempControlH.control_qty) ? 0 : Convert.ToDecimal(qcTempControlH.control_qty)) - Convert.ToDecimal(result.qualified_qty)).ToString(); } else { result.temp_control_qty = qcTempControlH.control_qty; result.qualified_qty = "0"; result.unQualified_qty = "0"; } return result; } catch (Exception e) { Log.Error(e.Message, e); throw Oops.Bah(e.Message); } } /// /// 挑选不合格数量完成 /// /// /// [HttpPost] public async Task SavePickComplete(Dictionary dic) { try { var id = dic["id"].ToString(); //保存暂控处理单状态为挑选完成之前,需要确保所有的8号线入库任务全部完成,挑选完成之后,对接bip(暂时不对接),把不合格总数量传给BIP; //并且生成一个其它出库(从中储仓出到次品仓),其他入库(从中储仓入到次品仓)任务 var qcTempControlH = await _db.Queryable().Where(r => r.id == id).FirstAsync(); if (qcTempControlH == null) throw Oops.Bah($"数据不存在"); //质检任务主表 var qcCheckExecH = await _db.Queryable().Where(r => r.bill_code == qcTempControlH.source_code).FirstAsync(); var qcTempControlDs = await _db.Queryable().Where(r => r.bill_id == qcTempControlH.id).ToListAsync(); //找到所有的预任务id var pre_task_ids = qcTempControlDs.Where(r => !string.IsNullOrEmpty(r.pre_task_id)).Select(r => r.pre_task_id).Distinct(); //查找到未完成的任务 var wmsDistasks = await _db.Queryable().Where(r => pre_task_ids.Contains(r.pretask_id) && r.status != WmsWareHouseConst.TASK_BILL_STATUS_COMPLE_ID).ToListAsync(); if (wmsDistasks != null && wmsDistasks.Count > 0) throw Oops.Bah($"还存在未完成的入库任务,挑选完成失败"); //计算所有合格数量 var qualifiedQty = qcTempControlDs.Sum(r => (string.IsNullOrEmpty(r.qualified_qty) ? 0 : Convert.ToDecimal(r.qualified_qty))); #region 生产报废记录 //新增生产报废记录 var prdTaskDefect = new PrdMoTaskDefectRecord(); //生产工单 var prdMo = await _db.Queryable().LeftJoin((a, b) => a.id == b.mo_id).Where((a, b) => b.mo_task_code == qcTempControlH.pro_task_code).FirstAsync(); var prdMoTask = await _db.Queryable().Where(r => r.mo_task_code == qcTempControlH.pro_task_code).FirstAsync(); prdTaskDefect.mo_code=prdMo?.mo_code; prdTaskDefect.mo_task_id = prdMoTask?.id; prdTaskDefect.mo_task_code=qcTempControlH.pro_task_code; prdTaskDefect.mo_task_type = prdMo?.mo_type; var basMaterial = await _db.Queryable().Where(r => r.id == qcTempControlH.material_id).FirstAsync(); prdTaskDefect.material_code = basMaterial?.code; prdTaskDefect.material_name=basMaterial?.name; prdTaskDefect.estimated_start_date = prdMoTask?.estimated_start_date; prdTaskDefect.estimated_end_date=prdMoTask?.estimated_end_date; prdTaskDefect.plan_qty = prdMoTask?.plan_qty; prdTaskDefect.scrap_qty = (string.IsNullOrEmpty(qcTempControlH.control_qty) ? 0 : Convert.ToDecimal(qcTempControlH.control_qty)) - qualifiedQty;//暂控数量-合格数量 prdTaskDefect.status = prdMoTask?.mo_task_status; prdTaskDefect.eqp_code = prdMoTask == null ? "" : (await _db.Queryable().FirstAsync(it => it.id == prdMoTask.eqp_id))?.code!; prdTaskDefect.mold_name = prdMoTask == null ? "" : (await _db.Queryable().FirstAsync(it => it.id == prdMoTask.mold_id))?.mold_name!; prdTaskDefect.create_time = DateTime.Now; prdTaskDefect.create_id = _userManager.UserId; await _db.Insertable(prdTaskDefect).ExecuteCommandAsync(); //新增生产报废记录子表 var prdMoTaskDefectRecordDs = new List(); foreach(var item in qcTempControlDs) { var prdMoTaskDefectRecordD = new PrdMoTaskDefectRecordD { bill_id = prdTaskDefect.id, source_id = item.id, carry_id = item.carry_id, carry_code = item.carry_code, scrap_qty = ((string.IsNullOrEmpty(item.qty) ? 0 : Convert.ToDecimal(item.qty)) - (string.IsNullOrEmpty(item.qualified_qty) ? 0 : Convert.ToDecimal(item.qualified_qty))).ToString(), submiter_id= item.submiter, maker_id =item.maker_id, make_time= prdTaskDefect.create_time }; prdMoTaskDefectRecordDs.Add(prdMoTaskDefectRecordD); } await _db.Insertable(prdMoTaskDefectRecordDs).ExecuteCommandAsync(); #endregion //载具台账变为空载具(载具状态:空闲、库位ID清空、检验状态:待检、子表数据清空) foreach(var item in qcTempControlDs) { var wmsCarryH = await _db.Queryable().Where(r => r.id == item.carry_id).FirstAsync(); wmsCarryH.carry_status = ((int)EnumCarryStatus.空闲).ToString(); wmsCarryH.is_check = ((int)EnumCheckConclusion.待检).ToString(); wmsCarryH.location_code = ""; wmsCarryH.location_id = ""; wmsCarryH.is_lock = 0; await _db.Updateable(wmsCarryH).ExecuteCommandAsync(); await _db.Updateable() .SetColumns(r => r.carry_status == ((int)EnumCarryStatus.空闲).ToString()) .SetColumns(r => r.is_check == ((int)EnumCheckConclusion.待检).ToString()) .SetColumns(r => r.location_code == "") .SetColumns(r => r.location_id == "") .SetColumns(r => r.is_lock == 0) .Where(r=>r.id==item.carry_id) .ExecuteCommandAsync(); await _db.Deleteable().Where(r => r.carry_id == item.carry_id).ExecuteCommandAsync(); } await _db.Ado.CommitTranAsync(); } catch (Exception e) { await _db.Ado.RollbackTranAsync(); Log.Error(e.Message, e); throw Oops.Bah(e.Message); } return "保存成功"; } } }