EmployLevelForm.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\libs\logging\operate\AdminOperate;
  8. use common\models\EmployLevel;
  9. use yii\helpers\Json;
  10. /**
  11. * Login form
  12. */
  13. class EmployLevelForm extends Model {
  14. public $id;
  15. public $levelName; // 级别名称
  16. public $locationNum; // 区域数量
  17. public $locationPerf; // 单区业绩
  18. public $newPerf; // 当区新增业绩
  19. public $minEmployLevel; // locationNum个区域内存在什么级别的会员
  20. public $sort; // 等级排序
  21. public $iconType; // 等级排序
  22. public $iconNum; // 等级排序
  23. public $otherDepartPerf; // 其它部门的累计推荐业绩
  24. public function init() {
  25. parent::init();
  26. $this->adminOperateLogger = new AdminOperate([
  27. 'fetchClass' => EmployLevel::class,
  28. ]);
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. public function rules() {
  34. return [
  35. [['id', 'levelName', 'locationNum', 'locationPerf', 'newPerf', 'minEmployLevel', 'sort', 'iconType', 'iconNum', 'otherDepartPerf'], 'trim', 'on' => ['add', 'edit']],
  36. [['id'], 'required', 'on' => 'edit'],
  37. [['id'], 'exist', 'targetClass' => EmployLevel::class, 'targetAttribute' => 'ID'],
  38. [['levelName', 'locationNum', 'locationPerf', 'newPerf', 'sort'], 'required'],
  39. [['minEmployLevel'], 'exist', 'targetClass' => EmployLevel::class, 'targetAttribute' => 'ID'],
  40. [['locationNum', 'sort'], 'integer'],
  41. [['locationPerf', 'newPerf', 'otherDepartPerf'], 'price'],
  42. [['iconType', 'iconNum'], 'number', 'max' => 10, 'min' => 0, 'on' => 'edit'],
  43. ['iconType', 'isIconType', 'on' => 'edit'],
  44. ['iconNum', 'isIconNum', 'on' => 'edit'],
  45. ];
  46. }
  47. /**
  48. * 图标类型判断
  49. * @param $attributes
  50. */
  51. public function isIconType($attributes) {
  52. if (!in_array($this->iconType, [0, 1, 2, 3])) {
  53. $this->addError($attributes, '请选择正确的图标');
  54. }
  55. }
  56. public function isIconNum($attributes) {
  57. if (!in_array($this->iconType, [0, 1, 2, 3])) {
  58. $this->addError($attributes, '请选择正确的图标');
  59. }
  60. }
  61. /**
  62. * 编辑
  63. * @return EmployLevel|null|static
  64. */
  65. public function edit() {
  66. if (!$this->validate()) {
  67. return null;
  68. }
  69. if ($this->scenario == 'add') {
  70. $this->addError('id', '提交场景不存在');
  71. return null;
  72. $model = new EmployLevel();
  73. $model->CREATE_ADMIN = Admin::getAdminNameById(\Yii::$app->user->id);
  74. $model->CREATED_AT = Date::nowTime();
  75. } elseif ($this->scenario == 'edit') {
  76. $model = EmployLevel::findOne(['ID' => $this->id]);
  77. $model->UPDATE_ADMIN = Admin::getAdminNameById(\Yii::$app->user->id);
  78. $model->UPDATED_AT = Date::nowTime();
  79. } else {
  80. $this->addError('id', '提交场景不存在');
  81. return null;
  82. }
  83. //$model->LEVEL_NAME = $this->levelName;
  84. //$model->LOCATION_NUM = $this->locationNum;
  85. //$model->LOCATION_PERF = $this->locationPerf;
  86. //$model->NEW_PERF = $this->newPerf;
  87. //$model->MIN_EMPLOY_LEVEL = $this->minEmployLevel;
  88. //$model->SORT = $this->sort;
  89. $model->ICON_TYPE = $this->iconType;
  90. $model->ICON_NUM = $this->iconNum;
  91. $model->OTHER_DEPART_PERF = $this->otherDepartPerf;
  92. if ($model->save()) {
  93. return $model;
  94. } else {
  95. $this->addErrors($model->getErrors());
  96. return null;
  97. }
  98. }
  99. /**
  100. * 聘级奖金配置
  101. * @return bool|null
  102. * @throws \yii\db\Exception
  103. */
  104. public function updateBonus(){
  105. $beforeData = EmployLevel::getBonusConfig();
  106. $this->adminOperateLogger->saveBeforeContent=$beforeData;
  107. $postData = \Yii::$app->request->post('data');
  108. $transaction = \Yii::$app->db->beginTransaction();
  109. try {
  110. foreach ($postData as $value) {
  111. $empLevelModel = EmployLevel::findOne(['ID' => $value['ID']]);
  112. if ($empLevelModel) {
  113. $empLevelModel->YC_PERCENT_ARR = Json::encode($value['YC_PERCENT_ARR']);
  114. // $empLevelModel->CF_BASE_NUM = $value['CF_BASE_NUM'];
  115. // $empLevelModel->CF_PERCENT = $value['CF_PERCENT'];
  116. // $empLevelModel->LX_BASE_NUM = $value['LX_BASE_NUM'];
  117. // $empLevelModel->LX_PERCENT = $value['LX_PERCENT'];
  118. $empLevelModel->RX_PERCENT = $value['RX_PERCENT'];
  119. $empLevelModel->LEVEL_SCORE = $value['LEVEL_SCORE'];
  120. $empLevelModel->UPGRADE_SCORE = $value['UPGRADE_SCORE'];
  121. if (!$empLevelModel->save()) {
  122. throw new \Exception(Form::formatErrorsForApi($empLevelModel->getErrors()));
  123. }
  124. }
  125. }
  126. $transaction->commit();
  127. } catch (\Exception $e) {
  128. $transaction->rollBack();
  129. $this->addError('updateBonus', $e->getMessage());
  130. return null;
  131. }
  132. $afterData = EmployLevel::getBonusConfig();
  133. $this->adminOperateLogger->saveAfterContent=$afterData;
  134. unset($beforeData,$afterData);
  135. $this->adminOperateLogger->clean()->save([
  136. 'optType' => '更新聘级奖金配置',
  137. ]);
  138. return true;
  139. }
  140. }