Files
fdesk/app/common/service/FinReceiptService.php
T

275 lines
8.6 KiB
PHP
Raw Normal View History

2025-09-23 15:13:56 +08:00
<?php
namespace app\common\service;
2025-10-27 10:57:47 +08:00
use app\common\logic\FinReceiptLogic;
2025-09-23 16:45:06 +08:00
use app\common\model\FinReceiptHis;
2025-09-23 15:13:56 +08:00
use app\common\validate\FinReceiptValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\FinReceipt;
2025-10-27 10:57:47 +08:00
use support\exception\BusinessException;
2025-09-23 15:13:56 +08:00
use support\Request;
class FinReceiptService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = FinReceipt::class;
/**
* 验证器
*/
protected $validator = FinReceiptValidate::class;
2025-10-27 10:57:47 +08:00
protected $logic;
public function __construct()
{
$this->logic = (new FinReceiptLogic());
}
2025-09-23 15:13:56 +08:00
/**
* 获取验证器
*/
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';
// ====== ====== 筛选 ====== ======
// 汇款国家
if (isset($p['country_id']) && !empty($p['country_id'])) {
$config['where']['country_id'] = $p['country_id'];
}
2025-09-23 16:45:06 +08:00
// 没有付款记录的收款记录
if (isset($p['payment_apply']) && $p['payment_apply'] == 'not') {
2025-09-24 10:16:34 +08:00
$config['where']['payment_apply_id'] = ['null'];
} elseif (isset($p['payment_apply']) && $p['payment_apply'] == 'already') {
2025-09-23 16:45:06 +08:00
$config['where']['payment_apply_id'] = ['not null'];
}
2025-09-25 17:48:31 +08:00
// 收款申请筛选
2025-09-23 16:45:06 +08:00
if (isset($p['payment_apply_id']) && !empty($p['payment_apply_id'])) {
$config['where']['payment_apply_id'] = $p['payment_apply_id'];
}
2025-09-23 15:13:56 +08:00
// 收款账户筛选
if (isset($p['account_id']) && !empty($p['account_id'])) {
$config['where']['account_id'] = $p['account_id'];
}
2025-09-23 16:45:06 +08:00
2025-09-23 15:13:56 +08:00
// 关键字
2025-10-23 11:27:24 +08:00
if (isset($p['keyword']) && $p['keyword']) {
2025-09-23 15:13:56 +08:00
$k = '%' . $p['keyword'] . '%';
$config['orWhere'][] = ['remitter', 'like', $k];
}
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)
{
if(isset($data['payment_apply_id']) && empty($data['payment_apply_id'])){
unset($data['payment_apply_id']);
}
$data['receipt_no'] = $this->generateUniqueReceipt();
2025-09-23 18:38:02 +08:00
$data['arrived_at'] = strtotime( $data['arrived_at']);
$data['created_by'] = $request->user->uid;
2025-09-23 15:14:08 +08:00
$data['updated_by'] = $request->user->uid;
2025-09-23 15:13:56 +08:00
return $data;
}
2025-09-23 16:45:06 +08:00
public function afterServiceStore($res)
{
$receipt = FinReceipt::where('id',$res->id)->first()->toArray();
$receipt = array_merge($receipt,['receipt_id' => $res->id]);
unset($receipt['id']);
FinReceiptHis::create($receipt);
}
2025-09-23 15:13:56 +08:00
public function beforeServiceUpdate($data, Request $request)
{
2025-09-23 15:14:08 +08:00
$data['updated_by'] = $request->user->uid;
2025-09-24 10:16:34 +08:00
if(isset($data['arrived_at']))
$data['arrived_at'] = strtotime($data['arrived_at']);
2025-09-23 18:38:02 +08:00
if(isset($data['received_at']))
$data['received_at'] = strtotime($data['received_at']);
2025-09-23 15:13:56 +08:00
return $data;
}
2025-09-23 16:45:06 +08:00
public function afterServiceUpdate($res)
{
2025-09-24 10:16:34 +08:00
$receipt = FinReceipt::where('id',$res['id'])->first()->toArray();
$receipt = array_merge($receipt,['receipt_id' => $res['id']]);
2025-09-23 16:45:06 +08:00
unset($receipt['id']);
FinReceiptHis::create($receipt);
}
2025-09-23 15:13:56 +08:00
/**
* 生成唯一的付款申请编码
* 格式:PA + 日期 + 6位字母数字组合
*
* @return string 唯一编码
*/
public function generateUniqueReceipt()
{
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$maxAttempts = 10; // 防止无限循环
$secondChar = 'RE' . date('Ymd');
for ($attempt = 0; $attempt < $maxAttempts; $attempt++) {
// 生成后6位随机字符
$suffix = '';
for ($i = 0; $i < 6; $i++) {
$suffix .= $chars[rand(0, strlen($chars) - 1)];
}
$ReceiptNo = $secondChar . $suffix;
// 检查是否已存在
$existing = FinReceipt::where('receipt_no', $ReceiptNo)->first();
if (!$existing) {
return $ReceiptNo;
}
}
// 如果1000次尝试后仍然重复,使用时间戳确保唯一性
$timestamp = substr(time(), -6);
return $secondChar . $timestamp;
}
2025-09-25 17:48:31 +08:00
/**
* 根据相似度排序付款记录
* @param Request $request
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Pagination\LengthAwarePaginator
*/
public function complexQueryIndex(Request $request)
{
$limit = (int)$request->get('size', 15);
$page = (int)$request->get('current', 1);
$param = $request->all();
$where = [];
// 汇款国家
if (isset($param['country_id']) && !empty($param['country_id'])) {
$where[] = ['country_id', $param['country_id']];
}
// 收款账户筛选
if (isset($param['account_id']) && !empty($param['account_id'])) {
$where[] = ['account_id', $param['account_id']];
}
// 收款申请筛选
if (isset($param['payment_apply_id']) && !empty($param['payment_apply_id'])) {
$where[]['payment_apply_id'] = $param['payment_apply_id'];
}
// 关键字
if (isset($param['keyword']) && $param['keyword']) {
$k = '%' . $param['keyword'] . '%';
$where[] = ['remitter', 'like', $k];
}
// 构建查询
$query = $this->modelClass::where($where);
// 没有付款记录的收款记录
if (isset($param['payment_apply']) && $param['payment_apply'] == 'not') {
$query->whereNull('payment_apply_id');
} elseif (isset($param['payment_apply']) && $param['payment_apply'] == 'already') {
$query->whereNotNull('payment_apply_id');
}
if (isset($param['scene']) && $param['scene'] == 'list') {
return $query->paginate($limit, ['*'], 'page', $page);
}
// 计算七天前的时间戳
$sevenDaysAgo = time() - (10 * 24 * 60 * 60);
// 构建相似度排序逻辑
$similarityOrder = [];
// 如果有传入参数,计算相似度
if (isset($param['account_id']) && !empty($param['account_id'])) {
$similarityOrder[] = "CASE WHEN account_id = {$param['account_id']} THEN 0 ELSE 1 END";
}
if (isset($param['country_id']) && !empty($param['country_id'])) {
$similarityOrder[] = "CASE WHEN country_id = {$param['country_id']} THEN 0 ELSE 1 END";
}
if (isset($param['keyword']) && $param['keyword']) {
$keyword = addslashes($param['keyword']);
$similarityOrder[] = "CASE WHEN remitter LIKE '%{$keyword}%' THEN 0 ELSE 1 END";
}
if (isset($param['amount']) && !empty($param['amount'])) {
$amount = (float)$param['amount'];
$similarityOrder[] = "ABS(amount - {$amount})";
}
// 构建排序SQL
$orderByClause = "";
if (!empty($similarityOrder)) {
$orderByClause = "CASE
WHEN created_at >= {$sevenDaysAgo} THEN
(" . implode(' + ', $similarityOrder) . ")
ELSE 999999
END ASC, ";
}
$orderByClause .= "CASE
WHEN created_at < {$sevenDaysAgo} THEN id
ELSE 0
END DESC";
$query->orderByRaw($orderByClause);
return $query->paginate($limit, ['*'], 'page', $page);
}
2025-10-27 10:57:47 +08:00
public function credit($id)
{
$finReceipt = FinReceipt::find($id);
if(!empty($finReceipt->received_at)){
throw new BusinessException('该记录已将确认入账');
}
2025-10-27 14:52:04 +08:00
$res = $this->logic->credit($id);
2025-10-27 11:57:00 +08:00
$this->afterServiceUpdate(['id'=>$id]);
2025-10-27 14:52:04 +08:00
return $res;
2025-10-27 10:57:47 +08:00
}
2025-09-23 15:13:56 +08:00
}