def get_line_data(token): res_dir = request.get_json() ids = None if res_dir and 'ids' in res_dir: ids = res_dir['ids'] month = res_dir[ 'month'] if res_dir and 'month' in res_dir else datetime.datetime.now( ).month master_sql = db.session.query( func.date_format(Orders.input_time, '%d').label('date'), func.sum(Orders.buy_num)).group_by('date').order_by( Orders.input_time).filter( extract('month', Orders.input_time) == month) master_sql = power_sql(master_sql, token) legend = get_legend(ids, True) data = [] for item in legend: _line_data = master_sql.filter(Orders.buy_product == item[1]).all() day = get_xAxis(month) _data = [] for i in range(0, len(day)): _data.append(0) for i in _line_data: _data[int(i[0]) - 1] = int(i[1]) data.append({ 'name': item[0], 'data': _data, 'type': 'line', 'animationEasing': 'cubicInOut' }) Succ200.data = data return Succ200.to_dict()
def express_list(): items = Express.query.filter_by(active=1).all() data = [] for item in items: data.append(item.to_dict()) Succ200.data = data return Succ200.to_dict()
def get_problem_info(token): # noinspection PyBroadException try: master_sql = Orders.query.filter_by(delivery_state=6) error_num = power_sql(master_sql, token).count() Succ200.data = error_num except Exception: return Error4018.to_dict() return Succ200.to_dict()
def get_sales_today(token): day = str(datetime.datetime.now().day) # noinspection PyBroadException try: master_sql = db.session.query(func.sum( Orders.buy_num)).filter(extract('day', Orders.input_time) == day) buy_num = power_sql(master_sql, token).scalar() or 0 Succ200.data = int(buy_num) except Exception: return Error4017.to_dict() return Succ200.to_dict()
def get_pie_data(token): users = db.session.query( Users.username.label('name'), func.sum(Orders.buy_num)).join( Users, Users.u_id == Orders.publicist).group_by('name').all() legend = [] data = {'legend': [], 'data': []} for item in users: data['legend'].append(item[0]) data['data'].append({'value': int(item[1]), 'name': item[0]}) Succ200.data = data return Succ200.to_dict()
def product_group_list(): """ 获取产品分类列表 :return: data=[{id,name,price}...] """ items = Product_group.query.all() data = [] for item in items: data.append(item.to_dict()) Succ200.data = data return Succ200.to_dict()
def del_express(token): res_dir = request.get_json() e_id = res_dir['id'] # noinspection PyBroadException try: _express = Express.query.filter_by(id=e_id).first() db.session.delete(_express) db.session.commit() except Exception: return Error409.to_dict() Succ200.data = None return Succ200.to_dict()
def update_logistics(token): res_dir = request.get_json() logistics_list = res_dir['list'] db.session.bulk_update_mappings(Orders, logistics_list) # noinspection PyBroadException try: db.session.bulk_update_mappings(Orders, logistics_list) db.session.flush() db.session.commit() except Exception: return Error409.to_dict() return Succ200.to_dict()
def creat(token): res_dir = request.get_json() # noinspection PyBroadException try: _express = Express(**res_dir) db.session.add(_express) db.session.flush() db.session.commit() except Exception: return Error409.to_dict() Succ200.data = {'id': _express.id, 'active': _express.active} return Succ200.to_dict()
def get_sales_year(token): year = str(datetime.datetime.now().year) power = token['power'] # noinspection PyBroadException try: master_sql = db.session.query(func.sum(Orders.buy_num)).filter( and_(Orders.delivery_state == 1, extract('year', Orders.input_time) == year)) buy_num = power_sql(master_sql, token).scalar() or 0 Succ200.data = int(buy_num) except Exception: return Error4015.to_dict() return Succ200.to_dict()
def order_input(token): u_id = token['u_id'] res_dir = list_data_handle(request.get_json(), u_id) # noinspection PyBroadException try: _order = Orders(**res_dir) db.session.add(_order) db.session.flush() db.session.commit() except Exception: return Error409.to_dict() Succ200.data = {'id': str(_order.id).zfill(6), 'delivery_state': '未发货'} return Succ200.to_dict()
def get_sales_volume(token): year = str(datetime.datetime.now().year) power = token['power'] u_id = token['u_id'] income = 0 # noinspection PyBroadException try: master_sql = db.session.query( Products.name.label('product'), func.sum(Orders.buy_num), Products.price, Products.convert, Products.cost, func.sum(Orders.payment)).join( Products, Products.id == Orders.buy_product).group_by('product').filter( and_(Orders.delivery_state == 1, extract('year', Orders.input_time) == year)) if power in INSIDE: payment_list = master_sql.all() # 把扣取用户的费用也给加上 退货产生的费用 charge = db.session.query(func.sum(Orders.payment)).filter( and_(Orders.delivery_state != 1, extract('year', Orders.input_time) == year)).all() charge = charge[0] for li in payment_list: num = int(li[1]) income += num * (li[2] * li[3]) + num * (li[4] or 0) payment = int(income) + int(charge[0] or 0) else: payment_list_0 = power_sql( master_sql, token).filter(Orders.payment == 0).all() payment_list_1 = power_sql( master_sql, token).filter(Orders.payment != 0).all() # 计算运费 sql = db.session.query(func.sum(Orders.freight)).filter( extract('year', Orders.input_time) == year) freight = power_sql(sql, token).all() for li in payment_list_0: num = int(li[1]) income += num * (li[2] * (1 - li[3])) - num * (li[4] or 0) for li in payment_list_1: income += int(li[5]) freight = freight[0][0] or 0 if freight[0] else 0 print(freight) payment = int(income) - int(freight) # master_sql = db.session.query(func.sum(Orders.payment)).filter( # and_(Orders.delivery_state == 1, extract('year', Orders.input_time) == year)) # payment = power_sql(master_sql, token).scalar() or 0 Succ200.data = payment except Exception: return Error4016.to_dict() return Succ200.to_dict()
def get_line_options(token): res_dir = request.get_json() ids = None if res_dir and 'ids' in res_dir: ids = res_dir['ids'] month = res_dir[ 'month'] if res_dir and 'month' in res_dir else datetime.datetime.now( ).month title = get_title(month) days = get_xAxis(month) legend = get_legend(ids) data = {'title': title, 'xAxis': days, 'legend': legend} Succ200.data = data return Succ200.to_dict()
def active(token): res_dir = request.get_json() e_id = res_dir['id'] e_bool = res_dir['bool'] # noinspection PyBroadException try: _express = Express.query.filter_by(id=e_id).first() _express.active = e_bool db.session.flush() db.session.commit() except Exception: return Error409.to_dict() Succ200.data = None return Succ200.to_dict()
def del_school_log(token): u_id = token['u_id'] res_dir = request.get_json() o_id = res_dir.get('id') _log = School_log.query.filter_by(id=o_id, u_id=u_id).first() # noinspection PyBroadException try: _log.status = 0 db.session.flush() db.session.commit() except Exception: return Error409.to_dict() Succ200.data = None return Succ200.to_dict()
def edit(token): res_dir = request.get_json() e_id = res_dir['id'] e_name = res_dir['name'] price = res_dir['price'] # noinspection PyBroadException try: _product = Products.query.filter_by(id=e_id).first() _product.name = e_name _product.price = price db.session.commit() except Exception: return Error409.to_dict() Succ200.data = None return Succ200.to_dict()
def apply_discount_state_change(token): power = token['power'] res_dir = request.get_json() # noinspection PyBroadException try: _order = Orders.query.filter_by(id=res_dir.get('id')).update({ 'apply_discount_state': res_dir['apply_discount_state'], 'price': res_dir['price'] }) db.session.flush() db.session.commit() except Exception: return Error409.to_dict() Succ200.data = None return Succ200.to_dict()
def del_product(token): """ 删除产品 :param token: :return: """ res_dir = request.get_json() p_id = res_dir['id'] # noinspection PyBroadException try: _product = Products.query.filter_by(id=p_id).first() db.session.delete(_product) db.session.commit() except Exception: return Error409.to_dict() Succ200.data = None return Succ200.to_dict()
def creat(token): """ 添加产品 :param token: :return: """ res_dir = request.get_json() # noinspection PyBroadException try: _product = Products(**res_dir) db.session.add(_product) db.session.flush() db.session.commit() except Exception: return Error409.to_dict() Succ200.data = {'id': _product.id, 'active': _product.active} return Succ200.to_dict()
def product_list(): """ 获取产品列表 :return: data=[{id,name,price}...] """ res_dir = request.get_json() if res_dir and 'ids' in res_dir: ids = res_dir['ids'].split(',') sql = Products.query.filter(Products.id.in_(ids)) else: sql = Products.query items = sql.all() data = [] for item in items: data.append(item.to_dict()) PRODUCT_LIST[item.id] = item.name Succ200.data = data return Succ200.to_dict()
def ppg_id_info(token): res_dir = request.get_json() ppg_id = res_dir.get('ppg_id') # noinspection PyBroadException try: _promotion = Promotion.query.filter_by(id=ppg_id).first() except Exception: return Error409.to_dict() if not _promotion: return Error405.to_dict() if not _promotion.promotion_user or not _promotion.promotion_school: return Error409.to_dict() Succ200.data = { 'school': _promotion.promotion_school.school_name, 'publicist': [_promotion.publicist, _promotion.promotion_user.username], 'school_code': _promotion.promotion_school.school_code } return Succ200.to_dict()
def del_list(token): res_dir = request.get_json() o_id = res_dir.get('id') power = token['power'] u_id = token['u_id'] if power not in INSIDE: # 如果不在可以看到所有订单的群组里 就添加搜索条件 _order = Orders.query.filter_by(id=o_id, input_staff=u_id).first() else: _order = Orders.query.filter_by(id=o_id).first() if not _order: return Error406.to_dict() # noinspection PyBroadException try: db.session.delete(_order) db.session.commit() except Exception: return Error409.to_dict() Succ200.data = None return Succ200.to_dict()
def update_school_log(token): res_dir = request.get_json() o_id = res_dir.get('id') u_id = token['u_id'] if 'school_code' in res_dir: del res_dir['school_code'] if 'id' in res_dir: del res_dir['id'] # noinspection PyBroadException try: _log = School_log.query.filter_by(id=o_id, u_id=u_id, status=1).update(res_dir) db.session.flush() db.session.commit() _log = School_log.query.filter_by(id=o_id, u_id=u_id, status=1).first() except Exception: return Error409.to_dict() Succ200.data = _log.to_dict() return Succ200.to_dict()
def active(token): """ 是否激活产品 :param token: :return: """ res_dir = request.get_json() p_id = res_dir['id'] e_bool = res_dir['bool'] # noinspection PyBroadException try: _product = Products.query.filter_by(id=p_id).first() _product.active = e_bool db.session.flush() db.session.commit() except Exception: return Error409.to_dict() Succ200.data = None return Succ200.to_dict()
def update_list(token): u_id = token['u_id'] power = token['power'] res_dir = list_data_handle(request.get_json(), u_id, False) # noinspection PyBroadException try: if power not in INSIDE: _order = Orders.query.filter_by(id=res_dir.get('id'), input_staff=u_id, delivery_state=0).update(res_dir) else: _order = Orders.query.filter( Orders.id == res_dir.get('id'), Orders.delivery_state != 3).update(res_dir) db.session.flush() db.session.commit() except Exception: return Error409.to_dict() Succ200.data = None return Succ200.to_dict()
def down_finance(token): res_dir = request.get_json() # 产品ID 回款情况 回款日期 收费 # 负责人 订单录入员 发单日期 物流状态 # 省 市 产品 数量 收件人 # 收件人号码 收件人地址 快递单号 退货单号 # 换货单号 sql = db.session.query( Orders.id, Orders.payment_state, Orders.refund_time, Orders.payment, Users.username, Orders.input_staff, Orders.delivery_time, Orders.delivery_state, Orders.province, Orders.city, Products.name, Orders.buy_num, Orders.consignee, Orders.phone, Orders.address, Orders.courier_code, Orders.courier_code_return, Orders.courier_code_relapse) \ .join(Users, Users.u_id == Orders.publicist) \ .join(Products) \ .filter(get_safety_list(**res_dir, token=token)) data = [] items = sql.all() keys = [ 'id', 'payment_state', 'refund_time', 'payment', 'username', 'input_staff', 'delivery_time', 'delivery_state', 'province', 'city', 'name', 'buy_num', 'consignee', 'phone', 'address', 'courier_code', 'courier_code_return', 'courier_code_relapse' ] for item in items: obj = {} for index, key in enumerate(keys): if key.find('time') != -1: obj[key] = datetime.strftime( item[index], "%Y-%m-%d") if item[index] else '暂未回款' elif key == 'input_staff': obj[key] = get_user_name(item[index]) elif key == 'payment_state': obj[key] = '已结算' if item[index] == 1 else '暂未回款' elif key == 'delivery_state': obj[key] = DELIVERY_STATE[item[index]] else: obj[key] = item[index] data.append(obj) Succ200.data = data return Succ200.to_dict()
def add_school_log(token): res_dir = request.get_json() title = res_dir['title'] content = res_dir['content'] school_code = res_dir['school_code'] u_id = token['u_id'] # noinspection PyBroadException try: Log = School_log() Log.school_code = school_code Log.u_id = u_id Log.title = title Log.content = content db.session.add(Log) db.session.flush() db.session.commit() except Exception: return Error409.to_dict() data = {'input_time': Log.input_time, 'id': Log.id} Succ200.data = data return Succ200.to_dict()
def get_school_log(token): u_id = token['u_id'] res_dir = request.get_json() school_code = res_dir['school_code'] data = [] if 'id' in res_dir: o_id = res_dir['id'] log = School_log.query.filter_by(school_code=school_code, u_id=u_id, status=1, id=o_id).first() data = log.to_dict() else: log = School_log.query.filter_by(school_code=school_code, u_id=u_id, status=1).all() for item in log: data.append(item.to_dict()) Succ200.data = data return Succ200.to_dict()
def order_list(token): u_id = token['u_id'] res_dir = request.get_json() page_index = res_dir.get('page') # 页数 limit = res_dir.get('limit') # 一页多少条 power = token['power'] day = str(datetime.now().day) if 'manage' in res_dir and res_dir.get('manage'): # 管理页面请求拦截 search_var(res_dir) if limit < 100: if power in PUBLICIST: # sql = Orders.query.filter(get_safety_list(**res_dir, token=token)).filter(publicist_safety(u_id)) sql = Orders.query.filter( get_safety_list(**res_dir, token=token)).filter( Orders.publicist == u_id) else: sql = Orders.query.filter( get_safety_list(**res_dir, token=token)) else: sql = Orders.query.with_entities( Orders.buy_product, Orders.parent, Orders.phone, Orders.address, Orders.price, Orders.buy_num, Orders.pay_method, Orders.remarks, Orders.id).filter(Orders.delivery_state == 0, Orders.apply_discount_state != 1, extract('day', Orders.delivery_time) <= day) else: sql = Orders.query.filter(Orders.input_staff == u_id, Orders.delivery_state == 0) # noinspection PyBroadException try: items = sql.limit(limit).offset((page_index - 1) * limit) total = sql.count() except Exception: return Error409.to_dict() data = {'items': [], 'total': total} if limit < 100: for item in items: dict_item = item.to_dict() dict_item['input_staff'] = get_user_name(dict_item['input_staff'] or u_id) dict_item['id'] = str(dict_item['id']).zfill(6) dict_item['ppg_id'] = str(dict_item['ppg_id']).zfill(6) dict_item['school'] = item.orders_school.school_name dict_item['publicist'] = get_user_name(dict_item['publicist']) data['items'].append(dict_item) else: for item in items: data['items'].append({ 'buy_product': item[0], 'parent': item[1], 'phone': item[2], 'address': item[3], 'price': item[4], 'buy_num': item[5], 'pay_method': item[6], 'remarks': item[7], 'id': str(item[8]).zfill(6) }) Succ200.data = data return Succ200.to_dict()