def delete_activity(activity_id: int, merchant_id: int = Depends(get_login_merchant), session: Session = Depends(create_session)): """ 删除营销活动活动\n :param activity_id: 活动id\n :return: """ ret_code = 0 ret_msg = "success" try: cur_merchant = json.loads(redis_client.hget("merchants", merchant_id)) if cur_merchant["merchant_type"] != 0: session.commit() return make_response(-1, "权限不足!") session.query(Activity).filter(Activity.id == activity_id).delete() # 删除缓存活动信息、商品折扣表 activity_key = f"activity_{activity_id}" discount_key = f"discount_of_activity_{activity_id}" pipe = redis_client.pipeline(transaction=True) pipe.delete(activity_key) pipe.delete(discount_key) pipe.execute() logger.info("删除活动成功!") session.commit() except Exception as e: session.rollback() logger.error(str(e)) ret_code = -1 ret_msg = str(e) return make_response(ret_code, ret_msg)
def add_products_to_activity(actvity_product: ActivityProductModel, merchant_id: int = Depends(get_login_merchant)): """ 添加商品到营销活动(必须在活动结束之前) \n :param: actvity_product: 活动商品及折扣信息 \n :return: """ ret_code = 0 ret_msg = "success" try: # 查询活动信息 activity_key = f"activity_{actvity_product.activity_id}" activity_info = redis_client.get(activity_key) if activity_info is None: return make_response(-1, "活动不存在或已结束!") activity = json.loads(activity_info) # 检查操作合法性(只能操作自己商户下的商品) cur_merchant = json.loads(redis_client.hget("merchants", merchant_id)) if cur_merchant["merchant_type"] != 1: return make_response(-1, "权限不足, 仅普通商户能执行此操作!") product_ids = actvity_product.product_discount_map.keys() pipe = redis_client.pipeline(transaction=False) for product_id in product_ids: pipe.hget("products", product_id) product_infos = pipe.execute() for product_info in product_infos: if product_info is None: return make_response(-1, "请确认商品都存在!") product = json.loads(product_info) if int(product.get("merchant_id")) != merchant_id: return make_response(-1, "非法操作, 仅能操作自己商户下的商品!") # 将商品折扣信息添加至活动折扣表, 并建立活动商品与活动的映射关系 discount_key = f"discount_of_activity_{actvity_product.activity_id}" act_endtime = datetime.strptime(activity.get("end_time"), "%Y-%y-%d %H:%M:%S") now = datetime.now() expire_time = (act_endtime - now).seconds pipe = redis_client.pipeline(transaction=True) for product_id, discount in actvity_product.product_discount_map.items( ): pipe.hset(discount_key, product_id, discount) pipe.set(f"activity_of_product_{product_id}", activity.get("id"), ex=expire_time) pipe.execute() logger.info("新增活动商品成功!") except Exception as e: logger.error(str(e)) ret_code = -1 ret_msg = str(e) return make_response(ret_code, ret_msg)
def modify_product(product_info: ProductModel, merchant_id: int = Depends(get_login_merchant), session: Session = Depends(create_session)): """ 修改商品(只能修改商品名称、商品简介、商品封面图片地址、商品详情图片地址、商品剩余库存、商品价格)\n :param product_info: 商品实体,参见ProductModel \n :return: 成功返回商品id """ ret_code = 0 ret_msg = "success" ret_data = {} try: if product_info.id is None: session.commit() return make_response(-1, "商品id不能为空!") product = session.query(Product).filter( Product.id == product_info.id).one_or_none() if product is None: session.commit() return make_response(-1, f"商品({product_info.id})不存在!") if product.merchant_id != merchant_id: session.commit() return make_response(-1, f"不允许修改他人账户下的商品!") product.product_name = product_info.product_name product.product_tag = product_info.product_tag product.product_cover = product_info.product_cover product.product_desc = product_info.product_desc product.detail_pictures = json.dumps(product_info.detail_pictures) product.has_stock_limit = product_info.has_stock_limit product.remain_stock = product_info.remain_stock product.price = product_info.price product.update_time = datetime.now() # 更新缓存 redis_client.hset("products", product.id, json.dumps(product.to_dict(), cls=JsonEncoder)) logger.info("更新redis成功!") session.commit() logger.info(f"商品信息修改成功, product_id: {product.id}") ret_data["product_id"] = product.id except Exception as e: session.rollback() logger.error(str(e)) ret_code = -1 ret_msg = str(e) return make_response(ret_code, ret_msg, ret_data)
def add_activity(activity: ActivityModel, merchant_id: int = Depends(get_login_merchant), session: Session = Depends(create_session)): """ 新建营销活动\n :param activity: 活动信息\n :return: """ ret_code = 0 ret_msg = "success" try: cur_merchant = json.loads(redis_client.hget("merchants", merchant_id)) if cur_merchant["merchant_type"] != 0: session.commit() return make_response(-1, "权限不足!") now = datetime.now() act = Activity(activity.act_name, activity.act_cover, activity.begin_time, activity.end_time, now, now) session.add(act) # 活动存入redis并添加商品折扣表 activity_key = f"activity_{act.id}" discount_key = f"discount_of_activity_{act.id}" expire_time = (activity.end_time - now).seconds pipe = redis_client.pipeline(transaction=True) pipe.set(activity_key, json.dumps(act.to_dict(), cls=JsonEncoder), ex=expire_time) # 创建一个空的商品折扣表, 插入一条空数据占位符 pipe.hset(discount_key, "", "") pipe.expire(discount_key, expire_time) pipe.execute() logger.info("活动redis信息初始化成功!") session.commit() logger.info(f"新建营销活动成功,活动id: {act.id}") except Exception as e: session.rollback() logger.error(str(e)) ret_code = -1 ret_msg = str(e) return make_response(ret_code, ret_msg)
def add_product(product_info: ProductModel, merchant_id: int = Depends(get_login_merchant), session: Session = Depends(create_session)): """ 新增商品\n :param product_info: 商品实体,参见ProductModel \n :return: 成功返回商品id """ ret_code = 0 ret_msg = "success" ret_data = {} try: now = datetime.now() product = Product(merchant_id, product_info.product_name, product_info.product_tag, product_info.product_cover, product_info.product_desc, json.dumps(product_info.detail_pictures), product_info.has_stock_limit, product_info.remain_stock, product_info.price, now, now) session.add(product) session.flush() # 商品信息存入缓存 logger.info(f"redis prduct_info: f{product.to_dict()}") redis_client.hset("products", product.id, json.dumps(product.to_dict(), cls=JsonEncoder)) # 将商品id保存至商户,便于根据商户id查询对应商品 redis_client.sadd(f"products_of_merchant_{merchant_id}", product.id) logger.info("商品存入redis成功!") session.commit() logger.info(f"新增商品成功, product_id: {product.id}") ret_data["product_id"] = product.id except Exception as e: session.rollback() logger.error(str(e)) ret_code = -1 ret_msg = str(e) return make_response(ret_code, ret_msg, ret_data)
def get_sale_statistics(begin_time: datetime, end_time: datetime, merchant_id: int = Depends(get_login_merchant), session: Session = Depends(create_session)): """ 获取商城begin_time到end_time时间段内销量统计,含商城订单总数量、总金额,及各商户订单数量、金额分布(仅管理员有权限)\n :param begin_time: 开始时间\n :param end_time: 结束时间\n :return: """ ret_code = 0 ret_msg = "success" ret_data = { "total_deal_amount": 0, "total_deal_money": 0, "distributions": [] } try: cur_merchant = json.loads(redis_client.hget("merchants", merchant_id)) if cur_merchant["merchant_type"] != 0: session.commit() return make_response(-1, "权限不足!") statistics = session.query( Deal.merchant_id.label("merchant_id"), func.count(Deal.deal_no).label("deal_amount"), func.sum(Deal.money).label("total_money"), ).filter(Deal.create_time.between(begin_time, end_time)).group_by(Deal.merchant_id) merchant_infos = redis_client.hgetall("merchants") logger.info(f"商户列表: {merchant_infos}") distributions = [] total_deal_amount = 0 total_deal_money = 0 for statistic in statistics: total_deal_amount += statistic.deal_amount total_deal_money += statistic.total_money merchant_info = json.loads(merchant_infos[str( statistic.merchant_id)]) distributions.append({ "merchant_id": statistic.merchant_id, "merchant_name": merchant_info["merchant_name"], "deal_amount": statistic.deal_amount, "total_money": statistic.total_money }) ret_data["total_deal_amount"] = total_deal_amount ret_data["total_deal_money"] = total_deal_money ret_data["distributions"] = distributions session.commit() except Exception as e: session.rollback() logger.error(str(e)) ret_code = -1 ret_msg = str(e) return make_response(ret_code, ret_msg, ret_data)
def app_shutdown(): logger.info("******************** App Close ********************\n")
def app_start(): logger.info("******************** App Start ********************")