/*
*所有关于Dt_boxing_head类的业务代码应在此处编写
*可使用repository.调用常用方法,获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Dt_boxing_headService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using WIDESEA.Core.BaseProvider;
using WIDESEA.Core.Extensions.AutofacManager;
using WIDESEA.Entity.DomainModels;
using System.Linq;
using WIDESEA.Core.Utilities;
using System.Linq.Expressions;
using WIDESEA.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using WIDESEA.Services.IRepositories;
using Newtonsoft.Json;
using System.Text;
using System.Net;
using System.IO;
using System;
using WIDESEA.Services.Repositories;
using System.Threading;
using WIDESEA.Common;
using WIDESEA_Services;
using WIDESEA.Core.Services;
using WIDESEA.Core.Enums;
using WIDESEA.Core.ManageUser;
using System.Collections.Generic;
using WIDESEA.Services.Services.ToMes;
namespace WIDESEA.Services.Services
{
public partial class Dt_boxing_headService
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IDt_boxing_headRepository _repository;//访问数据库
[ActivatorUtilitiesConstructor]
public Dt_boxing_headService(
IDt_boxing_headRepository dbRepository,
IHttpContextAccessor httpContextAccessor
)
: base(dbRepository)
{
_httpContextAccessor = httpContextAccessor;
_repository = dbRepository;
//多租户会用到这init代码,其他情况可以不用
//base.Init(dbRepository);
}
///
/// 创建组盘信息
///
///
///
public WebResponseContent CreateBoxingInfo(SaveModel saveModel)
{
WebResponseContent content = new WebResponseContent();
try
{
//托盘RFID
string rfid = saveModel.MainData["rfid"].ToString();
//通过MD5生成的轴承唯一编码
string qrCode = saveModel.MainData["qrcode"].ToString();
if (string.IsNullOrEmpty(rfid) || string.IsNullOrEmpty(qrCode))
throw new Exception($"托盘码和轴承的唯一编码都不能为空");
//查询唯一码是否由系统正常生成
Dt_mes_goods_info mesInfo = Dt_mes_goods_infoRepository.Instance.FindFirst(x => x.mesInfo_qrCode == qrCode);
if (mesInfo == null)
throw new Exception($"无法找到与轴承唯一编码:【{qrCode}】对应的轴承信息");
//查询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}】已经存在于库存中");
VV_ContainerInfo qrCodeConInfo = VV_ContainerInfoRepository.Instance.FindFirst(x => x.containerdtl_goodsCode == qrCode);
if (qrCodeConInfo != null)
throw new Exception($"轴承唯一编码:【{qrCode}】已经存在于库存中");
VV_ContainerInfo_EmptyPallet conInfoEmpty = VV_ContainerInfo_EmptyPalletRepository.Instance.FindFirst(x => x.containerhead_barcode == rfid);
if (conInfoEmpty != null)
throw new Exception($"托盘码:【{rfid}】已经存在于空托盘库存中");
//查找该托盘是否之前入过库,组盘进库的托盘之前必须已经入过库的
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}】该托盘无入库记录,无法组盘,请先将其空托盘状态入库,在呼叫空托盘出库进行组盘入库");
//根据唯一编码信息找到提前录入的信息、以及轴承的重量
mesInfo = Dt_mes_goods_infoRepository.Instance.FindFirst(x => x.mesInfo_qrCode == qrCode);
// ToMesService.Mes_GetBearingInfo(qrCode);
//添加组盘头和组盘体的信息
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 = mesInfo.mesInfo_carType;
newBoxDetail.boxdtl_qty = "1";
newBoxDetail.boxdtl_operator = UserContext.Current.UserTrueName;
newBoxDetail.boxdtl_inboundUnit = mesInfo.mesInfo_inUnit;
newBoxDetail.boxdtl_standard = mesInfo.mesInfo_standard;
newBoxDetail.boxdtl_carType = mesInfo.mesInfo_carType;
newBoxDetail.boxdtl_type = mesInfo.mesInfo_type;
newBoxDetail.boxdtl_barcode = rfid;
newBoxDetail.boxdtl_madeUnit = mesInfo.mesInfo_madeUnit;
newBoxDetail.boxdtl_madeDate = mesInfo.mesInfo_madeDate.ToString();
newBoxDetail.boxdtl_number = mesInfo.mesInfo_number;
newBoxDetail.boxdtl_qrCode = mesInfo.mesInfo_qrCode;
//newBoxDetail.boxdtl_text1 =
Dt_boxing_headRepository.Instance.Add(newBoxHead, true);
Dt_boxing_detailRepository.Instance.Add(newBoxDetail, true);
//获取空货位
Dt_locationinfo emptyLocation = CommonFunction.GetEmptyLocationAction();
//获取任务
Dt_taskinfo tmpTaskInfo = CommonFunction.AddWMSTask_BoxPalletIn(emptyLocation,
rfid, mesInfo.mesInfo_carType, mesInfo.mesInfo_qrCode,"");
//修改货位状态
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;
}
///
/// 创建空托入库任务
///
///
///
public WebResponseContent CreateEmptyPalletTask(SaveModel saveModel)
{
WebResponseContent content = new WebResponseContent();
try
{
string barcode = saveModel.MainData["barcode"].ToString();
Dt_taskinfo oldTask = Dt_taskinfoRepository.Instance.FindFirst(r => r.task_barcode == barcode);
if (null != oldTask)
throw new Exception($"当前已存在托盘号:{barcode}的任务,请先检查。");
VV_ContainerInfo_EmptyPallet emptyCon = VV_ContainerInfo_EmptyPalletRepository.Instance.FindFirst(x => x.containerhead_barcode == barcode);
if (emptyCon != null)
throw new Exception($"库存中存在托盘号:【{barcode}】的【空托】库存,请人工核实,所在货位:{emptyCon.location_id}");
//说明是空托入库
Dt_locationinfo emptyLocation = CommonFunction.GetEmptyLocationAction();
//在此处生成WMS任务和下发WCS任务---------空托入库
Dt_taskinfo tmpTaskInfo = null;
content = Dt_taskinfoRepository.Instance.DbContextBeginTransaction(() =>
{
//在事务里面,尝试新建空托盘入库任务
tmpTaskInfo = CommonFunction.AddWMSTask_EmptyPalletIn(emptyLocation, barcode);
CommonFunction.ChangeLocationState(emptyLocation, LocationState.LocationState_Empty_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;
}
///
/// 查询一个托盘号对应的组盘信息
///
///
///
public WebResponseContent GetBoxingInfo(SaveModel saveModel)
{
WebResponseContent content = new WebResponseContent();
try
{
string barcode = saveModel.MainData["barcode"].ToString();
if (barcode != null)
{
Dt_boxing_head boxInfo = Dt_boxing_headRepository.Instance.FindFirst(x => x.boxhead_barcode == barcode);
if (boxInfo != null)
{
content.OK(data: boxInfo);
}
else
{
content.OK(data: null);
}
}
else
{
content.Error("Error:查询的托盘号值为 Null");
}
}
catch (Exception ex)
{
content.Error(ex.Message);
}
return content;
}
}
}