lqmac 10 months ago
parent a3ce788013
commit 9042626a77
  1. 76
      app/admin/controller/Goods.php
  2. 18
      app/common.php
  3. 15
      app/common/model/Goods.php
  4. 49
      app/job/controller/goods/GoodsAddPrice.php
  5. 86
      app/job/service/goods/GoodsAddPrice.php

@ -19,6 +19,7 @@ use app\store\model\Goods as GoodsModel;
use app\store\model\GoodsCategoryRel;
use app\store\model\GoodsSku;
use app\store\model\goods\Import as ImportModel;
use app\job\controller\goods\GoodsAddPrice as GoodsAddPriceJob;
/**
* 商品管理控制器
@ -132,7 +133,7 @@ class Goods extends Controller
// 商品详情
$model = GoodsModel::detail($goodsId);
// echo "<pre>";
// print_r($this->postForm());
// print_r($model);
// exit();
// 更新记录
if ($model->edit($this->postForm(), $model)) {
@ -144,7 +145,18 @@ class Goods extends Controller
return $this->renderSuccess('更新成功');
}
$goods_sku = GoodsModel::where('goods_id', $goodsId)->where('store_id', 0)->find();
// var_dump($goods_sku->cost_price_min);
// var_dump($goods_sku->markup_rate);
//成本价加价之后的处理
list($cost_price, $profit, $profit_rate) = getGoodsCostAndProfitAndProfitRate($goods_sku->goods_price_min, $goods_sku->cost_price_min, $goods_sku->markup_rate);
$goods_sku->cost_price_min = $cost_price;
$goods_sku->profit = $profit;
$goods_sku->profit_rate = $profit_rate;
// var_dump($cost_price);
// var_dump($profit);
// var_dump($profit_rate);
// exit();
$goods_data = [
'goods_name' => $goods_sku->name,
'content' => $goods_sku->content,
@ -335,36 +347,50 @@ class Goods extends Controller
* @param int $rate [description]
*/
public function addPrice(array $categoryIds, int $rate){
ini_set('memory_limit', '1024M');
set_time_limit(0);
// ini_set('memory_limit', '1024M');
// set_time_limit(0);
$goods = GoodsCategoryRel::whereIn('category_id',$categoryIds)->where('store_id', 0)->select()->toArray();
if (!$goods) {
return $this->renderSuccess('没有需要加价的商品');
}
$goods = GoodsModel::whereIn('goods_id', array_column($goods, "goods_id"))->where('store_id', 0)->select()->toArray();
$goods = GoodsModel::whereIn('goods_id', array_column($goods, "goods_id"))->where('store_id', 0)->where('channel','sjdngys')->select()->toArray();
if (!$goods) {
return $this->renderSuccess('没有需要加价的商品');
}
foreach ($goods as $key => $value) {
$net_price = round($value['goods_price_min'] / (1 - ($rate / 100)), 0);
$profit = (float)$net_price - (float)$value['cost_price_min'];
$profit_rate = (float)$net_price > 0 ? bcmul((string)($profit / (float)$net_price) , (string)100, 2) : 0.00;
$goodsData = [
'goods_price_min' => $net_price,
'goods_price_max' => $net_price,
'line_price_min' => $net_price,
'line_price_max' => $net_price,
'profit_rate' => $profit_rate,
'profit' => $profit,
'update_time' => time()
];
GoodsModel::where('goods_id', $value['goods_id'])->where('store_id', 0)->update($goodsData);
$goodsSkuData = [
'goods_price' => $net_price,
'update_time' => time()
];
GoodsSku::where('goods_id', $value['goods_id'])->where('store_id', 0)->update();
}
GoodsAddPriceJob::dispatch([
'categoryIds' => $categoryIds,
'rate' => $rate,
]);
// 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 $this->renderSuccess('加价成功');
}

@ -594,3 +594,21 @@ function httpRequest($url, $method="GET", $postfields = null, $headers = array()
return json_decode($response, true);
//return array($http_code, $response,$requestinfo);
}
/**
* 获取加价之后的成本价
* [getGoodsCostPrice description]
* @param [type] $cost_price [description]
* @param [type] $rate [description]
* @return [type] [description]
*/
function getGoodsCostAndProfitAndProfitRate($net_price, $cost_price, $rate){
if ($rate > 0) {
$cost_price = round($cost_price / (1 - ($rate / 100)), 0);
}
$profit = (float)$net_price - (float)$cost_price;
$profit_rate = (float)$net_price > 0 ? bcmul(bcdiv((string)$profit, (string)$net_price, 4), (string)100, 2) : 0.00;
return [$cost_price, $profit, $profit_rate];
}

@ -254,7 +254,13 @@ class Goods extends BaseModel
->paginate($listRows);
// 整理列表数据并返回
return $this->setGoodsListData($list);
foreach ($list as &$goods) {
$goods['dic'] = 'admin';
$goods = $this->setGoodsData($goods, null);
}
return $list;
//return $this->setGoodsListData($list);
}
/**
@ -584,7 +590,12 @@ class Goods extends BaseModel
protected function setGoodsData($goodsInfo, callable $callback = null)
{
$channel = Channel::withoutGlobalScope()->where('code', $goodsInfo['channel'])->find();
$goodsInfo['channel_name'] = $channel['alias'] ?? "";
if (isset($goodsInfo['dic']) && $goodsInfo['dic'] == 'admin') {
$goodsInfo['channel_name'] = $channel['name'] ?? "";
} else {
$goodsInfo['channel_name'] = $channel['alias'] ?? "";
}
$goodsInfo['goods_images'] = helper::getArrayColumn($goodsInfo['images'], 'file');
// 商品主图

@ -0,0 +1,49 @@
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\job\controller\goods;
use app\job\service\goods\GoodsAddPrice as GoodsAddPriceService;
use cores\BaseJob;
use cores\traits\QueueTrait;
use cores\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
/**
* 队列任务:商品批量导入
* Class Import
* @package app\job\controller
*/
class GoodsAddPrice extends BaseJob
{
use QueueTrait;
/**
* 消费队列任务:商品导入
* @param array $data 参数 [index队列顺序;totalCount商品总数量;list商品列表;storeId商城ID]
* @return bool 返回结果
* @throws BaseException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function handle(array $data): bool
{
$time = date('H:i:s');
echo "\n ---- GoodsAddPrice ---- {$time} ---- \n";
$service = new GoodsAddPriceService;
return $service->batch($data['categoryIds'], $data['rate']);
}
}

@ -0,0 +1,86 @@
<?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;
}
}
Loading…
Cancel
Save