using HslCommunication;
|
using Newtonsoft.Json;
|
using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Security.Policy;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEA_Core;
|
using WIDESEA_Core.BaseRepository;
|
using WIDESEA_Core.BaseServices;
|
using WIDESEA_Core.Helper;
|
using WIDESEA_ISquareCabinServices;
|
using WIDESEA_Model.Models;
|
using static WIDESEA_DTO.SquareCabin.OrderDto;
|
using static WIDESEA_DTO.SquareCabin.TowcsDto;
|
|
|
namespace WIDESEA_SquareCabinServices
|
{
|
public class MedicineGoodsServices : ServiceBase<Dt_MedicineGoods, IRepository<Dt_MedicineGoods>>, IMedicineGoodsServices
|
{
|
public MedicineGoodsServices(IRepository<Dt_MedicineGoods> BaseDal) : base(BaseDal)
|
{
|
}
|
public IRepository<Dt_MedicineGoods> Repository => BaseDal;
|
|
static string SearchDate = "";
|
|
/// <summary>
|
/// 获取药品基础信息同步接口
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent GetMedicineGoodsInfom()
|
{
|
var responseContent = new WebResponseContent();
|
try
|
{
|
|
var url = "http://121.37.118.63:80/GYZ2/95fck/goodsInfo";
|
|
|
if (string.IsNullOrEmpty(SearchDate)) SearchDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
//请求参数
|
var requestData = new
|
{
|
searchDate = "2022-10-10 20:45:16" // 正确的格式
|
};
|
//if (string.IsNullOrEmpty(SearchDate))
|
//{
|
// // 默认查询过去24小时的数据
|
// SearchDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
|
//}
|
//// 请求参数
|
//var requestData = new
|
//{
|
// searchDate = SearchDate
|
//};
|
//SearchDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
|
// 发起请求
|
var result = HttpHelper.Post(url, requestData.ToJsonString());
|
|
// 反序列化
|
var response = JsonConvert.DeserializeObject<UpstreamResponse<MedicationsInfo>>(result);
|
|
if (response.resultCode != "0")
|
{
|
// 调用异常接口
|
SendErrorToUpstream(5, "", response.resultMsg ?? "上游接口返回失败", "");
|
return responseContent.Error(response.resultMsg ?? "上游接口返回失败");
|
}
|
if (response.data == null || !response.data.Any())
|
{
|
return responseContent.OK("无新药品数据");
|
}
|
|
//获取所有已存在的商品编号
|
var existingGoodsNos = BaseDal.Db.Queryable<Dt_MedicineGoods>()
|
.Select(g => g.Goods_no)
|
.ToList();
|
|
// 收集所有要插入的实体
|
var entityList = response.data.Where(Medicat => !existingGoodsNos.Contains(Medicat.goods_no)).Select(Medicat => new Dt_MedicineGoods
|
{
|
Goods_no = Medicat.goods_no,
|
MaterialCode=Medicat.materialCode,
|
Goods_spm = Medicat.goods_spm,
|
Model = Medicat.model,
|
Factory = Medicat.factory,
|
Unit = Medicat.unit,
|
// 使用 ?? 运算符提供默认值
|
Item_Iength = Medicat.item_length ,
|
Item_width = Medicat.item_width,
|
Item_hight = Medicat.item_hight,
|
Item_volumn = Medicat.item_volumn,
|
Item_weight = Medicat.item_weight,
|
Storage_cond = Medicat.storage_cond,
|
Remark = Medicat.remark,
|
GoodStatus = "未下发",
|
ModifyDate = Medicat.modify_date
|
}).ToList();
|
// 一次性批量插入数据库
|
BaseDal.Db.Insertable(entityList).ExecuteCommand();
|
|
ProductSynchronous();
|
return responseContent.OK("同步成功");
|
}
|
|
catch (Exception ex)
|
{
|
SendErrorToUpstream(5, "", ex.Message, "");
|
return responseContent.Error(ex.Message);
|
}
|
}
|
|
|
/// <summary>
|
/// 发给下游wcs同步药品信息
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent ProductSynchronous()
|
{
|
try
|
{
|
//先找到所有未下发的药品
|
var list = BaseDal.QueryData(x => x.GoodStatus == "未下发").ToList();
|
if (list == null || !list.Any())
|
{
|
return new WebResponseContent().OK("无未下发药品");
|
}
|
//找到了就将整个list发给下游系统
|
foreach (var item in list)
|
{
|
var medicineDTO = new ProductInfo
|
{
|
//用户code
|
customerCode = "905",
|
//物料类型
|
materialCode =item.MaterialCode,
|
//产品编码
|
productCode= item.Goods_no,
|
//产品名
|
productName =item.Goods_spm ,
|
//产品条码
|
productBarCode= item.Goods_no,
|
|
//规格
|
productSpecifications=item.Model,
|
//单位
|
unit = item.Unit,
|
//长
|
singleProductLongNum = item.Item_Iength.ToString(),
|
//宽
|
singleProductWideNum = item.Item_weight.ToString(),
|
//高
|
singleProductHighNum = item.Item_hight.ToString(),
|
//重量
|
singleProductWeight = item.Item_weight.ToString(),
|
//体积
|
singleProductVolume = item.Item_volumn.ToString(),
|
//是否取消 0是不删除,1删除
|
isDelete = "0"
|
};
|
var url = "http://172.16.1.2:9357/file-admin/api/product/productSynchronous";
|
var result = HttpHelper.Post(url, medicineDTO.ToJsonString());
|
var resp = JsonConvert.DeserializeObject<TowcsResponse<object>>(result);
|
|
if (resp!=null&&resp.code=="0")
|
{
|
//更新药品状态-已完成
|
BaseDal.Db.Updateable<Dt_MedicineGoods>()
|
.SetColumns(x => x.GoodStatus == "下发完成")
|
.Where(x => x.Goods_no == item.Goods_no)
|
.ExecuteCommand();
|
}
|
else
|
{
|
SendErrorToUpstream(5, medicineDTO.materialCode, resp?.msg ?? "WCS药品信息同步失败", "");
|
|
return new WebResponseContent { Status = false, Message = "订单推送失败" };
|
}
|
|
}
|
return new WebResponseContent { Status = true, Message = "成功" };
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine("ProductSynchronous 异常:" + ex.Message);
|
return new WebResponseContent { Status = false, Message = ex.Message };
|
}
|
|
}
|
|
/// <summary>
|
/// 同步删除删除商品信息
|
/// 药品编码
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent DeleteProduct(string goodsno)
|
{
|
|
try
|
{
|
var goods = BaseDal.QueryData(x => x.Goods_no == goodsno).FirstOrDefault();
|
if (goods == null)
|
{
|
return new WebResponseContent
|
{
|
Status = false,
|
Message = $"商品编号 {goodsno} 不存在"
|
};
|
}
|
var medicineDTO = new ProductInfo
|
{
|
//用户code
|
customerCode = "905",
|
//物料类型
|
materialCode = "YY",
|
//产品编码
|
productCode = goods.Goods_no,
|
//产品名
|
productName = goods.Goods_spm,
|
//产品条码
|
productBarCode = goods.Goods_no,
|
|
//规格
|
productSpecifications = goods.Model,
|
//单位
|
unit = goods.Unit,
|
//长
|
singleProductLongNum = goods.Item_Iength.ToString(),
|
//宽
|
singleProductWideNum = goods.Item_weight.ToString(),
|
//高
|
singleProductHighNum = goods.Item_hight.ToString(),
|
//重量
|
singleProductWeight = goods.Item_weight.ToString(),
|
//体积
|
singleProductVolume = goods.Item_volumn.ToString(),
|
//是否取消 0是不删除,1删除
|
isDelete = "1"
|
};
|
var url = "http://172.16.1.2:9357/file-admin/api/product/productSynchronous";
|
//var url = "http://172.16.1.245:9357/file-admin/api/product/productSynchronous";
|
|
var result = HttpHelper.Post(url, medicineDTO.ToJsonString());
|
var resp = JsonConvert.DeserializeObject<TowcsResponse<object>>(result);
|
if (resp != null && resp.code == "0")
|
{
|
//我自己也删除
|
BaseDal.DeleteData(goods);
|
return new WebResponseContent { Status = true, Message = "删除成功" };
|
}
|
return new WebResponseContent { Status = false, Message = "删除失败请重试" };
|
|
|
}
|
catch (Exception ex)
|
{
|
|
return new WebResponseContent { Status=false,Message = 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://121.37.118.63:80/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);
|
}
|
}
|
|
|
}
|
}
|