324 lines
12 KiB
PHP
324 lines
12 KiB
PHP
<?php
|
|
|
|
namespace app\psi\logic;
|
|
|
|
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\model\psi\product\StoreProductPrice;
|
|
use app\common\model\psi\product\StoreProductPriceList;
|
|
use app\common\model\psi\warehouse\Warehouse;
|
|
use app\common\model\psi\product\StoreProduct;
|
|
use app\common\service\xlsx\StoreProductPriceXlsx;
|
|
use Exception;
|
|
use support\exception\BusinessException;
|
|
use think\facade\Db;
|
|
|
|
|
|
/**
|
|
* StoreProductPrice逻辑
|
|
* Class StoreProductPriceLogic
|
|
* @package app\psi\logic
|
|
*/
|
|
class StoreProductPriceLogic extends BaseLogic
|
|
{
|
|
|
|
|
|
/**
|
|
* @notes 添加
|
|
* @param array $params
|
|
* @author admin
|
|
* @date 2025/04/03 15:19
|
|
*/
|
|
public static function add(array $params)
|
|
{
|
|
$data = [
|
|
'bhoid' => $params['bhoid'],
|
|
'offer_id' => $params['offer_id'],
|
|
'product_id' => $params['product_id'],
|
|
'purchase_price' => $params['purchase_price'],
|
|
'create_time' => time(),
|
|
'update_time' => time(),
|
|
'status' => 0,
|
|
'warehouse_id' => $params['warehouse_id'],
|
|
'warehouse_product_id' => $params['warehouse_product_id'],
|
|
];
|
|
$productPriceRate = self::getProductPriceRate($params['product_id']);
|
|
if (!empty($productPriceRate)) {
|
|
$priceArray = self::setProductPrice($params['purchase_price'], $productPriceRate);
|
|
$data = array_merge($data, $priceArray);
|
|
}
|
|
$data['price_config'] = [];
|
|
$find = StoreProductPrice::where(['offer_id' => $params['id']])->find();
|
|
if ($find) {
|
|
$find->save($data);
|
|
} else {
|
|
StoreProductPrice::create($data);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 编辑
|
|
* @param array $params
|
|
* @return bool
|
|
* @author admin
|
|
* @date 2025/04/03 15:19
|
|
*/
|
|
public static function edit(array $params): bool
|
|
{
|
|
Db::startTrans();
|
|
try {
|
|
$find = StoreProductPrice::where('id', $params['id'])->find();
|
|
if ($find) {
|
|
$productPriceListAttrs = [];
|
|
if ($find['purchase_lv'] != $params['purchase_lv']) {
|
|
$productPriceListAttrs['supply_rate'] = $params['purchase_lv'] * 100;
|
|
}
|
|
if ($find['cost_lv'] != $params['cost_lv']) {
|
|
$productPriceListAttrs['merchant_rate'] = $params['cost_lv'] * 100;
|
|
}
|
|
if ($find['vip_lv'] != $params['vip_lv']) {
|
|
$productPriceListAttrs['vip_rate'] = $params['vip_lv'] * 100;
|
|
}
|
|
if ($find['price_lv'] != $params['price_lv']) {
|
|
$productPriceListAttrs['price_rate'] = $params['price_lv'] * 100;
|
|
}
|
|
if (!empty($productPriceListAttrs)) {
|
|
$id = StoreProductPriceList::where('product_id', $find['product_id'])->value('id');
|
|
if (empty($id)) {
|
|
$productPriceListAttrs = array_merge($productPriceListAttrs, ['product_id' => $find['product_id']]);
|
|
StoreProductPriceList::create($productPriceListAttrs);
|
|
} else {
|
|
StoreProductPriceList::where('id', $id)->update($productPriceListAttrs);
|
|
}
|
|
}
|
|
|
|
$find->save([
|
|
'status' => 1,
|
|
'purchase' => $params['purchase'],
|
|
'purchase_lv' => $params['purchase_lv'],
|
|
'cost' => $params['cost'],
|
|
'cost_lv' => $params['cost_lv'],
|
|
'vip_price' => $params['vip_price'],
|
|
'vip_lv' => $params['vip_lv'],
|
|
'price' => $params['price'],
|
|
'price_lv' => $params['price_lv'],
|
|
'price_config' => $params['price_config'],
|
|
]);
|
|
|
|
// TODO 请求多门店系统,更新商品价格
|
|
// $productPrice = StoreProduct::where('id', $find['product_id'])->value('vip_price');
|
|
// if ($productPrice != $find['vip_price']) {
|
|
// SqlChannelPriceLog($find['product_id'], 43, $productPrice, $find['vip_price']);
|
|
// }
|
|
// StoreProduct::where('id', $find['product_id'])->update([
|
|
// 'purchase' => $find['purchase'],
|
|
// 'cost' => $find['cost'],
|
|
// 'vip_price' => $find['vip_price'],
|
|
// 'price' => $find['vip_price'],
|
|
// 'ot_price' => $find['price']
|
|
// ]);
|
|
// StoreBranchProduct::where('product_id', $find['product_id'])->update([
|
|
// 'purchase' => $find['purchase'],
|
|
// 'cost' => $find['cost'],
|
|
// 'vip_price' => $find['vip_price'],
|
|
// 'price' => $find['vip_price'],
|
|
// 'ot_price' => $find['price']
|
|
// ]);
|
|
//
|
|
// $productSourceLinkInfoLogic = new ProductSourceLinkInfoLogic();
|
|
// $productSourceLinkInfoLogic->storeProductPrice = $find;
|
|
// $productSourceLinkInfoLogic->updateSupplyPrice();
|
|
}
|
|
Db::commit();
|
|
return true;
|
|
} catch (\Throwable $e) {
|
|
Db::rollback();
|
|
throw new BusinessException($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 确认改价
|
|
* @param array $params
|
|
* @return bool
|
|
*/
|
|
public static function enterPrice(array $params): bool
|
|
{
|
|
Db::startTrans();
|
|
try {
|
|
$find = StoreProductPrice::where('id', $params['id'])->find();
|
|
if ($find) {
|
|
$productPriceRate = StoreProductPriceList::whereIn('product_id', $find['product_id'])->findOrEmpty()->toArray();
|
|
$update = ['status' => 1];
|
|
if (!empty($productPriceRate)) {
|
|
$update = self::setProductPrice($find['purchase_price'], $productPriceRate);
|
|
$update['status'] = 1;
|
|
}
|
|
$find->save($update);
|
|
|
|
// TODO 请求多门店系统,更新商品价格
|
|
// $productPrice = StoreProduct::where('id', $find['product_id'])->value('vip_price');
|
|
// if ($productPrice != $find['vip_price']) {
|
|
// SqlChannelPriceLog($find['product_id'], 43, $productPrice, $find['vip_price']);
|
|
// }
|
|
// StoreProduct::where('id', $find['product_id'])->update([
|
|
// 'purchase' => $find['purchase'] ?? 0,
|
|
// 'cost' => $find['cost'] ?? 0,
|
|
// 'vip_price' => $find['vip_price'] ?? 0,
|
|
// 'price' => $find['vip_price'] ?? 0,
|
|
// 'ot_price' => $find['price'] ?? 0,
|
|
// ]);
|
|
// StoreBranchProduct::where('product_id', $find['product_id'])->update([
|
|
// 'purchase' => $find['purchase'] ?? 0,
|
|
// 'cost' => $find['cost'] ?? 0,
|
|
// 'vip_price' => $find['vip_price'] ?? 0,
|
|
// 'price' => $find['vip_price'] ?? 0,
|
|
// 'ot_price' => $find['price'] ?? 0,
|
|
// ]);
|
|
}
|
|
Db::commit();
|
|
return true;
|
|
} catch (\Throwable $e) {
|
|
Db::rollback();
|
|
throw new BusinessException($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @notes 删除
|
|
* @param array $params
|
|
* @return bool
|
|
* @author admin
|
|
* @date 2025/04/03 15:19
|
|
*/
|
|
public static function delete(array $params): bool
|
|
{
|
|
return StoreProductPrice::destroy($params['id']);
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 获取详情
|
|
* @param $params
|
|
* @return array
|
|
* @author admin
|
|
* @date 2025/04/03 15:19
|
|
*/
|
|
public static function detail($params): array
|
|
{
|
|
return StoreProductPrice::findOrEmpty($params['id'])->toArray();
|
|
}
|
|
|
|
/**
|
|
* 设置商品分组价格
|
|
* @param $purchasePrice float 采购价
|
|
* @param $productPriceRate array 价格比例
|
|
* @return array
|
|
*/
|
|
public static function setProductPrice(float $purchasePrice, array $productPriceRate)
|
|
{
|
|
$result = [];
|
|
$result['purchase_lv'] = bcdiv($productPriceRate['supply_rate'], 100, 2);
|
|
$result['purchase'] = bcmul($purchasePrice, $result['purchase_lv'], 2);
|
|
$result['cost_lv'] = bcdiv($productPriceRate['merchant_rate'], 100, 2);
|
|
$result['cost'] = bcmul($result['purchase'], $result['cost_lv'], 2);
|
|
$result['vip_lv'] = bcdiv($productPriceRate['vip_rate'], 100, 2);
|
|
$result['vip_price'] = bcmul($result['purchase'], $result['vip_lv'], 2);
|
|
$result['price_lv'] = bcdiv($productPriceRate['price_rate'], 100, 2);
|
|
$result['price'] = bcmul($result['purchase'], $result['price_lv'], 2);
|
|
$lastNum = substr($result['vip_price'], -1);
|
|
if ($lastNum > 0) {
|
|
$result['vip_price'] = ceil($result['vip_price'] * 10);
|
|
$result['vip_price'] = bcdiv($result['vip_price'], 10, 2);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 导出
|
|
* @param $params
|
|
* @return string
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public static function export($params)
|
|
{
|
|
$query = StoreProductPrice::field('*');
|
|
if (!empty($params['store_name'])) {
|
|
$productIds = StoreProduct::where('store_name', 'like', "%{$params['store_name']}%")->column('id');
|
|
$query->whereIn('product_id', $productIds);
|
|
}
|
|
if ($params['status'] != '') {
|
|
$query->where('status', $params['status']);
|
|
}
|
|
if (!empty($params['start_time'])) {
|
|
$query->where('create_time', '>=', strtotime($params['start_time']));
|
|
}
|
|
if (!empty($params['end_time'])) {
|
|
$query->where('create_time', '<=', strtotime($params['end_time']));
|
|
}
|
|
$data = $query->order('id desc')->select()->toArray();
|
|
$warehouses = Warehouse::field('id,name')->whereIn('id', array_unique(array_column($data, 'warehouse_id')))->select()->toArray();
|
|
$warehouses = reset_index($warehouses, 'id');
|
|
$products = StoreProduct::field('id,store_name')->whereIn('id', array_unique(array_column($data, 'product_id')))->select()->toArray();
|
|
$products = reset_index($products, 'id');
|
|
foreach ($data as &$item) {
|
|
$item['warehouse'] = $warehouses[$item['warehouse_id']]['name'] ?? '';
|
|
$item['store_name'] = $products[$item['product_id']]['store_name'] ?? '';
|
|
}
|
|
return (new StoreProductPriceXlsx())->export($data);
|
|
}
|
|
|
|
/**
|
|
* 商品价格趋势
|
|
* @param $params
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public static function chart($params)
|
|
{
|
|
$list = StoreProductPrice::where('product_id', $params['product_id'])
|
|
->field('id,purchase_price,purchase,create_time')
|
|
->order('id desc')->limit(30)
|
|
->select()->toArray();
|
|
foreach ($list as &$item) {
|
|
$item['date'] = date('m-d', strtotime($item['create_time']));
|
|
if ($params['type'] == 1) {
|
|
$item['price'] = $item['purchase_price'];
|
|
} else {
|
|
$item['price'] = $item['purchase'];
|
|
}
|
|
}
|
|
asort($list);
|
|
return [
|
|
'name' => '价格趋势',
|
|
'series' => [['name' => '价格', 'value' => array_column($list, 'price')]],
|
|
'x' => array_column($list, 'date'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 获取商品价格比例
|
|
* @param $productId
|
|
* @return array|int[]
|
|
*/
|
|
public static function getProductPriceRate($productId)
|
|
{
|
|
$list = StoreProductPriceList::where('product_id', $productId)->findOrEmpty()->toArray();
|
|
if (empty($list)) {
|
|
$list = [
|
|
'supply_rate' => 110,
|
|
'merchant_rate' => 103,
|
|
'vip_rate' => 115,
|
|
'price_rate' => 120,
|
|
];
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
} |