| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- <?php
- namespace common\models\forms;
- use common\components\Model;
- use common\helpers\Cache;
- use common\helpers\Date;
- use common\helpers\Form;
- use common\helpers\Tool;
- use common\helpers\user\Cash;
- use common\helpers\user\Info;
- use common\models\Recharge;
- use common\models\UserInfo;
- use yii\base\Exception;
- /**
- * Login form
- */
- class RechargeForm extends Model {
- public $selectedIds;
- public $auditStatus;
- public $createRemark;
- public $userName;
- public $applyAmount;
- public $openBank;
- public $bankAddress;
- public $bankNo;
- // public $bankProvince;
- // public $bankCity;
- // public $bankCounty;
- public $sn;
- public $amount;
- public $bankRealName;
- private $_userId;
- /**
- * @inheritdoc
- */
- public function rules() {
- return [
- [['userName', 'idCard', 'applyAmount', 'auditStatus', 'remark', 'selectedIds', 'sn','amount', 'bankRealName', 'bankNo'], 'trim'],
- [['selectedIds', 'auditStatus', 'userName','applyAmount','openBank','bankNo'/*,'bankAddress'*/], 'required'],
- [['selectedIds'], 'exist', 'targetClass' => Recharge::class, 'targetAttribute' => 'ID', 'message' => '充值申请不存在'],
- [['userName'], 'exist', 'targetClass' => UserInfo::class, 'targetAttribute' => 'USER_NAME', 'message' => '会员不存在'],
- [['applyAmount'], 'price'],
- [['applyAmount'], 'isApplyAmount'],
- [['selectedIds'], 'isSelected'],
- //[['sn'], 'isSn'],
- ];
- }
- /**
- * 指定场景
- * @return array
- */
- public function scenarios() {
- $parentScenarios = parent::scenarios();
- $customScenarios = [
- 'addByAdmin' => ['userName', 'applyAmount'],
- 'addByUser' => ['applyAmount','openBank','bankNo','bankAddress'/*,'currency'*/],
- 'statusByAdmin' => ['selectedIds', 'auditStatus', 'createRemark'],
- ];
- return array_merge($parentScenarios, $customScenarios);
- }
- /**
- * @return array
- */
- public function attributeLabels() {
- return [
- 'selectedIds' => '充值申请ID',
- 'userName' => '会员编号',
- 'applyAmount' => '申请充值的金额',
- 'openBank' => '汇款银行',
- 'bankNo' => '汇款账号',
- // 'bankAddress' => '银行支行',
- // 'bankProvince' => '银行省',
- // 'bankCity' => '银行市',
- // 'bankCounty' => '银行县',
- ];
- }
- /**
- * 校验申请金额是否小于当前余额并符合配置中的设置
- * @param $attribute
- * @return bool
- */
- public function isApplyAmount($attribute) {
- if ($this->applyAmount <= 0) {
- $this->addError('scenario', '充值金额必须大于0');
- }
- if ((int)($this->applyAmount)!=$this->applyAmount) {
- $this->addError('scenario', '充值金额必须是整数');
- }
- if ($this->scenario == 'addByUser') {
- $this->_userId = \Yii::$app->user->id;
- } elseif ($this->scenario == 'addByAdmin') {
- $userInfo = UserInfo::findOneAsArray(['USER_NAME' => $this->userName]);
- if (!$userInfo) {
- $this->addError('scenario', $this->userName . '会员不存在');
- }else{
- $this->_userId = $userInfo['USER_ID'];
- }
- } else {
- $this->addError($attribute, '场景不存在');
- return false;
- }
- }
- /**
- * 批量数据
- * @param $attributes
- */
- public function isSelected($attributes) {
- if (!$this->selectedIds) {
- $this->addError($attributes, '必须选择一条数据');
- }
- // if (!is_array($this->selectedIds)) {
- // $this->selectedIds = [$this->selectedIds];
- // }
- }
- /**
- * 判断信息是否一致
- * @param $attribute
- * @throws Exception
- */
- public function isSn($attribute) {
- $sn = $this->sn;
- if (!$oneWithdraw = Recharge::findOneAsArray('SN=:SN AND AUDIT_STATUS=:AUDIT_STATUS', [':SN' => $sn, ':AUDIT_STATUS' => Recharge::STATUS_AUDITED])) {
- $this->addError($attribute, '不存在充值流水号为' . $sn . '的已审核记录');
- }
- if ($oneWithdraw) {
- $info = Info::baseInfo($oneWithdraw['USER_ID']);
- if ($this->userName != $info['USER_NAME']) {
- $this->addError($attribute, 'Excel中充值流水号' . $sn . '的用户名' . $this->userName . '与系统中的信息【' . $info['USER_NAME'] . '】不一致');
- }
- if ($this->amount != $oneWithdraw['AMOUNT']) {
- $this->addError($attribute, 'Excel中充值流水号' . $sn . '的充值金额' . $this->amount . '与系统中的信息【' . $oneWithdraw['AMOUNT'] . '】不一致');
- }
- if ($this->bankRealName != $oneWithdraw['REAL_NAME']) {
- $this->addError($attribute, 'Excel中充值流水号' . $sn . '的实时开户名' . $this->bankRealName . '与系统中的信息【' . $oneWithdraw['REAL_NAME'] . '】不一致');
- }
- if ($this->bankNo != $oneWithdraw['BANK_NO']) {
- $this->addError($attribute, 'Excel中充值流水号' . $sn . '的实时银行账户' . $this->bankNo . '与系统中的信息【' . $oneWithdraw['BANK_NO'] . '】不一致');
- }
- }
- }
- /**
- * 管理员充值
- * @return null|string
- * @throws \yii\db\Exception
- */
- public function recharge() {
- if (!$this->validate()) {
- return false;
- }
- $db = \Yii::$app->db;
- $transaction = $db->beginTransaction();
- try {
- Cash::changeUserCash($this->_userId, 'CASH', $this->applyAmount, ['REMARK' => '后台管理员充值']);
- $transaction->commit();
- } catch (Exception $e) {
- $transaction->rollBack();
- $this->addError('add', $e->getMessage());
- return false;
- }
- return true;
- }
- /**
- * 添加充值申请
- * @return null|string
- * @throws \yii\db\Exception
- */
- public function add() {
- if (!$this->validate()) {
- return false;
- }
- $db = \Yii::$app->db;
- $transaction = $db->beginTransaction();
- try {
- $nowTime = Date::nowTime();
- $period = Recharge::getPeriod($nowTime);
- //增加记录
- $userInfo = Info::baseInfo($this->_userId);
- $rechargeModel = new Recharge();
- $rechargeModel->SN = $this->_generateSn();
- $rechargeModel->USER_ID = $this->_userId;
- $rechargeModel->REAL_NAME = Info::getUserRealNameByUserId($this->_userId);
- $rechargeModel->ID_CARD = $userInfo['ID_CARD'];
- $rechargeModel->RECHARGE_PERIOD_NUM = $period['nowPeriodNum'];
- $rechargeModel->RECHARGE_YEAR = $period['nowYear'];
- $rechargeModel->RECHARGE_MONTH = $period['nowMonth'];
- $rechargeModel->AMOUNT = $this->applyAmount;
- $rechargeModel->OPEN_BANK = $this->openBank;
- $rechargeModel->BANK_ADDRESS = $this->bankAddress;
- $rechargeModel->BANK_NO = $this->bankNo;
- // $rechargeModel->BANK_PROVINCE = $this->bankProvince ?? 0;
- // $rechargeModel->BANK_CITY = $this->bankCity ?? 0;
- // $rechargeModel->BANK_COUNTY = $this->bankCounty ?? 0;
- $rechargeModel->P_MONTH = Date::ociToDate($period['yearMonth'], Date::OCI_TIME_FORMAT_SHORT_MONTH);
- $rechargeModel->AUDIT_STATUS = Recharge::STATUS_APPLIED;
- $rechargeModel->CREATED_AT = $nowTime;
- if (!$rechargeModel->save()) {
- throw new Exception(Form::formatErrorsForApi($rechargeModel->getErrors()));
- }
- $transaction->commit();
- } catch (Exception $e) {
- $transaction->rollBack();
- $this->addError('add', $e->getMessage());
- return false;
- }
- return $rechargeModel;
- }
- /**
- * 生成流水号
- * @return string
- */
- private function _generateSn() {
- return 'CZ' . Date::today('Ymd') . $this->_random(10, 1);
- }
- /**
- * 生成随机数
- *
- * @param $length
- * @param int $numeric
- * @return string
- */
- private function _random($length, $numeric = 0) {
- $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
- $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
- $hash = '';
- $max = strlen($seed) - 1;
- for ($i = 0; $i < $length; $i++) {
- $hash .= $seed[mt_rand(0, $max)];
- }
- return $hash;
- }
- /**
- * 设置充值订单的状态
- * @return null|static
- * @throws \yii\db\Exception
- */
- public function changeStatus() {
- if (!$this->validate()) {
- return null;
- }
- $logs = [];
- $db = \Yii::$app->db;
- $transaction = $db->beginTransaction();
- try {
- $oneRecharge = Recharge::findOne(['ID' => $this->selectedIds]);
- //判断状态
- if (($msg = Recharge::chkAuditStatus($oneRecharge->SN, $oneRecharge->AUDIT_STATUS, $this->auditStatus)) != '') {
- throw new Exception($msg);
- }
- //待审核->已审核 修改会员现金钱包
- if ($this->auditStatus == Recharge::STATUS_AUDITED) {
- Cash::changeUserCash($oneRecharge->USER_ID, 'CASH', abs($oneRecharge->AMOUNT), ['REMARK' => '会员充值申请审核']);
- }
- $oneRecharge->REMARK = $this->createRemark ?? '';
- $oneRecharge->AUDIT_ADMIN = \Yii::$app->user->id;
- $oneRecharge->AUDIT_STATUS = $this->auditStatus;
- $oneRecharge->AUDITED_AT = Date::nowTime();
- if (!$oneRecharge->save()) {
- throw new Exception(Form::formatErrorsForApi($oneRecharge->getErrors()));
- }
- $logs[$this->selectedIds] = $oneRecharge->SN;
- $transaction->commit();
- } catch (Exception $e) {
- $transaction->rollBack();
- $this->addError('auditStatus', $e->getMessage());
- return null;
- }
- return ['logs' => $logs, 'status' => $this->auditStatus];
- }
- }
|