using Masuit.Tools;
|
using Newtonsoft.Json;
|
using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEA_Core;
|
using WIDESEA_Core.BaseServices;
|
using WIDESEA_Core.Helper;
|
using WIDESEA_ISquareCabinRepository;
|
using WIDESEA_ISquareCabinServices;
|
using WIDESEA_Model.Models;
|
using WIDESEA_SquareCabinRepository;
|
using static System.Net.WebRequestMethods;
|
using static WIDESEA_DTO.SquareCabin.OrderDto;
|
|
namespace WIDESEA_SquareCabinServices
|
{
|
public class CabinOrderServices : ServiceBase<Dt_CabinOrder, ICabinOrderRepository>, ICabinOrderServices
|
{
|
public CabinOrderServices(ICabinOrderRepository BaseDal) : base(BaseDal)
|
{
|
}
|
|
/// <summary>
|
/// 获取上游系统的入库单
|
/// </summary>
|
/// <param name="searchDate"></param>
|
/// <returns></returns>
|
public WebResponseContent GetUpstreamOrder(DateTime searchDate)
|
{
|
var responseContent = new WebResponseContent();
|
|
try
|
{
|
// 请求地址
|
var url = "http://127.0.0.1:9090/GYZ2/95fck/inOrder";
|
|
// 请求参数
|
var requestData = new
|
{
|
searchDate = searchDate.ToString("yyyy-MM-dd HH:mm:ss")
|
};
|
|
// 发起请求
|
var result = HttpHelper.Post(url, requestData.ToJsonString());
|
|
|
// 反序列化
|
var response = JsonConvert.DeserializeObject<UpstreamResponse<UpstreamOrderInfo>>(result);
|
|
if (response.resultCode != "0")
|
{
|
// 调用异常接口
|
SendErrorToUpstream(1, "", response.resultMsg ?? "上游接口返回失败", "");
|
return responseContent.Error(response.resultMsg ?? "上游接口返回失败");
|
}
|
|
if (response.data == null || !response.data.Any())
|
{
|
return responseContent.OK("无新入库单数据");
|
}
|
|
Db.Ado.BeginTran();
|
|
foreach (var order in response.data)
|
{
|
try
|
{
|
// 插入入库单表
|
var entityOrder = new Dt_CabinOrder
|
{
|
Order_no = order.order_no,
|
Order_type = order.order_type,
|
Supplier_no = order.supplier_no,
|
Account_tiem= order.account_time,
|
};
|
|
var orderId = Db.Insertable(entityOrder).ExecuteReturnIdentity(); //就是返回主键ID。
|
|
// 插入入库明细表
|
var detailEntities = order.details.Select(d => new Dt_CabinOrderDetail
|
{
|
OrderId = orderId,
|
Goods_no = d.goods_no,
|
Order_qty = d.order_qty,
|
Batch_num = d.batch_num,
|
Exp_date = d.exp_date,
|
Warehouse_no = d.warehouse_no,
|
Status = 0,
|
}).ToList();
|
|
Db.Insertable(detailEntities).ExecuteCommand();
|
}
|
catch (Exception innerEx)
|
{
|
// 针对某条订单报错时,推送异常给上游
|
SendErrorToUpstream(1, order.order_no, innerEx.Message, "");
|
throw; // 抛出异常,让外层捕获回滚
|
}
|
}
|
|
Db.Ado.CommitTran();
|
|
return responseContent.OK("同步入库单成功");
|
}
|
catch (Exception ex)
|
{
|
// 全局异常时,也推送异常给上游
|
SendErrorToUpstream(1, "", ex.Message, "");
|
Db.Ado.RollbackTran();
|
return responseContent.Error("同步失败: " + ex.Message);
|
}
|
}
|
|
/// <summary>
|
/// 入库报完成接口
|
/// </summary>
|
/// <param name="order_no">入库单号</param>
|
/// <returns></returns>
|
public WebResponseContent CompleteOrder(string order_no)
|
{
|
var responseContent = new WebResponseContent();
|
try
|
{
|
if (string.IsNullOrWhiteSpace(order_no))
|
{
|
return responseContent.Error("入库单号不可以为空");
|
}
|
var url = " http://127.0.0.1:9090/GYZ2/95fck/inOrderOk";
|
var reslut=HttpHelper.Post(url, new {order_no }.ToJsonString());
|
var response = JsonConvert.DeserializeObject<UpstreamOrderResponse>(reslut);
|
if (response.resultCode != "0")
|
{
|
SendErrorToUpstream(2, "", "上游接口返回失败", "");
|
return responseContent.Error(response.resultMsg ?? "上游接口返回失败");
|
}
|
return responseContent.OK("操作成功");
|
}
|
|
catch (Exception ex)
|
{
|
SendErrorToUpstream(1, "", ex.Message, "");
|
return responseContent.Error("同步失败: " + ex.Message);
|
}
|
|
}
|
|
/// <summary>
|
/// 推送异常信息给上游系统1.入库单接口;2.入库单报完成接口;3.出库单接口;4.出库报完成接口;5.药品基础信息同步接口;6.供应商信息接口;7.客户信息接口;8.库存
|
/// </summary>
|
public void SendErrorToUpstream(int type, string code, string message, string remark)
|
{
|
try
|
{
|
var url = "http://127.0.0.1:9090/GYZ2/95fck/lexceptionLogog";
|
|
var requestData = new
|
{
|
type = type.ToString(),
|
code = code,
|
message = message,
|
remark = remark
|
};
|
|
var result = HttpHelper.Post(url, requestData.ToJsonString());
|
// 可以反序列化检查 resultCode 是否为0
|
}
|
catch (Exception e)
|
{
|
// 这里不要再抛异常了,避免死循环
|
Console.WriteLine("异常接口推送失败:" + e.Message);
|
}
|
}
|
|
|
|
|
|
}
|
}
|