DrawCashController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace backend\controllers;
  3. use common\models\xhMerchantAsset;
  4. use common\services\xhCommonService;
  5. use Yii;
  6. use common\services\xhMerchantService;
  7. use common\services\xhWxOpenService;
  8. use common\services\xhWxOpenAdminService;
  9. use common\components\configDict;
  10. use common\services\xhMerchantAssetService;
  11. use common\services\xhDrawCashService;
  12. use common\components\stringUtil;
  13. use common\services\xhMerchantCapitalService;
  14. use common\services\xhTMessageService;
  15. use common\components\util;
  16. use common\models\xhDrawCash;
  17. use common\components\page;
  18. use common\components\weixinUtil;
  19. class DrawCashController extends BackendController
  20. {
  21. public function actionList()
  22. {
  23. $get = Yii::$app->request->get();
  24. $query = xhDrawCash::find();
  25. $query->where(['merchantId' =>$this->merchantId]);
  26. $countQuery = clone $query;
  27. $count = $countQuery->count();
  28. $page = isset($get['page']) ? $get['page'] : 1;
  29. $pageSize = 20;
  30. $offset = ($page - 1) * $pageSize;
  31. $models = $query->offset($offset)->orderBy('createTime desc')->limit($pageSize)->asArray()->all();
  32. $page = new page($count,$page,$pageSize);
  33. $paging = $page->fpage();
  34. $now = time();
  35. return $this->render('list', ['models' => $models,'paging' => $paging]);
  36. }
  37. /**
  38. * 进行提现操作,全额提现
  39. */
  40. public function actionExec()
  41. {
  42. $connection = Yii::$app->db;//事务处理
  43. $transaction = $connection->beginTransaction();
  44. try {
  45. $merchantAsset = $this->merchantAsset;
  46. $amount = $merchantAsset['balance'];
  47. if($amount <= 0){
  48. util::failInfo('余额不足');
  49. }
  50. $mBalance = stringUtil::calcSub($merchantAsset['balance'], $amount);
  51. $newFreezeBalance = stringUtil::calcAdd($merchantAsset['freezeBalance'],$amount);
  52. $date = date("Y-m-d H:i:s");
  53. $id = stringUtil::generateMerchantCashOrderNo($this->merchantId);
  54. $cashData = ['id'=>$id,'amount'=>$amount,'merchantId'=>$this->merchantId,'balance'=>$mBalance,'addTime'=>time(),'createTime'=>$date];
  55. $order = xhDrawCashService::add($cashData);
  56. $orderId = $order['id'];
  57. xhMerchantAssetService::updateByMerchantId($this->merchantId, ['balance'=>$mBalance,'freezeBalance'=>$newFreezeBalance]);
  58. $now = time();
  59. $capitalTypeArray = configDict::getConfig('capitalType','xhDrawCash');
  60. $capitalType = $capitalTypeArray['id'];
  61. $mIncome = $merchantAsset['totalIncome'];
  62. $mExpend = $merchantAsset['totalExpend'];
  63. $payWay = configDict::getConfig('payWay','other');
  64. $capitalData = [
  65. 'relateId' => (string)$orderId,
  66. 'balance' => $mBalance,
  67. 'totalIncome' => $mIncome,
  68. 'totalExpend' => $mExpend,
  69. 'amount' => $amount,
  70. 'io' => 0,
  71. 'payWay' => $payWay,
  72. 'event' => '申请提现',
  73. 'merchantId' => $this->merchantId,
  74. 'userId' => 0,
  75. 'userName' => '',
  76. 'alipayId' => '',
  77. 'affectBalance' => 1,//会影响商家的余额
  78. 'createTime' => $date,
  79. 'addTime' => $now,
  80. 'capitalType' => $capitalType,
  81. 'operateId' => $this->adminId,
  82. ];
  83. xhMerchantCapitalService::add($capitalData);//商家资金流水记录增加
  84. /*
  85. $wxOpenId = configDict::getConfig('openId');
  86. $open = xhWxOpenService::getById($wxOpenId);
  87. $merchantId = $open['merchantId'];
  88. $adminId = $open['adminId'];
  89. $merchant = xhMerchantService::getById($merchantId);
  90. $openAdmin = xhWxOpenAdminService::getById($adminId);
  91. $openId = $openAdmin['openId'];
  92. $shortTempId = 'OPENTM200605630';//任务处理通知
  93. $allTM = xhTMessageService::getList($merchantId);
  94. $tempId = $allTM[$shortTempId];
  95. $data = [
  96. "touser" => $openId,
  97. "template_id" => $tempId,
  98. "url" => "",
  99. "data" => [
  100. "first" => ["value" => $merchant['merchantName'].'申请提现'.$amount.'元',"color" => "#173177"],
  101. "keyword1" => ["value" => "商家提现","color" => "#173177"],
  102. "keyword2" => ["value" => "待办","color" => "#173177"],
  103. "remark" => ["value" => "请及时处理","color" => "#173177"]]];
  104. weixinUtil::sendTaskInform($data,$merchant);
  105. */
  106. xhCommonService::sendMobileMsg('15280215347','有客户申请提现,请及时处理');
  107. $transaction->commit();
  108. util::successInfo();
  109. }catch (Exception $e) {
  110. $transaction->rollBack();
  111. util::failInfo();
  112. }
  113. }
  114. }