You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.0 KiB

using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac.Attribute;
using Newtonsoft.Json;
using Repository.Station;
using Service.Cloud.Client;
using Service.Cloud.Common;
using Service.Cloud.Msg.Cloud.Req;
using Service.Cloud.Msg.Host.Resp;
namespace Service.Cloud.Handler;
[Scope]
public class ElectricityPriceModelHandler : IBaseHandler
{
private ElecPriceModelVersionRepository _versionRepository;
private ElecPriceModelVersionDetailRepository _detailRepository;
public ElectricityPriceModelHandler(ElecPriceModelVersionRepository elecPriceModelVersionRepository,
ElecPriceModelVersionDetailRepository elecPriceModelVersionDetailRepository)
{
this._versionRepository = elecPriceModelVersionRepository;
this._detailRepository = elecPriceModelVersionDetailRepository;
}
//两个电价帧用这个,另一个不用
public bool CanHandle(string cmd)
{
return CloudConst.setStaPrice == cmd;
}
public void Handle(string t)
{
SetStaPrice? priceModel = JsonConvert.DeserializeObject<SetStaPrice>(t);
if (priceModel != null)
{
if (priceModel.priceTemp.Count>0)
{
ElecPriceModelVersion version = _versionRepository.QueryByClause(d => d.Ceid == priceModel.priceTemp[0].ceid);
if (version == null)
{
var version01 = _versionRepository.QueryListByClause(d => d.Ceid != null, 1, "desc");
version = new ElecPriceModelVersion
{
Version = version01 == null ? 1:version01[0].Version + 1,
Ceid = priceModel.priceTemp[0].ceid,
};
}
version.StartTime = DateTime.Now;
version.EndTime = DateTime.Now.AddYears(10);
_versionRepository.InsertOrUpdate(version);
List<PriceTemp>? segs = priceModel.priceTemp;
if (segs is { Count: > 0 })
{
List<ElecPriceModelVersionDetail> versionDetails = segs.Select(d => new ElecPriceModelVersionDetail()
{
Version = version.Version,
Price = (int)d.ep,//* 10000
Type = d.pr,
StartHour = d.st.Hour,
StartMinute = d.st.Minute,
StartSecond = d.st.Second,
EndHour = d.et.Hour,
EndMinute = d.et.Minute,
EndSecond = d.et.Second
}).ToList();
//先删除同模型ID的数据
_detailRepository.Delete(u=>u.Version== version.Version);
_detailRepository.Insert(versionDetails);
}
}
SetStaPriceRes resp = new SetStaPriceRes();
CloudClientMgr.CloudClient?.Publish(resp);
}
}
}