mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-06 03:58:04 +08:00
71 lines
2.4 KiB
PHP
71 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace app\data\service;
|
|
|
|
use think\admin\Service;
|
|
|
|
/**
|
|
* 订单数据服务
|
|
* Class OrderService
|
|
* @package app\data\service
|
|
*/
|
|
class OrderService extends Service
|
|
{
|
|
/**
|
|
* 获取随机减免金额
|
|
* @return float
|
|
*/
|
|
public function getReduct(): float
|
|
{
|
|
return rand(1, 100) / 100;
|
|
}
|
|
|
|
/**
|
|
* 同步订单关联商品的库存
|
|
* @param string $order_no 订单编号
|
|
* @return boolean
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function syncStock(string $order_no): bool
|
|
{
|
|
$map = ['order_no' => $order_no];
|
|
$codes = $this->app->db->name('ShopOrderItem')->where($map)->column('goods_code');
|
|
foreach (array_unique($codes) as $code) GoodsService::instance()->syncStock($code);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 绑定订单详情数据
|
|
* @param array $data
|
|
* @param bool $fromer
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function buildOrderData(array &$data = [], $fromer = true): array
|
|
{
|
|
// 关联发货信息
|
|
$nobs = array_unique(array_column($data, 'order_no'));
|
|
$trucks = $this->app->db->name('ShopOrderSend')->whereIn('order_no', $nobs)->column('*', 'order_no');
|
|
foreach ($trucks as &$item) unset($item['id'], $item['uid'], $item['status'], $item['deleted'], $item['create_at']);
|
|
// 关联订单商品
|
|
$query = $this->app->db->name('ShopOrderItem')->where(['status' => 1, 'deleted' => 0]);
|
|
$items = $query->withoutField('id,uid,status,deleted,create_at')->whereIn('order_no', $nobs)->select()->toArray();
|
|
// 关联用户数据
|
|
$fields = 'username,phone,nickname,headimg,status';
|
|
UserService::instance()->buildByUid($data, 'uid', 'user', $fields);
|
|
if ($fromer) UserService::instance()->buildByUid($data, 'puid1', 'fromer', $fields);
|
|
foreach ($data as &$vo) {
|
|
[$vo['sales'], $vo['truck'], $vo['items']] = [0, $trucks[$vo['order_no']] ?? [], []];
|
|
foreach ($items as $item) if ($vo['order_no'] === $item['order_no']) {
|
|
$vo['sales'] += $item['stock_sales'];
|
|
$vo['items'][] = $item;
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
} |