vd/app/Domains/Virtual/Http/Controllers/CompanyController.php
2019-03-28 18:12:39 +08:00

117 lines
2.5 KiB
PHP

<?php
namespace App\Domains\Virtual\Http\Controllers;
use App\Core\Controller;
use Illuminate\Http\Request;
use App\Exceptions\NotExistException;
use App\Domains\Virtual\Services\CompanyService;
use App\Domains\Virtual\Repositories\CompanyRepository;
class CompanyController extends Controller
{
protected $request;
protected $companyService;
/**
* 构造函数,自动注入.
*/
public function __construct(Request $request, CompanyService $companyService)
{
$this->request = $request;
$this->companyService = $companyService;
}
/**
* 列表.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$conditions = $this->request->all();
$conditions['limit'] = $this->request->get('limit', 20);
$companies = $this->companyService->index($conditions);
$companies->load(['accounts', 'addresses']);
$companies->map(function ($item) {
$item->extends = $item->extends ?: [
'bank_account' => '',
'alipay_account' => '',
'wechat_account' => '',
];
});
return res($companies, '企业列表', 201);
}
/**
* 企业详情.
*
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$node = app(CompanyRepository::class)->find($id);
if (!$node) {
throw new NotExistException('企业不存在或已删除');
}
$node->load(['addresses']);
$node->extends = $node->extends ?: [
'bank_account' => '',
'alipay_account' => '',
'wechat_account' => '',
];
return res($node, '企业详情', 201);
}
/**
* 创建.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$attributes = $this->request->all();
$account = $this->companyService->store($attributes);
return res($account, '创建成功');
}
/**
* 编辑.
*
* @return \Illuminate\Http\Response
*/
public function update($id)
{
$attributes = $this->request->all();
$attributes['id'] = $id;
$account = $this->companyService->store($attributes);
return res($account, '修改成功');
}
/**
* 删除.
*
* @return \Illuminate\Http\Response
*/
public function destroy()
{
$ids = $this->request->ids();
$this->companyService->destroy($ids);
return res(true, '删除成功');
}
}