297 lines
9.9 KiB
PHP
297 lines
9.9 KiB
PHP
|
|
<?php
|
|||
|
|
|
|||
|
|
namespace app\common\service;
|
|||
|
|
|
|||
|
|
use app\common\model\InvRequisitionItem;
|
|||
|
|
use app\common\validate\InvRequisitionItemReturnValidate;
|
|||
|
|
use app\common\traits\crud\CrudServiceTrait;
|
|||
|
|
use app\common\model\InvRequisitionItemReturn;
|
|||
|
|
use support\Request;
|
|||
|
|
use support\Db;
|
|||
|
|
use support\exception\BusinessException;
|
|||
|
|
use app\common\service\UserService;
|
|||
|
|
use Illuminate\Database\Eloquent\Model;
|
|||
|
|
use app\common\model\InvRequisitionItemReturnAttachment;
|
|||
|
|
|
|||
|
|
class InvRequisitionItemReturnService
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
use CrudServiceTrait;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 指定当前要操作的模型
|
|||
|
|
*/
|
|||
|
|
protected string $modelClass = InvRequisitionItemReturn::class;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 验证器
|
|||
|
|
*/
|
|||
|
|
protected $validator = InvRequisitionItemReturnValidate::class;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取验证器
|
|||
|
|
*/
|
|||
|
|
public function getValidator()
|
|||
|
|
{
|
|||
|
|
return new $this->validator;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 将二维数组按指定 key 重组为关联数组,该 key 的值作为新数组的键,
|
|||
|
|
* 其余元素组成子数组作为值。
|
|||
|
|
*
|
|||
|
|
* @param array $array 原始二维数组
|
|||
|
|
* @param string $keyField 用作新键的字段名(如 'id')
|
|||
|
|
* @return array 重组后的数组
|
|||
|
|
*/
|
|||
|
|
private function groupArrayByKey($array, $keyField = 'id')
|
|||
|
|
{
|
|||
|
|
$result = [];
|
|||
|
|
foreach ($array as $item) {
|
|||
|
|
if (!isset($item[$keyField])) {
|
|||
|
|
continue; // 跳过没有该 key 的项
|
|||
|
|
}
|
|||
|
|
$key = $item[$keyField];
|
|||
|
|
// 移除用作键的字段
|
|||
|
|
unset($item[$keyField]);
|
|||
|
|
$result[$key] = $item;
|
|||
|
|
}
|
|||
|
|
return $result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 创建物品归还申请前置处理
|
|||
|
|
*
|
|||
|
|
* @param array $data 归还申请数据
|
|||
|
|
* @param Request $request 请求对象
|
|||
|
|
* @return array 处理后的数据
|
|||
|
|
*/
|
|||
|
|
public function beforeServiceStore($data, Request $request): array
|
|||
|
|
{
|
|||
|
|
try {
|
|||
|
|
// // 模拟数据
|
|||
|
|
// $data = [
|
|||
|
|
// [
|
|||
|
|
// 'requisition_item_id' => 9,
|
|||
|
|
// 'return_qty' => 1,
|
|||
|
|
// 'apply_remark' => '申请备注',
|
|||
|
|
// 'attachment' => [
|
|||
|
|
// [
|
|||
|
|
// 'original_name' => '附件1.jpg',
|
|||
|
|
// 'file_path' => '/uploads/2023/07/01/attachment1.jpg'
|
|||
|
|
// ],
|
|||
|
|
// [
|
|||
|
|
// 'original_name' => '附件2.jpg',
|
|||
|
|
// 'file_path' => '/uploads/2023/07/01/attachment2.jpg'
|
|||
|
|
// ]
|
|||
|
|
// ]
|
|||
|
|
// ]
|
|||
|
|
// ];
|
|||
|
|
|
|||
|
|
// 获取所有申请项ID
|
|||
|
|
$reqItemIds = array_column($data, 'requisition_item_id');
|
|||
|
|
|
|||
|
|
// 参数校验
|
|||
|
|
if (empty($reqItemIds)) {
|
|||
|
|
throw new BusinessException('未提供有效的申请项ID');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 将申请项按 ID 重组为关联数组
|
|||
|
|
$newReqItems = $this->groupArrayByKey($data, 'requisition_item_id');
|
|||
|
|
|
|||
|
|
// 获取申请项明细
|
|||
|
|
$reqItemsArr = InvRequisitionItem::whereIn('id', $reqItemIds)->get()->toArray();
|
|||
|
|
|
|||
|
|
// 检查是否获取到有效数据
|
|||
|
|
if (empty($reqItemsArr)) {
|
|||
|
|
throw new BusinessException('未找到对应的申请项');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 获取用户信息
|
|||
|
|
$userService = new UserService();
|
|||
|
|
$user = $userService->getUserInfo($request->user->uid, '');
|
|||
|
|
|
|||
|
|
$arr = [];
|
|||
|
|
foreach ($reqItemsArr as $reqItem) {
|
|||
|
|
$reqItemId = $reqItem['id'];
|
|||
|
|
|
|||
|
|
// 检查是否有对应的新申请项数据
|
|||
|
|
if (!isset($newReqItems[$reqItemId])) {
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$arr[] = [
|
|||
|
|
'requisition_id' => $reqItem['requisition_id'],
|
|||
|
|
'requisition_item_id' => $reqItemId,
|
|||
|
|
'item_id' => $reqItem['item_id'],
|
|||
|
|
'apply_user_id' => $user['user']['id'],
|
|||
|
|
'apply_user_name' => $user['user']['name'],
|
|||
|
|
'return_qty' => $newReqItems[$reqItemId]['return_qty'],
|
|||
|
|
'apply_remark' => $newReqItems[$reqItemId]['apply_remark'] ?? '',
|
|||
|
|
'attachment' => $newReqItems[$reqItemId]['attachment'],
|
|||
|
|
'return_at' => time()
|
|||
|
|
];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return $arr;
|
|||
|
|
} catch (BusinessException $e) {
|
|||
|
|
throw $e;
|
|||
|
|
} catch (\Throwable $th) {
|
|||
|
|
throw new BusinessException('创建物品归还申请失败:' . $th->getMessage());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 处理物品申请
|
|||
|
|
*
|
|||
|
|
* @param Request $request
|
|||
|
|
* @param array $data
|
|||
|
|
* @return array
|
|||
|
|
*/
|
|||
|
|
public function serviceStore(Request $request, array $data, string $scene = '')
|
|||
|
|
{
|
|||
|
|
try {
|
|||
|
|
// 关联表增加事务处理
|
|||
|
|
return Db::transaction(function () use ($request, $data) {
|
|||
|
|
$data = $this->preProcess('Store', $data, $request);
|
|||
|
|
$dl = $this->getDataLimitFromRequest($request);
|
|||
|
|
foreach ($data as $item) {
|
|||
|
|
$created = $this->logicCreate($item, $dl);
|
|||
|
|
$this->processRequisitionItemReturnAttachment($created, $item['attachment']);
|
|||
|
|
}
|
|||
|
|
return $this->postProcess('Store', $created);
|
|||
|
|
});
|
|||
|
|
} catch (\Throwable $e) {
|
|||
|
|
throw new BusinessException("创建物品申请失败:" . $e->getMessage());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 更新物品归还申请前置处理
|
|||
|
|
*
|
|||
|
|
* @param array $data 归还申请数据
|
|||
|
|
* @param Request $request 请求对象
|
|||
|
|
* @return array 处理后的数据
|
|||
|
|
*/
|
|||
|
|
public function beforeServiceUpdate($data, Request $request): array
|
|||
|
|
{
|
|||
|
|
// // 模拟数据
|
|||
|
|
// $data = [
|
|||
|
|
// 'id' => 1,
|
|||
|
|
// 'requisition_item_id' => 9,
|
|||
|
|
// 'return_qty' => 1,
|
|||
|
|
// 'apply_remark' => '申请备注22',
|
|||
|
|
// 'attachment' => [
|
|||
|
|
// [
|
|||
|
|
// 'original_name' => '附件1.jpg',
|
|||
|
|
// 'file_path' => '/uploads/2023/07/01/attachment1.jpg'
|
|||
|
|
// ],
|
|||
|
|
// [
|
|||
|
|
// 'original_name' => '附件2.jpg',
|
|||
|
|
// 'file_path' => '/uploads/2023/07/01/attachment2.jpg'
|
|||
|
|
// ],
|
|||
|
|
// [
|
|||
|
|
// 'original_name' => '附件3.jpg',
|
|||
|
|
// 'file_path' => '/uploads/2023/07/01/attachment3.jpg'
|
|||
|
|
// ]
|
|||
|
|
// ]
|
|||
|
|
// ];
|
|||
|
|
|
|||
|
|
$data['return_at'] = time();
|
|||
|
|
return $data;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 更新物品归还申请
|
|||
|
|
*
|
|||
|
|
* @param Request $request
|
|||
|
|
* @param array $data
|
|||
|
|
* @return array
|
|||
|
|
*/
|
|||
|
|
public function serviceUpdate(Request $request, array $data, string $scene = '')
|
|||
|
|
{
|
|||
|
|
try {
|
|||
|
|
// 关联表增加事务处理
|
|||
|
|
return Db::transaction(function () use ($request, $data) {
|
|||
|
|
$data = $this->preProcess('Update', $data, $request);
|
|||
|
|
$config = $this->buildQueryConfig($request, 'update');
|
|||
|
|
|
|||
|
|
$attachment = $data['attachment'];
|
|||
|
|
unset($data['attachment']);
|
|||
|
|
|
|||
|
|
$this->logicUpdateBy($config, $data);
|
|||
|
|
// 获取更新后的完整记录
|
|||
|
|
$updated = $this->logicShow($config);
|
|||
|
|
// 处理附件更新
|
|||
|
|
$this->processRequisitionItemReturnAttachment($updated, $attachment, true);
|
|||
|
|
return $this->postProcess('Update', $updated);
|
|||
|
|
});
|
|||
|
|
} catch (\Throwable $e) {
|
|||
|
|
throw new BusinessException("更新物品归还申请失败:" . $e->getMessage());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 处理物品申请附件
|
|||
|
|
*
|
|||
|
|
* @param Model $created
|
|||
|
|
* @param array $items
|
|||
|
|
* @param bool $isUpdate
|
|||
|
|
* @return void
|
|||
|
|
*/
|
|||
|
|
private function processRequisitionItemReturnAttachment(Model $created, array $attachment, bool $isUpdate = false): void
|
|||
|
|
{
|
|||
|
|
if ($isUpdate) {
|
|||
|
|
$this->clearRequisitionItemReturnAttachment($created->id);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
foreach ($attachment as $file) {
|
|||
|
|
$attachmentData = [
|
|||
|
|
'return_id' => $created->id,
|
|||
|
|
'file_name' => $file['original_name'],
|
|||
|
|
'file_path' => $file['file_path']
|
|||
|
|
];
|
|||
|
|
InvRequisitionItemReturnAttachment::create($attachmentData);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 清空物品申请附件
|
|||
|
|
*
|
|||
|
|
* @param int $returnId
|
|||
|
|
* @return void
|
|||
|
|
*/
|
|||
|
|
private function clearRequisitionItemReturnAttachment(int $returnId): void
|
|||
|
|
{
|
|||
|
|
InvRequisitionItemReturnAttachment::where('return_id', $returnId)->delete();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
public function returnConfirm(Request $request, array $data, string $scene = '')
|
|||
|
|
{
|
|||
|
|
try {
|
|||
|
|
|
|||
|
|
// 关联表增加事务处理
|
|||
|
|
return Db::transaction(function () use ($request, $data) {
|
|||
|
|
$data = $this->preProcess('Update', $data, $request);
|
|||
|
|
$config = $this->buildQueryConfig($request, 'update');
|
|||
|
|
|
|||
|
|
$attachment = $data['attachment'];
|
|||
|
|
unset($data['attachment']);
|
|||
|
|
|
|||
|
|
$this->logicUpdateBy($config, $data);
|
|||
|
|
// 获取更新后的完整记录
|
|||
|
|
$updated = $this->logicShow($config);
|
|||
|
|
// 处理附件更新
|
|||
|
|
$this->processRequisitionItemReturnAttachment($updated, $attachment, true);
|
|||
|
|
return $this->postProcess('Update', $updated);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
} catch (\Throwable $e) {
|
|||
|
|
throw new BusinessException("确认归还物品失败:" . $e->getMessage());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|