feat(purchase_order): 添加合并今日商户和平台订单功能

This commit is contained in:
mkm 2024-08-01 18:03:45 +08:00
parent a6031b3318
commit 66571b43a1
2 changed files with 95 additions and 45 deletions

View File

@ -2,6 +2,7 @@
namespace app\admin\controller\purchase_order;
use app\admin\controller\BaseAdminController;
use app\admin\lists\purchase_order\PurchaseOrderLists;
use app\admin\logic\purchase_order\PurchaseOrderLogic;
class PurchaseOrderController extends BaseAdminController{
@ -10,4 +11,18 @@ class PurchaseOrderController extends BaseAdminController{
return $this->dataLists(new PurchaseOrderLists());
}
/**
* 合并今日商户订单
*/
public function add(){
PurchaseOrderLogic::StoreTodayOrder();
return $this->success('合并成功');
}
/**
* 合并今日平台订单
*/
public function platform(){
PurchaseOrderLogic::platformTodayOrder();
return $this->success('合并成功');
}
}

View File

@ -5,6 +5,9 @@ namespace app\admin\logic\purchase_order;
use app\common\model\purchase_order\PurchaseOrder;
use app\common\logic\BaseLogic;
use app\common\model\purchase_order_info\PurchaseOrderInfo;
use app\common\model\store_order\StoreOrder;
use app\common\model\store_order_cart_info\StoreOrderCartInfo;
use think\facade\Db;
@ -18,31 +21,48 @@ class PurchaseOrderLogic extends BaseLogic
/**
* @notes 添加采购订单
* @notes 合并今日商户订单
* @param array $params
* @return bool
* @author admin
* @date 2024/08/01 16:32
*/
public static function add(array $params): bool
public static function StoreTodayOrder(): bool
{
Db::startTrans();
try {
PurchaseOrder::create([
'store_id' => $params['store_id'],
'order_arr' => $params['order_arr'],
'order_id' => $params['order_id'],
'total' => $params['total'],
'actual' => $params['actual'],
'money' => $params['money'],
'paid' => $params['paid'],
'file' => $params['file'],
'data' => $params['data'],
'is_opurchase' => $params['is_opurchase'],
'is_mer' => $params['is_mer'],
'storage' => $params['storage']
]);
$store_arr = StoreOrder::where(['paid' => 1, 'refund_status' => 0])->whereDay('create_time')->group('store_id')->column('store_id');
$purchaseOrderInfo = new PurchaseOrderInfo();
foreach ($store_arr as $store_id) {
$purchase_order_info = [];
$order_arr = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'store_id' => $store_id])->whereDay('create_time')->column('id');
$price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'store_id' => $store_id])->whereDay('create_time')->field('SUM(pay_price) as pay_price,SUM(total_price) as total_price')->find();
$data = [
'store_id' => $store_id,
'order_arr' => json_encode($order_arr),
'order_id' => getNewOrderId('CG'),
'total' => $price['total_price'],
'actual' => $price['pay_price'],
'money' => $price['pay_price'],
'paid' => 1,
'is_mer' => 1,
'create_time' => time(),
'update_time' => time(),
];
$res = PurchaseOrder::create($data);
$info = StoreOrderCartInfo::where('oid', 'in', $order_arr)->field('store_id, product_id,price,SUM(total_price) as total_price, SUM(cart_num) as cart_num')->group('store_id, product_id')->select();
foreach ($info as $item) {
$arr['oid'] = $res['id'];
$arr['store_id'] = $item['store_id'];
$arr['product_id'] = $item['product_id'];
$arr['price'] = $item['price'];
$arr['total_price'] = $item['total_price'];
$arr['cart_num'] = $item['cart_num'];
$arr['status'] = 1;
$purchase_order_info[] = $arr;
}
$purchaseOrderInfo->saveAll($purchase_order_info);
}
Db::commit();
return true;
} catch (\Exception $e) {
@ -52,43 +72,58 @@ class PurchaseOrderLogic extends BaseLogic
}
}
/**
* @notes 编辑采购订单
* @notes 合并今日平台订单
* @param array $params
* @return bool
* @author admin
* @date 2024/08/01 16:32
*/
public static function edit(array $params): bool
public static function platformTodayOrder()
{
Db::startTrans();
try {
PurchaseOrder::where('id', $params['id'])->update([
'store_id' => $params['store_id'],
'order_arr' => $params['order_arr'],
'order_id' => $params['order_id'],
'total' => $params['total'],
'actual' => $params['actual'],
'money' => $params['money'],
'paid' => $params['paid'],
'file' => $params['file'],
'data' => $params['data'],
'is_opurchase' => $params['is_opurchase'],
'is_mer' => $params['is_mer'],
'storage' => $params['storage']
]);
// Db::startTrans();
// try {
$purchaseOrderInfo = new PurchaseOrderInfo();
$purchase_order_info=[];
$order_arr = PurchaseOrder::whereDay('create_time')->where('is_mer',1)->column('id');
// $price = PurchaseOrder::whereDay('create_time')->field('SUM(actual) as pay_price,SUM(total) as total_price')->find();
// $data = [
// 'store_id' => 0,
// 'order_arr' => json_encode($order_arr),
// 'order_id' => getNewOrderId('PT'),
// 'total' => $price['total_price'],
// 'actual' => $price['pay_price'],
// 'money' => $price['pay_price'],
// 'paid' => 1,
// 'is_mer' => 2,
// 'create_time' => time(),
// 'update_time' => time(),
// ];
// $res = PurchaseOrder::create($data);
$info = StoreOrderCartInfo::where('oid', 'in', $order_arr)->field('store_id, product_id,price,SUM(total_price) as total_price, SUM(cart_num) as cart_num')->group('store_id, product_id')->select();
d($info);
foreach ($info as $item) {
$arr['oid'] = $res['id'];
$arr['store_id'] = $item['store_id'];
$arr['product_id'] = $item['product_id'];
$arr['price'] = $item['price'];
$arr['total_price'] = $item['total_price'];
$arr['cart_num'] = $item['cart_num'];
$arr['status'] = 1;
$purchase_order_info[] = $arr;
}
$purchaseOrderInfo->saveAll($purchase_order_info);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
// Db::commit();
// return true;
// } catch (\Exception $e) {
// Db::rollback();
// d($e);
// self::setError($e->getMessage());
// return false;
// }
}
/**
* @notes 删除采购订单
* @param array $params
@ -113,4 +148,4 @@ class PurchaseOrderLogic extends BaseLogic
{
return PurchaseOrder::findOrEmpty($params['id'])->toArray();
}
}
}