BalanceRechargeLog.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace common\models\user;
  3. use common\models\base\BaseActiveRecord;
  4. use services\common\UserWalletService;
  5. use common\enums\RedisKeyEnum;
  6. use common\enums\UserWalletEnum;
  7. use common\helpers\LockHelper;
  8. use common\enums\StatusEnum;
  9. use common\models\user\User;
  10. use Exception;
  11. use Yii;
  12. use yii\helpers\Json;
  13. /**
  14. * This is the model class for table "{{%balance_recharge_log}}".
  15. *
  16. * @property int $id
  17. * @property int $mall_id
  18. * @property int $user_id
  19. * @property float $num 充值金额
  20. * @property int $withdraw_status 申请状态 0--申请 1--同意 2--驳回
  21. * @property string $remark 备注
  22. * @property string $payment_evidence 支付凭证
  23. * @property int $status 状态[0禁用 1启用 -1删除]
  24. * @property int $created_at 添加时间戳
  25. * @property int $updated_at 更新时间戳
  26. */
  27. class BalanceRechargeLog extends BaseActiveRecord
  28. {
  29. const STATUS_APPLY = 0;
  30. const STATUS_AGREE = 1;
  31. const STATUS_REJECT = 2;
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public static function tableName()
  36. {
  37. return '{{%balance_recharge_log}}';
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['mall_id', 'user_id'], 'required'],
  46. [['mall_id', 'user_id', 'withdraw_status', 'status', 'created_at', 'updated_at'], 'integer'],
  47. [['num'], 'number'],
  48. [['remark', 'payment_evidence'], 'string', 'max' => 255],
  49. ];
  50. }
  51. public function getUser(): \yii\db\ActiveQuery
  52. {
  53. return $this->hasOne(User::class, ['id' => 'user_id']);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'ID',
  62. 'mall_id' => 'Mall ID',
  63. 'user_id' => 'User ID',
  64. 'num' => 'Num',
  65. 'withdraw_status' => 'Withdraw Status',
  66. 'remark' => 'Remark',
  67. 'payment_evidence' => 'Payment Evidence',
  68. 'status' => 'Status',
  69. 'created_at' => 'Created At',
  70. 'updated_at' => 'Updated At',
  71. ];
  72. }
  73. /**
  74. * 保存数据
  75. * @param array $params
  76. * @return bool|UserWallet
  77. */
  78. public static function setData(array $params){
  79. try{
  80. if(!empty($params['user_id'])){
  81. $exist = self::find()->where(['user_id'=>$params['user_id'],'withdraw_status'=>BalanceRechargeLog::STATUS_APPLY])->select('id')->one();
  82. if($exist){
  83. throw new \Exception('尚有未审核的申请!');
  84. }
  85. }
  86. if(!empty($params['id'])){
  87. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
  88. if(empty($model)) throw new \Exception('数据不存在或已删除');
  89. }else{
  90. $model = new self();
  91. $model->mall_id = $params['mall_id'];
  92. $model->loadDefaultValues();
  93. }
  94. if(!$model->load($params,'') || !$model->save()){
  95. throw new Exception($model->getErrorMessage());
  96. }
  97. return $model->toArray();
  98. }catch(\Exception $e){
  99. self::$static_error = $e->getMessage();
  100. return false;
  101. }
  102. }
  103. /**
  104. * @throws Exception
  105. */
  106. public static function updateLog($params)
  107. {
  108. $lock_key = RedisKeyEnum::suffix('BalanceRecharge', $params['id']);
  109. $identification = uniqid();
  110. if (!LockHelper::lock($lock_key, $identification, 100, 100)) {
  111. \Yii::error('充值审核 获取锁(' . $lock_key . ')失败阻塞,充值审核');
  112. return false;
  113. }
  114. $where = [['id' => $params['id']]];
  115. $log = self::getOne($where);
  116. if (!$log) {
  117. throw new \Exception('记录不存在');
  118. }
  119. if ($log->withdraw_status == 1) {
  120. throw new \Exception('审核已通过');
  121. }
  122. if ($log->withdraw_status == 2) {
  123. throw new \Exception('审核已驳回');
  124. }
  125. if ($params['status'] <= $log->withdraw_status) {
  126. throw new \Exception('状态错误, 请刷新重试');
  127. }
  128. $t = \Yii::$app->db->beginTransaction();
  129. try {
  130. switch ($params['status']) {// 1=提现申请通过,2=拒绝提现
  131. case 1:
  132. self::remit($log,$params);
  133. break;
  134. case 2:
  135. self::reject($log,$params);
  136. break;
  137. default:
  138. throw new \Exception('错误的类型');
  139. }
  140. $t->commit();
  141. LockHelper::uLock($lock_key, $identification);
  142. } catch (\Exception $exception) {
  143. $t->rollBack();
  144. LockHelper::uLock($lock_key, $identification);
  145. \Yii::error("BalanceRecharge error=" . $exception->getFile() . ";line:" . $exception->getLine() . ";message:" . $exception->getMessage());
  146. throw new \Exception($exception->getMessage());
  147. }
  148. return true;
  149. }
  150. /**
  151. * @throws Exception
  152. */
  153. private static function remit($log,$params)
  154. {
  155. $userInfo = User::getOne([['id' => $log->user_id, 'status' => StatusEnum::ENABLED]]);
  156. if (!$userInfo) {
  157. throw new \Exception('该用户不存在!');
  158. }
  159. $desc = "用户线下充值到余额:" . $log->num . "元";
  160. $data = [
  161. 'message_id' => 0,
  162. 'mall_id' => $log->mall_id, 'user_id' => $log->user_id,
  163. 'wallet_type' => UserWalletService::WALLET_TYPE_BALANCE,
  164. 'is_frozen' => false, 'unfrozen' => false, 'money' => abs($log->num), 'desc' => $desc,
  165. 'source_table' => self::tableName(), 'source_table_id' => $log->id,
  166. 'from_type' => UserWalletEnum::RECHARGE,
  167. 'capital_type' => UserWalletEnum::RECHARGE_OFFLINE,
  168. ];
  169. $res = UserWalletService::handle(0, $data);
  170. if ($res) {
  171. $log->withdraw_status = BalanceRechargeLog::STATUS_AGREE;
  172. if(!empty($params['remark'])){
  173. $log->remark = $params['remark'];
  174. }
  175. if(!$log->save()){
  176. throw new \Exception('保存失败');
  177. }
  178. }else{
  179. throw new \Exception($msg);
  180. }
  181. }
  182. /**
  183. * @Note:拒绝
  184. * @param $log
  185. * @return void
  186. * @throws \Exception
  187. */
  188. private static function reject($log,$params)
  189. {
  190. if (empty($params['remark'])) {
  191. throw new \Exception('请填写备注');
  192. }
  193. $log->withdraw_status = BalanceRechargeLog::STATUS_REJECT;
  194. $log->remark = $params['remark'];
  195. if (!$log->save()) {
  196. throw new \Exception('保存失败');
  197. }
  198. return true;
  199. }
  200. }