From f1525293645db5ba76bcb02f96914117fa4e65e5 Mon Sep 17 00:00:00 2001 From: mzl Date: Mon, 15 Sep 2025 15:01:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=8E=A8=E9=80=81=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=82=AE=E7=AE=B1&&=E5=A2=9E=E5=8A=A0=E7=BB=88?= =?UTF-8?q?=E7=89=88=E4=BF=9D=E5=AD=98=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/model/SysDict.php | 28 ++++++++++++ app/common/model/SysDictItem.php | 8 ++++ .../service/MessageNotificationsService.php | 10 +++++ app/common/service/StockOrdersService.php | 2 +- app/common/service/StockOrdersService_bak.php | 2 +- app/common/service/SysDictService.php | 22 ++++++++++ .../approval/service/ApprovalFlowService.php | 43 ++++++++++++------- app/controller/IndexController.php | 28 +++++++++++- app/crm/controller/StockOrdersController.php | 16 +++++++ app/crm/service/StockOrdersService.php | 26 ++++++++++- app/crm/service/StockOrdersService_bak.php | 2 +- app/functions.php | 28 ++++++++++++ app/queue/redis/Notice.php | 17 +++++++- config/route.php | 1 + 14 files changed, 209 insertions(+), 24 deletions(-) diff --git a/app/common/model/SysDict.php b/app/common/model/SysDict.php index 02534df..70e604a 100644 --- a/app/common/model/SysDict.php +++ b/app/common/model/SysDict.php @@ -62,4 +62,32 @@ class SysDict extends Model { return $this->belongsTo(SysDictCategory::class, 'category_id', 'id'); } + + /** + * 一个字典有多个字典项 + */ + public function items() + { + return $this->hasMany(SysDictItem::class, 'dict_id', 'id'); + } + + /** + * 通过 dict_code 快速获取所有 item_value + */ + public function scopeGetItemValuesByCode($query, $dictCode) + { + return $query->where('dict_code', $dictCode) + ->with('items') // 预加载关联项 + ->first()?->items->pluck('item_value'); // 返回 item_value 数组 + } + + /** + * 通过 dict_code 获取完整字典项(含 label + value) + */ + public function scopeGetItemsByCode($query, $dictCode) + { + return $query->where('dict_code', $dictCode) + ->with('items') + ->first()?->items; + } } \ No newline at end of file diff --git a/app/common/model/SysDictItem.php b/app/common/model/SysDictItem.php index f53a7a9..8fbceb9 100644 --- a/app/common/model/SysDictItem.php +++ b/app/common/model/SysDictItem.php @@ -56,4 +56,12 @@ class SysDictItem extends Model { return $this->belongsTo(SysDict::class, 'dict_id', 'id'); } + + /** + * 字典项属于一个字典 + */ + public function dict() + { + return $this->belongsTo(SysDict::class, 'dict_id', 'id'); + } } \ No newline at end of file diff --git a/app/common/service/MessageNotificationsService.php b/app/common/service/MessageNotificationsService.php index 806bc1d..a225a73 100644 --- a/app/common/service/MessageNotificationsService.php +++ b/app/common/service/MessageNotificationsService.php @@ -273,4 +273,14 @@ class MessageNotificationsService { return User::where('status', 1)->pluck('id')->toArray(); } + + + public function getUserEmailsById(array $con = []): array + { + $user = User::where('status', 1); + if (count($con) > 0) { + return $user->whereIn('id', $con)->where('email', '!=', '')->pluck('email')->toArray(); + } + return $user->where('email', '!=', '')->pluck('email')->toArray(); + } } \ No newline at end of file diff --git a/app/common/service/StockOrdersService.php b/app/common/service/StockOrdersService.php index ccc1603..ea78b6f 100644 --- a/app/common/service/StockOrdersService.php +++ b/app/common/service/StockOrdersService.php @@ -140,7 +140,7 @@ class StockOrdersService { // 启动审批 $appro = new \app\common\utils\approval\service\ApprovalFlowService(); - $appro->start('stock_order_flow', 'stock_orders', $item->id, $item->toArray(), $item->salesperson_id); + $appro->start('stock_orders', 'stock_orders', $item->id, $item->toArray(), $item->salesperson_id); // 异步生成pdf文件 \Webman\RedisQueue\Redis::send('stock-order-pdf', ['id'=>$item->id]); } diff --git a/app/common/service/StockOrdersService_bak.php b/app/common/service/StockOrdersService_bak.php index dfaffda..65ef296 100644 --- a/app/common/service/StockOrdersService_bak.php +++ b/app/common/service/StockOrdersService_bak.php @@ -227,7 +227,7 @@ class StockOrdersService_bak } // 启动审批 $appro = new \app\common\utils\approval\service\ApprovalFlowService(); - $result = $appro->start('stock_order_flow', 'stock_orders', $res->id, $res->toArray(), $res->salesperson_id); + $result = $appro->start('stock_orders', 'stock_orders', $res->id, $res->toArray(), $res->salesperson_id); var_dump($result); // 异步生成pdf文件 diff --git a/app/common/service/SysDictService.php b/app/common/service/SysDictService.php index 57ab33c..4f22070 100644 --- a/app/common/service/SysDictService.php +++ b/app/common/service/SysDictService.php @@ -25,4 +25,26 @@ class SysDictService { return $this->logic->getList(); } + + + + public static function pluckValuesByCode(string $dictCode): \Illuminate\Support\Collection + { + return SysDict::where('dict_code', $dictCode) + ->where('status', 1) + ->first()?->items() + ->where('status', 1) + ->orderBy('sort') + ->pluck('item_value') ?? collect(); + } + + public static function pluckKeyValueByCode(string $dictCode): \Illuminate\Support\Collection + { + return SysDict::where('dict_code', $dictCode) + ->where('status', 1) + ->first()?->items() + ->where('status', 1) + ->orderBy('sort') + ->pluck('item_value', 'item_label') ?? collect(); + } } \ No newline at end of file diff --git a/app/common/utils/approval/service/ApprovalFlowService.php b/app/common/utils/approval/service/ApprovalFlowService.php index b3bc2e9..2854e4e 100644 --- a/app/common/utils/approval/service/ApprovalFlowService.php +++ b/app/common/utils/approval/service/ApprovalFlowService.php @@ -565,23 +565,34 @@ class ApprovalFlowService /** 发送通知(接你自己的消息系统即可) */ private function sendNotification(array $userIds, object $instance, string $action, bool $isCC): void { - $map = ['pending'=>'待审批','approve'=>'已通过','reject'=>'已拒绝','revoked'=>'已撤回']; - $prefix = $isCC ? '[抄送]' : ''; - $text = $map[$action] ?? $action; - $msg = "{$prefix}您有一条审批 {$text}:流程ID {$instance->id},类型 {$instance->flow_type}"; - // MessageService::send($userIds, $msg); - - $pushData = [ - 'sender_id' => 0, - 'sender_type' => 'system', - 'title' => $msg, - 'content' => $msg, - 'scene' => 1, - 'recipient' => $userIds, - 'push_method' => 1 + $map = [ + 'pending' => '待审批', + 'approve' => '已通过', + 'reject' => '已拒绝', + 'revoked' => '已撤回' ]; - $msgId = (new MessageNotificationsService())->pushNotice($pushData); - var_dump($msgId); + $prefix = $isCC ? '【抄送】' : '【审批】'; + $text = $map[$action] ?? $action; + + $approvalType = array_flip(\app\common\service\SysDictService::pluckKeyValueByCode('approval_type')->toArray()); + + switch ($instance->flow_type) { + case 'stock_orders': // 备货单 + $stockOrder = \app\common\model\StockOrders::find($instance->biz_id); + $title = $prefix."备货单:{$stockOrder->order_no}"; + $flowType = $approvalType[$instance->flow_type]; + $content = "{$prefix}你有一条审批 {$text}:类型 {$flowType},业务单号 {$stockOrder->order_no}"; + break; + case 'stock_orders_nullify': // 备货单作废 + break; + } + + noticePush([ + 'title' => $title, + 'content' => $content, + 'recipient' => $userIds, + 'email' => 1 + ]); } private function notifyInitiator(object $instance, string $result): void diff --git a/app/controller/IndexController.php b/app/controller/IndexController.php index 9779886..319aa49 100644 --- a/app/controller/IndexController.php +++ b/app/controller/IndexController.php @@ -2,6 +2,7 @@ namespace app\controller; +use app\common\service\SysDictService; use app\common\service\UploadService; use app\common\utils\scaffold\Generate; use support\Request; @@ -100,7 +101,7 @@ class IndexController { - // $flowType = 'stock_order_flow'; + // $flowType = 'stock_orders'; // $bizType = 'stock_orders'; // $bizId = 32; // $formData = []; @@ -133,7 +134,30 @@ class IndexController // $result = $service->createdPdfFile($param['id']); // return json($result); - \Webman\RedisQueue\Redis::send('stock-order-pdf', ['id'=>82]); + // \Webman\RedisQueue\Redis::send('stock-order-pdf', ['id'=>82]); + + + // $approvalType = array_flip(\app\common\service\SysDictService::pluckKeyValueByCode('approval_type')->toArray()); + // $result = $approvalType['stock_orders']; + // var_dump($result); + + + // $list = \app\common\model\User::where('status', 1)->where('email', '!=', '')->pluck('email')->toArray(); + + // var_dump($list); + + $approvalType = array_flip(\app\common\service\SysDictService::pluckKeyValueByCode('approval_type')->toArray()); + + $title = "【审批】备货单:BH20250911001"; + $flowType = $approvalType['stock_orders']; + $content = "【审批】你有一条审批 待审批:类型 {$flowType},业务单号 BH20250911001"; + + noticePush([ + 'title' => $title, + 'content' => $content, + 'recipient' => [1], + 'email' => 1 + ]); } catch (\Throwable $th) { //throw $th; var_dump($th->getMessage().$th->getLine().$th->getFile()); diff --git a/app/crm/controller/StockOrdersController.php b/app/crm/controller/StockOrdersController.php index b4d2034..77f8586 100644 --- a/app/crm/controller/StockOrdersController.php +++ b/app/crm/controller/StockOrdersController.php @@ -182,6 +182,22 @@ class StockOrdersController extends BaseController } } + /** + * 最终审批 + */ + public function ultimateApproval(Request $request) + { + try { + $raw = $this->service->serviceUltimateApproval($request); + $data = $this->transformData($raw, method_exists($this, 'approvalFormat') + ? [$this, 'approvalFormat'] + : null); + return $this->success($data); + } catch (\Throwable $e) { + return $this->error($e->getMessage()); + } + } + public function desc(Request $request) { diff --git a/app/crm/service/StockOrdersService.php b/app/crm/service/StockOrdersService.php index 7713c89..df4085a 100644 --- a/app/crm/service/StockOrdersService.php +++ b/app/crm/service/StockOrdersService.php @@ -15,6 +15,7 @@ use support\Db; use support\Model; use app\admin\model\ApprovalInstances; use app\admin\model\ApprovalRecords; +use support\exception\BusinessException; class StockOrdersService { @@ -140,7 +141,7 @@ class StockOrdersService { // 启动审批 $appro = new \app\common\utils\approval\service\ApprovalFlowService(); - $appro->start('stock_order_flow', 'stock_orders', $item->id, $item->toArray(), $item->salesperson_id); + $appro->start('stock_orders', 'stock_orders', $item->id, $item->toArray(), $item->salesperson_id); // 异步生成pdf文件 \Webman\RedisQueue\Redis::send('stock-order-pdf', ['id'=>$item->id]); } @@ -393,6 +394,29 @@ class StockOrdersService return $this->postProcess('Approval', $item); } + /** + * 最终审批 + */ + public function serviceUltimateApproval(Request $request) + { + $config = $this->buildQueryConfig($request, 'detail'); + + $item = $this->logicShow($config); + foreach ($item->items as $k => $v) { + if ($v->vin === '') { + throw new BusinessException('存在未填写vin的车辆,无法完成提交审核'); + } + } + + + $this->logicUpdateBy($config, [ + 'approval_status' => 1 + ]); + // $item = $this->logicShow($config); + $this->startApproval($item); + return $this->postProcess('UltimateApproval', $item); + } + /** * 创建 * diff --git a/app/crm/service/StockOrdersService_bak.php b/app/crm/service/StockOrdersService_bak.php index 356fcca..6e1f287 100644 --- a/app/crm/service/StockOrdersService_bak.php +++ b/app/crm/service/StockOrdersService_bak.php @@ -236,7 +236,7 @@ class StockOrdersService_bak } // 启动审批 $appro = new \app\common\utils\approval\service\ApprovalFlowService(); - $result = $appro->start('stock_order_flow', 'stock_orders', $res->id, $res->toArray(), $res->salesperson_id); + $result = $appro->start('stock_orders', 'stock_orders', $res->id, $res->toArray(), $res->salesperson_id); var_dump($result); // 异步生成pdf文件 diff --git a/app/functions.php b/app/functions.php index d12b5ae..7ae0d0a 100644 --- a/app/functions.php +++ b/app/functions.php @@ -359,4 +359,32 @@ if (!function_exists('generateStockNo')) { return $prefix . $date . $paddedSerial; } +} + +/** + * 发送通知 + * + * @param string $title + * @param string $content + * @param string $url + * @param string $type + * @return void + */ +if (!function_exists('noticePush')) { + function noticePush(array $notice = []) + { + /* + 消息字段: + 'sender_id' //发送人 + 'sender_type' //发送人类型 + 'title' //标题 + 'content' //内容 + 'scene' //场景 + 'recipient' //接收人 + 'push_method' //推送方式 + // 如果需要同时 邮箱推送 增加推送邮箱字段 + 'email' //邮箱 + */ + \Webman\RedisQueue\Redis::send('notice', $notice); + } } \ No newline at end of file diff --git a/app/queue/redis/Notice.php b/app/queue/redis/Notice.php index 3a310c8..f166085 100644 --- a/app/queue/redis/Notice.php +++ b/app/queue/redis/Notice.php @@ -17,10 +17,23 @@ class Notice implements Consumer public function consume($data) { try { - (new \app\common\service\MessageNotificationsService())->pushNotice($data); + // 应用消息 + $service = new \app\common\service\MessageNotificationsService(); + $service->pushNotice($data); + + // 邮件消息 + if (isset($data['email']) && $data['email'] === 1) { + $emails = $service->getUserEmailsById($data['recipient']); + foreach ($emails as $email) { + if (empty($email)) { + continue; + } + \app\common\utils\email\MailSend::send($email, $data['title'], $data['content']); + } + } return true; } catch (\Throwable $th) { - var_dump('4 :'.$th->getFile()."|".$th->getLine()."|".$th->getMessage()); + flg("消息推送异常:".$th->getFile()."|".$th->getLine()."|".$th->getMessage()); throw new BusinessException($th->getFile()."|".$th->getLine()."|".$th->getMessage()); } } diff --git a/config/route.php b/config/route.php index 61f16eb..d840743 100644 --- a/config/route.php +++ b/config/route.php @@ -387,6 +387,7 @@ Route::group('/crm', function () { Route::any('/print', [app\crm\controller\StockOrdersController::class, 'print']); Route::any('/approval', [app\crm\controller\StockOrdersController::class, 'approval']); Route::any('/detail', [app\crm\controller\StockOrdersController::class, 'detail']); + Route::any('/ultimate/approval', [app\crm\controller\StockOrdersController::class, 'ultimateApproval']); }); // 审批记录