/// <summary>
        /// 新增体重
        /// </summary>
        /// <param name="input">体重输入</param>
        /// <returns>是否成功</returns>
        public OutputBase Add(AddWeightInput input)
        {
            var weight      = Mapper.Map <AddWeightInput, Weight>(input);
            var measureTime = DateTime.Now;

            weight.MeasureTime = measureTime;
            _repository.Add(weight);

            return(_unitWork.Commit() ? OutputBase.Success("新增报警记录成功") : OutputBase.Fail("新增报警记录失败"));
        }
        /// <summary>
        /// 新增体重
        /// </summary>
        /// <param name="input">体重输入</param>
        /// <returns>是否成功</returns>
        public async Task <OutputBase> Add(AddWeightInput input)
        {
            var weight      = Mapper.Map <AddWeightInput, Weight>(input);
            var measureTime = DateTime.Now;

            weight.MeasureMethod = (int)MeasureMethodEnum.App;
            weight.MeasureTime   = measureTime;
            _repository.Add(weight);

            var latestPostDialysisWeight = await _repository.GetLatestPostDialysisWeight(input.PatientId);

            if (latestPostDialysisWeight == null)
            {
                return(_unitWork.Commit() ? OutputBase.Success("新增报警记录成功") : OutputBase.Fail("新增报警记录失败"));
            }

            var alarmEntity = new Alarm
            {
                PostDialysisWeight = latestPostDialysisWeight.Value,
                PostDialysisTime   = latestPostDialysisWeight.MeasureTime,
                PatientId          = input.PatientId,
                CurrentWeight      = input.Value,
                CurrentWeightTime  = measureTime,
                IsConfirmed        = false,
                WeightOverflow     = Math.Round(input.Value - latestPostDialysisWeight.Value, 2)
            };
            var percentage = Math.Abs(alarmEntity.WeightOverflow / alarmEntity.PostDialysisWeight);

            if (percentage >= 0.03m)
            {
                _alarmRepository.Add(alarmEntity);
                var patient = await _patientRepository.GetPatientById(input.PatientId);

                string content       = string.Format("{0}产生体重预警", patient.PatientName);
                var    messageEntity = new Message
                {
                    IsRead    = false,
                    Content   = content,
                    ReceiveId = patient.DoctorId.HasValue ? patient.DoctorId.Value : 0,
                    Title     = "产生体重预警",
                    Type      = (int)MessageTypeEnum.产生体重预警,
                    SendId    = input.PatientId,
                    SendName  = patient.PatientName,
                    Url       = string.Empty,
                    AppType   = (int)AppTypeEnum.Doctor
                };
                _messageRepository.Add(messageEntity);

                if (!_unitWork.Commit())
                {
                    return(OutputBase.Fail("新增报警记录失败"));
                }
                #region 异步发送JPush Notification、Message
                ThreadPool.QueueUserWorkItem(delegate
                {
                    string tag;
                    if (patient.DoctorId.HasValue)
                    {
                        tag = patient.DoctorId.Value.ToString();
                    }
                    else
                    {
                        tag = input.HospitalId.ToString();
                    }
                    new JPushMessage(AppTypeEnum.Doctor, (int)JPushKeyEnum.GenerateAlarm, tag, content, messageEntity.OuterId, _optionsAccessor.IsDevModel).SendPush();
                    new JPushNotification(AppTypeEnum.Doctor, (int)JPushKeyEnum.GenerateAlarm, tag, content, messageEntity.OuterId, _optionsAccessor.IsDevModel).SendPush();
                });
                #endregion
                return(OutputBase.Success("新增报警记录成功"));
            }

            return(_unitWork.Commit() ? OutputBase.Success("新增报警记录成功") : OutputBase.Fail("新增报警记录失败"));
        }
 public async Task <OutputBase> Add([FromBody] AddWeightInput input)
 {
     return(await _service.Add(input));
 }
Ejemplo n.º 4
0
        public override Empty AddWeight(AddWeightInput input)
        {
            Assert(input.ProfitId != null, "Invalid profit id.");
            Assert(input.Receiver != null, "Invalid receiver address.");
            Assert(input.Weight >= 0, "Invalid weight.");

            if (input.EndPeriod == 0)
            {
                // Which means this profit receiver will never expired.
                input.EndPeriod = long.MaxValue;
            }

            var profitId   = input.ProfitId;
            var profitItem = State.ProfitItemsMap[profitId];

            Assert(profitItem != null, "Profit item not found.");

            if (profitItem == null)
            {
                return(new Empty());
            }

            Assert(input.EndPeriod >= profitItem.CurrentPeriod, "Invalid end period.");

            profitItem.TotalWeight += input.Weight;

            State.ProfitItemsMap[profitId] = profitItem;

            var profitDetail = new ProfitDetail
            {
                StartPeriod = profitItem.CurrentPeriod,
                EndPeriod   = input.EndPeriod,
                Weight      = input.Weight,
            };
            var currentProfitDetails = State.ProfitDetailsMap[profitId][input.Receiver];

            if (currentProfitDetails == null)
            {
                // TODO: Reduce Resource token of Profit Contract from DApp Developer because this behaviour will add a new key.
                currentProfitDetails = new ProfitDetails
                {
                    Details = { profitDetail }
                };
            }
            else
            {
                currentProfitDetails.Details.Add(profitDetail);
            }

            // Remove details too old.
            foreach (var detail in currentProfitDetails.Details.Where(
                         d => d.EndPeriod != long.MaxValue && d.LastProfitPeriod >= d.EndPeriod &&
                         d.EndPeriod.Add(profitItem.ExpiredPeriodNumber) < profitItem.CurrentPeriod))
            {
                currentProfitDetails.Details.Remove(detail);
            }

            State.ProfitDetailsMap[profitId][input.Receiver] = currentProfitDetails;

            return(new Empty());
        }