Пример #1
0
        public IHttpActionResult AddPneumoniaBotOperationRecord(PneumoniaBotOperationRecordInputDto dto)
        {
            var result = _pneumoniaBotService.AddPneumoniaBotOperationRecord(dto);

            return(Ok(result));
        }
        /// <summary>
        /// 新增 肺炎Bot操作记录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ReturnValueModel AddPneumoniaBotOperationRecord(PneumoniaBotOperationRecordInputDto dto)
        {
            ReturnValueModel            rvm   = new ReturnValueModel();
            PneumoniaBotOperationRecord model = null;
            bool isInsert = true;

            if (dto.ClickTime > 0 && dto.LeaveTime > 0)
            {
                model = _rep.FirstOrDefault <PneumoniaBotOperationRecord>(o =>
                                                                          o.UnionId == dto.UnionId && o.Remark == dto.ClickTime.ToString() && o.ControlId == dto.ControlId);
                isInsert = model == null ? true : false;
            }
            if (model == null)
            {
                model = new PneumoniaBotOperationRecord
                {
                    Id             = Guid.NewGuid().ToString(),
                    UserId         = dto.UserId,
                    UnionId        = dto.UnionId,
                    OpenId         = dto.OpenId,
                    WxName         = dto.WxName,
                    WxPicture      = dto.WxPicture,
                    WxGender       = dto.WxGender,
                    WxCountry      = dto.WxCountry,
                    WxCity         = dto.WxCity,
                    WxProvince     = dto.WxProvince,
                    ModulesClicked = dto.ModulesClicked,
                    ControlId      = dto.ControlId,
                    ControlName    = dto.ControlName,
                    ClickTime      = DateTime.UtcNow.AddHours(8),
                    CreateTime     = DateTime.UtcNow.AddHours(8),
                };
            }

            if (dto.ClickTime > 0 && dto.LeaveTime > 0)
            {
                model.Remark    = dto.ClickTime.ToString();
                model.ClickTime = Tool.GetDateTimeMilliseconds(dto.ClickTime.GetValueOrDefault());
                model.LeaveTime = Tool.GetDateTimeMilliseconds(dto.LeaveTime.GetValueOrDefault());

                var time1 = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd hh:mm:ss}", model.ClickTime));
                var time2 = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd hh:mm:ss}", model.LeaveTime));

                TimeSpan clickTime = new TimeSpan(time1.Ticks);
                TimeSpan leaveTime = new TimeSpan(time2.Ticks);
                TimeSpan ts        = clickTime.Subtract(leaveTime).Duration();

                string dateDiff;
                if (ts.Days > 0)
                {
                    dateDiff = $"{ts.Days.ToString()}天{ts.Hours.ToString()}小时{ts.Minutes.ToString()}分钟{ts.Seconds.ToString()}秒";
                }
                else if (ts.Hours > 0)
                {
                    dateDiff = $"{ts.Hours.ToString()}小时{ts.Minutes.ToString()}分钟{ts.Seconds.ToString()}秒";
                }
                else if (ts.Minutes > 0)
                {
                    dateDiff = $"{ts.Minutes.ToString()}分钟{ts.Seconds.ToString()}秒";
                }
                else
                {
                    dateDiff = $"{ts.Seconds.ToString()}秒";
                }

                model.ResidenceTime = dateDiff;
                //  model.PlayTime = dateDiff;
            }

            if (string.IsNullOrEmpty(dto.UserId) && string.IsNullOrEmpty(dto.UnionId))
            {
                model.Type = 0;
            }
            else if (!string.IsNullOrEmpty(dto.UserId) && string.IsNullOrEmpty(dto.UnionId))
            {
                model.Type = 1;
            }
            else
            {
                model.Type = 2;
            }

            if (isInsert)
            {
                _rep.Insert(model);
            }
            else
            {
                _rep.Update(model);
            }
            _rep.SaveChanges();

            rvm.Msg     = "success";
            rvm.Success = true;
            rvm.Result  = "";
            return(rvm);
        }