DoubleCopyPoolLevel.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace addons\DoubleCopy\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. use yii\db\Exception;
  6. use yii\db\Expression;
  7. /**
  8. * This is the model class for table "{{%addons_double_copy_pool_level}}".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城ID
  12. * @property string $name 名称
  13. * @property int $level 等级(权重)
  14. * @property float $achievement 所需一二市场业绩,单位:元
  15. * @property float $single_market 单个市场业绩,单位:元
  16. * @property float $current_money 当前池金额,单位:元
  17. * @property float $total_money 累计池金额,单位:元
  18. * @property int $bonus_num 当前分红人数
  19. * @property string|null $goods_setting 商品设置
  20. * @property array|null $extra_pool_level 商品设置
  21. * @property int $status 状态[-1:删除;0:禁用;1启用]
  22. * @property int|null $created_at 创建时间
  23. * @property int|null $updated_at 更新时间
  24. * @property int $is_enable_extra_pool 更新时间
  25. * @property float $current_score 当前池积分
  26. * @property float $total_score 累计池积分
  27. */
  28. class DoubleCopyPoolLevel extends \common\models\base\BaseActiveRecord
  29. {
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public static function tableName()
  34. {
  35. return '{{%addons_double_copy_pool_level}}';
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function rules()
  41. {
  42. return [
  43. [['mall_id', 'level', 'bonus_num', 'status', 'created_at', 'updated_at', 'is_enable_extra_pool'], 'integer'],
  44. [['achievement', 'single_market', 'current_money', 'total_money'], 'number'],
  45. [['goods_setting'], 'string'],
  46. [['extra_pool_level'], 'safe'],
  47. [['name'], 'string', 'max' => 250],
  48. [['current_score', 'total_score'], 'number'],
  49. ];
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function attributeLabels()
  55. {
  56. return [
  57. 'id' => 'ID',
  58. 'mall_id' => '商城ID',
  59. 'name' => '名称',
  60. 'level' => '等级(权重)',
  61. 'achievement' => '所需一二市场总业绩,单位:元',
  62. 'single_market' => '单个市场业绩,单位:元',
  63. 'current_money' => '当前池金额,单位:元',
  64. 'total_money' => '累计池金额,单位:元',
  65. 'bonus_num' => '当前分红人数',
  66. 'goods_setting' => '商品设置',
  67. 'status' => '状态[-1:删除;0:禁用;1启用]',
  68. 'created_at' => '创建时间',
  69. 'updated_at' => '更新时间',
  70. ];
  71. }
  72. /**
  73. * 获取权重
  74. * @Author: lun
  75. * @DateTime: 2022/5/14 0014 10:00
  76. * @Copyright: copyright (c) 2021 广东七件事集团
  77. * @param $mall_id
  78. * @return array
  79. */
  80. public static function getLevelWeights($mall_id) {
  81. $list = self::find()->select('level')->where(['status' => [StatusEnum::DISABLED,StatusEnum::ENABLED], 'mall_id' => $mall_id])->column();
  82. $newList = [];
  83. for ($i = 1; $i <= 10; $i++) {
  84. $newList[] = [
  85. 'name' => '权重' . $i,
  86. 'level' => $i,
  87. 'disabled' => in_array($i, $list),
  88. ];
  89. }
  90. return $newList;
  91. }
  92. /**
  93. * 保存数据
  94. * @Author: lun
  95. * @DateTime: 2022/9/16 0016 9:49
  96. * @Copyright: copyright (c) 2021 广东七件事集团
  97. * @param array $params
  98. * @return array|bool
  99. */
  100. public static function setData(array $params){
  101. try{
  102. if(!empty($params['id'])){
  103. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
  104. if(empty($model)) throw new \Exception('等级不存在或已删除');
  105. // 判断等级是否有被修改
  106. if ($model->level != $params['level']) {
  107. // 判断该等级下是否有股东存在
  108. $user = DoubleCopyPoolUser::find()->where(['mall_id' => $model->mall_id, 'level' => $model->level, 'status' => StatusEnum::ENABLED])->asArray()->one();
  109. if ($user) throw new Exception('该等级下存在用户,不能被修改');
  110. }
  111. }else{
  112. $model = new self();
  113. $model->mall_id = $params['mall_id'];
  114. $model->loadDefaultValues();
  115. }
  116. if(!$model->load($params,'') || !$model->save()){
  117. throw new Exception($model->getErrorMessage());
  118. }
  119. return $model->toArray();
  120. }catch(\Exception $e){
  121. self::$static_error = $e->getMessage();
  122. return false;
  123. }
  124. }
  125. /**
  126. * 增加奖金池金额
  127. * @Author: lun
  128. * @DateTime: 2022/9/16 0016 15:35
  129. * @Copyright: copyright (c) 2021 广东七件事集团
  130. * @param $mall_id
  131. * @param $level
  132. * @param array $params
  133. * @throws Exception
  134. */
  135. public static function savePoolMoney($mall_id, $level, $params = [])
  136. {
  137. $update_data['updated_at'] = time();
  138. foreach ($params as $key => $value) {
  139. $symbol = $value >= 0 ? '+' : '-';
  140. $update_data[$key] = new Expression($key.$symbol.abs($value));
  141. }
  142. $res = self::updateAll($update_data, ['mall_id' => $mall_id, 'level' => $level, 'status' => StatusEnum::ENABLED]);
  143. if (!$res) throw new Exception('修改能量池基金失败');
  144. }
  145. /**
  146. * 获取等级map
  147. * @Author: lun
  148. * @DateTime: 2022/9/16 0016 16:52
  149. * @Copyright: copyright (c) 2021 广东七件事集团
  150. * @param $mall_id
  151. * @return array
  152. */
  153. public static function getMap($mall_id)
  154. {
  155. $data = [];
  156. $list = self::find()->select('level,name')->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->orderBy('level asc')->asArray()->all();
  157. if ($list) {
  158. foreach ($list as $item) {
  159. $data[$item['level']] = $item['name'];
  160. }
  161. }
  162. return $data;
  163. }
  164. }