WorkerAccountLogLogic.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace app\common\logic;
  3. use app\common\enum\worker\WorkerAccountLogEnum;
  4. use app\common\model\master_worker\MasterWorker;
  5. use app\common\model\master_worker\MasterWorkerAccountLog;
  6. use think\Exception;
  7. class WorkerAccountLogLogic extends BaseLogic
  8. {
  9. /**
  10. * @param $work //工单对象
  11. * @param $change_amount //变动数量
  12. * @param $change_type //变动类型
  13. * @param $action //动作 1-增加 2-减少
  14. * @return bool|void
  15. * @throws Exception
  16. */
  17. public static function addAccountLog($work,$changeAmount,$changeType,$action, string $remark = '', array $extra = [])
  18. {
  19. $worker = MasterWorker::findOrFail($work->master_worker_id);
  20. if($worker->isEmpty()) {
  21. throw new Exception('师傅不存在');
  22. }
  23. $changeObject = WorkerAccountLogEnum::getChangeObject($changeType);
  24. if(!$changeObject) {
  25. throw new Exception('结算错误');
  26. }
  27. switch ($changeObject) {
  28. // 用户余额
  29. case WorkerAccountLogEnum::UM:
  30. $left_amount = $worker->user_money;
  31. break;
  32. // 其他
  33. }
  34. $data = [
  35. 'sn' => generate_sn(MasterWorkerAccountLog::class, 'sn', 20),
  36. 'title'=>$work->title,
  37. 'worker_id' => $work->master_worker_id,
  38. 'change_object' => $changeObject,
  39. 'change_type' => $changeType,
  40. 'action' => $action,
  41. 'left_amount' => $left_amount,
  42. 'change_amount' => $changeAmount,
  43. 'work_sn' => $work->work_sn,
  44. 'remark' => $remark,
  45. 'extra' => $extra ? json_encode($extra, JSON_UNESCAPED_UNICODE) : '',
  46. ];
  47. MasterWorkerAccountLog::create($data);
  48. $worker->user_money = $left_amount + ($action==1?$changeAmount:-$changeAmount);
  49. $worker->save();
  50. return true;
  51. }
  52. }