ChangeBalanceForm.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. // if ($this->amount < 0 && $this->type == 'lx' && abs($this->amount) > Balance::getBalanceLX($this->_userId)) {
  156. // $this->addError('scenario', '会员领袖分红余额不足,不允许调整');
  157. // }
  158. }
  159. /**
  160. * 校验状态
  161. * @param $attribute
  162. */
  163. public function isStatus($attribute) {
  164. // 获取当前提现单的状态
  165. $oneData = BalanceAudit::findOneAsArray(['ID' => $this->id]);
  166. switch ($this->auditStatus) {
  167. case 'un':
  168. $this->addError($attribute, '不能设置为待审核状态' . $this->auditStatus);
  169. break;
  170. case 'true':
  171. if ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['true']['value']) {
  172. $this->addError($attribute, '已经审核通过不能重复审核');
  173. } elseif ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['reject']['value']) {
  174. $this->addError($attribute, '已经审核拒绝不能重复审核');
  175. }
  176. break;
  177. case 'reject':
  178. if ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['true']['value']) {
  179. $this->addError($attribute, '已经审核通过不能重复审核');
  180. } elseif ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['reject']['value']) {
  181. $this->addError($attribute, '已经审核拒绝不能重复审核');
  182. }
  183. break;
  184. default:
  185. $this->addError($attribute, '状态参数有误');
  186. }
  187. }
  188. public function validateRejectCode($attribute) {
  189. $adminId = \Yii::$app->user->id;
  190. $redisCode = \Yii::$app->redis->getset('balanceCode_'.$adminId,'');
  191. if ($this->balanceCode!=$redisCode) {
  192. $this->addError($attribute, '余额调整审核校验码失败');
  193. }
  194. }
  195. /**
  196. * 批量数据
  197. * @param $attributes
  198. */
  199. public function isSelected($attributes) {
  200. if (!$this->selected) {
  201. $this->addError($attributes, '必须选择一条数据');
  202. }
  203. if (!is_array($this->selected)) {
  204. $this->selected = [$this->selected];
  205. }
  206. $this->selected = array_unique($this->selected);
  207. if ($this->auditStatus == 'true') {
  208. foreach ($this->selected as $key => $select) {
  209. $model = BalanceAudit::findOne(['ID' => $select]);
  210. if (!$this->allowMinus) {
  211. $oneUserBonusModel = UserBonus::findOneAsArray(['USER_ID' => $model->USER_ID]);
  212. if ($model->AMOUNT < 0 && abs($oneUserBonusModel[strtoupper($model->TYPE)]) < abs($model->AMOUNT)) unset($this->selected[$key]);
  213. } else {
  214. if (in_array($model->TYPE, ['cf', 'lx'])) {
  215. $oneUserBonusModel = UserBonus::findOneAsArray(['USER_ID' => $model->USER_ID]);
  216. if ($model->AMOUNT < 0 && abs($oneUserBonusModel[strtoupper($model->TYPE)]) < abs($model->AMOUNT)) unset($this->selected[$key]);
  217. }
  218. }
  219. if ($model->AUDIT_STATUS > \Yii::$app->params['auditStatus']['un']['value']) unset($this->selected[$key]);
  220. }
  221. }
  222. $this->selected = array_values($this->selected);
  223. if (!$this->selected) {
  224. $this->addError($attributes, '没有符合条件的数据');
  225. }
  226. }
  227. /**
  228. * 添加变动申请
  229. * @return BalanceAudit|null
  230. * @throws \yii\db\Exception
  231. */
  232. public function changeAdd() {
  233. if (!$this->validate()) {
  234. return null;
  235. }
  236. $db = \Yii::$app->db;
  237. $transaction = $db->beginTransaction();
  238. try {
  239. // 添加申请
  240. $userInfo = Info::baseInfo($this->_userId);
  241. $model = new BalanceAudit();
  242. $model->USER_ID = $this->_userId;
  243. $model->LAST_DEC_LV = $userInfo['DEC_LV'];
  244. $model->LAST_EMP_LV = $userInfo['EMP_LV'];
  245. $model->LAST_DEC_ROLE_ID = $userInfo['DEC_ROLE_ID'];
  246. $model->TYPE = $this->type;
  247. $model->DEAL_TYPE = $this->dealType;
  248. $model->AMOUNT = $this->amount;
  249. $model->CREATE_REMARK = $this->remark;
  250. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['un']['value'];
  251. $model->REMARK_IS_SHOW = $this->isShow;
  252. $model->UPDATE_ADMIN_ID = \Yii::$app->user->id;
  253. $model->CREATED_AT = Date::nowTime();
  254. if (!$model->save()) {
  255. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  256. }
  257. $adminId = \Yii::$app->user->id;
  258. \Yii::$app->redis->del('balanceCode_'.$adminId);
  259. $transaction->commit();
  260. } catch (Exception $e) {
  261. $transaction->rollBack();
  262. $this->addError('changeAdd', $e->getMessage());
  263. return null;
  264. }
  265. // 记录日志
  266. $this->adminOperateLogger->fetchClass = BalanceAudit::class;
  267. $this->adminOperateLogger->afterInsert($model)->clean()->save([
  268. 'optType' => '申请变动会员余额',
  269. 'userId' => $model->USER_ID,
  270. 'userName' => $userInfo['USER_NAME'],
  271. 'remark' => $model->CREATE_REMARK,
  272. ]);
  273. return $model;
  274. }
  275. /**
  276. * 修改信息
  277. * @return null
  278. * @throws \yii\db\Exception
  279. */
  280. public function edit() {
  281. //关闭修改
  282. return null;
  283. if (!$this->validate()) {
  284. return null;
  285. }
  286. $db = \Yii::$app->db;
  287. $transaction = $db->beginTransaction();
  288. try {
  289. $model = $this->_model;
  290. $model->TYPE = $this->type;
  291. $model->DEAL_TYPE = $this->dealType;
  292. $model->AMOUNT = $this->amount;
  293. $model->CREATE_REMARK = $this->remark;
  294. $model->REMARK_IS_SHOW = $this->isShow;
  295. if (!$model->save()) {
  296. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  297. }
  298. $transaction->commit();
  299. } catch (Exception $e) {
  300. $transaction->rollBack();
  301. $this->addError('edit', $e->getMessage());
  302. return null;
  303. }
  304. return $model;
  305. }
  306. /**
  307. * 审核通过
  308. * @return null
  309. * @throws \yii\db\Exception
  310. */
  311. public function pass() {
  312. if (!$this->validate()) {
  313. return null;
  314. }
  315. // 日志记录操作前的数据
  316. if($this->type=='cash'){
  317. $beforeData = UserWallet::getCashByUserId($this->_userId);
  318. $this->adminOperateLogger->fetchClass = UserWallet::class;
  319. }else {
  320. $beforeData = UserBonus::getBonusByUserId($this->_userId);
  321. $this->adminOperateLogger->fetchClass = UserBonus::class;
  322. }
  323. $this->adminOperateLogger->beforeUpdate($beforeData);
  324. $db = \Yii::$app->db;
  325. $transaction = $db->beginTransaction();
  326. try {
  327. $model = $this->_model;
  328. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus'][$this->auditStatus]['value'];
  329. $model->AUDIT_ADMIN_ID = \Yii::$app->user->id;
  330. $model->TYPE = $this->type;
  331. $model->DEAL_TYPE = $this->dealType;
  332. $model->AMOUNT = $this->amount;
  333. $model->REMARK_IS_SHOW = $this->isShow;
  334. $model->CREATE_REMARK = $this->remark;
  335. $model->AUDITED_AT = Date::nowTime();
  336. if (!$model->save()) {
  337. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  338. }
  339. // 如果是审核通过状态
  340. if ($this->auditStatus == 'true') {
  341. if($model->TYPE=='cash') {
  342. Cash::changeUserCash($model->USER_ID, 'CASH', $model->AMOUNT, ['REMARK' => '后台管理员充值']);
  343. }else{
  344. 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)]);
  345. }
  346. }
  347. $adminId = \Yii::$app->user->id;
  348. \Yii::$app->redis->del('balanceCode_'.$adminId);
  349. $transaction->commit();
  350. } catch (Exception $e) {
  351. $transaction->rollBack();
  352. $this->addError('pass', $e->getMessage());
  353. return null;
  354. }
  355. // 记录日志
  356. if($this->type=='cash'){
  357. $afterData = UserWallet::getCashByUserId($this->_userId);
  358. $this->adminOperateLogger->fetchClass = UserWallet::class;
  359. }else {
  360. $afterData = UserBonus::getBonusByUserId($this->_userId);
  361. $this->adminOperateLogger->fetchClass = UserBonus::class;
  362. }
  363. $this->adminOperateLogger->afterInsert($afterData)->clean()->save([
  364. 'optType' => '审核变动会员余额',
  365. 'userId' => $model->USER_ID,
  366. 'userName' => Info::getUserNameByUserId($model->USER_ID),
  367. 'remark' => $model->CREATE_REMARK,
  368. ]);
  369. return $model;
  370. }
  371. /**
  372. * 审核余额变动
  373. * @return null
  374. * @throws \yii\db\Exception
  375. */
  376. public function changeAudit() {
  377. if (!$this->validate()) {
  378. return null;
  379. }
  380. $uids=[];
  381. if($this->auditStatus=='true'){
  382. foreach ($this->selected as $select) {
  383. $oneBalanceAudit = BalanceAudit::findOneAsArray('ID=:ID',[':ID'=>$select],'USER_ID');
  384. $uids[]=$oneBalanceAudit['USER_ID'];
  385. }
  386. }
  387. if($this->auditStatus=='true'){
  388. $this->adminOperateLogger->fetchClass = UserBonus::class;
  389. $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($uids, 'USER_ID');
  390. }else{
  391. $this->adminOperateLogger->fetchClass = BalanceAudit::class;
  392. $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($this->selected, 'ID');
  393. }
  394. $db = \Yii::$app->db;
  395. $transaction = $db->beginTransaction();
  396. $logs=[];
  397. try {
  398. foreach ($this->selected as $select) {
  399. $model = BalanceAudit::findOne(['ID' => $select]);
  400. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus'][$this->auditStatus]['value'];
  401. $model->AUDIT_ADMIN_ID = \Yii::$app->user->id;
  402. $model->CREATE_REMARK = $this->remark;
  403. $model->AUDITED_AT = Date::nowTime();
  404. if (!$model->save()) {
  405. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  406. }
  407. if ($model->AUDIT_STATUS == \Yii::$app->params['auditStatus']['true']['value']) {
  408. 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)]);
  409. }
  410. $logs[]=['userId'=>$select,'type'=>$model->TYPE];
  411. }
  412. $adminId = \Yii::$app->user->id;
  413. \Yii::$app->redis->del('balanceCode_'.$adminId);
  414. $transaction->commit();
  415. } catch (Exception $e) {
  416. $transaction->rollBack();
  417. $this->addError('changeAudit', $e->getMessage());
  418. return null;
  419. }
  420. if($this->auditStatus=='true'){
  421. $this->adminOperateLogger->fetchClass = UserBonus::class;
  422. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($uids, 'USER_ID')->clean()->save([
  423. 'optType' => '审核会员余额',
  424. 'remark' => $this->remark,
  425. ]);
  426. }else{
  427. $this->adminOperateLogger->fetchClass = BalanceAudit::class;
  428. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($this->selected, 'ID')->clean()->save([
  429. 'optType' => '审核会员余额',
  430. 'remark' => $this->remark,
  431. ]);
  432. }
  433. return $logs;
  434. }
  435. /**
  436. * 导入会员余额调整
  437. * @return null|static
  438. * @throws \yii\db\Exception
  439. */
  440. public function excelChangeBalance() {
  441. if (!$this->validate()) {
  442. return null;
  443. }
  444. $db = \Yii::$app->db;
  445. $transaction = $db->beginTransaction();
  446. try {
  447. $userInfo = Info::baseInfo($this->_userId);
  448. // 添加申请
  449. $model = new BalanceAudit();
  450. $model->USER_ID = $this->_userId;
  451. $model->LAST_DEC_LV = $userInfo['DEC_LV'];
  452. $model->LAST_EMP_LV = $userInfo['EMP_LV'];
  453. $model->LAST_DEC_ROLE_ID = $userInfo['DEC_ROLE_ID'];
  454. $model->TYPE = $this->type;
  455. $model->DEAL_TYPE = $this->dealType;
  456. $model->AMOUNT = $this->amount;
  457. $model->CREATE_REMARK = $this->remark;
  458. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['un']['value'];
  459. $model->REMARK_IS_SHOW = $this->isShow;
  460. $model->UPDATE_ADMIN_ID = \Yii::$app->user->id;
  461. $model->CREATED_AT = Date::nowTime();
  462. if (!$model->save()) {
  463. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  464. }
  465. $transaction->commit();
  466. } catch (Exception $e) {
  467. $transaction->rollBack();
  468. $this->addError('auditStatus', $e->getMessage());
  469. return null;
  470. }
  471. // 日志记录操作前的数据
  472. $beforeData = UserBonus::getBonusByUserId($this->_userId);
  473. $this->adminOperateLogger->fetchClass = UserBonus::class;
  474. $this->adminOperateLogger->beforeUpdate($beforeData);
  475. // 记录日志
  476. $this->adminOperateLogger->fetchClass = BalanceAudit::class;
  477. $this->adminOperateLogger->afterInsert($model)->clean()->save([
  478. 'optType' => '批量申请变动会员余额',
  479. 'userId' => $model->USER_ID,
  480. 'userName' => $userInfo['USER_NAME'],
  481. 'remark' => $model->CREATE_REMARK,
  482. ]);
  483. return $model;
  484. }
  485. /**
  486. * 删除前
  487. * @param $selected
  488. */
  489. public function beforeDelete($selected) {
  490. $this->adminOperateLogger->fetchClass = BalanceAudit::class;
  491. $this->adminOperateLogger->setIsBatch(true)->beforeDelete($selected, 'ID');
  492. }
  493. /**
  494. * 删除
  495. * @param $selected
  496. * @throws Exception
  497. */
  498. public function delete($selected) {
  499. $this->adminOperateLogger->clean()->save([
  500. 'optType' => '删除余额调整待审核数据',
  501. ]);
  502. }
  503. }