using Masuit.Tools;
|
using Newtonsoft.Json;
|
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_DTO.SquareCabin;
|
using WIDESEA_ISquareCabinRepository;
|
using WIDESEA_ISquareCabinServices;
|
using WIDESEA_Model.Models;
|
using WIDESEA_SquareCabinRepository;
|
using static WIDESEA_DTO.SquareCabin.OrderDto;
|
|
namespace WIDESEA_SquareCabinServices
|
{
|
public class DeliveryOrderServices : ServiceBase<Dt_DeliveryOrder, IDeliveryOrderRepository>, IDeliveryOrderServices
|
{
|
public DeliveryOrderServices(IDeliveryOrderRepository BaseDal) : base(BaseDal)
|
{
|
}
|
|
|
|
|
|
/// <summary>
|
/// 获取上游出库单 0成功1失败
|
/// </summary>
|
/// <param name="searchDate"></param>
|
/// <returns></returns>
|
public WebResponseContent GetUpstreamOutOrder(DateTime searchDate)
|
{
|
var responseContent = new WebResponseContent();
|
try
|
{
|
//请求地址
|
var url = "http:/127.0.0.1:9000/GYZ2/95fck/outOrder";
|
//请求参数
|
var requestDate = new
|
{
|
searchDate = searchDate.ToString("yyyy-MM-dd:mm:ss")
|
};
|
|
//发起请求
|
var reslu = HttpHelper.Post(url, requestDate.ToJsonString());
|
//反序列化
|
var response = JsonConvert.DeserializeObject<UpstreamResponse<UpstramOutOrderInfo>>(reslu);
|
|
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 outorder in response.data)
|
{
|
try
|
{
|
//插入出库单
|
var OutOders = new Dt_DeliveryOrder
|
{
|
Out_no = outorder.out_no,
|
Out_type = outorder.out_type,
|
Client_no=outorder.client_no,
|
Account_time = outorder.account_time,
|
};
|
var outorderId = Db.Insertable(outorder).ExecuteReturnIdentity();//返回主键id
|
|
//插入出库单明细
|
var OutOrderDetails = outorder.details.Select(d => new Dt_DeliveryOrderDetail
|
{
|
DeliveryOrderId = outorderId,
|
Goods_no = d.goods_no,
|
Order_qty = d.out_qty,
|
Batch_num = d.batch_num,
|
Exp_date = d.exp_date
|
}).ToList();
|
Db.Insertable(OutOrderDetails).ExecuteCommand();
|
}
|
catch (Exception ex)
|
{
|
|
// 针对某条订单报错时,推送异常给上游
|
SendErrorToUpstream(3, outorder.out_no, ex.Message, "");
|
throw; // 抛出异常,让外层捕获回滚
|
}
|
}
|
Db.Ado.CommitTran();
|
return responseContent.OK("同步入库单成功");
|
}
|
catch (Exception ex)
|
{
|
// 全局异常时,也推送异常给上游
|
SendErrorToUpstream(3, "", ex.Message, "");
|
Db.Ado.RollbackTran();
|
return responseContent.Error("同步失败: " + ex.Message);
|
}
|
}
|
|
|
/// <summary>
|
/// 出库报完成接口
|
/// </summary>
|
/// <param name="out_no">出库单号</param>
|
/// <returns></returns>
|
public WebResponseContent CompleteOutOrder(string out_no)
|
{
|
var responseContent = new WebResponseContent();
|
try
|
{
|
if (string.IsNullOrWhiteSpace(out_no))
|
{
|
return responseContent.Error("入库单号不可以为空");
|
}
|
var url = " http://127.0.0.1:9090/GYZ2/95fck/outOrderOk";
|
var reslut = HttpHelper.Post(url, new {out_no }.ToJsonString());
|
var response = JsonConvert.DeserializeObject<UpstreamOrderResponse>(reslut);
|
if (response.resultCode != "0")
|
{
|
SendErrorToUpstream(4, "", "上游接口返回失败", "");
|
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/exceptionLog";
|
|
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);
|
}
|
}
|
}
|
}
|