using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using WIDESEA.Common;
using WIDESEA.Core.Enums;
using WIDESEA.Core.ManageUser;
using WIDESEA.Core.Services;
using WIDESEA.Core.Utilities;
using WIDESEA.Entity.DomainModels;
using WIDESEA.Services.IServices.ToApp;
using WIDESEA.Services.Repositories;
using WIDESEA.Services.Services;
using WIDESEA_Entity.DomainModels;
namespace WIDESEA_Services.Services
{
public partial class ToAPPOperation : IToAPPInterface
{
///
/// 入库组盘
///
///
///
public WebResponseContent InboundBoxingAction(SaveModel saveModel)
{
WebResponseContent content = new WebResponseContent();
try
{
Dt_general_info general_Info = Dt_general_infoRepository.Instance.FindFirst(x => true);
string currentModel = general_Info.general_box_empty_flag;
if (!"box".Equals(currentModel))
throw new Exception("当前非轴承入库模式,请切换模式后进行入库.");
//托盘RFID
string rfid = saveModel.MainData["rfid"].ToString();
string qrCode = saveModel.MainData["qrcode"].ToString();
if (string.IsNullOrEmpty(rfid) || string.IsNullOrEmpty(qrCode))
throw new Exception($"托盘码和轴承的唯一编码都不能为空");
//厂家
string madeUnit = saveModel.MainData["madeUnit"].ToString();
//轮对
string materielId = saveModel.MainData["materielId"].ToString();
//型号
string materielType = saveModel.MainData["materielType"].ToString();
//检修类型
string NewOrOld = saveModel.MainData["NewOrOld"].ToString();
//批次
string LotNumber = saveModel.MainData["LotNumber"].ToString();
//生产日期
string ProductDate = saveModel.MainData["ProductDate"].ToString();
if (string.IsNullOrEmpty(madeUnit) || string.IsNullOrEmpty(materielId) || string.IsNullOrEmpty(materielType) || string.IsNullOrEmpty(NewOrOld))
throw new Exception($"厂家/轮对/型号/检修类型都不能为空");
Dt_materiel_zc_info zcInfo = Dt_materiel_zc_infoRepository.Instance.FindFirst(r => r.materiel_zc_id == materielType
&& r.materiel_zc_car_id == materielId && r.materiel_zc_name == madeUnit);
if (null == zcInfo)
throw new Exception($"未找到车型:{materielId},轴承型号:{materielType},厂家:{madeUnit}的基础信息,请先维护.");
Dt_weighing_standards standars = Dt_weighing_standardsRepository.Instance.FindFirst(r => r.weighing_materiel_id == materielId &&
r.weighing_bearing_id == materielType && r.weighing_manufacturer == madeUnit && r.weighing_type == NewOrOld);
if (null == standars)
throw new Exception($"未找到称重标准:{materielId},轴承型号:{materielType},厂家:{madeUnit},检修类型:{NewOrOld}的基础信息,请先维护.");
//查询RFID和qrcode是否已经存在于库内,是否已经组盘
Dt_boxing_head boxHead = Dt_boxing_headRepository.Instance.FindFirst(x => x.boxhead_barcode == rfid);
if (boxHead != null)
throw new Exception($"托盘码:【{rfid}】已经存在于组盘记录中");
Dt_boxing_detail boxDetail = Dt_boxing_detailRepository.Instance.FindFirst(x => x.boxdtl_qrCode == qrCode);
if (boxDetail != null)
throw new Exception($"轴承唯一编码:【{qrCode}】已经存在于组盘记录中");
//查询RFID托盘号是否有未完成的任务
Dt_taskinfo taskInfo = Dt_taskinfoRepository.Instance.FindFirst(x => x.task_barcode == rfid);
if (taskInfo != null)
throw new Exception($"托盘码:【{rfid}】还有正在进行的任务");
//查询RFID已经存在于库存中
VV_ContainerInfo conInfo = VV_ContainerInfoRepository.Instance.FindFirst(x => x.containerhead_barcode == rfid);
if (conInfo != null)
throw new Exception($"托盘码:【{rfid}】已经存在于库存中,货位号:{conInfo.location_id}");
VV_ContainerInfo qrCodeConInfo = VV_ContainerInfoRepository.Instance.FindFirst(x => x.containerdtl_goodsCode == qrCode);
if (qrCodeConInfo != null)
throw new Exception($"轴承唯一编码:【{qrCode}】已经存在于库存中,货位号:{qrCodeConInfo.location_id}");
VV_ContainerInfo_EmptyPallet conInfoEmpty = VV_ContainerInfo_EmptyPalletRepository.Instance.FindFirst(x => x.containerhead_barcode == rfid);
if (conInfoEmpty != null)
throw new Exception($"托盘码:【{rfid}】已经存在于空托盘库存中,货位号:{conInfoEmpty.location_id}");
//查找该托盘是否之前入过库,组盘进库的托盘之前必须已经入过库的
Dt_container_head_hty head_Hty = Dt_container_head_htyRepository.Instance.FindFirst(x => x.containerhead_barcode == rfid);
if (head_Hty == null)
throw new Exception($"托盘码:【{rfid}】该托盘无入库记录,无法组盘,请先将其空托盘状态入库,在呼叫空托盘出库进行组盘入库");
//判断轴承有效期是否超过2年
if (DateTime.Now > Convert.ToDateTime(ProductDate).AddYears(2))
{
throw new Exception($"无法组盘:轴承有超过2年有效期");
}
//添加组盘头和组盘体的信息
content = Dt_boxing_headRepository.Instance.DbContextBeginTransaction(() =>
{
Dt_boxing_head newBoxHead = new Dt_boxing_head();
Dt_boxing_detail newBoxDetail = new Dt_boxing_detail();
newBoxHead.boxhead_id = Guid.NewGuid();
newBoxHead.boxhead_areaid = "";
newBoxHead.boxhead_barcode = rfid;
newBoxHead.boxhead_creator = UserContext.Current.UserTrueName;
newBoxHead.boxhead_createtime = DateTime.Now;
newBoxHead.boxhead_lastdatetime = DateTime.Now;
newBoxHead.boxhead_weight = "";
newBoxDetail.boxdtl_id = Guid.NewGuid();
newBoxDetail.boxdtl_headid = newBoxHead.boxhead_id.ToString();
newBoxDetail.boxdtl_materielid = materielId;
newBoxDetail.boxdtl_qty = "1";
newBoxDetail.boxdtl_operator = UserContext.Current.UserTrueName;
newBoxDetail.boxdtl_inboundUnit = "智粤";
newBoxDetail.boxdtl_standard = NewOrOld;
newBoxDetail.boxdtl_carType = materielId;
newBoxDetail.boxdtl_type = materielType;
newBoxDetail.boxdtl_barcode = rfid;
newBoxDetail.boxdtl_madeUnit = madeUnit;
newBoxDetail.boxdtl_madeDate = DateTime.Today.ToString();
newBoxDetail.boxdtl_number = qrCode;
newBoxDetail.boxdtl_qrCode = qrCode;
//2024年6月24日 新增批次 生产日期
newBoxDetail.boxdtl_text2 = LotNumber;
newBoxDetail.boxdtl_text3 = ProductDate;
Dt_boxing_headRepository.Instance.Add(newBoxHead, true);
Dt_boxing_detailRepository.Instance.Add(newBoxDetail, true);
Dt_mes_goods_info info = new Dt_mes_goods_info();
info.mesInfo_id = Guid.NewGuid();
info.mesInfo_inUnit = "智粤";
info.mesInfo_madeUnit = madeUnit;
info.mesInfo_carType = materielId;
info.mesInfo_type = materielType;
info.mesInfo_madeDate = DateTime.Now;
info.mesInfo_number = qrCode;
info.mesInfo_standard = NewOrOld;
info.mesInfo_createType = "mes";
info.mesInfo_qrCode = qrCode;
info.mesInfo_createDate = DateTime.Now;
Dt_mes_goods_infoRepository.Instance.Add(info, true);
//获取空货位
Dt_locationinfo emptyLocation = CommonFunction.GetEmptyLocationAction();
//获取任务
Dt_taskinfo tmpTaskInfo = CommonFunction.AddWMSTask_BoxPalletIn(emptyLocation, rfid, materielId, qrCode, materielType);
//修改货位状态
CommonFunction.ChangeLocationState(emptyLocation, LocationState.LocationState_Box_Inbound_Wait_Executing.ToString());
//当上面事务执行成功后,向WCS下发入库任务
content = WCSApi.SendTaskToWCS(new List() { tmpTaskInfo });
if (content.Status)
content.OK($"轴承组盘成功,任务托盘码为 = {tmpTaskInfo.task_barcode},货位号:{tmpTaskInfo.task_fromlocationid}");
else
content.Error($"轴承组盘失败,向WCS下发任务出错,错误信息 = {content.Message}");
return content;
});
}
catch (Exception ex)
{
content.Error("轴承组盘失败:" + ex.Message);
}
Logger.AddLog(LoggerType.Add, saveModel, content, content);
return content;
}
}
}