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

98 lines
3.9 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
*/
6 months ago
public function batch(array $categoryIds, int $rate, array $channels): bool
6 months ago
{
6 months ago
ini_set('memory_limit', '1024M');
set_time_limit(0);
$goods = GoodsModel::alias('g')
->join('goods_category_rel c', 'g.goods_id = c.goods_id')
->where('c.store_id',0)
->where('g.is_delete',0)
->whereIn('c.category_id', $categoryIds)
->whereIn('g.channel',$channels)
->order("g.goods_id asc")
->select()
->toArray();
6 months ago
if (!$goods) {
return true;
}
6 months ago
//更新总后台当前商品的价格
GoodsModel::whereIn('goods_id', array_column($goods, "goods_id"))->update(['markup_rate' => $rate, 'update_time' => time()]);
6 months ago
foreach ($goods as $key => $value) {
//更新origin_goods_id为当前商品id的价格
6 months ago
$goods_list = GoodsModel::where('origin_goods_id', $value['goods_id'])
->where('is_delete', 0)
->field(['goods_id','cost_price_min','goods_price_min','markup_rate'])
->select()
->toArray();
6 months ago
if (!$goods_list) {
continue;
}
6 months ago
$item = $goods_list[0];
if ($item['markup_rate'] > 0) {
$item['cost_price_min'] = round($item['cost_price_min'] * (1 - ($rate / 100)), 0);
6 months ago
}
6 months ago
$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()
];
GoodsModel::whereIn('goods_id', array_column($goods_list, "goods_id"))->update($goodsData);
$goodsSkuData = [
'cost_price' => $cost_price,
'update_time' => time()
];
GoodsSku::whereIn('goods_id', array_column($goods_list, "goods_id"))->update($goodsSkuData);
6 months ago
}
6 months ago
6 months ago
return true;
}
}