126 lines
3.3 KiB
PHP
126 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace app\common\service;
|
|
|
|
use app\common\model\FinPaymentStock;
|
|
use app\common\validate\FinPaymentApplyValidate;
|
|
use app\common\traits\crud\CrudServiceTrait;
|
|
use app\common\model\FinPaymentApply;
|
|
use support\Redis;
|
|
use support\Request;
|
|
|
|
class FinPaymentApplyService
|
|
{
|
|
|
|
use CrudServiceTrait;
|
|
|
|
/**
|
|
* 指定当前要操作的模型
|
|
*/
|
|
protected string $modelClass = FinPaymentApply::class;
|
|
|
|
/**
|
|
* 验证器
|
|
*/
|
|
protected $validator = FinPaymentApplyValidate::class;
|
|
|
|
/**
|
|
* 获取验证器
|
|
*/
|
|
public function getValidator()
|
|
{
|
|
return new $this->validator;
|
|
}
|
|
|
|
/**
|
|
* 构建查询条件
|
|
*/
|
|
protected function buildQueryConfig(Request $request, string $scene = 'index'): array
|
|
{
|
|
$p = $request->all();
|
|
|
|
$config = [];
|
|
$config['model'] = $this->modelClass;
|
|
switch ($scene) {
|
|
case 'index':
|
|
// ====== ====== 排序 ====== ======
|
|
$config['order']['id'] = 'desc';
|
|
break;
|
|
case 'show':
|
|
$config['where']['id'] = $p['id'];
|
|
break;
|
|
case 'update':
|
|
case 'destroy':
|
|
$config['where']['id'] = $p['id'];
|
|
break;
|
|
case 'store':
|
|
break;
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
|
|
public function beforeServiceStore($data, Request $request)
|
|
{
|
|
$data['apply_no'] = $this->generateUniqueApplyNo();
|
|
$data['doc_user_id'] = $request->user->uid;
|
|
$data['submit_files'] = json_encode($data['submit_files']);
|
|
redis::set('stock_' . $data['doc_user_id'], json_encode($request->post('stock')), 'EX', 30);
|
|
return $data;
|
|
}
|
|
|
|
|
|
public function afterServiceStore($res)
|
|
{
|
|
$data = [
|
|
'payment_apply_id' => $res['id'],
|
|
];
|
|
$stock = json_decode(redis::get('stock_' . $res['doc_user_id']), true);
|
|
redis::del('stock' . $res['doc_user_id']);
|
|
foreach ($stock as $k => $v) {
|
|
$data['stock_id'] = $v['stock_id'];
|
|
$data['pi_no'] = $v['pi_on'];
|
|
FinPaymentStock::create($data);
|
|
}
|
|
}
|
|
|
|
public function beforeServiceUpdate($data, Request $request)
|
|
{
|
|
$data['submit_files'] = json_encode($data['submit_files']);
|
|
return $data;
|
|
}
|
|
|
|
|
|
/**
|
|
* 生成唯一的付款申请编码
|
|
* 格式:PA + 日期 + 6位字母数字组合
|
|
*
|
|
* @return string 唯一编码
|
|
*/
|
|
public function generateUniqueApplyNo()
|
|
{
|
|
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
$maxAttempts = 10; // 防止无限循环
|
|
$secondChar = 'PA' . date('Ymd');
|
|
|
|
for ($attempt = 0; $attempt < $maxAttempts; $attempt++) {
|
|
// 生成后6位随机字符
|
|
$suffix = '';
|
|
for ($i = 0; $i < 6; $i++) {
|
|
$suffix .= $chars[rand(0, strlen($chars) - 1)];
|
|
}
|
|
|
|
$applyNo = $secondChar . $suffix;
|
|
|
|
// 检查是否已存在
|
|
$existing = FinPaymentApply::where('apply_no', $applyNo)->first();
|
|
if (!$existing) {
|
|
return $applyNo;
|
|
}
|
|
}
|
|
|
|
// 如果1000次尝试后仍然重复,使用时间戳确保唯一性
|
|
$timestamp = substr(time(), -6);
|
|
return $secondChar . $timestamp;
|
|
}
|
|
} |