def confirm(self, order): api = GetPifaData(self.current_user.passport_id, AuthFunc.gen_token()) ret_dict = api.confirm_pf_demand_order(self.current_user.phone, order.object_id, order.id) if not ret_dict["success"]: return self.send_fail(ret_dict.get("msg", "")) return self.send_success()
def get(self): api = GetPifaData(self.current_user.passport_id, AuthFunc.gen_token()) ret_dict = api.get_pf_shops(self.current_user.phone) if not ret_dict["success"]: return self.send_fail(ret_dict.get("msg", "")) data_list = [] for data in ret_dict["data_list"]: data_list.append({ "shop_id": check_int(PfSimpleEncrypt.decrypt(data.get("shop_id", 0))), "shop_name": data.get("shop_name", ""), "purchase_times": data.get("purchase_times", 0), "latest_order_time": data.get("latest_order_time", ""), "is_online_order_open": data.get("is_online_order_open", 0), }) return self.send_success(data_list=data_list)
def post(self): pf_shop_id = self.args["pf_shop_id"] pf_shop_name = self.args["pf_shop_name"] demand_date = self.args["demand_date"] goods_list = self.args["goods_list"] valid, message = self.validate_goods_list(goods_list) if not valid: return self.send_fail(message) # 添加到本地数据库 new_order = models.ExternalDemandOrder( creator_id=self.current_user.id, target=1, object_id=pf_shop_id, object_name=pf_shop_name, demand_date=demand_date, ) self.session.add(new_order) self.session.flush() demand_list = [] for goods_data in goods_list: goods_name = goods_data["goods_name"] demand_amount = check_float(goods_data["demand_amount"]) demand_unit = check_int(goods_data["demand_unit"]) remarks = goods_data.get("remarks", "") new_order_goods = models.ExternalDemandOrderGoods( creator_id=self.current_user.id, order_id=new_order.id, goods_name=goods_name, demand_amount=check_int(demand_amount * 100), demand_unit=demand_unit, remarks=remarks, ) self.session.add(new_order_goods) self.session.flush() unit_map = { 0: "斤", 1: "件", 2: "kg", 3: "个", 4: "份", 5: "盒", 6: "只", 7: "包" } demand_list.append({ "external_line_id": new_order_goods.id, "name": goods_name, "amount": demand_amount, "unit_text": unit_map.get(demand_unit, "斤"), "remark": remarks, }) api = GetPifaData(self.current_user.passport_id, AuthFunc.gen_token()) ret_dict = api.send_pf_demand_order(self.current_user.phone, pf_shop_id, new_order.id, demand_date, demand_list) if not ret_dict["success"]: return self.send_fail(ret_dict.get("msg", "")) self.session.commit() return self.send_success()
def get(self, order_id): order = self.session.query(models.ExternalDemandOrder) \ .filter(models.ExternalDemandOrder.id == order_id, models.ExternalDemandOrder.creator_id == self.current_user.id) \ .first() if not order: return self.send_fail("没有找到该订货单") goods_list = order.goods_list.all() # 从批发获取订单详情 api = GetPifaData(self.current_user.passport_id, AuthFunc.gen_token()) ret_dict = api.get_pf_demand_order(self.current_user.phone, order.object_id, order.id) if not ret_dict["success"]: return self.send_fail(ret_dict.get("msg", "")) pf_order_data = ret_dict.get("data", {}) pf_goods_dict = { goods_data.get("external_id"): goods_data for goods_data in pf_order_data.get("demand_lines", []) } demand_goods_list = [] for goods in goods_list: # 同步批发订单信息 pf_goods = pf_goods_dict.get(goods.id) if not goods.syncronized and pf_goods: sale_record_info = pf_goods.get("sale_record_info", {}) goods.confirmed_amount = check_int( sale_record_info.get("amount", 0) * 100) goods.confirmed_unit = check_int( sale_record_info.get("unit", 0)) goods.price = check_int( sale_record_info.get("unit_price", 0) * 100) goods.total_money = check_int( sale_record_info.get("sales_money", 0) * 100) # goods.syncronized = 1 demand_goods_list.append({ "id": goods.id, "goods_name": goods.goods_name, "demand_amount": check_float(goods.demand_amount / 100), "demand_unit": goods.demand_unit, "remarks": goods.remarks, "confirmed_amount": check_float(goods.confirmed_amount / 100), "confirmed_unit": goods.confirmed_unit, "price": check_float(goods.price / 100), "total_money": check_float(goods.total_money / 100), }) data = { "shop_id": order.object_id, "shop_name": order.object_name, "demand_date": TimeFunc.time_to_str(order.demand_date, "date"), "create_time": TimeFunc.time_to_str(order.create_time), "status": order.status, "total_money": check_float(pf_order_data.get("order_money", 0) / 100), "goods_list": demand_goods_list, } self.session.commit() return self.send_success(data=data)