Browse Source

提交

master
gwbvipvip 6 months ago
parent
commit
1cf4fd64d3
3 changed files with 138 additions and 26 deletions
  1. +22
    -0
      BPA.SAAS.KitChenManage.Application/Device/Dtos/DeviceGoodsInputDto.cs
  2. +94
    -26
      BPA.SAAS.KitChenManage.Application/Device/Services/DevicePushRecodeService.cs
  3. +22
    -0
      BPA.SAAS.KitChenManage.Core/Model/BPA_DeviceGoods.cs

+ 22
- 0
BPA.SAAS.KitChenManage.Application/Device/Dtos/DeviceGoodsInputDto.cs View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPA.SAAS.KitChenManage.Application.Device.Dtos
{
public class DeviceGoodsInputDto
{
public string DeviceId { get; set; }

public List<Goods> GoodsList { get; set; }
}

public class Goods
{
public string GoodsId { get; set; }

public string GoodsName { get; set; }
}
}

+ 94
- 26
BPA.SAAS.KitChenManage.Application/Device/Services/DevicePushRecodeService.cs View File

@@ -29,7 +29,7 @@ using uPLibrary.Networking.M2Mqtt;

namespace BPA.SAAS.KitChenManage.Application.Device.Services
{
public class DevicePushRecodeService: IDevicePushRecodeService, ITransient
public class DevicePushRecodeService : IDevicePushRecodeService, ITransient
{
private readonly ISqlSugarClient _db;
private readonly IMqttClient _mqttClient;
@@ -39,18 +39,18 @@ namespace BPA.SAAS.KitChenManage.Application.Device.Services
public DevicePushRecodeService(ISqlSugarClient db, IMqttClient mqttClient)
{
_db = db;
_mqttClient=mqttClient;
_mqttClient = mqttClient;
mqttServer = new(mqttClient);
}
public async Task<string> Test()
{
var getGoodsAttributeurl = "http://localhost:5009/api/ExternalPlatform/Goods/GetGoodsPageList";//获取商品属性
var key = "47e5c071-6283-4d9d-8dcb-36ba95aa8db9";
var str = "current=1&pageSize=10&"+ key;
var str = "current=1&pageSize=10&" + key;
var sign = MD5Encryption.Encrypt(str.ToUpper());
var respon = await getGoodsAttributeurl.SetHeaders(new
{
sign= sign,
sign = sign,
key = key
}).SetHttpMethod(HttpMethod.Get).SetBody("{\"current\":1,\"pageSize\":10}", "application/json").PostAsStringAsync();
return respon;
@@ -63,25 +63,25 @@ namespace BPA.SAAS.KitChenManage.Application.Device.Services
public async Task<PageUtil> Page(DevicePushRecodeDtoPageInput input)
{
RefAsync<int> total = 0;
var res = await _db.Queryable<BPA_DevicePushRecode>().Where(x=>x.Type== input.Type)
.WhereIF(!string.IsNullOrWhiteSpace(input.DeviceName),x=>x.DeviceName.Contains(input.DeviceName))
.WhereIF(!string.IsNullOrWhiteSpace(input.Status), x => x.Status==Convert.ToInt32(input.Status))
.Select(t => new
var res = await _db.Queryable<BPA_DevicePushRecode>().Where(x => x.Type == input.Type)
.WhereIF(!string.IsNullOrWhiteSpace(input.DeviceName), x => x.DeviceName.Contains(input.DeviceName))
.WhereIF(!string.IsNullOrWhiteSpace(input.Status), x => x.Status == Convert.ToInt32(input.Status))
.Select(t => new
{
CreateAt = t.CreateAt,
CreateBy = t.CreateBy,
Id = t.Id,
DeviceId = t.DeviceId,
DeviceName= t.DeviceName,
Type =t.Type,
Topic=t.Topic,
DataResore=t.DataResore,
Status=t.Status,
Description=t.Description,
MessageId=t.MessageId,
TopicRely=t.TopicRely,
DataResoreRely=t.DataResoreRely,
DescriptionRely=t.DescriptionRely,
DeviceName = t.DeviceName,
Type = t.Type,
Topic = t.Topic,
DataResore = t.DataResore,
Status = t.Status,
Description = t.Description,
MessageId = t.MessageId,
TopicRely = t.TopicRely,
DataResoreRely = t.DataResoreRely,
DescriptionRely = t.DescriptionRely,
}).OrderBy(x => x.CreateAt, OrderByType.Desc).ToPageListAsync(input.Current, input.PageSize, total);
PageUtil util = new PageUtil()
{
@@ -122,7 +122,8 @@ namespace BPA.SAAS.KitChenManage.Application.Device.Services
var res = await _db.Insertable(data).CallEntityMethod(t => t.Create()).ExecuteCommandAsync();
//周期性任务 (10 秒后执行1次任务)
var trigger = TriggerBuilder.Default.WithInterval(TimeSpan.FromSeconds(10)).WithRepeatCount(1).Build();
TaskServicesManager.GetOrAdd(input[i].MessageId.ToString(), async (provider, token) => {
TaskServicesManager.GetOrAdd(input[i].MessageId.ToString(), async (provider, token) =>
{
await CallBackDevicePushRecode(input[i].MessageId.ToString(), groupId);
}, trigger);
//订阅回复数据下发topics
@@ -148,7 +149,7 @@ namespace BPA.SAAS.KitChenManage.Application.Device.Services
if (string.IsNullOrEmpty(Topic)) throw Oops.Oh("请配置相关topic");
//var maxmessageId=_db.Queryable<BPA_DevicePushRecode>().Max(x => x.MessageId);
if (string.IsNullOrWhiteSpace(input[i].MessageId)) input[i].MessageId = Guid.NewGuid().ToString();
// var messageId = Guid.NewGuid().ToString();
// var messageId = Guid.NewGuid().ToString();
data[i].Topic = Topic;
data[i].Status = 0;
data[i].DataResore = JsonConvert.SerializeObject(DataResore.Data);
@@ -166,7 +167,8 @@ namespace BPA.SAAS.KitChenManage.Application.Device.Services
var res = await _db.Insertable(data).CallEntityMethod(t => t.Create()).ExecuteCommandAsync();
//周期性任务 (10 秒后执行1次任务)
var trigger = TriggerBuilder.Default.WithInterval(TimeSpan.FromSeconds(10)).WithRepeatCount(1).Build();
TaskServicesManager.GetOrAdd(input[i].MessageId.ToString(), async (provider, token) => {
TaskServicesManager.GetOrAdd(input[i].MessageId.ToString(), async (provider, token) =>
{
await CallBackDevicePushRecode(input[i].MessageId.ToString(), groupId);
}, trigger);
//订阅回复数据下发topics
@@ -181,7 +183,7 @@ namespace BPA.SAAS.KitChenManage.Application.Device.Services
/// <param name="messageId"></param>
/// <param name="groupId"></param>
/// <returns></returns>
public async Task CallBackDevicePushRecode(string messageId,string groupId)
public async Task CallBackDevicePushRecode(string messageId, string groupId)
{
var model = await _db.Queryable<BPA_DevicePushRecode>().Where(x => x.MessageId.ToString() == messageId.ToString() && x.GroupId == groupId).FirstAsync();
if (model.Status == 0)
@@ -205,14 +207,15 @@ namespace BPA.SAAS.KitChenManage.Application.Device.Services
data.DataResoreRely = "";
// data.MessageId= messageId;
var respush = await mqttServer.Publish(data.Topic, new PushData() { Data = data.DataResore, DeviceId = data.DeviceAutoKey }, data.MessageId);
if (respush!= "success")
if (respush != "success")
{
data.Description = respush;
data.Status = 2;
}
var res = await _db.Updateable(data).ExecuteCommandAsync();
var trigger = TriggerBuilder.Default.WithInterval(TimeSpan.FromSeconds(10)).WithRepeatCount(1).Build();
TaskServicesManager.GetOrAdd(data.MessageId.ToString(), async (provider, token) => {
TaskServicesManager.GetOrAdd(data.MessageId.ToString(), async (provider, token) =>
{
await CallBackDevicePushRecode(data.MessageId.ToString(), groupId);
}, trigger);
await BindMqReceived(data.MessageId.ToString(), groupId);
@@ -244,7 +247,7 @@ namespace BPA.SAAS.KitChenManage.Application.Device.Services
/// <param name="messageId"></param>
/// <param name="groupId"></param>
/// <returns></returns>
private async Task BindMqReceived(string messageId,string groupId)
private async Task BindMqReceived(string messageId, string groupId)
{
var status = true;
_mqttClient.UseApplicationMessageReceivedHandler(async e =>
@@ -287,10 +290,75 @@ namespace BPA.SAAS.KitChenManage.Application.Device.Services

}
var getGoodsAttributeurl = BaseServerUrl + "api/device/updatethirdpartypushrecord";//修改第三方订单的回调
await getGoodsAttributeurl.SetHttpMethod(HttpMethod.Post).SetBody("{\"messageId\":"+ messageId + ",\"isPush\":"+ status + ",\"pushTime\":"+DateTime.Now+"}").PostAsStringAsync();
await getGoodsAttributeurl.SetHttpMethod(HttpMethod.Post).SetBody("{\"messageId\":" + messageId + ",\"isPush\":" + status + ",\"pushTime\":" + DateTime.Now + "}").PostAsStringAsync();
_db.Updateable(model).ExecuteCommand();
}
});
}





#region 设备商品

/// <summary>
/// 获取设备商品
/// </summary>
/// <param name="deviceId"></param>
/// <returns></returns>
public async Task<List<Goods>> GetDeviceGoods(string deviceId)
{
var result = new List<Goods>();

result = await _db.Queryable<BPA_DeviceGoods>().Where(x => x.DeviceId == deviceId)
.Select(x => new Goods()
{
GoodsId = x.GoodsId,
GoodsName = x.GoodsName
}).ToListAsync();

return result;
}

/// <summary>
/// 编辑设备商品
/// </summary>
/// <param name="inputDto"></param>
/// <returns></returns>
public async Task<bool> EditDeviceGoods(DeviceGoodsInputDto inputDto)
{
try
{
_db.Ado.BeginTran();

//删除店铺商品
await _db.Deleteable<BPA_DeviceGoods>().Where(it => it.DeviceId == inputDto.DeviceId).ExecuteCommandAsync();

var list = new List<BPA_DeviceGoods>();
foreach (var item in inputDto.GoodsList)
{
list.Add(new BPA_DeviceGoods()
{
DeviceId = inputDto.DeviceId,
GoodsId = item.GoodsId,
GoodsName = item.GoodsName,
});
}
var res = await _db.Insertable(list).ExecuteCommandAsync();

return res > 0;

_db.Ado.CommitTran();
}
catch (Exception e)
{
_db.Ado.RollbackTran();
return false;
}
}

#endregion

}
}

+ 22
- 0
BPA.SAAS.KitChenManage.Core/Model/BPA_DeviceGoods.cs View File

@@ -0,0 +1,22 @@
using BPA.SAAS.KitChenManage.Core.Base;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPA.SAAS.KitChenManage.Core.Model
{

[SugarTable("bpa_devicegoods")]
public class BPA_DeviceGoods:IBaseEntity, IGroupId
{
public string DeviceId { get; set; }

public string GoodsId { get; set; }

public string GoodsName { get; set;}
public string GroupId { get; set; }
}
}

Loading…
Cancel
Save