12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?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 buildItemData(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, 'from', 'fromer', $fields);
- foreach ($data as &$vo) {
- $vo['sales'] = 0;
- $vo['truck'] = $trucks[$vo['order_no']] ?? [];
- $vo['items'] = [];
- foreach ($items as $item) if ($vo['order_no'] === $item['order_no']) {
- $vo['sales'] += $item['stock_sales'];
- $vo['items'][] = $item;
- }
- }
- return $data;
- }
- }
|