123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace app\admin\controller\order;
- use app\admin\validate\order\ReturnValidate;
- use app\controller\Curd;
- use app\model\GoodsComponent;
- use app\model\Order;
- use app\model\OrderExpress;
- use app\model\OrderReturn;
- use app\model\OrderSheet;
- use support\exception\BusinessException;
- use support\Request;
- use support\Response;
- class RefundController extends Curd
- {
- public function __construct()
- {
- $this->model = new OrderReturn();
- $this->validate = true;
- $this->validateClass=new ReturnValidate();
- }
- public function select(Request $request): Response
- {
- [$where, $format, $limit, $field, $order] = $this->selectInput($request);
- $order = $request->get('order', 'desc');
- $field = $field ?? 'order_return_addtimes';
- $where['order_return_category'] = '退款';
- $query = $this->doSelect($where, $field, $order);
- return $this->doFormat($query, $format, $limit);
- }
- protected function doSelect(array $where, string $field = null, string $order = 'desc')
- {
- $model = $this->model->with([
- 'member' => function ($query) {
- $query->select('member_id', 'member_mobile');
- },
- 'cert'=>function($query){
- $query->select('join_cert_member_id','member_cert_name');
- },
- 'order' => function ($query) {
- $query->select('order_id', 'order_name');
- }
- ]);
- foreach ($where as $column => $value) {
- if (is_array($value)) {
- if ($value[0] === 'like' || $value[0] === 'not like') {
- $model = $model->where($column, $value[0], "%$value[1]%");
- } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
- $model = $model->where($column, $value[0], $value[1]);
- } elseif ($value[0] == 'in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereIn($column, $valArr);
- } elseif ($value[0] == 'not in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereNotIn($column, $valArr);
- } elseif ($value[0] == 'null') {
- $model = $model->whereNull($column);
- } elseif ($value[0] == 'not null') {
- $model = $model->whereNotNull($column);
- } elseif ($value[0] !== '' || $value[1] !== '') {
- $model = $model->whereBetween($column, $value);
- }
- } else {
- $model = $model->where($column, $value);
- }
- }
- if ($field) {
- $model = $model->orderBy($field, $order);
- }
- return $model;
- }
- public function afterQuery($items)
- {
- foreach ($items as &$item){
- if (!empty($item['order_return_apply_json']) && is_json($item['order_return_apply_json'])){
- $json = json_decode($item['order_return_apply_json'],true);
- $item['order_return_apply_json'] = $json['apply'] ?? '';
- }
- if (!empty($item['order_return_recharge_json']) && is_json($item['order_return_recharge_json'])){
- $json = json_decode($item['order_return_recharge_json'],true);
- $item['order_return_recharge_json'] = $json['change'] ?? '';
- }
- }
- return $items;
- }
- protected function updateInput(Request $request): array
- {
- $primary_key = $this->model->getKeyName();
- $id = $request->post($primary_key);
- $data = $this->inputFilter($request->post());
- $model = $this->model->find($id);
- if (!$model) {
- throw new BusinessException('记录不存在', 2);
- }
- if (empty($model->order_return_accept_datetimes) && $model->order_return_status == 'PENDING' && $data['order_return_status'] == 'DOING'){
- $data['order_return_accept_datetimes'] = date('Y-m-d H:i:s');
- }
- if (!empty($data['order_return_apply_json'])){
- $data['order_return_apply_json'] = json_encode(['apply'=>$data['order_return_apply_json']]);
- }
- if (!empty($data['order_return_recharge_json'])){
- $data['order_return_recharge_json'] = json_encode(['change'=>$data['order_return_recharge_json']]);
- }
- unset($data[$primary_key]);
- return [$id, $data];
- }
- /**
- * @Desc 订单商品详情
- * @Author Gorden
- * @Date 2024/3/29 8:50
- *
- * @param Request $request
- * @return Response
- */
- public function sheet(Request $request)
- {
- $orderId = $request->get('order_id');
- $orderSheet = OrderSheet::with([
- 'member' => function ($query) {
- $query->select('member_id', 'member_mobile');
- },
- 'goods' => function ($query) {
- $query->select('goods_id', 'goods_name', 'goods_cover', 'goods_market_price', 'goods_sales_price', 'goods_classify');
- },
- 'memberInfo',
- 'cert',
- 'refund'
- ])->where('join_sheet_order_id', $orderId)
- ->get()
- ->toArray();
- foreach ($orderSheet as &$item) {
- $item['goods']['goods_cover'] = getenv('STORAGE_DOMAIN') . $item['goods']['goods_cover'];
- if (!empty($item['goods']) && $item['goods']['goods_classify'] == 'PACKAGE') {
- $components = GoodsComponent::with('goods')
- ->where('join_component_master_goods_id', $item['goods']['goods_id'])
- ->select('join_component_master_goods_id', 'join_component_goods_id', 'goods_component_price',
- 'goods_component_price', 'goods_component_config_json')
- ->get()
- ->toArray();
- $goodsArr = [];
- foreach ($components as $component) {
- $configJson = !empty($component['goods_component_config_json']) ? json_decode($component['goods_component_config_json'], true) : [];
- if (!empty($component['goods'])) {
- $goodsArr[] = [
- 'goods_name' => $component['goods']['goods_name'],
- 'goods_cover' => getenv('STORAGE_DOMAIN') . $component['goods']['goods_cover'],
- 'nbr' => $configJson['nbr'] ?? 0,
- ];
- }
- }
- $item['goods']['components'] = $goodsArr;
- }
- if (!empty($item['refund'])){
- if (!empty($item['refund']['order_return_apply_json']) && is_json($item['refund']['order_return_apply_json'])){
- $json = json_decode($item['refund']['order_return_apply_json'],true);
- $item['refund']['order_return_apply_json'] = $json['reason'] ?? '';
- }
- if (!empty($item['refund']['order_return_recharge_json']) && is_json($item['refund']['order_return_recharge_json'])){
- $json = json_decode($item['refund']['order_return_recharge_json'],true);
- $item['refund']['order_return_recharge_json'] = $json['change'] ?? '';
- }
- }
- }
- $order = Order::where('order_id', $orderId)->first();
- $express = OrderExpress::where('join_express_order_id', $orderId)->first();
- $data = [
- 'order' => $order,
- 'sheet' => $orderSheet,
- 'express' => $express
- ];
- return json_success('', $data);
- }
- }
|