leftJoin('company b', 'b.id=a.company_id AND b.is_del=' . CommonModel::$del_normal) ->leftJoin('card c', 'c.id=a.card_id AND c.is_del=' . CommonModel::$del_normal) ->leftJoin('account d', 'd.id=a.uid AND d.is_del=' . CommonModel::$del_normal) ->leftJoin('addr e', 'e.id=a.addr_id AND e.is_del=' . CommonModel::$del_normal) ->where('a.is_del', CommonModel::$del_normal); if ($data['masterOrderCode'] != '') $db->whereLike('a.masterOrderCode', '%' . $data['masterOrderCode'] . '%'); if ($data['orderCode'] != '') $db->whereLike('a.orderCode', '%' . $data['orderCode'] . '%'); if ($data['status'] != '') $db->where('a.status', $data['status']); if ($data['name'] !== '') $db->whereLike('d.name', '%' . $data['name'] . '%'); if ($data['username']) $db->whereLike('d.username', '%' . $data['username'] . '%'); if (($data['start_date'] != '') && ($data['end_date'] != '')) $db->whereBetween('a.addtime', [$data['start_date'], $data['end_date']]); if ($data['type'] !== '') $db->where('a.type', $data['type']); $count = $db->count('a.id'); $list = $db ->field('a.id,a.masterOrderCode,a.orderCode,a.type,b.title company_title,c.title card_title,d.name,d.username,a.num,e.contactor,e.mobile,a.status,a.post_name,a.post_code,a.addtime,e.addr_code,"" addr_name,e.addr,"" good_code,"" good_name,""good_cover_img,a.child_id') ->page($data['page'], $data['size']) ->withAttr('addr_code', function ($val) { return explode(',', $val); }) ->withAttr('addr_name', function ($val, $da) { return $da['addr_code'] ? get_addr_name($da['addr_code']) : ''; }) ->order('a.addtime desc') ->select() ->toArray(); //所有服务 $service = ServiceModel::where('is_del', CommonModel::$del_normal) ->whereIn('id', array_column($list, 'child_id')) ->column('title', 'id'); $good = GoodModel::where('is_del', CommonModel::$del_normal) ->whereIn('id', array_column($list, 'child_id')) ->column('good_cover_img,good_code,good_name', 'id'); foreach ($list as &$order) { if ($order['type'] == MasterOrderModel::$type_service) $order['good_name'] = $service[$order['child_id']] ?? ''; else $order = array_merge($order, $good[$order['child_id']]); } return json_show(CommonModel::$success, '获取订单列表成功', ['count' => $count, 'list' => $list]); } //详情 public static function read(int $id = 0): Json { $rs = $db = OrderModel::alias('a') ->field('a.id,a.orderCode,a.type,b.title company_title,c.title card_title,d.name,d.username,a.num,e.contactor,e.mobile,e.addr_code,e.addr,a.status,a.post_name,a.post_code,a.addtime,"" addr_name,"" good_code,"" good_name,"" good_cover_img,a.child_id') ->leftJoin('company b', 'b.id=a.company_id AND b.is_del=' . CommonModel::$del_normal) ->leftJoin('card c', 'c.id=a.card_id AND c.is_del=' . CommonModel::$del_normal) ->leftJoin('account d', 'd.id=a.uid AND d.is_del=' . CommonModel::$del_normal) ->leftJoin('addr e', 'e.id=a.addr_id AND e.is_del=' . CommonModel::$del_normal) ->where(['a.is_del' => CommonModel::$del_normal, 'a.id' => $id]) ->withAttr('addr_code', function ($val) { return explode(',', $val); }) ->withAttr('addr_name', function ($val, $da) { return $da['addr_code'] ? get_addr_name($da['addr_code']) : ''; }) ->findOrEmpty() ->toArray(); if ($rs['type'] == MasterOrderModel::$type_service) $rs['good_name'] = ServiceModel::where(['is_del' => CommonModel::$del_normal, 'id' => $rs['child_id']])->value('title'); else $rs = array_merge($rs, GoodModel::where(['is_del' => CommonModel::$del_normal, 'id' => $rs['child_id']])->field('good_code,good_name,good_cover_img')->findOrEmpty()->toArray()); return json_show(CommonModel::$success, '获取商品详情成功', $rs); } //导出 public static function export(array $data = []): Json { $da = []; try { verify_token($data['token']); $db = OrderModel::alias('a') ->leftJoin('company b', 'b.id=a.company_id AND b.is_del=' . CommonModel::$del_normal) ->leftJoin('card c', 'c.id=a.card_id AND c.is_del=' . CommonModel::$del_normal) ->leftJoin('account d', 'd.id=a.uid AND d.is_del=' . CommonModel::$del_normal) ->leftJoin('addr e', 'e.id=a.addr_id AND e.is_del=' . CommonModel::$del_normal) ->where('a.is_del', CommonModel::$del_normal); if ($data['masterOrderCode'] != '') $db->whereLike('a.masterOrderCode', '%' . $data['masterOrderCode'] . '%'); if ($data['orderCode'] != '') $db->whereLike('a.orderCode', '%' . $data['orderCode'] . '%'); if ($data['status'] != '') $db->where('a.status', $data['status']); if ($data['name'] !== '') $db->whereLike('d.name', '%' . $data['name'] . '%'); if ($data['username']) $db->whereLike('d.username', '%' . $data['username'] . '%'); if (($data['start_date'] != '') && ($data['end_date'] != '')) $db->whereBetween('a.addtime', [$data['start_date'], $data['end_date']]); if ($data['type'] !== '') $db->where('a.type', $data['type']); $list = $db ->field('a.id `订单ID`,a.masterOrderCode `主订单编号`,a.orderCode `订单编号`,a.type `订单类型`,b.title `业务公司`,c.title `卡类型`,d.name `购买用户名`,d.username `购买账号`,a.num `购买数量`,e.contactor `收货联系人`,e.mobile `收货联系电话`,a.status `状态`,a.post_name `物流公司`,a.post_code `物流单号`,a.addtime `创建时间`,e.addr_code,"" `收货地址`,e.addr,"" `商品编码`,"" `商品(服务)名称`,a.child_id') ->order('a.addtime desc') ->cursor(); $status = [ MasterOrderModel::$order_status_wait_pay => '待支付', MasterOrderModel::$order_status_pay_fail => '支付失败', MasterOrderModel::$order_status_not_deliver => '待发货', MasterOrderModel::$order_status_deliver => '已发货', MasterOrderModel::$order_status_receipt => '已收货', ]; $type = [MasterOrderModel::$type_exchange_good => '兑换商品订单', MasterOrderModel::$type_shopping_good => '商城商品订单', MasterOrderModel::$type_service => '服务订单']; foreach ($list as $v) { $v = $v->toArray(); if ($v['订单类型'] == MasterOrderModel::$type_service) $v['商品(服务)名称'] = ServiceModel::where(['is_del' => CommonModel::$del_normal, 'id' => $v['child_id']])->value('title', ''); else { $good = GoodModel::where(['is_del' => CommonModel::$del_normal, 'id' => $v['child_id']]) ->field('good_code,good_name') ->findOrEmpty() ->toArray(); $v['商品编码'] = $good['good_code'] ?? ''; $v['商品(服务)名称'] = $good['good_name'] ?? ''; }; $v['收货地址'] = $v['addr_code'] ? get_addr_name($v['addr_code']) . $v['addr'] : $v['addr']; $v['状态'] = $status[$v['状态']] ?? ''; $v['订单类型'] = $type[$v['订单类型']] ?? ''; unset($v['addr_code']); unset($v['addr']); unset($v['child_id']); $da[] = $v; } if (empty($da)) throw new Exception('没有可供导出的数据'); } catch (Exception $exception) { $da = [[$exception->getMessage()]]; } catch (ValidateException $validateException) { $da = [[$validateException->getError()]]; } $headerArr = array_keys($da[0]); excel_save('订单导出' . date('YmdHis'), $headerArr, $da); } //发货 public static function deliver(array $list = []) { $db = new OrderModel(); $temp = $db ->field('id,orderCode') ->where(['is_del' => CommonModel::$del_normal]) ->where('status', '<>', MasterOrderModel::$order_status_not_deliver) ->whereIn('orderCode', array_column($list, 'orderCode')) ->findOrEmpty(); if (!$temp->isEmpty()) return json_show(CommonModel::$error_param, $temp->orderCode . '订单并不是未发货状态'); $val = Validate::rule([ 'orderCode|订单编号' => 'require|length:18', 'post_name|物流公司' => 'require|max:255', 'post_code|物流单号' => 'require|max:255', ]); Db::startTrans(); try { $date = date('Y-m-d H:i:s'); $insert_express_data = []; foreach ($list as $order) { if (!$val->check($order)) throw new Exception($val->getError()); $db->where([ 'is_del' => CommonModel::$del_normal, 'orderCode' => $order['orderCode'], 'status' => MasterOrderModel::$order_status_not_deliver, ])->save([ 'status' => MasterOrderModel::$order_status_deliver, 'post_name' => $order['post_name'], 'post_code' => $order['post_code'], 'updater' => self::$uname, 'updaterid' => self::$uid, 'updatetime' => $date, ]); $insert_express_data[] = [ 'post_code' => $order['post_code'], 'post_data' => '', 'status' => 1, 'addtime' => $date, 'updatetime' => $date, ]; } if ($insert_express_data) Db::name('express_data')->insertAll($insert_express_data); Db::commit(); return json_show(CommonModel::$success, '发货成功'); } catch (Exception $exception) { Db::rollback(); return json_show(CommonModel::$error_param, '发货失败,' . $exception->getMessage()); } } }