def get_device_number(): dbg('get_device_number') reply, status_code = {'code': 0, 'msg': ''}, 200 agent_id = current_user.agent_id agent = current_user.cur_agent if agent.salesman: numbers, devices = dbapi.get_device(agent_id=agent_id, salesman=1, count=True) else: numbers, devices = dbapi.get_device(agent_id=agent_id, count=True) imeis = [] if devices: imeis = list(map(lambda x: x.imei, devices)) onlines = dbapi.get_cws_device_status_numbers(imeis, 1) offlines = numbers - onlines unknown = 0 # offlines = dbapi.get_cws_device_status_numbers(imeis, 0) # unknown = dbapi.get_cws_device_status_numbers(imeis, 4) reply['data'] = { 'numbers': numbers, 'onlines': onlines, 'offlines': offlines, 'unknown': unknown } return make_response(jsonify(reply), status_code)
def god_get_device(): dbg('god_get_device') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data.get('imei') device_id = data.get('device_id') dbg(data) except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) god = dbapi.get_god(openluat_user_id=current_user.id) if not god: raise ApiError('ERROR_GOD_NOT_FOUND', error.ERROR_GOD_NOT_FOUND) role_bit = int(current_user.role[5]) if role_bit not in (1, 2): raise ApiError('ERROR_AGENT_NO_PERMISSION', error.ERROR_AGENT_NO_PERMISSION) device = None if imei: device = dbapi.get_device(imei=imei) elif device_id: device = dbapi.get_device(id=int(device_id)) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) if role_bit == 1: god_agents = dbapi.get_god_agent(god_id=god.id) g = (god_agent.agent_id for god_agent in god_agents) if device.agent_id not in g: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) device_address = dbapi.get_device_address(id=device.address_id) address = "" if device_address: address = device_address.address agentname = '' agent = dbapi.get_agent(id=device.owner_agent_id) if agent: agentname = agent.name reply['data'] = { 'id': device.id, 'imei': device.imei, 'address': address, 'operator': agentname, 'remark': device.remark } return make_response(jsonify(reply), status_code)
def get_wallet_balance(): dbg('get_wallet_balance') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) user_id = g.oauth_user.user_id wallet = dbapi.get_wallet(role=0, user_id=user_id, agent_id=device.owner_agent_id) agent_setting = dbapi.get_agent_setting(agent_id=device.agent_id) wallet_pay_enable = 1 if agent_setting and agent_setting.wallet_pay_enable == 0: wallet_pay_enable = 0 reply['data'] = { 'balance': wallet.balance, 'wallet_pay_enable': wallet_pay_enable } return make_response(jsonify(reply), status_code)
def get_wallet_products(): dbg('get_wallet_products') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('get products error', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) agent_id = device.owner_agent_id products = dbapi.get_product(agent_id=agent_id, cat=99) data = [] if products: for product in products: dic = { 'id': product.id, 'title': product.title, 'body': product.body, 'value': product.value, 'price': product.price, } data.append(dic) reply['data'] = data return make_response(jsonify(reply), status_code)
def get_products(): dbg('get_products') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('get products error', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) device_products = dbapi.get_device_product(device_id=device.id) data = [] if device_products: for device_product in device_products: product = dbapi.get_product(product_id=device_product.product_id) if not product: continue dic = { 'id': product.id, 'title': product.title, 'body': product.body, 'value': product.value, 'price': product.price, 'unit': device.product_unit, 'number': str(Fraction(product.value, device.product_unit_pluse)), } data.append(dic) reply['data'] = data return make_response(jsonify(reply), status_code)
def get_ads(): dbg('get_ads') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) ads = dbapi.get_ad(agent_id=device.owner_agent_id, using=1) data = [] for ad in ads: info = { 'id': ad.id, 'name': ad.name, 'desc': ad.desc, 'img': url_for('static', filename=ad.img), 'url': ad.url } data.append(info) reply['data'] = data return make_response(jsonify(reply), status_code)
def get_nopay_count(): dbg('get_nopay_count') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) if not device.nopay: raise ApiError('ERROR_DEVICE_NOT_SUP_NOPAY', error.ERROR_DEVICE_NOT_SUP_NOPAY) user_id = g.oauth_user.user_id user_start_counter = dbapi.get_user_start_counter(user_id=user_id, imei=imei) if not user_start_counter: user_start_counter = dbapi.make_new_user_start_counter(user_id, imei) db.session.commit() user_start_counters = dbapi.get_user_start_counter( user_id=user_id, address_id=device.address_id) count = 0 for user_start_counter in user_start_counters: count += user_start_counter.count reply['data'] = {'count': count, 'nopay': device.nopay} return make_response(jsonify(reply), status_code)
def god_recycle_device(): dbg('god_recycle_device') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('update devices error', error.ERROR_PARAM) god = dbapi.get_god(openluat_user_id=current_user.id) if not god: raise ApiError('ERROR_GOD_NOT_FOUND', error.ERROR_GOD_NOT_FOUND) role_bit = int(current_user.role[5]) if role_bit not in (1, 2): raise ApiError('ERROR_AGENT_NO_PERMISSION', error.ERROR_AGENT_NO_PERMISSION) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) if role_bit == 1: god_agents = dbapi.get_god_agent(god_id=god.id) g = (god_agent.agent_id for god_agent in god_agents) if device.agent_id not in g: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) update = {} update['agent_id'] = 0 update['address_id'] = 0 update['salesman_agent_id'] = 0 update['l4'] = 1 update['l3'] = 0 update['l2'] = 0 update['l1'] = 0 device = dbapi.update_device(device, **update) # device_distribution dbapi.delete_device_distribution_total(imei) # device_distribution_salesman dbapi.delete_device_distribution_salesman(imei) # device_product dbapi.delete_device_product(device.id) # pay dbapi.delete_pays_and_records(imei) try: db.session.commit() except: db.session.rollback() raise return make_response(jsonify(reply), status_code)
def send_trans_data_to_server(self, imei, msg): try: cur_count = self.request.retries dbg(cur_count) time_list = (5, 15, 30, 60, 120) device = dbapi.get_device(imei=imei) if device: agent_setting = dbapi.get_agent_setting(agent_id=device.agent_id) if agent_setting and agent_setting.trans_url: r = requests.post(agent_setting.trans_url, json={ 'imei': imei, 'message': msg }) if r.status_code == requests.codes.ok: return 'ok' else: dbg(r.text) raise Exception('wrong data') else: dbg('no agent_setting or trans_url for agent_id: %d' % device.agent_id) raise Exception('url error') else: dbg('no device: %s' % imei) raise Exception('no device') except: print_exception_info() if cur_count < len(time_list): countdown = time_list[cur_count] raise self.retry(countdown=countdown, max_retries=5) else: return 'error'
def get_nopay_salesman_devices(): dbg('get_nopay_salesman_devices') reply, status_code = {'code': 0, 'msg': ''}, 200 page = request.args.get('page', 1, type=int) psize = request.args.get('psize', 10, type=int) agent_id = current_user.agent_id agent = dbapi.get_agent(id=agent_id) if not agent: raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND) count = 0 data = [] if agent.salesman == 0: count, devices = dbapi.get_device(agent_id=agent_id, salesman=0, page=page, psize=psize, nopay=1) elif agent.salesman == 1: count, devices = dbapi.get_device(agent_id=agent_id, salesman=1, page=page, psize=psize, nopay=1) if count: if devices: for device in devices: device_address = dbapi.get_device_address(id=device.address_id) address = "" if device_address: address = device_address.address imei = device.imei nopay_count = dbapi.get_record_no_pay(imei=imei, count=True) info = { 'id': device.id, 'imei': device.imei, 'cat': device.cat, 'address': address, 'remark': device.remark, 'nopay_count': nopay_count } data.append(info) reply['data'] = {'count': count, 'devices': data} return make_response(jsonify(reply), status_code)
def add_device(): dbg('add_device') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] cat = data['cat'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) if len(imei) != 15: raise ApiError('ERROR_INVALIDATE_IMEI', error.ERROR_INVALIDATE_IMEI) agent_id = current_user.agent_id agent = dbapi.get_agent(id=agent_id) if not agent or agent.level != 4: raise ApiError('ERROR_AGENT_NO_PERMISSION', error.ERROR_AGENT_NO_PERMISSION) device = dbapi.get_device(imei=imei) if device: if device.agent_id != 0: raise ApiError('ERROR_DEVICE_EXISTS', error.ERROR_DEVICE_EXISTS) else: # update device device.agent_id = agent_id device.owner_agent_id = agent_id device.cat = cat else: address_id = 0 device = dbapi.make_new_device(imei, cat, agent_id, address_id) try: db.session.commit() except: db.session.rollback() raise if not device: raise Exception('add device db error') data = {} if device: # 添加第一次设备分销 from_agent, to_agent = 0, agent_id dbapi.make_new_device_distribution(device.id, device.imei, from_agent, to_agent) data = {'id': device.id, 'imei': device.imei, 'cat': device.cat} try: db.session.commit() except: db.session.rollback() raise reply['data'] = data return make_response(jsonify(reply), status_code)
def get_products(): dbg('get_products') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() # cat 和 device_id 二选一 cat = data.get('cat') device_id = data.get('device_id') except: print_exception_info() raise ApiError('get products error', error.ERROR_PARAM) data = [] if cat is not None: agent_id = current_user.agent_id products = dbapi.get_product(agent_id=agent_id, cat=cat) if products: for product in products: info = { 'id': product.id, 'cat': product.cat, 'title': product.title, 'body': product.body, 'value': product.value, 'price': product.price, 'inventory': product.inventory } data.append(info) if device_id is not None: device = dbapi.get_device(device_id=device_id) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) device_products = dbapi.get_device_product(device_id=device_id) if device_products: for device_product in device_products: product = dbapi.get_product( product_id=device_product.product_id) info = { 'id': product.id, 'cat': product.cat, 'title': product.title, 'body': product.body, 'value': product.value, 'price': product.price, 'inventory': product.inventory, 'unit': device.product_unit } data.append(info) reply['data'] = data return make_response(jsonify(reply), status_code)
def mahcine_stop(): dbg('machine_stop') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('get machine record error', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) record = dbapi.get_record(imei=imei, recent=1) if not record: raise ApiError('ERROR_RECORD_NOT_FOUND', error.ERROR_RECORD_NOT_FOUND) succ, result = launch_relay_signal_deivce_v2(imei, 0, device.cat) if not succ: try: data = json.loads(result) dbg((data['code'], data['msg'])) reply['code'] = data['code'] reply['msg'] = data['msg'] status_code = 422 except: print_exception_info() reply['code'] = 99 reply['msg'] = 'server error' status_code = 500 return make_response(jsonify(reply), status_code) try: record = dbapi.update_record(record, status=1) db.session.commit() except: db.session.rollback() raise now = int(time.time()) etime = int(record.etime.timestamp()) if record.status == 1: etime = now reply['data'] = { 'id': record.id, 'stime': int(record.stime.timestamp()), 'etime': etime, 'now': now, 'user_id': record.user_id, } return make_response(jsonify(reply), status_code)
def device_multi_distribution(): dbg('device_multi_distribution') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imeis = data['imeis'] to_agent = data['to_agent'] except: print_exception_info() raise ApiError('update devices error', error.ERROR_PARAM) agent = dbapi.get_agent(id=to_agent) if not agent or agent.level != 4: raise ApiError('can only give level 4') for imei in imeis: device = dbapi.get_device(imei=imei) if not device: db.session.rollback() dbg('imei: %s not exists' % imei) reply['code'] = 999 reply['msg'] = 'imei: %s 不存在' % imei status_code = 422 return make_response(jsonify(reply), status_code) elif device.agent_id != 0: db.session.rollback() dbg('imei: %s already first_distribution' % imei) reply['code'] = 999 reply['msg'] = 'imei: %s 已经被首次分配' % imei status_code = 422 return make_response(jsonify(reply), status_code) # 1. 插入设备分组 from_agent = device.agent_id device_distribution = dbapi.make_new_device_distribution( device.id, device.imei, from_agent, to_agent) # 2. 更细设备 device.agent_id = to_agent device.owner_agent_id = to_agent device.utime = datetime.now() db.session.add(device) try: db.session.commit() except: db.session.rollback() raise return make_response(jsonify(reply), status_code)
def device_multi_add_coupon(): dbg('device_multi_add_coupon') reply ,status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() device_ids = data['device_ids'] device_ids = list(set(device_ids)) coupon_id = data['coupon_id'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) coupon = dbapi.get_coupon(id=coupon_id) if not coupon: raise ApiError('ERROR_COUPON_NOT_FOUND', error.ERROR_COUPON_NOT_FOUND) for device_id in device_ids: device = dbapi.get_device(id=device_id) if not device: db.session.rollback() dbg('device_id: %s not exists' % device_id) reply['code'] = 999 reply['msg'] = '自编号: %s 不存在' % device_id status_code = 422 return make_response(jsonify(reply), status_code) device_coupon = dbapi.get_device_coupon(device_id=device_id, coupon_id=coupon_id) if device_coupon: db.session.rollback() dbg('device_id: %d added before' % device_id) reply['code'] = 999 reply['msg'] = '自编号: %s 已添加该优惠券' % device_id status_code = 422 return make_response(jsonify(reply), status_code) else: # 建立设备和优惠券之间的联系 device_coupon = dbapi.make_new_device_coupon(coupon_id, device_id) # 更新pay表中的imei,用户查询订单收益 pay = dbapi.get_pay(pay_id=coupon.pay_id) pay = dbapi.update_pay(pay, imei=device.imei) try: db.session.commit() except: db.session.rollback() raise return make_response(jsonify(reply), status_code)
def make_pay(): dbg('make_pay') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() product_id = data['product_id'] trade_type = data['trade_type'] pay_mode = data['pay_mode'] imei = data['imei'] if pay_mode == 2: cur_url = data['url'] except: print_exception_info() raise ApiError('pay error', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) agent_id = device.owner_agent_id user_id = g.oauth_user.user_id max_id = dbapi.get_max_pay_id() str_max_id = '%04d' % max_id if len(str_max_id) > 4: str_max_id = str_max_id[-4:] trade_no = str(pay_mode) + datetime.now().strftime( "%Y%m%d%H%M%S") + str_max_id product = dbapi.get_product(product_id=product_id) if pay_mode == 1: # 微信支付 data = wechat_make_new_pay(product, trade_no, agent_id, user_id, imei, trade_type) elif pay_mode == 2: # 支付宝支付 data = ali_make_new_pay(product, trade_no, agent_id, user_id, imei, cur_url) elif pay_mode == 3: if product.cat == 99: raise ApiError('not support') # 钱包支付 data = wallet_make_new_pay(product, trade_no, agent_id, user_id, imei) else: raise ApiError('ERROR_PAY', error.ERROR_PAY) reply['data'] = data return make_response(jsonify(reply), status_code)
def recycle_device(): dbg('recycle_device') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('update devices error', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) update = {} update['agent_id'] = device.agent_id update['address_id'] = 0 update['salesman_agent_id'] = 0 update['l4'] = 1 update['l3'] = 0 update['l2'] = 0 update['l1'] = 0 device = dbapi.update_device(device, **update) # device_distribution dbapi.delete_device_distribution(imei) # device_distribution_salesman dbapi.delete_device_distribution_salesman(imei) # device_product dbapi.delete_device_product(device.id) # pay dbapi.delete_pays_and_records(imei) try: db.session.commit() except: db.session.rollback() raise return make_response(jsonify(reply), status_code)
def update_device_rate(): dbg('update_device_rate') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] rate = float(data['rate']) except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) if rate < 0 or rate > 1: raise ApiError('rate not support') dd = dbapi.get_device_distribution(imei=imei, from_agent=current_user.agent_id) if not dd: raise ApiError('ERROR_NOT_DISTRIBUTION', error.ERROR_NOT_DISTRIBUTION) device = dbapi.get_device(id=dd.device_id) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUNDE', error.ERROR_DEVICE_NOT_FOUND) # 更新device_distribution dd = dbapi.update_device_distribution(dd, rate=rate) db.session.commit() # 计算l4,l3,l2,l1 level = current_user.cur_agent.level agent_levels = [] rates_all = [device.l1, device.l2, device.l3, device.l4] dds = dbapi.get_device_distribution(device_id=device.id) for dd in dds: agent = dbapi.get_agent(id=dd.to_agent) agent_levels.append(agent.level) update = calc_device_profit(level, rate, agent_levels, rates_all) # 更新device dbg(update) device = dbapi.update_device(device, **update) db.session.commit() return make_response(jsonify(reply), status_code)
def manual_refund(): dbg('manual_refund') reply ,status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() pay_id = data['pay_id'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) pay = dbapi.get_pay(pay_id=pay_id) if not pay: raise ApiError('ERROR_PAY_NOT_FOUND', error.ERROR_PAY_NOT_FOUND) # 充值套餐、广告优惠券,不支持退款 not_support_list = (99, 199) if pay.cat in not_support_list: raise ApiError('not support this', error.ERROR_AGENT_NO_PERMISSION) imei = pay.imei device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) if device.owner_agent_id != current_user.agent_id: raise ApiError('ERROR_AGENT_NO_PERMISSION', error.ERROR_AGENT_NO_PERMISSION) refund_available = check_refund_available(device, pay.total_fee) if not refund_available: raise ApiError('ERROR_REFUND_NOT_AVAILABLE', error.ERROR_REFUND_NOT_AVAILABLE) if pay.status != 1: raise ApiError('ERROR_NOT_PAY', error.ERROR_NOT_PAY) product = dbapi.get_product(product_id=pay.product_id) if not product: raise ApiError('can not find product: %s' % pay.product_id) # 退款 pay_refund(pay, product) db.session.commit() return make_response(jsonify(reply), status_code)
def refund_income(total_fee, imei): dbg('refund_income') device = dbapi.get_device(imei=imei) if not device: dbg('no device: %s' % imei) return dds = dbapi.get_device_distribution(device_id=device.id) rates = (device.l4, device.l3, device.l2, device.l1) for i, dd in enumerate(dds): if i == len(dds) - 1 and device.salesman_agent_id != 0: # 包含业务员 salesman_rates = (device.sl1, device.sl2, device.sl3) all_fee = total_fee * rates[i] fee_sum = 0 ddss = dbapi.get_device_distribution_salesman(device_id=device.id) for iterm in ddss: # 级业务员提成 cur_agent = dbapi.get_agent(id=iterm.to_agent) if cur_agent: salseman_remark = '业务员退款扣除提成slevel: %d' % cur_agent.slevel fee = all_fee * salesman_rates[cur_agent.slevel - 1] receipt = -fee make_refund_ticheng_wallet_receipt(cur_agent, receipt, salseman_remark) fee_sum += fee # 退款扣除各所属代理商 agent = dbapi.get_agent(id=dd.to_agent) if not agent: dbg('no agent: %d' % dd.to_agent) continue remark = '退款扣除提成' receipt = -(all_fee - fee_sum) make_refund_ticheng_wallet_receipt(agent, receipt, remark) else: # 不包含业务员 agent = dbapi.get_agent(id=dd.to_agent) if not agent: dbg('no agent: %d' % dd.to_agent) continue receipt = -total_fee * rates[4 - agent.level] remark = '退款扣除提成' make_refund_ticheng_wallet_receipt(agent, receipt, remark)
def random_get_coupon_receipt(): dbg('random_get_coupon_receipt') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('get machine record error', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) coupons = dbapi.get_coupon(device_id=device.id, paid=1, inventory=1) if coupons: coupon = random.choice(coupons) coupon_id = coupon.id code = gen_coupon_receipt_code(coupon_id, coupon.prefix) user_id = g.oauth_user.user_id coupon_receipt = dbapi.make_new_coupon_receipt(coupon_id, user_id, code) product = dbapi.get_product(product_id=coupon.product_id) if product.inventory <= 0: raise ApiError('ERROR_COUPON_NO_AVAILABLE', error.ERROR_COUPON_NO_AVAILABLE) product = dbapi.update_product(product, inventory=product.inventory - 1) color = get_boxed_reverse_color(coupon.img) reply['data'] = { 'img': url_for('static', filename=coupon.img), 'code': code, 'color': color } else: raise ApiError('ERROR_COUPON_NO_AVAILABLE', error.ERROR_COUPON_NO_AVAILABLE) db.session.commit() return make_response(jsonify(reply), status_code)
def get_coupon_devices(): dbg('get_coupon_devices') reply ,status_code = {'code': 0, 'msg': ''}, 200 page = request.args.get('page', 1, type=int) psize = request.args.get('psize', 10, type=int) address_id = request.args.get('address_id', 0, type=int) agent_id = current_user.agent_id count = 0 data = [] count, devices = dbapi.get_device(owner_agent_id=agent_id, page=page, psize=psize, coupon=1, address_id=address_id) if count: if devices: for device in devices: device_address = dbapi.get_device_address(id=device.address_id) address = "" if device_address: address = device_address.address device_coupons = dbapi.get_device_coupon(device_id=device.id) coupons = list(map(lambda x:x.coupon_id, device_coupons)) info = { 'id': device.id, 'imei': device.imei, 'cat': device.cat, 'address': address, 'remark': device.remark, 'coupon': device.coupon, 'coupons': coupons } data.append(info) reply['data'] = { 'count': count, 'devices': data } return make_response(jsonify(reply), status_code)
def get_nopay_devices(): dbg('get_nopay_devices') reply, status_code = {'code': 0, 'msg': ''}, 200 page = request.args.get('page', 1, type=int) psize = request.args.get('psize', 10, type=int) agent_id = current_user.agent_id count = 0 data = [] count, devices = dbapi.get_device(agent_id=agent_id, page=page, psize=psize, nopay=1) if count: if devices: for device in devices: device_address = dbapi.get_device_address(id=device.address_id) address = "" if device_address: address = device_address.address imei = device.imei nopay_count = dbapi.get_record_no_pay(imei=imei, count=True) info = { 'id': device.id, 'imei': device.imei, 'cat': device.cat, 'address': address, 'remark': device.remark, 'nopay_count': nopay_count } data.append(info) reply['data'] = {'count': count, 'devices': data} return make_response(jsonify(reply), status_code)
def machine_start_coupon_nopay(): dbg('machine_start_coupon_nopay') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() cat = data['cat'] imei = data['imei'] value = data['value'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) if not device.coupon: raise ApiError('ERROR_DEVICE_NOT_SUP_NOPAY', error.ERROR_DEVICE_NOT_SUP_NOPAY) user_id = g.oauth_user.user_id receipt = None coupon_receipts = dbapi.get_coupon_receipt(device_id=device.id, user_id=user_id) for coupon_receipt in coupon_receipts: if coupon_receipt.started == 0: receipt = coupon_receipt if not receipt: raise ApiError('ERROR_NOPAY_NOT_ENOUGH', error.ERROR_NOPAY_NOT_ENOUGH) agent_id = device.agent_id if cat in (0, 2, 3): device_type = device.cat succ, result = launch_relay_signal_deivce_v2(imei, value, device_type) if not succ: try: data = json.loads(result) dbg((data['code'], data['msg'])) reply['code'] = data['code'] reply['msg'] = data['msg'] status_code = 422 except: print_exception_info() reply['code'] = 99 reply['msg'] = 'server error' status_code = 500 return make_response(jsonify(reply), status_code) stime = datetime.now() etime = stime + timedelta(seconds=value) # 1. 记录开始记录 record = dbapi.make_new_record_coupon_no_pay(imei, user_id, agent_id, stime, etime) db.session.commit() reply['data'] = { 'id': record.id, 'stime': int(record.stime.timestamp()), 'etime': int(record.etime.timestamp()) } elif cat == 1: # 娃娃机 value = 1 succ, result = launch_pulse_signal_deivce(imei, value, device.high, device.low) if not succ: try: data = json.loads(result) dbg((data['code'], data['msg'])) reply['code'] = data['code'] reply['msg'] = data['msg'] status_code = 422 except: print_exception_info() reply['code'] = 99 reply['msg'] = 'server error' status_code = 500 return make_response(jsonify(reply), status_code) stime = datetime.now() etime = stime # 1. 记录开始记录 record = dbapi.make_new_record_coupon_no_pay(imei, user_id, agent_id, stime, etime) db.session.commit() # 记录优惠券已启动 receipt = dbapi.update_coupon_receipt(receipt, started=1) db.session.commit() return make_response(jsonify(reply), status_code)
def machine_start_nopay(): dbg('machine_start_nopay') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() cat = data['cat'] imei = data['imei'] value = data['value'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) if not device.nopay: raise ApiError('ERROR_DEVICE_NOT_SUP_NOPAY', error.ERROR_DEVICE_NOT_SUP_NOPAY) user_id = g.oauth_user.user_id user_start_counter = dbapi.get_user_start_counter(user_id=user_id, imei=imei) if not user_start_counter: user_start_counter = dbapi.make_new_user_start_counter(user_id, imei) db.session.commit() user_start_counters = dbapi.get_user_start_counter( user_id=user_id, address_id=device.address_id) count = 0 for counter in user_start_counters: count += counter.count if count >= device.nopay: raise ApiError('ERROR_NOPAY_NOT_ENOUGH', error.ERROR_NOPAY_NOT_ENOUGH) agent_id = device.agent_id wechat_config = dbapi.get_wechat_config(imei=imei) appid = wechat_config.appid appsecret = wechat_config.appsecret openid = session['openid'] wechat_info = wechatsdk.get_wechat_user_info(appid, appsecret, openid) if not wechat_info or wechat_info['subscribe'] != 1: raise ApiError('ERROR_WECHAT_NOT_SUBSCRIBE', error.ERROR_WECHAT_NOT_SUBSCRIBE) if cat in (0, 2, 3): device_type = device.cat succ, result = launch_relay_signal_deivce_v2(imei, value, device_type) if not succ: try: data = json.loads(result) dbg((data['code'], data['msg'])) reply['code'] = data['code'] reply['msg'] = data['msg'] status_code = 422 except: print_exception_info() reply['code'] = 99 reply['msg'] = 'server error' status_code = 500 return make_response(jsonify(reply), status_code) stime = datetime.now() etime = stime + timedelta(seconds=value) # 1. 记录开始记录 record = dbapi.make_new_record_no_pay(imei, user_id, agent_id, stime, etime) db.session.commit() reply['data'] = { 'id': record.id, 'stime': int(record.stime.timestamp()), 'etime': int(record.etime.timestamp()) } elif cat == 1: # 娃娃机 value = 1 succ, result = launch_pulse_signal_deivce(imei, value, device.high, device.low) if not succ: try: data = json.loads(result) dbg((data['code'], data['msg'])) reply['code'] = data['code'] reply['msg'] = data['msg'] status_code = 422 except: print_exception_info() reply['code'] = 99 reply['msg'] = 'server error' status_code = 500 return make_response(jsonify(reply), status_code) stime = datetime.now() etime = stime # 1. 记录开始记录 record = dbapi.make_new_record_no_pay(imei, user_id, agent_id, stime, etime) db.session.commit() # 记录启动次数 count = user_start_counter.count + 1 user_start_counter = dbapi.update_user_counter(user_start_counter, count=count) db.session.commit() return make_response(jsonify(reply), status_code)
def get_god_devices(): dbg('get_god_devices') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() god_id = data['god_id'] page = request.args.get('page', 1, type=int) psize = request.args.get('psize', 10, type=int) except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) role_bit = int(current_user.role[5]) if role_bit != 2: raise ApiError('ERROR_AGENT_NO_PERMISSION', error.ERROR_AGENT_NO_PERMISSION) count = 0 data = [] ct1, agents = dbapi.get_god_agent(god_id=god_id, page=1, psize=999) for agent in agents: agent_id = agent.id ct2, devices = dbapi.get_device(agent_id=agent_id, page=page, psize=psize) if ct2: count += ct2 for device in devices: device_address = dbapi.get_device_address(id=device.address_id) address = "" if device_address: address = device_address.address operator = '' operator_level = 0 agent = dbapi.get_agent(id=device.owner_agent_id) if agent: operator = agent.name operator_level = agent.level imei = device.imei status = dbapi.get_cws_device_status(imei) online = -1 if status: online = status.get('online', -1) latest_report = dbapi.get_device_latestreport(imei) if not latest_report: online = 0 else: if imei == '868575021151255': dbg(latest_report['time']) nowts = datetime.now().timestamp() lrts = latest_report['time'].timestamp() if nowts - lrts > 150: if online == 1: online = 0 info = { 'id': device.id, 'imei': device.imei, 'cat': device.cat, 'address': address, 'use_state': 0, # 0空闲,1使用 'comm_state': online, # 0关机,1在线 'operator': operator, 'operator_level': operator_level, 'owner_agent_id': device.owner_agent_id, 'map_display': device.map_display, 'remark': device.remark, 'l4': device.l4, 'l3': device.l3, 'l2': device.l2, 'l1': device.l1 } data.append(info) reply['data'] = {'count': count, 'devices': data} return make_response(jsonify(reply), status_code)
def machine_start(): dbg('machine_start') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() cat = data['cat'] pay_id = data['pay_id'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) pay = dbapi.get_pay(pay_id=pay_id) if not pay: raise ApiError('machine start error', error.ERROR_PAY_NOT_FOUND) imei = pay.imei if pay.status != 1: raise ApiError('machine start error', error.ERROR_NOT_PAY) product = dbapi.get_product(product_id=pay.product_id) if not product: raise ApiError('can not find product: %s' % pay.product_id) if product.cat == 99: raise ApiError('ERROR_PAY_NOT_FOUND', error.ERROR_PAY_NOT_FOUND) if product.cat == 0: # 倒计时限制同时支付 last_pay = dbapi.get_last_pay(pay.imei, not_include_pay_id=pay_id) if last_pay and datetime.now().timestamp() - last_pay.utime.timestamp( ) < 5: # 退款 pay_refund(pay, product) raise ApiError('ERROR_PAY_TOO_FREQUENCE', error.ERROR_PAY_TOO_FREQUENCE) dbg('cat: %s' % cat) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) value = product.value record = dbapi.get_record(pay_id=pay.id) if record: raise ApiError('machine start error', error.ERROR_ALREADY_START) high = device.high low = device.low cat = product.cat dbg('imei: %s, value: %d, cat: %d, high: %d, low: %d' % (imei, value, cat, low, high)) cat = product.cat if cat in (0, 2, 3): device_type = device.cat succ, result = launch_relay_signal_deivce_v2(imei, value, device_type) elif cat == 1: if imei == '868575023189139' and value == 3: succ, result = launch_pulse_signal_deivce(imei, 2, high, low) time.sleep(2) succ, result = launch_pulse_signal_deivce(imei, 1, high, low) else: succ, result = launch_pulse_signal_deivce(imei, value, high, low) else: raise ApiError('not support') if not succ: # 退款 pay_refund(pay, product) try: data = json.loads(result) dbg((data['code'], data['msg'])) reply['code'] = data['code'] reply['msg'] = data['msg'] status_code = 422 except: print_exception_info() reply['code'] = 99 reply['msg'] = 'server error' status_code = 500 return make_response(jsonify(reply), status_code) if cat == 0: # 按摩椅需要记录开始记录 pay_way = pay.pay_mode stime = datetime.now() etime = stime + timedelta(seconds=product.value) record = dbapi.make_new_record(pay.id, pay_way, pay.user_id, product.agent_id, pay.product_id, stime, etime) else: # 投币器也需要记录开始记录 pay_way = pay.pay_mode stime = datetime.now() etime = stime record = dbapi.make_new_record(pay.id, pay_way, pay.user_id, product.agent_id, pay.product_id, stime, etime) db.session.commit() reply['data'] = { 'id': record.id, 'stime': int(record.stime.timestamp()), 'etime': int(record.etime.timestamp()), 'now': int(time.time()), 'user_id': record.user_id, } return make_response(jsonify(reply), status_code)
def device_multi_distribution(): dbg('device_multi_distribution') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imeis = data['imeis'] to_agent = data['to_agent'] rate = data['rate'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) agent_id = current_user.agent_id dbg(imeis) levels = ('l1', 'l2', 'l3', 'l4') for imei in imeis: dbg(imei) device = dbapi.get_device(imei=imei) if not device: db.session.rollback() dbg('imei: %s not exists' % imei) reply['code'] = 999 reply['msg'] = 'imei: %s 不存在' % imei status_code = 422 return make_response(jsonify(reply), status_code) elif device.owner_agent_id != agent_id: db.session.rollback() dbg('imei: %s no permission' % imei) reply['code'] = 999 reply['msg'] = 'imei: %s 没有权限分配' % imei status_code = 422 return make_response(jsonify(reply), status_code) elif device.salesman_agent_id != 0: db.session.rollback() dbg('imei: %s belong to salesman' % imei) reply['code'] = 999 reply['msg'] = 'imei: %s 已被分配给业务员' % imei status_code = 422 return make_response(jsonify(reply), status_code) update = {} # 1. 修改提成比例 cur_agent_level = current_user.cur_agent.level target_agent = dbapi.get_agent(id=to_agent) if not target_agent: db.session.rollback() raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND) tmp_levels = ['l1', 'l2', 'l3', 'l4'] tmp_levels.pop(cur_agent_level - 1) tmp_levels.pop(target_agent.level - 1) sum_rate = rate for level in tmp_levels: sum_rate += device.__dict__[level] update[levels[cur_agent_level - 1]] = rate update[levels[target_agent.level - 1]] = 1 - sum_rate if update[levels[target_agent.level - 1]] < 0: db.session.rollback() raise ApiError('ERROR_INVALIDATE_RATE', error.ERROR_INVALIDATE_RATE) # device_distributions = dbapi.get_device_distribution(device_id=device.id) # distri_count = 0 # if device_distributions: # distri_count = len(device_distributions) # if distri_count == 1: # update['l4'] = rate # update['l3'] = device.l4 - rate # upper_rate = device.l4 # if distri_count == 2: # update['l3'] = rate # update['l2'] = device.l3 - rate # upper_rate = device.l3 # if distri_count == 3: # update['l2'] = rate # update['l1'] = device.l2 - rate # upper_rate = device.l2 # try: # assert(upper_rate>rate) # except: # print_exception_info() # raise ApiError('ERROR_INVALIDATE_RATE', error.ERROR_INVALIDATE_RATE) # 2. 插入设备分组 from_agent = agent_id device_distribution = dbapi.make_new_device_distribution( device.id, device.imei, from_agent, to_agent, rate) # 3. 更细设备 update['agent_id'] = to_agent update['address_id'] = 0 device = dbapi.update_device(device, **update) db.session.add(device) # 4. 移除设备的套餐 dbapi.delete_device_product(device.id) # 5. 清除历史订单 dbapi.delete_pays_and_records(device.imei) try: db.session.commit() except: db.session.rollback() raise return make_response(jsonify(reply), status_code)
def device_salesman_multi_distribution(): dbg('device_salesman_multi_distribution') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imeis = data['imeis'] to_agent = data['to_agent'] sl1 = data['sl1'] sl2 = data['sl2'] sl3 = data['sl3'] my = data['self'] all_ = sl1 + sl2 + sl3 + my assert (math.isclose(all_, 1)) except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) agent_id = current_user.agent_id dbg(imeis) for imei in imeis: dbg(imei) device = dbapi.get_device(imei=imei) if not device: db.session.rollback() dbg('imei: %s not exists' % imei) reply['code'] = 999 reply['msg'] = 'imei: %s 不存在' % imei status_code = 422 return make_response(jsonify(reply), status_code) elif device.owner_agent_id != agent_id and device.salesman_agent_id != agent_id: db.session.rollback() dbg('imei: %s no permission' % imei) reply['code'] = 999 reply['msg'] = 'imei: %s 没有权限分配' % imei status_code = 422 return make_response(jsonify(reply), status_code) elif device.salesman_agent_id != 0 and current_user.cur_agent.salesman == 0: db.session.rollback() dbg('imei: %s belong to salesman' % imei) reply['code'] = 999 reply['msg'] = 'imei: %s 已被分配给业务员' % imei status_code = 422 return make_response(jsonify(reply), status_code) update = { 'sl1': data['sl1'], 'sl2': data['sl2'], 'sl3': data['sl3'], } # 1. 插入设备分组 from_agent = agent_id device_distribution = dbapi.make_new_device_distribution_salesman( device.id, device.imei, from_agent, to_agent) # 2. 更细设备 update['salesman_agent_id'] = to_agent if device.salesman_agent_id != 0: update.pop('sl1') update.pop('sl2') update.pop('sl3') dbg(update) device = dbapi.update_device(device, **update) db.session.add(device) # 3. 移除设备的套餐 # dbapi.delete_device_product(device.id) try: db.session.commit() except: db.session.rollback() raise return make_response(jsonify(reply), status_code)
def launch_pulse_signal_deivce(): dbg('launch_pulse_signal_deivce') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data.get('imei') device_id = data.get('device_id') pulse = int(data['pulse']) money = pulse device_type = 1 duration = 5 high = int(data['high']) low = int(data['low']) async_url = data.get('async_url') except: print_exception_info() raise ApiError('launch_pulse_signal_deivce error', error.ERROR_PARAM) if device_id: device = dbapi.get_device(id=device_id) if not device: raise ApiError('no permision', error.ERROR_DEVICE_NO_PERMISSION) imei = device.imei elif imei: imei = imei else: raise ApiError('no permision', error.ERROR_DEVICE_NO_PERMISSION) agent_id = current_user.agent_id if not (check_imei(imei, agent_id)): raise ApiError('no permision', error.ERROR_DEVICE_NO_PERMISSION) if async_url: data = { 'async_url': async_url, 'topic': 'deveventreq', 'device_type': device_type, 'money': money, 'duration': duration, 'high': high, 'low': low, 'pulse': pulse, 'imei': imei } event.send_data_to_device.delay(data, need_to_wait=True) return make_response(jsonify(reply), status_code) data = bytearray([0x54, device_type]) data += money.to_bytes(4, 'big') data += duration.to_bytes(4, 'big') data += high.to_bytes(4, 'big') data += low.to_bytes(4, 'big') data += pulse.to_bytes(4, 'big') dbg(data) if async_url: event.send_data_to_device.delay(async_url, 'deveventreq', imei, data, need_to_wait=True) return make_response(jsonify(reply), status_code) result = tool.send_data_to_device('deveventreq', imei, data, need_to_wait=True) if result == 0: return make_response(jsonify(reply), status_code) elif result['result'] == 0: raise ApiError("ERROR_FAIL", error.ERROR_DEVICE_BUSY) elif result['result'] == 1: return make_response(jsonify(reply), status_code)