leftJoin('service s', 's.id=a.service_id AND s.is_del=' . CommonModel::$del_normal) ->leftJoin('company b', 'b.id=s.company_id AND b.is_del=' . CommonModel::$del_normal) ->leftJoin('card c', 'c.id=s.card_id AND c.is_del=' . CommonModel::$del_normal) ->leftJoin('account d', 'd.id=a.uid AND d.is_del=' . CommonModel::$del_normal) ->leftJoin('company bb', 'bb.id=d.company_id AND bb.is_del=' . CommonModel::$del_normal) ->leftJoin('card cc', 'cc.id=d.card_id AND cc.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['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']]); $count = $db->count('a.id'); $list = $db ->field('a.id,a.orderCode,bb.title account_company_title,c.title account_card_title,d.name,d.username,a.num,e.contactor,e.mobile,e.addr_code,s.title,a.amount,a.status,a.addtime,a.post_name,a.post_code,"" addr_name,e.addr') ->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(); return json_show(CommonModel::$success, '获取服务订单列表成功', ['count' => $count, 'list' => $list]); } //详情 public static function read(int $id = 0): Json { $rs = OrderServiceModel::alias('a') ->field('a.id,a.orderCode,bb.title account_company_title,c.title account_card_title,d.name,d.username,a.num,e.contactor,e.mobile,e.addr_code,s.title,a.amount,a.status,a.addtime,a.post_name,a.post_code,"" addr_name,e.addr') ->leftJoin('service s', 's.id=a.service_id AND s.is_del=' . CommonModel::$del_normal) ->leftJoin('company b', 'b.id=s.company_id AND b.is_del=' . CommonModel::$del_normal) ->leftJoin('card c', 'c.id=s.card_id AND c.is_del=' . CommonModel::$del_normal) ->leftJoin('account d', 'd.id=a.uid AND d.is_del=' . CommonModel::$del_normal) ->leftJoin('company bb', 'bb.id=d.company_id AND bb.is_del=' . CommonModel::$del_normal) ->leftJoin('card cc', 'cc.id=d.card_id AND cc.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(); return json_show(CommonModel::$success, '获取商品详情成功', $rs); } //导出 public static function export(array $data = []): Json { $db = OrderServiceModel::alias('a') ->leftJoin('service s', 's.id=a.service_id AND s.is_del=' . CommonModel::$del_normal) ->leftJoin('company b', 'b.id=s.company_id AND b.is_del=' . CommonModel::$del_normal) ->leftJoin('card c', 'c.id=s.card_id AND c.is_del=' . CommonModel::$del_normal) ->leftJoin('account d', 'd.id=a.uid AND d.is_del=' . CommonModel::$del_normal) ->leftJoin('company bb', 'bb.id=d.company_id AND bb.is_del=' . CommonModel::$del_normal) ->leftJoin('card cc', 'cc.id=d.card_id AND cc.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['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']]); $list = $db ->field('a.id 订单ID,a.orderCode 订单编号,bb.title 购买账号业务公司,c.title 购买账号卡类型,d.name 购买用户名,d.username 购买账号,a.num 购买数量,e.contactor 收货联系人,e.mobile 收货联系电话,e.addr_code,s.title 服务名称,a.amount 支付金额,a.status 状态,a.addtime 创建时间,a.post_name 物流公司,a.post_code 物流单号,e.addr') ->order('a.addtime desc') ->cursor(); $status = [CommonModel::$status_not_deliver => '待发货', CommonModel::$status_deliver => '已发货', CommonModel::$status_receipt => '已收货']; $da = []; foreach ($list as $v) { $v = $v->toArray(); $v['收货地址'] = $v['addr_code']?get_addr_name($v['addr_code']).$v['addr']:$v['addr']; $v['状态'] = $status[$v['状态']] ?? ''; unset($v['addr_code']); unset($v['addr']); $da[] = $v; } if (empty($da)) $da[] = '没有可供导出的数据'; $headerArr = array_keys($da[0]); excel_save('服务订单导出' . date('YmdHis'), $headerArr, $da); } //发货 public static function deliver(array $list = []) { $db = new OrderServiceModel(); $temp = $db ->field('id,orderCode') ->where(['is_del' => CommonModel::$del_normal]) ->where('status', '<>', CommonModel::$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'); foreach ($list as $order) { if (!$val->check($order)) throw new Exception($val->getError()); $db->where([ 'is_del' => CommonModel::$del_normal, 'orderCode' => $order['orderCode'], 'status' => CommonModel::$status_not_deliver, ])->save([ 'status' => CommonModel::$status_deliver, 'post_name' => $order['post_name'], 'post_code' => $order['post_code'], 'updater' => self::$uname, 'updaterid' => self::$uid, 'updatetime' => $date, ]); } Db::commit(); return json_show(CommonModel::$success, '发货成功'); } catch (Exception $exception) { Db::rollback(); return json_show(CommonModel::$error_param, '发货失败,' . $exception->getMessage()); } } }