PerfAdjustmentForm.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Form;
  5. use common\libs\logging\operate\AdminOperate;
  6. use common\models\Period;
  7. use common\models\UserPerf;
  8. use yii\base\Exception;
  9. class PerfAdjustmentForm extends Model
  10. {
  11. public $USER_ID;
  12. public $USER_NAME;
  13. public $SURPLUS_1L;
  14. public $SURPLUS_1L_ZC;
  15. public $SURPLUS_1L_FX;
  16. public $SURPLUS_2L;
  17. public $SURPLUS_2L_ZC;
  18. public $SURPLUS_2L_FX;
  19. public function init()
  20. {
  21. parent::init();
  22. $this->adminOperateLogger = new AdminOperate([
  23. 'fetchClass' => UserPerf::class,
  24. ]);
  25. }
  26. /**
  27. * @inheritdoc
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['USER_ID', 'SURPLUS_1L', 'SURPLUS_1L_ZC', 'SURPLUS_1L_FX', 'SURPLUS_2L', 'SURPLUS_2L_ZC', 'SURPLUS_2L_FX'], 'required'],
  33. ];
  34. }
  35. /**
  36. * 指定场景
  37. * @return array
  38. */
  39. public function scenarios() {
  40. $parentScenarios = parent::scenarios();
  41. $customScenarios = [
  42. 'perfAdjustment' => ['USER_ID', 'USER_NAME', 'SURPLUS_1L', 'SURPLUS_1L_ZC', 'SURPLUS_1L_FX', 'SURPLUS_2L', 'SURPLUS_2L_ZC', 'SURPLUS_2L_FX']
  43. ];
  44. return array_merge($parentScenarios, $customScenarios);
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'USER_ID' => '会员ID',
  53. 'SURPLUS_1L' => '一市场综合结余业绩',
  54. 'SURPLUS_1L_ZC' => '一市场综合报单业绩',
  55. 'SURPLUS_1L_FX' => '一市场复消结余业绩',
  56. 'SURPLUS_2L' => '二市场综合结余业绩',
  57. 'SURPLUS_2L_ZC' => '二市场综合报单业绩',
  58. 'SURPLUS_2L_FX' => '二市场复消结余业绩',
  59. ];
  60. }
  61. /**
  62. * 调整会员业绩
  63. * @return bool|null
  64. */
  65. public function perfAdjustment(): ?bool
  66. {
  67. if (!$this->validate()) {
  68. return false;
  69. }
  70. $transaction = \Yii::$app->db->beginTransaction();
  71. try {
  72. // 修改前业绩
  73. $userPerf = UserPerf::find()
  74. ->where('USER_ID=:USER_ID', [':USER_ID' => $this->USER_ID])
  75. ->select('USER_ID,SURPLUS_1L,SURPLUS_1L_ZC,SURPLUS_1L_FX,SURPLUS_2L,SURPLUS_2L_ZC,SURPLUS_2L_FX')
  76. ->one();
  77. if (!$userPerf->toArray()) {
  78. throw new Exception('Member performance does not exist'); // 会员业绩不存在
  79. }
  80. $this->adminOperateLogger->beforeUpdate($userPerf);
  81. $modernPerf = [
  82. 'SURPLUS_1L' => $this->SURPLUS_1L,
  83. 'SURPLUS_1L_ZC' => $this->SURPLUS_1L_ZC,
  84. 'SURPLUS_1L_FX' => $this->SURPLUS_1L_FX,
  85. 'SURPLUS_2L' => $this->SURPLUS_2L,
  86. 'SURPLUS_2L_ZC' => $this->SURPLUS_2L_ZC,
  87. 'SURPLUS_2L_FX' => $this->SURPLUS_2L_FX,
  88. ];
  89. if (!UserPerf::updateAll($modernPerf, 'USER_ID=:USER_ID', [':USER_ID' => $this->USER_ID])) {
  90. throw new Exception(Form::formatErrorsForApi($userPerf->getErrors()));
  91. }
  92. $transaction->commit();
  93. $afterUpdate = UserPerf::find()
  94. ->where('USER_ID=:USER_ID', [':USER_ID' => $this->USER_ID])
  95. ->select('USER_ID,SURPLUS_1L,SURPLUS_1L_ZC,SURPLUS_1L_FX,SURPLUS_2L,SURPLUS_2L_ZC,SURPLUS_2L_FX')
  96. ->one();
  97. $this->adminOperateLogger->afterUpdate($afterUpdate)->clean()->save([
  98. 'optType' => 'perf adjustment', // 调整业绩
  99. 'userId' => $this->USER_ID,
  100. 'userName' => $this->USER_NAME,
  101. 'periodNum' => Period::instance()->getNowPeriodNum(),
  102. ]);
  103. } catch (Exception $e) {
  104. $transaction->rollBack();
  105. $this->addError('perfAdjustment', $e->getMessage());
  106. return false;
  107. }
  108. return true;
  109. }
  110. }