refactor(api): 重构 DemoLogic 中的数据库操作

- 将 Db::name('store_product_group_price') 替换为 StoreProductGroupPrice 模型
- 优化了查询、更新和插入操作的代码结构
- 提高了代码的可读性和维护性
This commit is contained in:
mkm 2025-01-04 23:01:02 +08:00
parent 3310584ca0
commit cf56637d82

View File

@ -4,6 +4,7 @@ namespace app\api\logic;
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use app\common\model\store_product\StoreProduct; use app\common\model\store_product\StoreProduct;
use app\common\model\store_product_group_price\StoreProductGroupPrice;
use think\facade\Db; use think\facade\Db;
class DemoLogic extends BaseLogic class DemoLogic extends BaseLogic
@ -18,53 +19,53 @@ class DemoLogic extends BaseLogic
Db::name('ceshi_two')->where('product_id', $v['product_id'])->update(['status' => 1]); Db::name('ceshi_two')->where('product_id', $v['product_id'])->update(['status' => 1]);
} }
//种养殖 //种养殖
$find = Db::name('store_product_group_price')->where('product_id', $v['product_id'])->where('group_id', 5)->find(); $find = StoreProductGroupPrice::where('product_id', $v['product_id'])->where('group_id', 5)->find();
if ($find) { if ($find) {
Db::name('store_product_group_price')->where('product_id', $v['product_id'])->where('group_id', 5)->update(['price' => bcadd($v['price8'], 0, 2)]); $find->save(['price' => bcadd($v['price8'], 0, 2)]);
} else { } else {
Db::name('store_product_group_price')->insert(['product_id' => $v['product_id'], 'group_id' => 5, 'price' => bcadd($v['price8'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv8'], 100), 100, 2)]); StoreProductGroupPrice::insert(['product_id' => $v['product_id'], 'group_id' => 5, 'price' => bcadd($v['price8'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv8'], 100), 100, 2)]);
} }
//食堂 //食堂
$find2 = Db::name('store_product_group_price')->where('product_id', $v['product_id'])->where('group_id', 7)->find(); $find7 = StoreProductGroupPrice::where('product_id', $v['product_id'])->where('group_id', 7)->find();
if ($find2) { if ($find7) {
Db::name('store_product_group_price')->where('product_id', $v['product_id'])->where('group_id', 7)->update(['price' => bcadd($v['price3'], 0, 2)]); $find7->save(['price' => bcadd($v['price3'], 0, 2)]);
} else { } else {
Db::name('store_product_group_price')->insert(['product_id' => $v['product_id'], 'group_id' => 7, 'price' => bcadd($v['price3'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv3'], 100), 100, 2)]); StoreProductGroupPrice::insert(['product_id' => $v['product_id'], 'group_id' => 7, 'price' => bcadd($v['price3'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv3'], 100), 100, 2)]);
} }
//酒店 //酒店
$find2 = Db::name('store_product_group_price')->where('product_id', $v['product_id'])->where('group_id', 6)->find(); $find6 = StoreProductGroupPrice::where('product_id', $v['product_id'])->where('group_id', 6)->find();
if ($find2) { if ($find6) {
Db::name('store_product_group_price')->where('product_id', $v['product_id'])->where('group_id', 6)->update(['price' => bcadd($v['price3'], 0, 2)]); $find6->save(['price' => bcadd($v['price3'], 0, 2)]);
} else { } else {
Db::name('store_product_group_price')->insert(['product_id' => $v['product_id'], 'group_id' => 6, 'price' => bcadd($v['price3'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv3'], 100), 100, 2)]); StoreProductGroupPrice::insert(['product_id' => $v['product_id'], 'group_id' => 6, 'price' => bcadd($v['price3'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv3'], 100), 100, 2)]);
} }
//一条龙 //一条龙
$find3 = Db::name('store_product_group_price')->where('product_id', $v['product_id'])->where('group_id', 18)->find(); $find3 = StoreProductGroupPrice::where('product_id', $v['product_id'])->where('group_id', 18)->find();
if ($find3) { if ($find3) {
Db::name('store_product_group_price')->where('product_id', $v['product_id'])->where('group_id', 18)->update(['price' => bcadd($v['price5'], 0, 2)]); $find3->save(['price' => bcadd($v['price5'], 0, 2)]);
} else { } else {
Db::name('store_product_group_price')->insert(['product_id' => $v['product_id'], 'group_id' => 18, 'price' => bcadd($v['price5'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv5'], 100), 100, 2)]); StoreProductGroupPrice::insert(['product_id' => $v['product_id'], 'group_id' => 18, 'price' => bcadd($v['price5'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv5'], 100), 100, 2)]);
} }
//厨师 //厨师
$find4 = Db::name('store_product_group_price')->where('product_id', $v['product_id'])->where('group_id', 1)->find(); $find4 = StoreProductGroupPrice::where('product_id', $v['product_id'])->where('group_id', 1)->find();
if ($find4) { if ($find4) {
Db::name('store_product_group_price')->where('product_id', $v['product_id'])->where('group_id', 1)->update(['price' => bcadd($v['price7'], 0, 2)]); $find4->save(['price' => bcadd($v['price7'], 0, 2)]);
} else { } else {
Db::name('store_product_group_price')->insert(['product_id' => $v['product_id'], 'group_id' => 1, 'price' => bcadd($v['price7'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv7'], 100), 100, 2)]); StoreProductGroupPrice::insert(['product_id' => $v['product_id'], 'group_id' => 1, 'price' => bcadd($v['price7'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv7'], 100), 100, 2)]);
} }
//商户会员 //商户会员
$find4 = Db::name('store_product_group_price')->where('product_id', $v['product_id'])->where('group_id', 19)->find(); $find4 = StoreProductGroupPrice::where('product_id', $v['product_id'])->where('group_id', 19)->find();
if ($find4) { if ($find4) {
Db::name('store_product_group_price')->where('product_id', $v['product_id'])->where('group_id', 19)->update(['price' => bcadd($v['price2'], 0, 2)]); $find4->save(['price' => bcadd($v['price2'], 0, 2)]);
} else { } else {
Db::name('store_product_group_price')->insert(['product_id' => $v['product_id'], 'group_id' => 19, 'price' => bcadd($v['price2'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv2'], 100), 100, 2)]); StoreProductGroupPrice::insert(['product_id' => $v['product_id'], 'group_id' => 19, 'price' => bcadd($v['price2'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv2'], 100), 100, 2)]);
} }
//食堂会员 //食堂会员
$find4 = Db::name('store_product_group_price')->where('product_id', $v['product_id'])->where('group_id', 20)->find(); $find20 = StoreProductGroupPrice::where('product_id', $v['product_id'])->where('group_id', 20)->find();
if ($find4) { if ($find20) {
Db::name('store_product_group_price')->where('product_id', $v['product_id'])->where('group_id', 20)->update(['price' => bcadd($v['price4'], 0, 2)]); $find20->save(['price' => bcadd($v['price4'], 0, 2)]);
} else { } else {
Db::name('store_product_group_price')->insert(['product_id' => $v['product_id'], 'group_id' => 20, 'price' => bcadd($v['price4'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv4'], 100), 100, 2)]); StoreProductGroupPrice::insert(['product_id' => $v['product_id'], 'group_id' => 20, 'price' => bcadd($v['price4'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv4'], 100), 100, 2)]);
} }
} }
} }