ChangeBalanceForm.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. namespace common\models\forms;
  3. use backendApi\modules\v1\models\Admin;
  4. use common\components\Model;
  5. use common\helpers\Date;
  6. use common\helpers\Form;
  7. use common\helpers\Tool;
  8. use common\helpers\user\Balance;
  9. use common\helpers\user\Cash;
  10. use common\helpers\user\Info;
  11. use common\libs\logging\operate\AdminOperate;
  12. use common\models\BalanceAudit;
  13. use common\models\DealType;
  14. use common\models\User;
  15. use common\models\UserBonus;
  16. use common\models\UserInfo;
  17. use common\models\UserWallet;
  18. use common\models\Withdraw;
  19. use yii\base\Exception;
  20. /**
  21. * Login form
  22. */
  23. class ChangeBalanceForm extends Model {
  24. public $id;
  25. public $selected;
  26. public $userName;
  27. public $realName;
  28. public $type;
  29. public $dealType;
  30. public $amount;
  31. public $remark;
  32. public $auditStatus;
  33. public $isShow;
  34. public $allowMinus;
  35. public $balanceCode;
  36. private $_model;
  37. private $_userId;
  38. public function init() {
  39. parent::init();
  40. $this->adminOperateLogger = new AdminOperate([
  41. 'fetchClass' => UserBonus::class,
  42. ]);
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function rules() {
  48. return [
  49. [['id', 'selected', 'userName', 'type', 'amount', 'remark', 'auditStatus', 'isShow', 'dealType', 'allowMinus'], 'trim'],
  50. [['id', 'userName', 'type', 'amount', 'remark', 'auditStatus', 'dealType','balanceCode'], 'required'],
  51. [['id'], 'exist', 'targetClass' => BalanceAudit::class, 'targetAttribute' => 'ID', 'message' => '余额变动申请不存在'],
  52. [['id'], 'initModel'],
  53. [['userName'], 'exist', 'targetClass' => UserInfo::class, 'targetAttribute' => 'USER_NAME', 'message' => '会员不存在'],
  54. [['userName'], 'isUserName'],
  55. [['type'], 'isType'],
  56. [['dealType'], 'isDealType'],
  57. [['amount'], 'fullPrice'],
  58. [['amount'], 'isAmount'],
  59. [['auditStatus'], 'isStatus'],
  60. [['selected'], 'isSelected'],
  61. [['balanceCode'], 'validateRejectCode'],
  62. ];
  63. }
  64. /**
  65. * 指定场景
  66. * @return array
  67. */
  68. public function scenarios() {
  69. $parentScenarios = parent::scenarios();
  70. $customScenarios = [
  71. 'changeAdd' => ['userName', 'type', 'amount', 'remark', 'isShow', 'dealType', 'balanceCode'],
  72. 'changeAudit' => ['selected', 'allowMinus', 'remark', 'auditStatus', 'balanceCode'],
  73. 'edit' => ['id', 'type', 'amount', 'remark', 'isShow', 'dealType'],
  74. 'pass' => ['id', 'type', 'amount', 'auditStatus', 'remark', 'isShow', 'dealType', 'balanceCode'],
  75. 'excelChangeBalance' => ['userName', 'realName', 'type', 'amount', 'dealType', 'remark', 'isShow'],
  76. ];
  77. return array_merge($parentScenarios, $customScenarios);
  78. }
  79. /**
  80. * @return array
  81. */
  82. public function attributeLabels() {
  83. return [
  84. 'id' => '余额变动申请的ID',
  85. 'userName' => '会员编号',
  86. 'type' => '余额类型',
  87. 'amount' => '变动金额',
  88. 'remark' => '备注',
  89. 'auditStatus' => '状态',
  90. 'isShow' => '前台显示备注',
  91. 'dealType' => '交易类型',
  92. 'balanceCode' => '余额调整校验码',
  93. ];
  94. }
  95. /**
  96. * 初始化广告model类
  97. * @param $attribute
  98. */
  99. public function initModel($attribute) {
  100. $model = $this->_model = BalanceAudit::findOne(['ID' => $this->id]);
  101. $this->_userId = $model['USER_ID'];
  102. if ($model->AUDIT_STATUS > \Yii::$app->params['auditStatus']['un']['value']) {
  103. $this->addError($attribute, '该申请已经被审核,不能重复审核');
  104. }
  105. }
  106. /**
  107. * 设置userId
  108. * @param $attribute
  109. */
  110. public function isUserName($attribute) {
  111. $userInfo = UserInfo::find()->select('USER_ID')->where(['USER_NAME' => $this->userName])->asArray()->one();
  112. if ($userInfo) {
  113. $this->_userId = $userInfo['USER_ID'];
  114. if ($this->scenario == 'excelChangeBalance') {
  115. $realName = Info::getUserRealNameByUserId($this->_userId);
  116. if ($realName != $this->realName) {
  117. $this->addError($attribute, '会员编号与会员姓名不匹配');
  118. }
  119. }
  120. } else {
  121. $this->addError($attribute, '会员不存在');
  122. }
  123. }
  124. /**
  125. * 校验余额类型
  126. * @param $attribute
  127. */
  128. public function isType($attribute) {
  129. if (!key_exists($this->type, BalanceAudit::TYPE)) {
  130. $this->addError($attribute, '余额类型不正确');
  131. }
  132. // 查看表里面是否有想同类型的未审核单,如果有的话,不允许再申请,需等审核完成以后,才能申请
  133. /*if(BalanceAudit::find()->where('USER_ID=:USER_ID AND TYPE=:TYPE AND STATUS=:STATUS', [':USER_ID'=>$this->_userId, ':TYPE'=>$this->type, ':STATUS'=>BalanceAudit::STATUS_APPLIED])->exists()){
  134. $this->addError($attribute, '已提交过该会员的其他余额调整申请,等待审核后,才可以申请新的余额调整申请');
  135. }*/
  136. }
  137. /**
  138. * 校验交易类型
  139. * @param $attribute
  140. */
  141. public function isDealType($attribute) {
  142. if (!key_exists($this->dealType, DealType::getTypes())) {
  143. $this->addError($attribute, '交易类型不正确');
  144. }
  145. }
  146. /**
  147. * 金额是否正确
  148. * @param $attribute
  149. */
  150. public function isAmount($attribute) {
  151. if ($this->amount == 0) $this->addError($attribute, '调整金额不能为0');
  152. if ($this->amount < 0 && $this->type == 'reconsume_points' && abs($this->amount) > Balance::getBalanceReconsumePoints($this->_userId)) {
  153. $this->addError('scenario', '会员复消积分余额不足,不允许调整');
  154. }
  155. }
  156. /**
  157. * 校验状态
  158. * @param $attribute
  159. */
  160. public function isStatus($attribute) {
  161. // 获取当前提现单的状态
  162. $oneData = BalanceAudit::findOneAsArray(['ID' => $this->id]);
  163. switch ($this->auditStatus) {
  164. case 'un':
  165. $this->addError($attribute, '不能设置为待审核状态' . $this->auditStatus);
  166. break;
  167. case 'true':
  168. if ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['true']['value']) {
  169. $this->addError($attribute, '已经审核通过不能重复审核');
  170. } elseif ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['reject']['value']) {
  171. $this->addError($attribute, '已经审核拒绝不能重复审核');
  172. }
  173. break;
  174. case 'reject':
  175. if ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['true']['value']) {
  176. $this->addError($attribute, '已经审核通过不能重复审核');
  177. } elseif ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['reject']['value']) {
  178. $this->addError($attribute, '已经审核拒绝不能重复审核');
  179. }
  180. break;
  181. default:
  182. $this->addError($attribute, '状态参数有误');
  183. }
  184. }
  185. public function validateRejectCode($attribute) {
  186. $adminId = \Yii::$app->user->id;
  187. $redisCode = \Yii::$app->redis->getset('balanceCode_'.$adminId,'');
  188. if ($this->balanceCode!=$redisCode) {
  189. $this->addError($attribute, '余额调整审核校验码失败');
  190. }
  191. }
  192. /**
  193. * 批量数据
  194. * @param $attributes
  195. */
  196. public function isSelected($attributes) {
  197. if (!$this->selected) {
  198. $this->addError($attributes, '必须选择一条数据');
  199. }
  200. if (!is_array($this->selected)) {
  201. $this->selected = [$this->selected];
  202. }
  203. $this->selected = array_unique($this->selected);
  204. if ($this->auditStatus == 'true') {
  205. foreach ($this->selected as $key => $select) {
  206. $model = BalanceAudit::findOne(['ID' => $select]);
  207. if (!$this->allowMinus) {
  208. $oneUserBonusModel = UserBonus::findOneAsArray(['USER_ID' => $model->USER_ID]);
  209. if ($model->AMOUNT < 0 && abs($oneUserBonusModel[strtoupper($model->TYPE)]) < abs($model->AMOUNT)) unset($this->selected[$key]);
  210. } else {
  211. if (in_array($model->TYPE, ['cf', 'lx'])) {
  212. $oneUserBonusModel = UserBonus::findOneAsArray(['USER_ID' => $model->USER_ID]);
  213. if ($model->AMOUNT < 0 && abs($oneUserBonusModel[strtoupper($model->TYPE)]) < abs($model->AMOUNT)) unset($this->selected[$key]);
  214. }
  215. }
  216. if ($model->AUDIT_STATUS > \Yii::$app->params['auditStatus']['un']['value']) unset($this->selected[$key]);
  217. }
  218. }
  219. $this->selected = array_values($this->selected);
  220. if (!$this->selected) {
  221. $this->addError($attributes, '没有符合条件的数据');
  222. }
  223. }
  224. /**
  225. * 添加变动申请
  226. * @return BalanceAudit|null
  227. * @throws \yii\db\Exception
  228. */
  229. public function changeAdd() {
  230. if (!$this->validate()) {
  231. return null;
  232. }
  233. $db = \Yii::$app->db;
  234. $transaction = $db->beginTransaction();
  235. try {
  236. // 添加申请
  237. $userInfo = Info::baseInfo($this->_userId);
  238. $model = new BalanceAudit();
  239. $model->USER_ID = $this->_userId;
  240. $model->LAST_DEC_LV = $userInfo['DEC_LV'];
  241. $model->LAST_EMP_LV = $userInfo['EMP_LV'];
  242. $model->LAST_DEC_ROLE_ID = $userInfo['DEC_ROLE_ID'];
  243. $model->TYPE = $this->type;
  244. $model->DEAL_TYPE = $this->dealType;
  245. $model->AMOUNT = $this->amount;
  246. $model->CREATE_REMARK = $this->remark;
  247. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['un']['value'];
  248. $model->REMARK_IS_SHOW = $this->isShow;
  249. $model->UPDATE_ADMIN_ID = \Yii::$app->user->id;
  250. $model->CREATED_AT = Date::nowTime();
  251. if (!$model->save()) {
  252. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  253. }
  254. $adminId = \Yii::$app->user->id;
  255. \Yii::$app->redis->del('balanceCode_'.$adminId);
  256. $transaction->commit();
  257. } catch (Exception $e) {
  258. $transaction->rollBack();
  259. $this->addError('changeAdd', $e->getMessage());
  260. return null;
  261. }
  262. // 记录日志
  263. $this->adminOperateLogger->fetchClass = BalanceAudit::class;
  264. $this->adminOperateLogger->afterInsert($model)->clean()->save([
  265. 'optType' => '申请变动会员余额',
  266. 'userId' => $model->USER_ID,
  267. 'userName' => $userInfo['USER_NAME'],
  268. 'remark' => $model->CREATE_REMARK,
  269. ]);
  270. return $model;
  271. }
  272. /**
  273. * 修改信息
  274. * @return null
  275. * @throws \yii\db\Exception
  276. */
  277. public function edit() {
  278. //关闭修改
  279. return null;
  280. if (!$this->validate()) {
  281. return null;
  282. }
  283. $db = \Yii::$app->db;
  284. $transaction = $db->beginTransaction();
  285. try {
  286. $model = $this->_model;
  287. $model->TYPE = $this->type;
  288. $model->DEAL_TYPE = $this->dealType;
  289. $model->AMOUNT = $this->amount;
  290. $model->CREATE_REMARK = $this->remark;
  291. $model->REMARK_IS_SHOW = $this->isShow;
  292. if (!$model->save()) {
  293. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  294. }
  295. $transaction->commit();
  296. } catch (Exception $e) {
  297. $transaction->rollBack();
  298. $this->addError('edit', $e->getMessage());
  299. return null;
  300. }
  301. return $model;
  302. }
  303. /**
  304. * 审核通过
  305. * @return null
  306. * @throws \yii\db\Exception
  307. */
  308. public function pass() {
  309. if (!$this->validate()) {
  310. return null;
  311. }
  312. // 日志记录操作前的数据
  313. if($this->type=='cash'){
  314. $beforeData = UserWallet::getCashByUserId($this->_userId);
  315. $this->adminOperateLogger->fetchClass = UserWallet::class;
  316. }else {
  317. $beforeData = UserBonus::getBonusByUserId($this->_userId);
  318. $this->adminOperateLogger->fetchClass = UserBonus::class;
  319. }
  320. $this->adminOperateLogger->beforeUpdate($beforeData);
  321. $db = \Yii::$app->db;
  322. $transaction = $db->beginTransaction();
  323. try {
  324. $model = $this->_model;
  325. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus'][$this->auditStatus]['value'];
  326. $model->AUDIT_ADMIN_ID = \Yii::$app->user->id;
  327. $model->TYPE = $this->type;
  328. $model->DEAL_TYPE = $this->dealType;
  329. $model->AMOUNT = $this->amount;
  330. $model->REMARK_IS_SHOW = $this->isShow;
  331. $model->CREATE_REMARK = $this->remark;
  332. $model->AUDITED_AT = Date::nowTime();
  333. if (!$model->save()) {
  334. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  335. }
  336. // 如果是审核通过状态
  337. if ($this->auditStatus == 'true') {
  338. if($model->TYPE=='cash') {
  339. Cash::changeUserCash($model->USER_ID, 'CASH', $model->AMOUNT, ['REMARK' => '后台管理员充值']);
  340. }else{
  341. Balance::changeUserBonus($model->USER_ID, $model->TYPE, $model->AMOUNT, ['REMARK' => $model->CREATE_REMARK, 'REMARK_IS_SHOW' => $model->REMARK_IS_SHOW, 'DEAL_TYPE_ID' => $model->DEAL_TYPE, 'DEAL_TYPE_IS_PRESET' => 0, 'ADMIN_NAME' => Admin::getAdminNameById(\Yii::$app->user->id)]);
  342. }
  343. }
  344. $adminId = \Yii::$app->user->id;
  345. \Yii::$app->redis->del('balanceCode_'.$adminId);
  346. $transaction->commit();
  347. } catch (Exception $e) {
  348. $transaction->rollBack();
  349. $this->addError('pass', $e->getMessage());
  350. return null;
  351. }
  352. // 记录日志
  353. if($this->type=='cash'){
  354. $afterData = UserWallet::getCashByUserId($this->_userId);
  355. $this->adminOperateLogger->fetchClass = UserWallet::class;
  356. }else {
  357. $afterData = UserBonus::getBonusByUserId($this->_userId);
  358. $this->adminOperateLogger->fetchClass = UserBonus::class;
  359. }
  360. $this->adminOperateLogger->afterInsert($afterData)->clean()->save([
  361. 'optType' => '审核变动会员余额',
  362. 'userId' => $model->USER_ID,
  363. 'userName' => Info::getUserNameByUserId($model->USER_ID),
  364. 'remark' => $model->CREATE_REMARK,
  365. ]);
  366. return $model;
  367. }
  368. /**
  369. * 审核余额变动
  370. * @return null
  371. * @throws \yii\db\Exception
  372. */
  373. public function changeAudit() {
  374. if (!$this->validate()) {
  375. return null;
  376. }
  377. $uids=[];
  378. if($this->auditStatus=='true'){
  379. foreach ($this->selected as $select) {
  380. $oneBalanceAudit = BalanceAudit::findOneAsArray('ID=:ID',[':ID'=>$select],'USER_ID');
  381. $uids[]=$oneBalanceAudit['USER_ID'];
  382. }
  383. }
  384. if($this->auditStatus=='true'){
  385. $this->adminOperateLogger->fetchClass = UserBonus::class;
  386. $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($uids, 'USER_ID');
  387. }else{
  388. $this->adminOperateLogger->fetchClass = BalanceAudit::class;
  389. $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($this->selected, 'ID');
  390. }
  391. $db = \Yii::$app->db;
  392. $transaction = $db->beginTransaction();
  393. $logs=[];
  394. try {
  395. foreach ($this->selected as $select) {
  396. $model = BalanceAudit::findOne(['ID' => $select]);
  397. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus'][$this->auditStatus]['value'];
  398. $model->AUDIT_ADMIN_ID = \Yii::$app->user->id;
  399. $model->CREATE_REMARK = $this->remark;
  400. $model->AUDITED_AT = Date::nowTime();
  401. if (!$model->save()) {
  402. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  403. }
  404. if ($model->AUDIT_STATUS == \Yii::$app->params['auditStatus']['true']['value']) {
  405. Balance::changeUserBonus($model->USER_ID, $model->TYPE, $model->AMOUNT, ['REMARK' => $model->CREATE_REMARK, 'REMARK_IS_SHOW' => $model->REMARK_IS_SHOW, 'DEAL_TYPE_ID' => $model->DEAL_TYPE, 'DEAL_TYPE_IS_PRESET' => 0, 'ADMIN_NAME' => Admin::getAdminNameById(\Yii::$app->user->id)]);
  406. }
  407. $logs[]=['userId'=>$select,'type'=>$model->TYPE];
  408. }
  409. $adminId = \Yii::$app->user->id;
  410. \Yii::$app->redis->del('balanceCode_'.$adminId);
  411. $transaction->commit();
  412. } catch (Exception $e) {
  413. $transaction->rollBack();
  414. $this->addError('changeAudit', $e->getMessage());
  415. return null;
  416. }
  417. if($this->auditStatus=='true'){
  418. $this->adminOperateLogger->fetchClass = UserBonus::class;
  419. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($uids, 'USER_ID')->clean()->save([
  420. 'optType' => '审核会员余额',
  421. 'remark' => $this->remark,
  422. ]);
  423. }else{
  424. $this->adminOperateLogger->fetchClass = BalanceAudit::class;
  425. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($this->selected, 'ID')->clean()->save([
  426. 'optType' => '审核会员余额',
  427. 'remark' => $this->remark,
  428. ]);
  429. }
  430. return $logs;
  431. }
  432. /**
  433. * 导入会员余额调整
  434. * @return null|static
  435. * @throws \yii\db\Exception
  436. */
  437. public function excelChangeBalance() {
  438. if (!$this->validate()) {
  439. return null;
  440. }
  441. $db = \Yii::$app->db;
  442. $transaction = $db->beginTransaction();
  443. try {
  444. $userInfo = Info::baseInfo($this->_userId);
  445. // 添加申请
  446. $model = new BalanceAudit();
  447. $model->USER_ID = $this->_userId;
  448. $model->LAST_DEC_LV = $userInfo['DEC_LV'];
  449. $model->LAST_EMP_LV = $userInfo['EMP_LV'];
  450. $model->LAST_DEC_ROLE_ID = $userInfo['DEC_ROLE_ID'];
  451. $model->TYPE = $this->type;
  452. $model->DEAL_TYPE = $this->dealType;
  453. $model->AMOUNT = $this->amount;
  454. $model->CREATE_REMARK = $this->remark;
  455. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['un']['value'];
  456. $model->REMARK_IS_SHOW = $this->isShow;
  457. $model->UPDATE_ADMIN_ID = \Yii::$app->user->id;
  458. $model->CREATED_AT = Date::nowTime();
  459. if (!$model->save()) {
  460. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  461. }
  462. $transaction->commit();
  463. } catch (Exception $e) {
  464. $transaction->rollBack();
  465. $this->addError('auditStatus', $e->getMessage());
  466. return null;
  467. }
  468. // 日志记录操作前的数据
  469. $beforeData = UserBonus::getBonusByUserId($this->_userId);
  470. $this->adminOperateLogger->fetchClass = UserBonus::class;
  471. $this->adminOperateLogger->beforeUpdate($beforeData);
  472. // 记录日志
  473. $this->adminOperateLogger->fetchClass = BalanceAudit::class;
  474. $this->adminOperateLogger->afterInsert($model)->clean()->save([
  475. 'optType' => '批量申请变动会员余额',
  476. 'userId' => $model->USER_ID,
  477. 'userName' => $userInfo['USER_NAME'],
  478. 'remark' => $model->CREATE_REMARK,
  479. ]);
  480. return $model;
  481. }
  482. /**
  483. * 删除前
  484. * @param $selected
  485. */
  486. public function beforeDelete($selected) {
  487. $this->adminOperateLogger->fetchClass = BalanceAudit::class;
  488. $this->adminOperateLogger->setIsBatch(true)->beforeDelete($selected, 'ID');
  489. }
  490. /**
  491. * 删除
  492. * @param $selected
  493. * @throws Exception
  494. */
  495. public function delete($selected) {
  496. $this->adminOperateLogger->clean()->save([
  497. 'optType' => '删除余额调整待审核数据',
  498. ]);
  499. }
  500. }