You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/job/service/goods/GoodsAddPrice.php

86 lines
3.6 KiB

6 months ago
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\job\service\goods;
use app\common\library\helper;
use app\common\service\BaseService;
use app\common\model\Goods as GoodsModel;
use app\common\model\GoodsSku;
use app\store\model\GoodsCategoryRel;
use cores\exception\BaseException;
use think\facade\Log;
use app\common\model\Channel;
use app\common\model\Region;
/**
* 服务类:商品批量导入
* Class Import
* @package app\job\service\goods
*/
class GoodsAddPrice extends BaseService
{
/**
* 批量导入商品
* @param array $list
* @param int $recordId
* @param int $storeId
* @return bool
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function batch(array $categoryIds, int $rate): bool
{
$goods = GoodsCategoryRel::whereIn('category_id',$categoryIds)->where('store_id', 0)->select()->toArray();
if (!$goods) {
return true;
}
$goods = GoodsModel::whereIn('goods_id', array_column($goods, "goods_id"))->where('store_id', 0)->where('channel','sjdngys')->select()->toArray();
if (!$goods) {
return true;
}
foreach ($goods as $key => $value) {
//更新总后台当前商品的价格
GoodsModel::where('goods_id', $value['goods_id'])->where('is_delete', 0)->where('store_id', 0)->update(['markup_rate' => $rate, 'update_time' => time()]);
//更新origin_goods_id为当前商品id的价格
$goods_list = GoodsModel::where('origin_goods_id', $value['goods_id'])->where('is_delete', 0)->select();
if (!$goods_list) {
continue;
}
foreach ($goods_list as $item) {
$cost_price = round($item['cost_price_min'] / (1 - ($rate / 100)), 0);
$profit = (float)$item['goods_price_min'] - (float)$cost_price;
$profit_rate = (float)$item['goods_price_min'] > 0 ? bcmul((string)($profit / (float)$item['goods_price_min']) , (string)100, 2) : 0.00;
$goodsData = [
'cost_price_min' => $cost_price,
'profit_rate' => $profit_rate,
'profit' => $profit,
'markup_rate' => $rate,
'update_time' => time()
];
// var_dump($goodsData);
// exit();
GoodsModel::where('goods_id', $item['goods_id'])->where('store_id', $item['store_id'])->update($goodsData);
$goodsSkuData = [
'cost_price' => $cost_price,
'update_time' => time()
];
GoodsSku::where('goods_id', $item['goods_id'])->where('store_id', $item['store_id'])->update($goodsSkuData);
}
}
return true;
}
}