def get_bill_range_ts(self, usrid): """ 用户账单时间戳区间 """ qs = self.filter(usrid=usrid, is_del=False).order_by('bill_at', 'pk') inst_first, inst_last = qs.first(), qs.last() ts_first, ts_last = None, None if isinstance(inst_first, BillDetail): ts_first = deal_time.time_floor_ts(inst_first.bill_at).timestamp() if isinstance(inst_last, BillDetail): ts_last = deal_time.time_floor_ts(inst_last.bill_at).timestamp() return ts_first, ts_last
def trigger_msg_add(self, contact, trigger, content, symbol, location=None, **kwargs): """ 触发消息添加 """ assert isinstance(contact, Contact) assert trigger in mc.TriggerType, f'trigger_error {contact.pk} {trigger} {symbol}' conv_info = contact.conv_info msg_body = dict(trigger=trigger, **kwargs) last_msg = conv_info.last_self_msg(contact.usrid) if isinstance(last_msg, Message) and last_msg.is_trigger and last_msg.is_conv_newest: created_at = deal_time.time_floor_ts(last_msg.created_at) if deal_time.get_now().diff(created_at).in_minutes() < 30: return last_msg # 30分钟内,重复触发消息,之前返回之前的消息 read_at = deal_time.get_now() # 触发消息,默认已读 conv_info.update_symbol(symbol) msg_type = mc.MSGType.Trigger msg = self._msg_create( convid=contact.convid, sender=contact.usrid, receiver=contact.touchid, symbol=symbol, msg_type=msg_type, msg_body=msg_body, content=content, location=location, read_at=read_at, ) return msg
def call_record_add(self, call): """ 通话账单添加 """ from server.applibs.outside.models import CallRecord assert isinstance(call, CallRecord), str(call) if not (call.call_ts > 0): return None bill_at = deal_time.time_floor_ts(call.callede_at) summary = f'与{call.msg_info.other_remark_name}通话' extra = dict(day_index=call.day_index) # is_free = call.day_index < 3 # 每天免单两次 is_free = True # 因小程序审核,计费功能2020-1105下线 inst, is_created = self.get_or_create(call_id=call.pk, defaults=dict( usrid=call.usrid, amount=call.fee, summary=summary, bill_at=bill_at, is_free=is_free, extra=extra, )) logger.info( f'call_record_add__done {inst.usrid} {inst.call_id} {inst.pk} {is_created}' ) inst.checkout() return inst
def checkout(self): """ 月度账单更新 """ if self.is_del: logger.warning(f'bill_detail__checkout_is_del {self.pk}') return month = deal_time.time_floor_ts(self.bill_at).replace(day=1).date() inst = BillMonth.objects.get_user_bill_month(month, self.usrid) self.month_id = inst.pk up_fields = ['month_id', 'updated_at'] self.save(update_fields=up_fields) inst.checkout()
def check_context(self): """ 消息创建后,更新坐标、及用户最新消息 """ # 消息列表是否显示时间:10分钟内未显示过时间 created = deal_time.time_floor_ts(self.created_at) is_exists_timed = self.conv_info.msg_qs.filter( convid=self.convid, pk__lt=self.pk, is_timed=True, created_at__gte=created.add(minutes=-10), ).exists() self.is_timed = not is_exists_timed self.save(update_fields=['is_timed', 'updated_at']) self.conv_info.check_msg() self.rtm_event_new_msg()
def query_call_ytx(self): """ 云讯,话单获取 """ if not (self.req_id and (self.provider == mc.ThirdProvider.YTX)): warn_msg = f'query_ytx__info_error {self.pk} {self.provider}' capture_message(warn_msg) logger.warning(warn_msg) return now = deal_time.get_now() created_at = deal_time.time_floor_ts(self.created_at) cut_seconds = (now - created_at).seconds if cut_seconds < 20: logger.warning(f'query_ytx__too_early {self.pk} {cut_seconds}') return # 查询太早没有结果 result = ytx_apis.YTXCallCdrByResIdOneApi( lastresid=self.req_id ).fetch_result() if not isinstance(result, dict): return # 无查询结果 self.call_result_ytx_up(result, action='query')
def sms_query(send_at, phone, bizid, out_id) -> dict: """ 短信结果 """ national = phonenumbers.parse(phone, None).national_number send_date = time_floor_ts(send_at).format('YYYYMMDD') is_dev_fake = bizid.startswith('dd-mock-') if is_dev_fake: mock_status = random.randint(mc.SMSStatus.Waiting, mc.SMSStatus.Success) error_code = 'DELIVERED' if mock_status == mc.SMSStatus.Success else f'mock-{mock_status}' result = { 'OutId': out_id, 'SendDate': get_tzcn_format(send_at), 'SendStatus': 3, 'ReceiveDate': get_tzcn_format(send_at), 'ErrCode': error_code, 'TemplateCode': 'dev-mock-TemplateCode', 'Content': 'dev-mock-Content', 'PhoneNum': 'dev-mock-PhoneNum', } return result request = CommonRequest() request.set_accept_format('json') request.set_domain('dysmsapi.aliyuncs.com') request.set_protocol_type('https') request.set_version('2017-05-25') request.set_method('POST') request.set_action_name('QuerySendDetails') request.add_query_param('RegionId', 'cn-hangzhou') request.add_query_param('CurrentPage', 1) # 当前页码 request.add_query_param('PageSize', 10) # 每页显示 1~50 request.add_query_param('PhoneNumber', national) # 手机号码 request.add_query_param('SendDate', send_date) # 发送日期 request.add_query_param('BizId', bizid) # 发送回执ID resp_dic = AliReqAction(request).do_req_action() results = resp_dic['SmsSendDetailDTOs']['SmsSendDetailDTO'] result = {row['OutId']: row for row in results}[out_id] return result
def updated_at_min(self): """ 更新时间去毫秒 """ if not self.updated_at: return None min_updated_at = deal_time.time_floor_ts(self.updated_at) return min_updated_at
def created_at_min(self): """ 创建时间去毫秒 """ if not self.created_at: return None min_created_at = deal_time.time_floor_ts(self.created_at) return min_created_at
def expire_at(self): """ 过期时间 """ if not self.session: return None at = time_floor_ts(self.session.expire_date) return at
def bound_date(self): if not self.bound_at: return None dt = time_floor_ts(self.bound_at).date() return dt
def last_login_at(self): """ 最后登录时间去毫秒 """ if not self.last_login: return None at = time_floor_ts(self.last_login) return at
def joined_at(self): """ 注册时间去毫秒 """ at = time_floor_ts(self.date_joined) return at