11-6 商品库存和销量修改

1.库存数量

    def perform_create(self, serializer):
        shop_cart = serializer.save()
        goods = shop_cart.goods
        goods.goods_num -= shop_cart.nums
        goods.save()

    def perform_destroy(self, instance):
        goods = instance.goods
        goods.goods_num += instance.nums
        goods.save()
        instance.delete()

    def perform_update(self, serializer):
        existed_record = ShoppingCart.objects.get(id=serializer.instance.id)
        existed_nums = existed_record.nums
        saved_record = serializer.save()
        nums = saved_record.nums -existed_nums
        goods = saved_record.goods
        goods.goods_num -= nums
        goods.save()

 

 

 

2.销量

                order_goods = existed_order.goods.all()
                for order_good in order_goods:
                    goods = order_good.goods
                    goods.sold_num += order_good.goods_num
                    goods.save()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的零食网店管理系统的代码示例,基于C语言和数据结构知识点: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_ITEM_NUM 100 // 商品列表最大数量 // 商品结构体 typedef struct { int code; // 商品代码 char name[50]; // 商品名称 double price; // 商品价格 int stock; // 库存数量 int sales_volume; // 销售量 } Item; Item items[MAX_ITEM_NUM]; // 商品列表 int item_count = 0; // 商品数量 // 登录函数,返回1表示登录成功,0表示登录失败 int login() { char username[20], password[20]; printf("请输入用户名和密码:\n"); printf("用户名:"); scanf("%s", username); printf("密码:"); scanf("%s", password); // 这里可以添加用户名和密码的验证逻辑 if (strcmp(username, "admin") == 0 && strcmp(password, "123456") == 0) { printf("登录成功!\n"); return 1; } else { printf("用户名或密码错误!\n"); return 0; } } // 显示主菜单 void show_main_menu() { printf("请选择操作:\n"); printf("1. 查看商品信息\n"); printf("2. 插入商品信息\n"); printf("3. 删除商品信息\n"); printf("4. 修改商品信息\n"); printf("5. 查看销量排行\n"); printf("0. 退出\n"); } // 显示商品信息 void show_items() { printf("商品列表:\n"); printf("代码\t名称\t价格\t库存\n"); for (int i = 0; i < item_count; i++) { printf("%d\t%s\t%.2f\t%d\n", items[i].code, items[i].name, items[i].price, items[i].stock); } } // 插入商品信息 void insert_item() { Item item; printf("请输入商品代码、名称、价格和库存数量:\n"); scanf("%d %s %lf %d", &item.code, item.name, &item.price, &item.stock); for (int i = 0; i < item_count; i++) { if (items[i].code == item.code) { printf("商品代码已存在!\n"); return; } } items[item_count++] = item; printf("商品信息已保存!\n"); } // 删除商品信息 void delete_item() { int code; printf("请输入要删除的商品代码:\n"); scanf("%d", &code); for (int i = 0; i < item_count; i++) { if (items[i].code == code) { // 将该商品从列表中删除 for (int j = i; j < item_count - 1; j++) { items[j] = items[j + 1]; } item_count--; printf("商品信息已删除!\n"); return; } } printf("找不到该商品代码!\n"); } // 修改商品信息 void update_item() { int code; printf("请输入要修改商品代码:\n"); scanf("%d", &code); for (int i = 0; i < item_count; i++) { if (items[i].code == code) { printf("请输入新的商品名称、价格和库存数量:\n"); scanf("%s %lf %d", items[i].name, &items[i].price, &items[i].stock); printf("商品信息已修改!\n"); return; } } printf("找不到该商品代码!\n"); } // 比较函数,用于排序 int cmp(const void *a, const void *b) { Item *item_a = (Item*)a; Item *item_b = (Item*)b; return item_b->sales_volume - item_a->sales_volume; } // 查看销量排行 void show_sales_rank() { int type; printf("请选择销量排行类型:\n"); printf("1. 销售量最多的商品\n"); printf("2. 销售额最高的商品\n"); scanf("%d", &type); switch (type) { case 1: // 按销售量排序 for (int i = 0; i < item_count; i++) { int max_index = i; for (int j = i + 1; j < item_count; j++) { if (items[j].sales_volume > items[max_index].sales_volume) { max_index = j; } } if (max_index != i) { Item temp = items[i]; items[i] = items[max_index]; items[max_index] = temp; } } printf("销售量最多的商品:\n"); printf("名称\t销售量\n"); for (int i = 0; i < item_count; i++) { printf("%s\t%d\n", items[i].name, items[i].sales_volume); } break; case 2: // 按销售额排序 for (int i = 0; i < item_count; i++) { items[i].sales_volume = items[i].price * items[i].sales_volume; } qsort(items, item_count, sizeof(Item), cmp); printf("销售额最高的商品:\n"); printf("名称\t销售额\n"); for (int i = 0; i < item_count; i++) { printf("%s\t%.2f\n", items[i].name, items[i].price * items[i].sales_volume); } break; default: printf("无效的输入!\n"); break; } } int main() { if (!login()) { return 0; } int choice; do { show_main_menu(); scanf("%d", &choice); switch (choice) { case 1: show_items(); break; case 2: insert_item(); break; case 3: delete_item(); break; case 4: update_item(); break; case 5: show_sales_rank(); break; case 0: printf("谢谢使用,再见!\n"); break; default: printf("无效的输入!\n"); break; } } while (choice != 0); return 0; } ``` 这个示例代码包括登录界面、主菜单、查看商品信息、插入商品信息、删除商品信息、修改商品信息和查看销量排行等功能,可以作为一个简单的零食网店管理系统的参考实现。当然,这只是一个简单的示例,实际的系统应该根据具体需求进行设计和开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值