AddonsBoss.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace addons\BossWeight\common\models;
  3. use addons\Agent\common\models\AgentLevel;
  4. use addons\BossWeight\common\enums\LevelEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\common\Addons;
  7. use common\models\mall\MallAddons;
  8. use common\models\user\User;
  9. use Yii;
  10. use yii\db\Exception;
  11. /**
  12. * This is the model class for table "{{%addons_boss_weight}}".
  13. *
  14. * @property int $id
  15. * @property int $mall_id 商城ID
  16. * @property int $user_id 用户ID
  17. * @property int $level 股东等级
  18. * @property float $commission 累计永久佣金,单位:元
  19. * @property float $extra_commission 累计额外佣金,单位:元
  20. * @property float $temp_extra_commission 额外分红金额,主要用于判断是否会导致超额
  21. * @property float $total_commission 累计总佣金,单位:元
  22. * @property int $upgrade_status 1条件升级,2购买指定商品升级,3手动升级
  23. * @property int $upgrade_level_at 股东等级升级时间
  24. * @property string $remark 备注
  25. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  26. * @property int $created_at 创建时间
  27. * @property int $updated_at 修改时间
  28. */
  29. class AddonsBoss extends \common\models\base\BaseActiveRecord
  30. {
  31. const ADDONS_NAME = 'BossWeight';
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public static function tableName()
  36. {
  37. return '{{%addons_boss_weight}}';
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['mall_id', 'user_id', 'level', 'upgrade_status', 'upgrade_level_at', 'status', 'created_at', 'updated_at','fixed_weight'], 'integer'],
  46. [['commission', 'extra_commission', 'temp_extra_commission', 'total_commission'], 'number'],
  47. [['remark'], 'string'],
  48. ];
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function attributeLabels()
  54. {
  55. return [
  56. 'id' => 'ID',
  57. 'mall_id' => '商城ID',
  58. 'user_id' => '用户ID',
  59. 'level' => '股东等级',
  60. 'commission' => '累计永久佣金,单位:元',
  61. 'extra_commission' => '累计额外佣金,单位:元',
  62. 'temp_extra_commission' => '额外分红金额,主要用于判断是否会导致超额',
  63. 'total_commission' => '累计总佣金,单位:元',
  64. 'upgrade_status' => '1条件升级,2购买指定商品升级,3手动升级',
  65. 'upgrade_level_at' => '股东等级升级时间',
  66. 'remark' => '备注',
  67. 'status' => '状态[-1:删除;0:禁用;1启用]',
  68. 'created_at' => '创建时间',
  69. 'updated_at' => '修改时间',
  70. 'fixed_weight' => '固定权重',
  71. ];
  72. }
  73. public function getUser()
  74. {
  75. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,parent_id');
  76. }
  77. /**
  78. * @desc:关联股东等级
  79. * @Author: hua
  80. * @Date: 2021/11/5
  81. * @Time: 10:04
  82. * @Copyright: copyright (c) 2021 广东七件事集团
  83. * @return \yii\db\ActiveQuery
  84. */
  85. public function getBossLevel(){
  86. return $this->hasOne(AddonsBossLevel::class, ['level' => 'level'])->andWhere(['<>','status',StatusEnum::DELETE]);
  87. }
  88. /**
  89. * 保存数据
  90. * @Author: lun
  91. * @DateTime: 2021/10/22 0022 17:13
  92. * @Copyright: copyright (c) 2021 广东七件事集团
  93. * @param array $params
  94. * @return AddonsBoss|bool
  95. */
  96. public static function setData(array $params){
  97. try{
  98. if(!empty($params['user_id'])){
  99. $model = self::findOne(['user_id' => $params['user_id'],'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED]);
  100. if(empty($model)) {
  101. $model = new self();
  102. $model->loadDefaultValues();
  103. $model->mall_id = $params['mall_id'];
  104. $model->upgrade_status = LevelEnum::UPGRADE_MANUAL;
  105. $model->upgrade_level_at = time();
  106. }
  107. } else {
  108. throw new \Exception('股东数据不存在或已删除');
  109. }
  110. if(!empty($params['fixed_weight'])){
  111. $params['fixed_weight'] = $model->fixed_weight + $params['fixed_weight'];
  112. }
  113. if(!$model->load($params,'') || !$model->save()){
  114. throw new Exception($model->getErrorMessage());
  115. }
  116. return $model;
  117. }catch(\Exception $e){
  118. self::setStaticError($e->getMessage());
  119. return false;
  120. }
  121. }
  122. /**
  123. * 获取股东列表
  124. * @Author: lun
  125. * @DateTime: 2021/10/25 0025 10:05
  126. * @Copyright: copyright (c) 2021 广东七件事集团
  127. * @param array $cond
  128. * @param array $with
  129. * @param bool $is_array
  130. * @param string $select
  131. * @return array|\yii\db\ActiveRecord[]
  132. */
  133. public static function getBoss(array $cond=[], array $with=[],bool $is_array=true,string $select='*'){
  134. $default_cond = [['<>','status',StatusEnum::DELETE]];
  135. $cond = array_merge($default_cond,$cond);
  136. $query = self::find()->select($select);
  137. self::paramPack(['where'=>$cond, 'with'=>$with],$query);
  138. $data = $query->asArray($is_array)->all();
  139. return $data;
  140. }
  141. /**
  142. * @desc:推广中心-调用
  143. * @Author: hua
  144. * @Date: 2021/11/12
  145. * @Time: 9:30
  146. * @Copyright: copyright (c) 2021 广东七件事集团
  147. * @param $user_id
  148. * @param $mall_id
  149. * @param $name
  150. * @return array
  151. */
  152. public static function getIncomeInfo($params){
  153. $mall_id = $params["mall_id"];
  154. $name = $params["addons_name"];
  155. $user_id = $params["user_id"];
  156. $is_install = MallAddons::isInstall($mall_id, $name);
  157. $data['total_commission'] = 0;
  158. $data['is_install'] = 0;
  159. if($is_install){
  160. $model = self::findOne(['user_id'=>$user_id,'status'=>StatusEnum::ENABLED]);
  161. $data['total_commission'] = $model['total_commission']??0;
  162. $data['is_install'] = $is_install;
  163. }
  164. return $data;
  165. }
  166. public static function getAddons(array $params){
  167. $addons_name = $params['addons_name'];
  168. $is_install = MallAddons::isInstall($params['mall_id'], $addons_name);
  169. if($is_install){
  170. $detail = Addons::getOne([['name' =>$addons_name], ['is_online' => 1]], true,);
  171. return [
  172. 'addons_name'=>$addons_name,
  173. 'title'=>$detail['title'],
  174. ];
  175. }
  176. return [];
  177. }
  178. }