WriteOffLevel.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace addons\WriteOff\common\models;
  3. use Yii;
  4. use common\enums\StatusEnum;
  5. /**
  6. * This is the model class for table "{{%addons_write_off_level}}".
  7. *
  8. * @property int $id
  9. * @property int $mall_id 商城ID
  10. * @property int $level 股东等级
  11. * @property string $name 等级名称
  12. * @property int|null $is_enable_write_off_reward 核销阶梯式奖励设置[0:关闭;1启用]
  13. * @property string|null $write_off_reward_proportion 奖励比例集合
  14. * @property int|null $is_enable_promote_reward 促销奖励设置[0:关闭;1启用]
  15. * @property float $promote_reward_prize 促销奖励比例
  16. * @property int|null $is_enable_promote_reward_achievement 促销业绩阶梯奖励设置[0:关闭;1启用]
  17. * @property string|null $promote_achievement_reward_proportion 促销奖励比例集合
  18. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  19. * @property int $created_at
  20. * @property int $updated_at
  21. */
  22. class WriteOffLevel extends \common\models\base\BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_write_off_level}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'level', 'is_enable_write_off_reward', 'is_enable_promote_reward', 'is_enable_promote_reward_achievement', 'status', 'created_at', 'updated_at','is_enable_promote_limit','promote_time'], 'integer'],
  38. [['write_off_reward_proportion', 'promote_achievement_reward_proportion'], 'string'],
  39. [['promote_reward_prize'], 'number'],
  40. [['name'], 'string', 'max' => 45],
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'mall_id' => 'Mall ID',
  51. 'level' => 'Level',
  52. 'name' => 'Name',
  53. 'is_enable_write_off_reward' => 'Is Enable Write Off Reward',
  54. 'write_off_reward_proportion' => 'Write Off Reward Proportion',
  55. 'is_enable_promote_reward' => 'Is Enable Promote Reward',
  56. 'promote_reward_prize' => 'Promote Reward Prize',
  57. 'is_enable_promote_reward_achievement' => 'Is Enable Promote Reward Achievement',
  58. 'promote_achievement_reward_proportion' => 'Promote Achievement Reward Proportion',
  59. 'status' => 'Status',
  60. 'created_at' => 'Created At',
  61. 'updated_at' => 'Updated At',
  62. ];
  63. }
  64. /**
  65. * 获取等级等级和权重
  66. * @Author: lun
  67. * @DateTime: 2021/10/21 0021 9:22
  68. * @Copyright: copyright (c) 2021 广东七件事集团
  69. * @param $mall_id
  70. * @return array
  71. */
  72. public static function getLevelWeights($mall_id) {
  73. $list = self::find()->select('level')->where(['status' => [StatusEnum::DISABLED,StatusEnum::ENABLED], 'mall_id' => $mall_id])->column();
  74. $newList = [];
  75. for ($i = 1; $i <= 10; $i++) {
  76. $newList[] = [
  77. 'name' => '等级' . $i,
  78. 'level' => $i,
  79. 'disabled' => in_array($i, $list),
  80. ];
  81. }
  82. return $newList;
  83. }
  84. /**
  85. * 保存数据
  86. * @Author: lun
  87. * @DateTime: 2021/10/20 0020 18:01
  88. * @Copyright: copyright (c) 2021 广东七件事集团
  89. * @param array $params
  90. * @return AddonsBossLevel|bool
  91. */
  92. public static function setData(array $params){
  93. $trans = Yii::$app->db->beginTransaction();
  94. try{
  95. if(!empty($params['id'])){
  96. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
  97. if(empty($model)) throw new \Exception('等级不存在或已删除');
  98. // 判断等级是否有被修改
  99. if (!empty($params['level']) && $model->level != $params['level']) {
  100. // 判断该等级下是否有股东存在
  101. $boss = WriteOffUser::find()->where(['mall_id' => $model->mall_id, 'level' => $model->level, 'status' => StatusEnum::ENABLED])->asArray()->one();
  102. if ($boss) throw new \Exception('该等级下存在核销员,不能被修改');
  103. }
  104. }else{
  105. $model = new self();
  106. $model->loadDefaultValues();
  107. $model->mall_id = $params['mall_id'];
  108. }
  109. if(!$model->load($params,'') || !$model->save()){
  110. throw new \Exception($model->getErrorMessage());
  111. }
  112. $trans->commit();
  113. return $model;
  114. }catch(\Exception $e){
  115. $trans->rollBack();
  116. self::$static_error = $e->getMessage();
  117. return false;
  118. }
  119. }
  120. public static function del($params){
  121. try{
  122. $model = self::findOne(['id'=>$params['id']]);
  123. if(!empty($model)){
  124. $res = WriteOffUser::findOne(['level'=>$model->level,'mall_id'=>$params['mall_id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  125. if(!empty($res)) throw new \Exception('该等级关联了核销员');
  126. $model->status = StatusEnum::DELETE;
  127. $res = $model->save();
  128. if($res==false) throw new \Exception($model->getErrorMessage());
  129. }
  130. return true;
  131. }catch (\Exception $e){
  132. self::$static_error = $e->getMessage();
  133. return false;
  134. }
  135. }
  136. public static function getBossLevel(array $cond=[], array $with=[],bool $is_array=true,string $select='*',$order=""){
  137. $default_cond = [['<>','status',StatusEnum::DELETE]];
  138. $cond = array_merge($default_cond,$cond);
  139. $query = self::find()->select($select);
  140. self::paramPack(['where'=>$cond, 'with'=>$with],$query);
  141. isset($order) && $query->orderBy($order);
  142. $data = $query->asArray($is_array)->all();
  143. return $data;
  144. }
  145. }