def get_category_by_prname(self): args = request.args.to_dict() log.info("args", args) if "token" not in args or "PRname" not in args: return PARAMS_MISS prname = get_str(args, "PRname") from ManagerSystem.models.model import Products pr_filter = {Products.PRname.like("%{0}%".format(prname))} from ManagerSystem.service.SProducts import SProducts spro = SProducts() pr_list = tolist(spro.get_product_by_filter(pr_filter)) pr_list = pr_list[:find_category_by_prname_size] log.info("pr list", pr_list) ctname_list = [] for pr in pr_list: ct_filter = {Category.CTid == pr.get("CTid")} ct_name = tolist( self.scategory.get_category_by_or_filter(ct_filter)) if not ct_name: continue ct_name = ct_name[0] log.info("ct name", ct_name) if ct_name in ctname_list: continue ctname_list.append(ct_name) response = get_response("SUCCESS_MESSAGE_GET_INFO", "OK") response["data"] = ctname_list[:find_category_by_prname_size] return response
def dealapproval(self, petypem, APcontent): if petypem == 304: from ManagerSystem.service.SProducts import SProducts spro = SProducts() pb = { "PBstatus": 201 } pb_list = [PB.PBid for PB in spro.get_pbid_by_prid(APcontent)] log.info("len pblist", len(pb_list)) for pbid in pb_list: spro.update_pb(pbid, pb)
def __init__(self): self.sstocks = SStocks() self.sproduct = SProducts() self.stock_key_list = ["STname", "STamount", "STabo"] self.stock_product_key_list = ["STid", "PBid", "PBnumber"]
class CStocks(): def __init__(self): self.sstocks = SStocks() self.sproduct = SProducts() self.stock_key_list = ["STname", "STamount", "STabo"] self.stock_product_key_list = ["STid", "PBid", "PBnumber"] def get_stock_all(self): args = request.args.to_dict() log.info("args", args) if "token" not in args: return PARAMS_MISS maid = token_to_usid(args.get("token")) try: if "PBid" not in args: prid_list = [ product.PRid for product in self.sproduct.get_product_by_maid(maid) ] log.info("prid list", prid_list) pbid_list = [] for prid in prid_list: pbid_list.extend([ productbrand.PBid for productbrand in self.sproduct.get_productbrand_by_prid(prid) ]) log.info("pbid list", pbid_list) stocks_list = [] for pbid in pbid_list: stock_product_list = tolist( self.sstocks.get_stocks_by_PBid(pbid)) log.info("stock_product_list", stock_product_list) for stock_product in stock_product_list: stock = todict( self.sstocks.get_stock_by_stid( stock_product.get("STid"))) log.info("stock", stock) stock_product.update(stock) stocks_list.extend(stock_product_list) else: stocks_list = tolist( self.sstocks.get_stocks_by_PBid(args.get("PBid"))) for stock_product in stocks_list: stock = todict( self.sstocks.get_stock_by_stid( stock_product.get("STid"))) stock_product.update(stock) response = get_response("SUCCESS_MESSAGE_GET_INFO", "OK") response["data"] = stocks_list return response except Exception as e: log.error("get error", e.message) return SYSTEM_ERROR def get_stock(self): args = request.args.to_dict() log.info("args", args) params_list = ["token", "STid"] for key in params_list: if key not in args: return PARAMS_MISS maid = token_to_usid(args.get("token")) prid_list = [ product.PRid for product in self.sproduct.get_product_by_maid(maid) ] pbid_list = [] for prid in prid_list: pbid_list.extend([ productbrand.PBid for productbrand in self.sproduct.get_productbrand_by_prid(prid) ]) stocks_list = [] for pbid in pbid_list: stocks_list.extend(tolist(self.sstocks.get_stocks_by_PBid(pbid))) if args.get("STid") not in stocks_list: return get_response("ERROR_MESSAGE_DB_ERROR", "MANAGERSYSTEMERROR", "ERROR_CODE_DB_ERROR") stock = todict(self.sstocks.get_stock_by_stid(args.get("STid"))) response = get_response("SUCCESS_MESSAGE_GET_INFO", "OK") response["data"] = stock return response def add_stock(self): args = request.args.to_dict() log.info("args", args) if "token" not in args: return PARAMS_MISS data = json.loads(request.data) log.info("data", data) if "STname" not in data: return PARAMS_MISS stock = {"STid": str(uuid.uuid1())} for key in self.stock_key_list: if key in data: stock[key] = data.get(key) try: self.sstocks.add_model("Stocks", **stock) except Exception as e: log.error("add stock error", e.message) return SYSTEM_ERROR return get_response("SUCCESS_MESSAGE_ADD_DATA", "OK") def add_stock_product(self): args = request.args.to_dict() log.info("args", args) if "token" not in args: return PARAMS_MISS data = json.loads(request.data) log.info("data", data) sp = {"SPid": str(uuid.uuid1())} for key in self.stock_product_key_list: if key not in data: return PARAMS_MISS sp[key] = data.get(key) try: self.sstocks.add_model("StocksProducts", **sp) return get_response("SUCCESS_MESSAGE_ADD_DATA", "OK") except Exception as e: log.error("add stock product error", e.message) return SYSTEM_ERROR def update_stock(self): args = request.args.to_dict() log.info("args", args) if "token" not in args: return PARAMS_MISS data = json.loads(request.data) log.info("data", data) stock = {} for key in self.stock_key_list: if key in data: stock[key] = data.get(key) stid = data.get("STid") try: update_result = self.sstocks.update_stock(stid, stock) log.info("update result", update_result) if not update_result: return get_response("ERROR_MESSAGE_DB_ERROR", "MANAGERSYSTEMERROR", "ERROR_CODE_DB_ERROR") return get_response("SUCCESS_MESSAGE_UPDATE_DATA", "OK") except Exception as e: log.error("add stock error", e.message) return SYSTEM_ERROR def update_stock_product(self): args = request.args.to_dict() log.info("args", args) if "token" not in args: return PARAMS_MISS data = json.loads(request.data) log.info("data", data) if "SPid" not in data: return PARAMS_MISS spid = data.get("SPid") sp = {} for key in self.stock_product_key_list: if key not in data: return PARAMS_MISS sp[key] = data.get(key) try: log.info("sp", sp) update_result = self.sstocks.update_stock_product(spid, sp) log.info("update result", update_result) if not update_result: return get_response("ERROR_MESSAGE_DB_ERROR", "MANAGERSYSTEMERROR", "ERROR_CODE_DB_ERROR") return get_response("SUCCESS_MESSAGE_UPDATE_DATA", "OK") except Exception as e: log.error("add stock product error", e.message) return SYSTEM_ERROR
def __init__(self): self.sorder = SOrder() self.suser = SManager() self.sproduct = SProducts()
class COrder(): def __init__(self): self.sorder = SOrder() self.suser = SManager() self.sproduct = SProducts() def get_order_list(self): try: args = request.args.to_dict() log.info("args", args) if "token" not in args or "page_size" not in args or "page_num" not in args: return PARAMS_MISS page_size = int(args.get("page_size")) page_num = int(args.get("page_num")) maid = token_to_usid(args.get("token")) from ManagerSystem.models.model import Products product_filter = {Products.MAid == maid} if args.get("PRname"): product_filter.add( Products.PRname.like("%{0}%".format(get_str( args, "PRname")))) product_list = tolist( self.sproduct.get_product_by_filter(product_filter)) log.info("product list ", product_list) pb_list = [] for product in product_list: product["PBunit"] = cvs.conversion_PBunit.get( product.get("PBunit", "其他币种")) product["PRbrand"] = cvs.conversion_PRbrand.get( product.get("PRbrand"), "其他") product["PRtype"] = cvs.conversion_PRtype.get( product.get("PRtype")) for pb in tolist( self.sproduct.get_pball_by_prid(product.get("PRid"))): pb.update(product) pb_list.append(pb) log.info("pb_list", pb_list) om_list = [] om_id_dict = {} from ManagerSystem.models.model import OrderMain omfilter = set() if args.get("OMstatus"): omstatus = cvs.conversion_OMstatus_reverse.get( get_str(args, "OMstatus")) omfilter.add(OrderMain.OMstatus == omstatus) if args.get("OMlogisticsName"): omfilter.add(OrderMain.OMlogisticsName == get_str( args, "OMlogisticsName")) if args.get("OMstartTime"): omfilter.add(OrderMain.OMtime >= TimeManager.get_db_time_str( args.get("OMstartTime"))) if args.get("OMendTime"): omfilter.add(OrderMain.OMtime <= TimeManager.get_db_time_str( args.get("OMendTime"))) for pb in pb_list: op_pb_list = tolist( self.sorder.get_order_part_list_by_pbid(pb.get("PBid"))) for op_pb in op_pb_list: op_pb.update(pb) if op_pb.get("OMid") in om_id_dict: om_id_dict[op_pb.get("OMid")].append(op_pb) else: om_id_dict[op_pb.get("OMid")] = [op_pb] log.info("om_id_dict", om_id_dict) om_count = {k: 0 for k in cvs.conversion_OMstatus} for om_id in om_id_dict: if args.get("OMid") and args.get("OMid") not in om_id: continue omfilter_tmp = omfilter.copy() omfilter_tmp.add(OrderMain.OMid == om_id) om_dict = tolist(self.sorder.get_om_by_filter(omfilter_tmp)) if not om_dict: continue om_dict = om_dict[0] om_dict.update({"order_item": om_id_dict.get(om_id)}) location = todict( self.sorder.get_location_by_usid(om_dict.pop("USid"))) log.info("location", location) om_count[om_dict.get("OMstatus")] += 1 om_dict.update(location) om_dict["OMcointype"] = cvs.conversion_PBunit.get( om_dict.get("OMcointype"), "其他币种") om_dict["OMstatus"] = cvs.conversion_OMstatus.get( om_dict.get("OMstatus"), 0) om_dict["OMtime"] = TimeManager.get_web_time_str( om_dict.get("OMtime")) om_list.append(om_dict) log.info("omlist", om_list) count = len(om_list) if page_size * page_num > count: page_num = count / page_size + 1 page_num = page_num if page_num > 0 else 1 om_list = om_list[(page_num - 1) * page_size:page_num * page_size] data = get_response("SUCCESS_MESSAGE_GET_INFO", "OK") data["data"] = { "count": count, "page_num": page_num, "page_size": page_size, "OrderMains": om_list, "OMcount": om_count } return data except Exception as e: log.error("get order list", e.message) import traceback print(traceback.format_exc()) return SYSTEM_ERROR def get_order_abo(self): try: args = request.args.to_dict() log.info("args", args) if "token" not in args or "OMid" not in args: return PARAMS_MISS maid = token_to_usid(args.get("token")) if not maid: return TOKEN_ERROR order_main = todict( self.sorder.get_order_main_by_om_id(get_str(args, "OMid"))) order_main.pop("USid") self._get_order_abo_by_order_main(order_main) log.info("order main", order_main) data = get_response("SUCCESS_MESSAGE_GET_INFO", "OK") data["data"] = order_main return data except Exception as e: log.error("get order abo", e.message) return SYSTEM_ERROR def _get_order_abo_by_order_main(self, order_main): order_part_list = tolist( self.sorder.get_order_part_list_by_omid(order_main.get("OMid"))) for order_part in order_part_list: order_part.update( self._get_product_into_order_abo(order_part.get("PBid"))) order_main["order_abo"] = order_part_list order_main["OMcointype"] = cvs.conversion_PBunit.get( order_main.get("OMcointype"), "其他币种") order_main["OMstatus"] = cvs.conversion_OMstatus.get( order_main.get("OMstatus"), 0) order_main["OMtime"] = TimeManager.get_web_time_str( order_main.get("OMtime")) location = todict( self.sorder.get_location_by_loid(order_main.get("LOid"))) if location.get("LOisedit") == 303: print(get_response("ERROR_MESSAGE_GET_LOCATION", "WORING_LOCATION")) order_main.update(location) order_main["CAid"] = order_main.pop("COid") def _get_product_into_order_abo(self, pbid): product = todict(self.sproduct.get_product_by_pbid(pbid)) if not product: raise Exception("SYSTEM ERROR NO FIDN PBID") product.update( todict(self.sproduct.get_product_by_prid(product.get("PRid")))) product.update(self._get_brinfo(product.get("BRid"))) product["PBunit"] = cvs.conversion_PBunit.get( product.get("PBunit", "其他币种")) product["PRbrand"] = cvs.conversion_PRbrand.get( product.get("PRbrand"), "其他") product["PRtype"] = cvs.conversion_PRtype.get(product.get("PRtype")) return product def _get_brinfo(self, brid): brinfo = {} while True: brand = todict(self.sproduct.get_brand_by_brid(brid)) if not (brand.get("BRkey") and brand.get("BRvalue")): error = "the brand does not have BRkey or BRvalue. brand = {0}".format( brand) raise Exception(error) if brand.get("BRkey") in brinfo: raise Exception( "the product has duplicate brand = {0}".format(brand)) brinfo[brand.get("BRkey")] = brand.get("BRvalue") if brand.get("BRfromid") == "0": break brid = brand.get("BRfromid") return brinfo def update_order_main(self): args = request.args.to_dict() log.info("args", args) if "token" not in args: return PARAMS_MISS maid = token_to_usid(args.get("token")) if not maid: return TOKEN_ERROR data = json.loads(request.data) log.info("data", data) if "OMstatus" not in data or "OMid" not in data: return PARAMS_MISS try: omstatus = cvs.conversion_OMstatus_reverse.get( get_str(data, "OMstatus")) update_result = self.sorder.update_order(get_str(data, "OMid"), {"OMstatus": omstatus}) log.info("update_result", update_result) if not update_result: return get_response("ERROR_MESSAGE_DB_ERROR", "MANAGERSYSTEMERROR", "ERROR_CODE_DB_ERROR") return get_response("SUCCESS_MESSAGE_UPDATE_ORDER", "OK") except Exception as e: log.error("update order", e.message) return SYSTEM_ERROR def get_omfilter(self): response = get_response("SUCCESS_MESSAGE_GET_INFO", "OK") response["data"] = [ { "name": "订单号", "value": "", "key": "OMid" }, { "name": "订单状态", "value": cvs.conversion_OMstatus_reverse.keys(), "key": "OMstatus" }, { "name": "下单时间", "value": "", "key": "OMstartTime" }, { "name": "下单时间", "value": "", "key": "OMendTime" }, { "name": "物流方式", "value": ["顺丰速运"], "key": "OMlogisticsName" }, { "name": "商品名称", "value": "", "key": "PRname" }, ] return response def get_order_situation(self): args = request.args.to_dict() log.info("args", args) if "token" not in args: return PARAMS_MISS from ManagerSystem.models.model import OrderMain days = int(args.get("days", 1)) maid = token_to_usid(args.get("token")) prid_list = self.sproduct.get_product_by_maid(maid) pbid_list = [] for pr in prid_list: pbid_list.extend( [pb.PBid for pb in self.sproduct.get_pbid_by_prid(pr.PRid)]) OMid_list = [] for pbid in pbid_list: OMid_list.extend([ op.OMid for op in self.sorder.get_order_part_list_by_pbid(pbid) ]) OMid_list = {}.fromkeys(OMid_list).keys() ordered_count = 0 # 下单总数 paying_count = 0 # 待付款 deliver_count = 0 # 待发货 received_count = 0 # 已收货 omprice = 0 # 订单总收入 refund_count = 0 # 退款中 week_payed_list = [] week_paying_list = [] for omid in OMid_list: om = self.sorder.get_om_by_filter({OrderMain.OMid == omid})[0] if om.OMtime > TimeManager.get_forward_time(days=-days): if om.OMstatus >= 56: refund_count += 1 if om.OMstatus >= 14: ordered_count += 1 omprice += om.OMprice elif om.OMstatus != 0: paying_count += 1 if om.OMstatus == 21: deliver_count += 1 if om.OMstatus >= 28: received_count += 1 for i in range(1, days + 1): if om.OMtime == TimeManager.get_forward_time(days=-i): if om.OMstatus >= 14: if len(week_payed_list) < i: week_payed_list.append(1) else: week_payed_list[i - 1] += 1 if om.OMstatus >= 21: if len(week_paying_list) < i: week_paying_list.append(1) else: week_paying_list[i - 1] += 1 else: if len(week_payed_list) < i: week_payed_list.append(0) else: week_payed_list[i - 1] += 0 if len(week_paying_list) < i: week_paying_list.append(0) else: week_paying_list[i - 1] += 0 # payed_filter = { # OrderMain.OMtime< TimeManager.get_forward_time(days=-days), # OrderMain.OMstatus >= 21, # } # payed_count = self.sorder.get_count_by_or_filter("model.OrderMain.OMid", payed_filter, set()) # omlist = self.sorder.get_om_by_filter(payed_filter) # omprice = 0 # for om in omlist: # omprice += om.OMprice # paying_filter = { # OrderMain.OMtime < TimeManager.get_forward_time(days=-days), # OrderMain.OMstatus < 21, # OrderMain.OMstatus != 0, # } # paying_count = self.sorder.get_count_by_or_filter("model.OrderMain.OMid", paying_filter, set()) # deliver_filter = { # OrderMain.OMstatus == 21, # } # deliver_count = self.sorder.get_count_by_or_filter("model.OrderMain.OMid", deliver_filter, set()) # week_payed_filter = { # OrderMain.OMtime < TimeManager.get_forward_time(days=-7), # OrderMain.OMstatus >= 21 # # } # week_payed_count = self.sorder.get_count_by_or_filter("model.OrderMain.OMid", week_payed_filter, set()) # week_paying_filter = { # OrderMain.OMtime < TimeManager.get_forward_time(days=-days), # OrderMain.OMstatus < 21, # OrderMain.OMstatus != 0, # } # week_paying_count = self.sorder.get_count_by_or_filter("model.OrderMain.OMid", week_paying_filter, set()) # week_payed_list = [] # week_paying_list = [] # for i in range(1, 8): # day_payed_filter = { # OrderMain.OMtime < TimeManager.get_forward_time(days=-i), # OrderMain.OMstatus >= 21 # } # count = self.sorder.get_count_by_or_filter(day_payed_filter) # week_payed_list.append(count) # day_paying_filter = { # OrderMain.OMtime < TimeManager.get_forward_time(days=-i), # OrderMain.OMstatus < 21, # OrderMain.OMstatus != 0, # } # count = self.sorder.get_count_by_or_filter(day_paying_filter) # week_paying_list.append(count) response = get_response("SUCCESS_MESSAGE_GET_INFO", "OK") response["data"] = { "ordered_count": ordered_count, "paying_count": paying_count, "deliver_count": deliver_count, "received_count": received_count, "refund_count": refund_count, "week_payed_list": week_payed_list, "week_paying_list": week_paying_list, "omprice": omprice, } return response
def __init__(self): from ManagerSystem.service.SProducts import SProducts from ManagerSystem.service.SStocks import SStocks self.sproduct = SProducts() self.stock = SStocks()
class CProducts(): def __init__(self): from ManagerSystem.service.SProducts import SProducts from ManagerSystem.service.SStocks import SStocks self.sproduct = SProducts() self.stock = SStocks() def get_info_by_id(self): args = request.args.to_dict() log.info("args", args) params_key_list = ["PRid", "token"] for param_key in params_key_list: if param_key not in args: return PARAMS_MISS PRid = args["PRid"] maid = args.get("token") product = self.sproduct.get_product_by_prid(PRid) log.info("product", product) if not product: return SYSTEM_ERROR from ManagerSystem.service.SCategory import SCategory from ManagerSystem.models.model import CategoryBrand category_filter = { CategoryBrand.CTid == product.CTid } category = tolist(SCategory().get_categorybrands_by_filter(category_filter)) log.info("cb list", category) for cb in category: if cb.get("CBvalue"): cb["CBvalue"] = str.split(get_str(cb, "CBvalue"), ",") product_price = [9999999, -1] product_volue = 0 product_price_list =[ pro.PBprice for pro in self.sproduct.get_pbprice_by_prid(PRid)] log.info("product_price_list", product_price_list) if not product_price_list: return SYSTEM_ERROR for row in product_price_list: if row < product_price[0]: product_price[0] = row if row > product_price[1]: product_price[1] = row product_price = [str(i) for i in product_price] prprice = "-".join(product_price) \ if product_price[0] != product_price[1] \ else product_price[0] product_volue_list = [pro.PBsalesvolume for pro in self.sproduct.get_pbvolume_by_prid(PRid)] log.info('product_volue_list', product_volue_list) if not product_volue_list: return SYSTEM_ERROR for row in product_volue_list: product_volue = product_volue + row product_info = {} product_info["PRid"] = PRid product_info["PRprice"] = prprice product_info["PRsalevolume"] = product_volue product_info["PRname"] = product.PRname product_info["PRvideo"] = product.PRvideo product_info["PRfranking"] = product.PRfranking product_info["PRinfo"] = product.PRinfo product_info["PRvideostart"] = product.PRvideostart product_info["PRimage"] = json.loads(product.PRimage) product_info["PRaboimage"] = json.loads(product.PRaboimage) PRbrand = product.PRbrand PRtype = product.PRtype product_info["PRbrand"] = json.loads(PRbrand) product_info["PRtype"] = conversion_PRtype.get(PRtype) brands, brands_key = self.get_brands(PRid) product_info["brands"] = brands product_info["brands_key"] = brands_key response_of_product = get_response("SUCCESS_MESSAGE_GET_PRODUCT", "OK") response_of_product["data"] = { "category": category, "product_info": product_info } return response_of_product def get_brands(self, prid): pblist = tolist(self.sproduct.get_pball_by_prid(prid)) log.info("pblist", pblist) brandskey = [] for pb in pblist: brid = pb.get("BRid") brand = self.sproduct.get_brand_by_brid(brid) pbnumber_list = [st.PBnumber for st in self.stock.get_stocks_by_PBid(pb.get("PBid"))] log.info("pbnumber_list", pbnumber_list) brands = [] while brid != "0": if brand.BRkey not in brandskey: brandskey.append(brand.BRkey) brands.append(brand.BRvalue) brid = brand.BRfromid brand = self.sproduct.get_brand_by_brid(brid) pb["BRands"] = brands pb['PBstatus'] = conversion_PBstatus.get(pb.get("PBstatus")) pb["PBunit"] = conversion_PBunit.get(pb.get("PBunit")) pb["PBnumber"] = sum(pbnumber_list) return pblist, brandskey def get_all(self): args = request.args.to_dict() log.info("args", args) if "token" not in args or "page_size" not in args or "page_num" not in args: return PARAMS_MISS page_size = int(args.get("page_size")) page_num = int(args.get("page_num")) maid = token_to_usid(args.get("token")) from ManagerSystem.models.model import Products pro_fillter = {Products.MAid == maid} sub_filter = set() if args.get("product_filter"): product_filter = get_str(args, "product_filter") sub_filter = { Products.PRname.like("%{0}%".format(product_filter)), Products.PRinfo.like("%{0}%".format(product_filter)) } pn, count = self.check_page_value(page_num, page_size, "model.Products.PRid", pro_fillter, sub_filter) start_num = (pn - 1) * page_size PRid_list = [products.PRid for products in self.sproduct.get_all_prid(start_num, page_size, pro_fillter, sub_filter)] log.info("PRid list", PRid_list) pbstatus = get_str(args, "PBstatus") # todo 增加遍历输出所有图片 # htv = float(args.get("htv", 0.48)) # from ManagerSystem.common.Gethdp import get_hdp # hdp = get_hdp(htv) product_infos = [] for PRid in PRid_list: product = todict(self.sproduct.get_product_by_prid(PRid)) log.info("product", product) if not product: return SYSTEM_ERROR product["PRid"] = PRid product["PRimage"] = json.loads(product.get("PRimage")) product["PRaboimage"] = json.loads(product.get("PRaboimage")) product["PRtype"] = conversion_PRtype.get(get_str(product, "PRtype")) product["PRtime"] = TimeManager.get_web_time_str(product.get("PRtime")) product["PRbrand"] = json.loads(product.get("PRbrand")) log.info("pbstatus", pbstatus) saleamount = 0 stockamout = 0 if not pbstatus: pb_list = tolist(self.sproduct.get_pball_by_prid(PRid)) else: pb_list = tolist(self.sproduct.get_pball_by_prid_pbstatus(PRid, conversion_PBstatus_reverse.get(pbstatus))) PRstatus = "" for pb in pb_list: saleamount += int(pb.get("PBsalesvolume")) PRstatus = conversion_PBstatus.get(pb.get("PBstatus")) stockamout += sum([int(st.get("PBnumber", 0)) for st in tolist(self.stock.get_stocks_by_PBid(pb.get("PBid")))]) pb.update(product) if not PRstatus: count -= 1 continue # product_infos.extend(pb_list) product["PRsalesvolume"] = saleamount product["PRstock"] = stockamout product["PRstatus"] = PRstatus product_infos.append(product) response_of_product = get_response("SUCCESS_MESSAGE_GET_INFO", "OK") response_of_product["data"] = { "count": count, "page_num": page_num, "page_size": page_size, "products": product_infos } log.info("response", response_of_product) return response_of_product def check_page_value(self, page_num, page_size, model_name, and_params, or_params): count = self.sproduct.get_count_by_or_filter(model_name, and_params, or_params) if page_size * page_num > count: page_num = count / page_size + 1 page_num = page_num if page_num > 0 else 1 return page_num, count def get_control_brand_by_prid(self): args = request.args.to_dict() log.info('args', args) data = request.data data = json.loads(data) log.info("data", data) if "token" not in args or "PRid" not in args: return PARAMS_MISS if data == {}: return PARAMS_MISS BRid = self.sproduct.get_brid_by_prid(args["PRid"]) log.info("BRid", BRid) if not BRid: return SYSTEM_ERROR key_list = [] brid = BRid[0] while brid != "0": Brand = self.sproduct.get_brand_by_brid(brid) brid, BRkey, BRvalue = Brand.BRfromid, Brand.BRkey, Brand.BRvalue key_list.append(BRkey) log.info("key list", key_list) BRid_list = self.sproduct.get_brid_by_prid(args["PRid"]) brid_list = self.sproduct.get_brid_by_prid(args["PRid"]) log.info("BRid list", BRid_list) i = len(BRid_list) while i > 0: raw = BRid_list[i - 1] row = BRid_list[i - 1] log.info("row", row) while row != "0": brand = self.sproduct.get_brand_by_brid(row) log.info("brand", brand) row, BRkey, BRvalue = brand.BRfromid, brand.BRkey, brand.BRvalue if BRkey in data.keys() and data[BRkey] != BRvalue: BRid_list.remove(raw) break i = i - 1 log.info("BRid list removed", BRid_list) back_data = {} for key in key_list: back_data.keys().append(key) back_data[key] = [] for BRid in BRid_list: brand = self.sproduct.get_brand_by_brid(BRid) BRkey, BRvalue = brand.BRkey, brand.BRvalue if BRkey == key and BRvalue not in back_data[key]: back_data[key].append(BRvalue) else: while BRid != "0": brand_parent = self.sproduct.get_brand_by_brid(BRid) BRid, BRkey, BRvalue = brand_parent.BRfromid, brand_parent.BRkey, brand_parent.BRvalue if BRvalue not in back_data[key] and BRkey == key: back_data[key].append(BRvalue) key_list_control = data.keys() i = len(key_list_control) j = 0 control = [] while i > 0: control.append(self.get_m_by_n(key_list_control[i - 1], key_list_control, data, brid_list, i - 1)) back_data[key_list_control[i - 1]] = control[j] #print key_list_control[i - 1] i = i - 1 j = j + 1 response = get_response("SUCCESS_MESSAGE_GET_INFO", "OK") response["data"] = back_data return response def get_pbid_by_all_brand(self): args = request.args.to_dict() print "=================args=================" print args print "=================args=================" data = request.data data = json.loads(data) print "=================data=================" print data print "=================data=================" if "token" not in args or "PRid" not in args: return PARAMS_MISS if data == {}: return PARAMS_MISS BRid = self.sproduct.get_brid_by_prid(args["PRid"]) print "=================BRid=================" print BRid print "=================BRid=================" if not BRid: return SYSTEM_ERROR key_list = [] brid = BRid[0] while brid != "0": Brand = self.sproduct.get_brand_by_brid(brid) brid, BRkey, BRvalue = Brand.BRfromid, Brand.BRkey, Brand.BRvalue key_list.append(BRkey) print "=================key_list=================" print key_list print "=================key_list=================" BRid_list = self.sproduct.get_brid_by_key_value(key_list[0], data[key_list[0]]) print "=================BRid_list=================" print BRid_list print "=================BRid_list=================" i = len(BRid_list) while i > 0: row = BRid_list[i - 1] raw = row while row != "0": brand = self.sproduct.get_brand_by_brid(row) print "=================brand=================" print brand print "=================brand=================" row, BRkey, BRvalue = brand.BRfromid, brand.BRkey, brand.BRvalue if data[BRkey] != BRvalue: BRid_list.remove(raw) i = i - 1 print "=================BRid_list=================" print BRid_list print "=================BRid_list=================" if len(BRid_list) != 1: return SYSTEM_ERROR BRid = BRid_list[0] pball = self.sproduct.get_pball_by_brid(BRid) print "=================pball=================" print pball print "=================pball=================" data = {} data["PBid"] = pball.PBid data["PBimage"] = pball.PBimage PBunit = pball.PBunit if PBunit == 401: data["PBunit"] = "$" elif PBunit == 402: data["PBunit"] = "¥" elif PBunit == 403: data["PBunit"] = "欧元" elif PBunit == 404: data["PBunit"] = "英镑" else: data["PBunit"] = "其他币种" data["PBprice"] = pball.PBprice data["PBsalesvolume"] = pball.PBsalesvolume data["PBscore"] = pball.PBscore response = get_response("SUCCESS_MESSAGE_GET_INFO", "OK") response["data"] = data return response def get_m_by_n(self, key, key_list, brands, brid_list, index): # 首先移除需要判断的key #key_list.remove(key) key_list[index] = 0 len_brid_list = len(brid_list) # 倒序循环,移除不合适的,放跳位 while len_brid_list > 0: # 备份一个brid,用于remove raw = brid_list[len_brid_list - 1] row = brid_list[len_brid_list - 1] log.info("raw", raw) # 循环,直至找到根节点的brid while row != "0": # 获取父节点和当前节点的key、value brand = self.sproduct.get_brand_by_brid(row) print "=================brand=================" print brand print "=================brand=================" # 替代当前值 row, BRkey, BRvalue = brand.BRfromid, brand.BRkey, brand.BRvalue # 判断,如果存在一个key对应的value和实际brands中key对应的value不同,移除当前的brid if BRkey in key_list and brands[BRkey] != BRvalue: brid_list.remove(raw) print "=================BRid_list_remove=================" print brid_list print "=================BRid_list_remove=================" len_brid_list = len_brid_list - 1 # 设置最后要返回的可选值 control_key = [] # 利用已经筛选好的brid_list进行处理 for BRid in brid_list: # 获取key和value brand = self.sproduct.get_brand_by_brid(BRid) BRkey, BRvalue = brand.BRkey, brand.BRvalue # 如果key属于设定的key且value不存在于要返回的list中,那么将value添加进该list if BRkey == key and BRvalue not in control_key: control_key.append(BRvalue) # 否则循环寻找父节点,重复判断逻辑,直到找到对应的key else: while BRid != "0": brand_parent = self.sproduct.get_brand_by_brid(BRid) BRid, BRkey, BRvalue = brand_parent.BRfromid, brand_parent.BRkey, brand_parent.BRvalue if BRvalue not in control_key and BRkey == key: control_key.append(BRvalue) key_list[index] = key return control_key def add_product(self): args = request.args.to_dict() log.info("args", args) data = json.loads(request.data) log.info("data", data) if "token" not in args: return PARAMS_MISS # if "PRid" not in data: # return PARAMS_MISS try: maid = token_to_usid(args.get("token")) except Exception as e: return TOKEN_ERROR # 库存 prid = data.get("PRid") # todo 拆分更新 prid = prid if prid else str(uuid.uuid1()) # try: if not self.add_brands(prid, data.get("brands"), data.get("brands_key")): return get_response("ERROR_MESSAGE_WRONG_BRNADS", "MANAGERSYSTEMERROR", "ERROR_CODE_WRONG_BRANDS") product = { "PRid": prid, "PRname": data.get("PRname", ""), "PRvideo": data.get("PRvideo", ""), "PRimage": json.dumps(data.get("PRimage", [])), "PRaboimage": json.dumps(data.get("PRaboimage", "")), "PRinfo": data.get("PRinfo", ""), "PRvideostart": data.get("PRvideostart", ""), "PRfranking": data.get("PRfranking", 0.0), "MAid": maid, "PRtype": conversion_PRtype_reverse.get(get_str(data, "PRtype", "自营")), "PRbrand": json.dumps(data.get("PRbrand")), "PRtime": TimeManager.get_db_time_str(), "CTid": data.get("CTid") } self.sproduct.add_model("Products", **product) self.add_approval(maid, prid) return get_response("SUCCESS_MESSAGE_ADD_DATA", "OK") # except Exception as e: # log.error("add pruduct error", e.message) # return SYSTEM_ERROR def update_product_status(self): args = request.args.to_dict() log.info("args", args) maid = token_to_usid(args.get("token")) if "token" not in args: return PARAMS_MISS data = json.loads(request.data, encoding="utf8") log.info("data", data) if "PRstatus" not in data or "PRid" not in data: return PARAMS_MISS prstatus = get_str(data, "PRstatus") pridlist = data.get("PRid") self.on_or_off_shelves(pridlist, conversion_PBstatus_reverse.get(prstatus), maid) return get_response("SUCCESS_MESSAGE_UPDATE_DATA", "OK") def on_or_off_shelves(self, prid_list, PBstatus, maid): for prid in prid_list: pbid_list = [pb.PBid for pb in self.sproduct.get_pbid_by_prid(prid)] for pbid in pbid_list: pb = { "PBstatus": PBstatus } # todo 增加审批流 update_result =self.sproduct.update_pb(pbid, pb) log.info("update result", update_result) if not update_result: raise Exception("update pb failed") if PBstatus == 205: self.add_approval(maid, prid) def add_approval(self, maid, prid): from ManagerSystem.service.SApproval import SApproval sapproval = SApproval() receive_list = sapproval.get_permission_by_petype_pesublevel(304, 1) apname = "商品发布审批" + str(uuid.uuid1()) for pe in receive_list: approval = { "APid": str(uuid.uuid1()), "APname": apname, "APstart": maid, "APreceive": pe.MAid, "PEtype": 304, "APcontent": prid, "APremark": "", "APstatus": 443, "APtime": TimeManager.get_db_time_str() } sapproval.add_model("Approval", **approval) def update_pro_info(self): args = request.args.to_dict() log.info("args", args) data = json.loads(request.data, encoding="utf8") log.info("data", data) if "token" not in args: return PARAMS_MISS if "PRid" not in data: return PARAMS_MISS prid = data.get("PRid") product = { "PRimage": json.dumps(data.get("PRimage", [])), "PRvideo": data.get("PRvideo", ""), "PRfranking": data.get("PRfranking", 0), "PRPoint": data.get("PRPoint", ""), "PRtime": TimeManager.get_db_time_str(), } if data.get("PRbrand"): product["PRbrand"] = json.dumps(data.get("PRbrand")) try: update_result = self.sproduct.update_product(prid, product) log.info("update result", update_result) if not update_result: return get_response("ERROR_MESSAGE_DB_ERROR", "MANAGERSYSTEMERROR", "ERROR_CODE_DB_ERROR") # brid_list = [br.BRid for br in self.sproduct.get_brid_by_prid(prid)] # while True: # fromid_list = [] # if not brid_list: # break # for brid in brid_list: # fromid = self.sproduct.get_brfromid_by_brid(brid) # if fromid != "0": # fromid_list.append(fromid) # self.sproduct.del_brand(brid) # brid_list = {}.fromkeys(fromid_list).keys() delete_result = self.sproduct.delete_pb(prid) log.info("delete result", delete_result) self.add_brands(prid, data.get("brands"), data.get("brands_key")) return get_response("SUCCESS_MESSAGE_UPDATE_DATA", "OK") except Exception as e: log.error("update product", e.message) return SYSTEM_ERROR def add_brands(self, prid, brands, brands_key): if not isinstance(brands, list): return False for i in brands: brid = "" brand_values = i.get("BRands") if len(brand_values) != len(brands_key): return False BRfromid = 0 for index, key in enumerate(brands_key): brandid = [br.BRid for br in self.sproduct.get_brid_by_key_value(key, brand_values[index])] if brandid: if not BRfromid: if len(brandid) != 1: return False BRfromid = brandid[0] else: BRfromid_tmp = [brfrom for brfrom in brandid if self.sproduct.get_brfromid_by_brid(brfrom) == BRfromid] if BRfromid_tmp: BRfromid = BRfromid_tmp[0] else: brid = str(uuid.uuid1()) self.sproduct.add_model("Brands", **{ "BRid": brid, "BRfromid": BRfromid, "BRvalue": brand_values[index], "BRkey": key }) BRfromid = brid else: brid = str(uuid.uuid1()) self.sproduct.add_model("Brands", **{ "BRid": brid, "BRfromid": BRfromid, "BRvalue": brand_values[index], "BRkey": key }) BRfromid = brid if not brid: brid = BRfromid from ManagerSystem.config.conversion import conversion_PBstatus_reverse stid = str(uuid.uuid1()) pbid = str(uuid.uuid1()) self.stock.add_model("Stocks", STid=stid, STname="库存"+prid, STamout=10 * 10 * 10 * 10 * 10 * 10) self.stock.add_model("StocksProducts", SPid=str(uuid.uuid1()), STid=stid, PBid=pbid, PBnumber=i.get("PBnumber")) self.sproduct.add_model("ProductsBrands", **{ "PBid": pbid, "PRid": prid, "BRid": brid, "PBprice": i.get("PBprice"), "PBunit": conversion_PBunit_reverse.get(get_str(i, "PBunit", "$")), "PBimage": i.get("PBimage"), "PBstatus": conversion_PBstatus_reverse.get(get_str(i, "PBstatus", "在售状态")), "PBsalesvolume": 0, "PBscore": 0, }) return True def get_prid(self): prid = str(uuid.uuid1()) response = get_response("SUCCESS_MESSAGE_GET_INFO", "OK") response["data"] = prid return response
def get_situation(self): args = request.args.to_dict() log.info("args", args) if "token" not in args: return PARAMS_MISS maid = token_to_usid(args.get("token")) try: manager = self.smanager.get_manager_by_maid(maid) if not manager: return SYSTEM_ERROR dealing_sum = 0 approvaling_sum = 0 deliver_sum = 0 ordered_sum = 0 paid_num = 0 ordered_price_sum = 0 payed_price_sum = 0 active_sum = 0 couponse_sum = 0 today_omprice = 0 today_vistor = 0 today_order = 0 today_payed = 0 yesterday_omprice = 0 yesterday_vistor = 0 yesterday_order = 0 yesterday_payed = 0 time_filter_week = TimeManager.get_forward_time(days=-7) time_filter_yesterday = TimeManager.get_forward_time( days=-1)[:8] + "000000" time_filter_today = TimeManager.get_db_time_str()[:8] + "000000" from ManagerSystem.service.SApproval import SApproval sapproval = SApproval() approval = [ apr.APname for apr in sapproval.get_approval_by_maid(maid) ] approval = {}.fromkeys(approval).keys() approvaling_sum = len(approval) from ManagerSystem.service.SOrder import SOrder from ManagerSystem.service.SProducts import SProducts from ManagerSystem.config.conversion import conversion_OMstatus, conversion_OMstatus_reverse sorder = SOrder() sproduct = SProducts() prid_list = [ product.PRid for product in sproduct.get_product_by_maid(maid) ] pbid_list = [] for prid in prid_list: pbid_list.extend( [pb.PBid for pb in sproduct.get_pbid_by_prid(prid)]) omid_list = [] for pbid in pbid_list: omid_list.extend([ op.OMid for op in sorder.get_order_part_list_by_pbid(pbid) ]) omid_list = {}.fromkeys(omid_list).keys() om_count = {k: 0 for k in conversion_OMstatus} for omid in omid_list: om = sorder.get_order_main_by_om_id(omid) om_count[om.OMstatus] += 1 if om.OMtime > time_filter_week: if om.OMstatus >= 21: paid_num += 1 payed_price_sum += om.OMprice if om.OMstatus >= 7: ordered_sum += 1 ordered_price_sum += om.OMprice if om.OMtime[:8] == time_filter_today[:8]: if om.OMstatus >= 21: today_omprice += om.OMprice today_payed += 1 if om.OMstatus >= 7: today_order += 1 elif om.OMtime[:8] == time_filter_yesterday[:8]: if om.OMstatus >= 21: yesterday_payed += 1 yesterday_omprice += om.OMprice if om.OMstatus >= 7: yesterday_order += 1 # om_count_result = {} # for status in om_count: # om_count_result[conversion_OMstatus.get(status)] = om_count.get(status, 0) deliver_sum = om_count.get(21) dealing_sum = deliver_sum + approvaling_sum from ManagerSystem.service.SActive import SActive from ManagerSystem.models.model import CouponsManager from ManagerSystem.models.model import CouponsActives sactive = SActive() cmlist = [ cm.COid for cm in sactive.get_cm_by_filter( {CouponsManager.MAid == maid}, set()) ] cmlist = {}.fromkeys(cmlist).keys() for coid in cmlist: ca = sactive.get_actives_by_filter( { CouponsActives.COid == coid, CouponsActives.COstatus == 551 }, set()) if ca: ca = ca[0] if ca.COgenre == 561: couponse_sum += 1 elif ca.COgenre == 562: active_sum += 1 visitors_sum = len( self.smanager.get_visitors_by_maid(maid, time_filter_week)) today_vistor = len( self.smanager.get_visitors_by_maid(maid, time_filter_today)) yesterday_vistor = len( self.smanager.get_visitors_by_maid(maid, time_filter_yesterday)) from decimal import Decimal vto = (Decimal(ordered_sum) / (Decimal(visitors_sum))).quantize( Decimal('0.00')) if visitors_sum != 0 else 0 vtp = (Decimal(paid_num) / (Decimal(visitors_sum))).quantize( Decimal('0.00')) if visitors_sum != 0 else 0 otp = (Decimal(paid_num) / (Decimal(ordered_sum))).quantize( Decimal('0.00')) if ordered_sum != 0 else 0 pct = (Decimal(payed_price_sum) / (Decimal(paid_num))).quantize( Decimal("0.00")) if paid_num != 0 else 0 response = get_response("SUCCESS_MESSAGE_GET_INFO", "OK") response["data"] = { "deliver_sum": deliver_sum, "approvaling_sum": approvaling_sum, "dealing_sum": dealing_sum, "om_count": om_count, "active_sum": active_sum, "couponse_sum": couponse_sum, "vtp": float(vtp), "vto": float(vto), "otp": float(otp), "today_vistor": today_vistor, "today_omprice": today_omprice, "today_order": today_order, "today_payed": today_payed, "yesterday_omprice": yesterday_omprice, "yesterday_vistor": yesterday_vistor, "yesterday_order": yesterday_order, "yesterday_payed": yesterday_payed, "paid_num": paid_num, "ordered_sum": ordered_sum, "visitors_sum": visitors_sum, "payed_price_sum": payed_price_sum, "ordered_price_sum": ordered_price_sum, "pct": float(pct), } return response except Exception as e: log.error("get situation", e.message) return SYSTEM_ERROR