132 lines
4.4 KiB
PHP
132 lines
4.4 KiB
PHP
<?php
|
|
namespace app\mobile\controller;
|
|
|
|
use app\common\controller\BaseController;
|
|
use app\common\model\Corp;
|
|
use app\common\model\InvItem;
|
|
use app\common\model\InvRequisitionApprove;
|
|
use app\common\model\InvRequisitionAttachment;
|
|
use app\common\model\InvRequisitionItem;
|
|
use app\common\service\OssService;
|
|
use app\common\traits\crud\CrudControllerTrait;
|
|
use app\common\service\InvRequisitionService;
|
|
use support\Request;
|
|
|
|
class InvRequisitionController extends BaseController
|
|
{
|
|
use CrudControllerTrait;
|
|
|
|
protected $service;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->service = new InvRequisitionService();
|
|
}
|
|
|
|
/**
|
|
* 提交物品申请
|
|
*
|
|
* @param Request $request
|
|
* @return mixed
|
|
*/
|
|
public function apply(Request $request)
|
|
{
|
|
try {
|
|
// 验证申请数据
|
|
$data = $this->validate($this->service->getValidator(), $request->post(), 'apply');
|
|
|
|
// 处理申请逻辑
|
|
$result = $this->service->serviceStore($request, $data, 'apply');
|
|
|
|
return $this->success($result, '申请提交成功');
|
|
} catch (\Throwable $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取列表
|
|
*
|
|
* @param Request $request 请求对象
|
|
* @return mixed 返回分页数据及总数
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
try {
|
|
$raw = $this->service->serviceList($request);
|
|
$uid = $request->user->uid;
|
|
$data = $this->formatindex($raw,$uid);
|
|
// $data = $this->transformData($raw, method_exists($this, 'indexFormat')
|
|
// ? [$this, 'indexFormat']
|
|
// : null);
|
|
return $this->success($data);
|
|
} catch (\Throwable $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function formatindex($p,$uid){
|
|
$records = $p->getCollection()
|
|
->map(fn($item) => $this->indexFormat($item, $uid))
|
|
->toArray();
|
|
|
|
return [
|
|
'records' => $records,
|
|
'total' => $p->total(),
|
|
'current' => $p->currentPage(),
|
|
'size' => $p->perPage(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 列表页数据格式化
|
|
*
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
protected function indexFormat($data, $uid)
|
|
{
|
|
if (isset($data['created_at']))
|
|
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
|
if (isset($data['updated_at']))
|
|
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
|
if (isset($data['id'])){
|
|
$data['item'] = InvRequisitionItem::where('requisition_id', $data['id'])->get();
|
|
foreach ($data['item'] as $key => $item){
|
|
$corpId = InvItem::where('id', $item->item_id)->value('company_id');
|
|
$item['company_name'] = Corp::where('id', $corpId)->value('name');
|
|
$aprove = InvRequisitionApprove::where('requisition_id', $data['id'])->where('approver_id', $uid)->orderBy('id','desc')->first();
|
|
$item['approval_action'] = $aprove?$aprove['action']:3; //查询人审核状态:0待审核 1通过 2驳回 3非审核人
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 详情页数据格式化
|
|
*
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
protected function showFormat($data)
|
|
{
|
|
if (isset($data['id'])){
|
|
$data['approve'] = InvRequisitionApprove::where('requisition_id', $data['id'])->get()->toArray();
|
|
$approve_id = !empty($data['approve'])?array_pop($data['approve'])['id']:null;
|
|
$data['approve_id'] = $approve_id;
|
|
}
|
|
if (isset($data['created_at']))
|
|
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
|
if (isset($data['updated_at']))
|
|
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
|
if (isset($data['id'])){
|
|
$data['item'] = InvRequisitionItem::where('requisition_id', $data['id'])->get();
|
|
$data['attachment'] = InvRequisitionAttachment::where('requisition_id', $data['id'])->get();
|
|
$oss = new OssService();
|
|
foreach ($data['attachment'] as $key => $value) {
|
|
$data['attachment'][$key]['file_path_pull'] = $oss->generatePrivateUrl($value['file_path']);
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
} |