MchAccountLog.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. /**
  10. * This is the model class for table "{{%mch_account_log}}".
  11. *
  12. */
  13. class MchAccountLog extends \yii\db\ActiveRecord
  14. {
  15. /**
  16. * @inheritdoc
  17. */
  18. public static function tableName()
  19. {
  20. return '{{%mch_account_log}}';
  21. }
  22. public static function saveLog($mch_id, $user_id, $amount, $log_type, $order_id = 0, $order_no = '', $desc = "", $profit = 0, $isTotal = 1)
  23. {
  24. $t = \Yii::$app->db->beginTransaction();
  25. try{
  26. $form = new self();
  27. $mch = Mch::findOne($mch_id);
  28. $form->store_id = $mch->store_id;
  29. $form->user_id = $user_id;
  30. $form->mch_id = $mch_id;
  31. $form->amount = $amount;
  32. $form->before = $mch->account_money;
  33. if ($log_type == AccountLog::LOG_TYPE_INCOME) {
  34. $form->after = $form->before + $amount;
  35. } else {
  36. $form->after = $form->before - $amount;
  37. }
  38. $form->desc = $desc;
  39. $form->order_id = $order_id;
  40. $form->order_no = $order_no;
  41. $form->operator = '';
  42. $form->operator_id = 0;
  43. $form->log_type = $log_type;
  44. $form->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  45. $form->created_at = time();
  46. if ($profit) {
  47. $form->profit = $profit;
  48. }
  49. if (!$form->save()) {
  50. \Yii::error([__METHOD__, $form->getErrors()]);
  51. throw new \Exception('入驻商佣金发放log保存失败' . array_shift($form->getFirstErrors()));
  52. }
  53. if ($log_type == AccountLog::LOG_TYPE_INCOME) {
  54. if($isTotal){
  55. $mch->account_money_total += $amount;
  56. }
  57. $mch->account_money += $amount;
  58. } else {
  59. $mch->account_money -= $amount;
  60. if($mch->account_money < 0){
  61. \Yii::error([__METHOD__, $mch->getErrors()]);
  62. throw new \Exception('入驻商佣金不足' . array_shift($mch->getFirstErrors()));
  63. }
  64. }
  65. $save = $mch->save();
  66. if(!$save){
  67. \Yii::error([__METHOD__, $mch->getErrors()]);
  68. throw new \Exception('入驻商佣金修改失败' . array_shift($mch->getFirstErrors()));
  69. }
  70. $t->commit();
  71. return [
  72. 'code' => 0,
  73. 'msg' => 'ok',
  74. ];
  75. } catch (\Exception $ex) {
  76. $t->rollBack();
  77. return [
  78. 'code' => 1,
  79. 'msg' => '操作失败,' . $ex->getMessage(),
  80. ];
  81. }
  82. }
  83. }