EmployLevelForm.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 $achieveMemberNum; // 当月邀请会员数
  25. public $achievePv; // 邀请会员数不及格后获奖BV值
  26. public $achievePerfPv; // 邀请会员数及格后获奖BV值
  27. public function init() {
  28. parent::init();
  29. $this->adminOperateLogger = new AdminOperate([
  30. 'fetchClass' => EmployLevel::class,
  31. ]);
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function rules() {
  37. return [
  38. [['id', 'levelName', 'locationNum', 'locationPerf', 'newPerf', 'minEmployLevel', 'sort', 'iconType', 'iconNum', 'otherDepartPerf', 'achieveMemberNum', 'achievePv', 'achievePerfPv'], 'trim', 'on' => ['add', 'edit']],
  39. [['id'], 'required', 'on' => 'edit'],
  40. [['id'], 'exist', 'targetClass' => EmployLevel::class, 'targetAttribute' => 'ID'],
  41. [['levelName', 'locationNum', 'locationPerf', 'newPerf', 'sort'], 'required'],
  42. [['minEmployLevel'], 'exist', 'targetClass' => EmployLevel::class, 'targetAttribute' => 'ID'],
  43. [['locationNum', 'sort'], 'integer'],
  44. [['locationPerf', 'newPerf', 'otherDepartPerf'], 'price'],
  45. [['iconType', 'iconNum'], 'number', 'max' => 10, 'min' => 0, 'on' => 'edit'],
  46. ['iconType', 'isIconType', 'on' => 'edit'],
  47. ['iconNum', 'isIconNum', 'on' => 'edit'],
  48. ];
  49. }
  50. /**
  51. * 图标类型判断
  52. * @param $attributes
  53. */
  54. public function isIconType($attributes) {
  55. if (!in_array($this->iconType, [0, 1, 2, 3])) {
  56. $this->addError($attributes, '请选择正确的图标');
  57. }
  58. }
  59. public function isIconNum($attributes) {
  60. if (!in_array($this->iconType, [0, 1, 2, 3])) {
  61. $this->addError($attributes, '请选择正确的图标');
  62. }
  63. }
  64. /**
  65. * 编辑
  66. * @return EmployLevel|null|static
  67. */
  68. public function edit() {
  69. if (!$this->validate()) {
  70. return null;
  71. }
  72. if ($this->scenario == 'add') {
  73. $this->addError('id', '提交场景不存在');
  74. return null;
  75. $model = new EmployLevel();
  76. $model->CREATE_ADMIN = Admin::getAdminNameById(\Yii::$app->user->id);
  77. $model->CREATED_AT = Date::nowTime();
  78. } elseif ($this->scenario == 'edit') {
  79. $model = EmployLevel::findOne(['ID' => $this->id]);
  80. $model->UPDATE_ADMIN = Admin::getAdminNameById(\Yii::$app->user->id);
  81. $model->UPDATED_AT = Date::nowTime();
  82. } else {
  83. $this->addError('id', '提交场景不存在');
  84. return null;
  85. }
  86. //$model->LEVEL_NAME = $this->levelName;
  87. //$model->LOCATION_NUM = $this->locationNum;
  88. //$model->LOCATION_PERF = $this->locationPerf;
  89. //$model->NEW_PERF = $this->newPerf;
  90. //$model->MIN_EMPLOY_LEVEL = $this->minEmployLevel;
  91. //$model->SORT = $this->sort;
  92. $model->ICON_TYPE = $this->iconType;
  93. $model->ICON_NUM = $this->iconNum;
  94. $model->OTHER_DEPART_PERF = $this->otherDepartPerf;
  95. $model->ACHIEVE_MEMBER_NUM = $this->achieveMemberNum ?? 0;
  96. $model->ACHIEVE_PV = $this->achievePv ?? 0;
  97. $model->ACHIEVE_PERF_PV = $this->achievePerfPv ?? 0;
  98. if ($model->save()) {
  99. return $model;
  100. } else {
  101. $this->addErrors($model->getErrors());
  102. return null;
  103. }
  104. }
  105. /**
  106. * 聘级奖金配置
  107. * @return bool|null
  108. * @throws \yii\db\Exception
  109. */
  110. public function updateBonus(){
  111. $beforeData = EmployLevel::getBonusConfig();
  112. $this->adminOperateLogger->saveBeforeContent=$beforeData;
  113. $postData = \Yii::$app->request->post('data');
  114. $transaction = \Yii::$app->db->beginTransaction();
  115. try {
  116. foreach ($postData as $value) {
  117. $empLevelModel = EmployLevel::findOne(['ID' => $value['ID']]);
  118. if ($empLevelModel) {
  119. $empLevelModel->YC_PERCENT_ARR = Json::encode($value['YC_PERCENT_ARR']);
  120. // $empLevelModel->CF_BASE_NUM = $value['CF_BASE_NUM'];
  121. // $empLevelModel->CF_PERCENT = $value['CF_PERCENT'];
  122. // $empLevelModel->LX_BASE_NUM = $value['LX_BASE_NUM'];
  123. // $empLevelModel->LX_PERCENT = $value['LX_PERCENT'];
  124. $empLevelModel->RX_PERCENT = $value['RX_PERCENT'];
  125. $empLevelModel->LEVEL_SCORE = $value['LEVEL_SCORE'];
  126. $empLevelModel->UPGRADE_SCORE = $value['UPGRADE_SCORE'];
  127. if (!$empLevelModel->save()) {
  128. throw new \Exception(Form::formatErrorsForApi($empLevelModel->getErrors()));
  129. }
  130. }
  131. }
  132. $transaction->commit();
  133. } catch (\Exception $e) {
  134. $transaction->rollBack();
  135. $this->addError('updateBonus', $e->getMessage());
  136. return null;
  137. }
  138. $afterData = EmployLevel::getBonusConfig();
  139. $this->adminOperateLogger->saveAfterContent=$afterData;
  140. unset($beforeData,$afterData);
  141. $this->adminOperateLogger->clean()->save([
  142. 'optType' => '更新聘级奖金配置',
  143. ]);
  144. return true;
  145. }
  146. }