feat: 优化了商品库存和订单处理的逻辑

This commit is contained in:
mkm 2024-08-25 16:26:52 +08:00
parent 9fca148539
commit 807b4cacd6
3 changed files with 30 additions and 111 deletions

View File

@ -30,9 +30,7 @@ class StoreBranchProductLogic extends BaseLogic
{
Db::startTrans();
try {
StoreBranchProduct::create([
]);
StoreBranchProduct::create([]);
Db::commit();
return true;
@ -52,19 +50,19 @@ class StoreBranchProductLogic extends BaseLogic
*/
public static function edit(array $params): bool
{
$StoreProduct=StoreBranchProduct::where('id', $params['id'])->find();
if($params['status']==1 &&$StoreProduct['price']==0){
$StoreProduct = StoreBranchProduct::where('id', $params['id'])->find();
if ($params['status'] == 1 && $StoreProduct['price'] == 0) {
self::setError('商品价格不能为0,无法上架');
return false;
}
Db::startTrans();
try {
$data=[
'store_name'=>$params['store_name'],
'image'=>$params['image'],
'price'=>$params['price'],
'unit'=>$params['unit'],
'status'=>$params['status'],
$data = [
'store_name' => $params['store_name'],
'image' => $params['image'],
'price' => $params['price'],
'unit' => $params['unit'],
'status' => $params['status'],
];
StoreBranchProduct::where('id', $params['id'])->update($data);
Db::commit();
@ -82,24 +80,23 @@ class StoreBranchProductLogic extends BaseLogic
* @author admin
* @date 2024/06/07 13:56
*/
public static function stock(array $params,$type=1): bool
public static function stock(array $params, $type = 1): bool
{
Db::startTrans();
try {
if($type==1){
StoreBranchProduct::where('id', $params['id'])->inc('stock',$params['nums'])->update();
$find=StoreProduct::where('id', $params['product_id'])->find();
if($find){
$stock=bcadd($find['stock'],$params['nums'],2);
StoreProduct::where('id', $params['product_id'])->update(['stock'=>$stock,'total_price'=>bcmul($stock,$find['purchase'],2)]);
}
}else{
StoreBranchProduct::where('id', $params['id'])->dec('stock',$params['nums'])->update();
$find=StoreProduct::where('id', $params['product_id'])->find();
if($find){
$stock=bcsub($find['stock'],$params['nums'],2);
StoreProduct::where('id', $params['product_id'])->update(['stock'=>$stock,'total_price'=>bcmul($stock,$find['purchase'],2)]);
}
$find = StoreProduct::where('id', $params['product_id'])->find();
$storeBranchProduct = StoreBranchProduct::where('id', $params['id'])->find();
if ($type == 1) {
$stock = bcadd($find['stock'], $params['nums'], 2);
$branchStock = bcadd($storeBranchProduct['stock'], $params['nums'], 2);
StoreBranchProduct::where('id', $params['id'])->update(['stock' => $stock, 'total_price' => bcmul($branchStock, $find['purchase'], 2)]);
StoreProduct::where('id', $params['product_id'])->update(['stock' => $stock, 'total_price' => bcmul($stock, $find['purchase'], 2)]);
} else {
$branchStock = bcsub($storeBranchProduct['stock'], $params['nums'], 2);
$stock = bcsub($find['stock'], $params['nums'], 2);
StoreBranchProduct::where('id', $params['id'])->update(['stock' => $stock, 'total_price' => bcmul($branchStock, $find['purchase'], 2)]);
StoreProduct::where('id', $params['product_id'])->update(['stock' => $stock, 'total_price' => bcmul($stock, $find['purchase'], 2)]);
}
Db::commit();
return true;
@ -117,8 +114,8 @@ class StoreBranchProductLogic extends BaseLogic
*/
public static function delete(array $params): bool
{
$stock=StoreBranchProduct::where('id', $params['id'])->value('stock');
if($stock>0){
$stock = StoreBranchProduct::where('id', $params['id'])->value('stock');
if ($stock > 0) {
self::setError('商品库存不为0,无法删除');
return false;
}
@ -138,4 +135,4 @@ class StoreBranchProductLogic extends BaseLogic
{
return StoreBranchProduct::findOrEmpty($params['id'])->toArray();
}
}
}

View File

@ -48,7 +48,7 @@ class StoreOrderLogic extends BaseLogic
$cartId[] = $res['id'];
}
$user = User::where('id', $params['user_id'])->find();
$params['shipping_type']=4;
$params['shipping_type']=2;
$params['pay_type'] = 7;
$order = OrderLogic::createOrder($cartId, null, $user, $params);
if (OrderLogic::hasError()) {

View File

@ -2,6 +2,7 @@
namespace app\api\controller;
use app\admin\logic\store_branch_product\StoreBranchProductLogic;
use app\common\logic\StoreFinanceFlowLogic;
use app\admin\logic\store_product\StoreProductLogic;
use app\admin\validate\tools\GenerateTableValidate;
@ -13,8 +14,10 @@ use app\common\model\Config as ModelConfig;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_finance_flow\StoreFinanceFlow;
use app\common\model\store_order\StoreOrder;
use app\common\model\store_product\StoreProduct;
use app\common\model\system_store\SystemStore;
use app\common\model\user\User;
use app\common\model\warehouse_product\WarehouseProduct;
use app\common\service\pay\PayService;
use app\common\service\PushService;
use app\common\service\wechat\WechatTemplate;
@ -42,87 +45,6 @@ class IndexController extends BaseApiController
public function index()
{
return json([1]);
$financeFlow = new StoreFinanceFlow();
$order_id = 544;
$store_id = 3;
$staff_id = 3;
Db::startTrans();
try {
// $res = $financeFlow->where('order_id', $order_id)->update(['store_id' => $store_id, 'staff_id' => $staff_id]);
// if ($res) {
// $order['store_id'] =$store_id;
// }
$financeFlowLogic = new StoreFinanceFlowLogic();
//other_uid大于0的
$select_1 = $financeFlow->where(['order_id' => $order_id, 'financial_pm' => 1, 'status' => 0])->where('other_uid', '>', 0)->select();
foreach ($select_1 as $k => $v) {
if ($v['other_uid'] > 0) {
$financeFlowLogic->updateStatusUser($v['id'], $v['other_uid'], $v['number'], $v['order_id']);
}
}
$deposit = $financeFlow->where(['order_id' => $order_id, 'financial_pm' => 0, 'financial_type' => 11, 'status' => 0])->value('number') ?? 0;
$money = $financeFlow->where(['order_id' => $order_id, 'financial_pm' => 1, 'financial_type' => 2, 'status' => 0])->value('number') ?? 0;
$financeFlowLogic->updateStatusStore($order_id, $store_id, $money, $deposit);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
d($e);
return false;
}
$all_where['paid'] = 1;
d(OrderLogic::dayPayPrice($all_where, date('Y-m-d', time())));
$uid = 9;
$a = PushService::push('wechat_mmp_' . $uid, $uid, ['type' => 'INDUSTRYMEMBERS', 'msg' => '支付超时,订单已被取消,请重新提交订单', 'data' => ['id' => 5]]);
return json($a);
try {
$a = new WechatTemplate();
$a->NewQuotationNotification(['openid' => 'ocqhF6UfFQXE-SbzbP5YVQJlQAh0', 'data1' => '阿萨', 'data2' => date('Y-m-d H:i:s'), 'data3' => '占山', 'data3' => 18982406440]);
} catch (Exception $e) {
d($e);
}
d(22);
$auth_code = $this->request->get('code');
$pay = (new PayService());
$order = [
'description' => '条码商品',
'out_trade_no' => (string)time(),
'payer' => [
'auth_code' => (string)$auth_code
],
'amount' => [
'total' => 1,
],
'scene_info' => [
"store_info" => [
'id' => '1'
]
],
];
try {
$a = $pay->wechat->pos($order);
} catch (\Exception $th) {
d($th);
}
d($a);
$params = ['store_id' => 2, 'pay_type' => 17];
$a = StoreOrderLogic::createOrder([1], 0, null, $params);
d($a);
return json(['msg' => create_password(123456, '11d3')]);
}
/**