123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- import datetime
- import logging
- from apilib.monetary import RMB, VirtualCoin
- from apilib.utils_sys import memcache_lock
- from apps.web.constant import DEALER_CONSUMPTION_AGG_KIND
- from apps.web.device.models import Group, Device
- from apps.web.eventer import EventBuilder
- from apps.web.eventer.base import WorkEvent
- from apps.web.helpers import device_lock_key
- from apps.web.user.models import ServiceProgress, MyUser, ConsumeRecord
- logger = logging.getLogger(__name__)
- class builder(EventBuilder):
- def __getEvent__(self, device_event):
- event_data = self.deviceAdapter.analyze_event_data(device_event['data'])
- if event_data is None or 'cmdCode' not in event_data:
- return None
- if event_data['cmdCode'] in ['30', '36', '34', '79']:
- return DianchuanCarChargingWorkEvent(self.deviceAdapter, event_data)
- return None
- class DianchuanCarChargingWorkEvent(WorkEvent):
- def do(self, **args):
- devNo = self.device['devNo']
- logger.info('dian chuan car charging event detected, devNo=%s,curInfo=%s' % (devNo, self.event_data))
- if self.event_data['cmdCode'] == '79':
- self.do_login()
- if self.event_data['cmdCode'] == '30':
- self.do_check_prot()
- elif self.event_data['cmdCode'] == '36':
- self.do_finish()
- def do_login(self):
- """
- 处理登录请求
- """
- self.device.deviceAdapter._qr_code_devNo_issue()
- def do_finish(self):
- sequanceNo = self.event_data['seqNo']
- self.device.deviceAdapter._response_finished(sequanceNo)
- with memcache_lock(key=device_lock_key(sequanceNo), value=sequanceNo, expire=360) as acquired:
- if not acquired:
- return
- portStr = str(self.event_data["port"])
- order = ConsumeRecord.objects.filter(devNo=self.device.devNo, sequanceNo=sequanceNo).first() # type: ConsumeRecord
- if not order:
- logger.warning('order<no={}> is not exists.'.format(sequanceNo))
- self.device.deviceAdapter._response_finished(sequanceNo)
- consumeDict = {
- 'chargeIndex': self.event_data['port'],
- 'total_spend_elec': self.event_data['total_spend_elec'],
- 'total_spend_money': self.event_data['total_spend_money'],
- 'duration': self.event_data['eDuration'],
- }
- user = MyUser.objects.get(
- openId=order.openId, groupId=order.groupId)
- ctrInfo = Device.get_dev_control_cache(self.device.devNo)
- lineInfo = ctrInfo.get(portStr, {})
- payCoins = VirtualCoin(lineInfo["coins"])
- spendCoins = VirtualCoin(self.event_data["total_spend_money"])
- refundCoins = payCoins - spendCoins
- refundedMoney = RMB(0)
- group = Group.get_group(self.device['groupId'])
- serviceChargeOn = self.device.bill_as_service_feature.on
- if serviceChargeOn:
- serviceCharge = self.device.bill_as_service_feature.service_charge
- totalElec = float(self.event_data['total_spend_elec'])
- totalServiceFee = VirtualCoin(totalElec * float(serviceCharge))
- refundCoins = payCoins - spendCoins - totalServiceFee
- logger.info(
- "[Dian Chuan Car Charge device <{}> refund money is <{}>]".format(self.device.devNo, refundedMoney))
- try:
- is_cash = False
- consumeDict.update({DEALER_CONSUMPTION_AGG_KIND.COIN: VirtualCoin(payCoins).mongo_amount})
- if refundedMoney > RMB(0) or refundCoins > VirtualCoin(0):
- consumeDict.update(
- {DEALER_CONSUMPTION_AGG_KIND.COIN: (VirtualCoin(payCoins) - refundCoins).mongo_amount})
- self.refund_net_pay(user, lineInfo, refundedMoney, refundCoins, consumeDict, is_cash)
- ServiceProgress.update_progress_and_consume_rcd(
- self.device.ownerId,
- {
- 'open_id': lineInfo['openId'],
- 'port': int(portStr),
- 'device_imei': self.device.devNo,
- 'isFinished': False
- },
- consumeDict)
- finally:
- extra = []
- if serviceChargeOn:
- extra.append({u'电费金额': '{}(元)'.format(spendCoins)})
- extra.append({u'服务费金额': '{}(元)'.format(totalServiceFee)})
- extra.append({u'消费电量': '{}(度)'.format(totalElec)})
- if DEALER_CONSUMPTION_AGG_KIND.REFUNDED_CASH in consumeDict:
- real_refund = RMB(consumeDict[DEALER_CONSUMPTION_AGG_KIND.REFUNDED_CASH])
- if real_refund > RMB(0):
- extra.append({u'消费金额': '{}(元)'.format(RMB(0) - real_refund)})
- extra.append({u'退款金额': '{}(元)'.format(real_refund)})
- else:
- extra.append({u'消费金额': '{}(元)'.format(0)})
- elif DEALER_CONSUMPTION_AGG_KIND.REFUNDED_COINS in consumeDict:
- real_refund = VirtualCoin(consumeDict[DEALER_CONSUMPTION_AGG_KIND.REFUNDED_COINS])
- if real_refund > VirtualCoin(0):
- extra.append({u'消费金额': '{}(金币)'.format(VirtualCoin(payCoins) - real_refund)})
- extra.append({u'退款金额': '{}(金币)'.format(real_refund)})
- else:
- extra.append({u'消费金额': '{}(金币)'.format(VirtualCoin(payCoins))})
- else:
- extra.append({u'消费金额': '{}(金币)'.format(VirtualCoin(payCoins))})
- self.notify_user_service_complete(
- service_name='充电',
- openid=order.user.managerialOpenId,
- port=portStr,
- address=group['address'],
- reason=self.event_data['reason'],
- finished_time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
- extra=extra)
- ServiceProgress.update_progress_and_consume_rcd(
- self.device['ownerId'],
- {
- 'open_id': order.openId,
- 'device_imei': self.device['devNo'],
- 'port': self.event_data['port'],
- 'isFinished': False
- },
- consumeDict
- )
- Device.clear_port_control_cache(self.device.devNo, portStr)
- def do_check_port(self):
- data = self.event_data
- print (data)
|