DoubleCopyUser.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace addons\DoubleCopy\common\models;
  3. use addons\DoubleCopy\common\enums\DoubleCopyEnum;
  4. use common\enums\StatusEnum;
  5. use common\enums\UserWalletEnum;
  6. use common\models\common\CapitalLog;
  7. use common\models\mall\MallAddons;
  8. use common\models\user\User;
  9. use common\models\user\UserPartitionRelationship;
  10. use services\common\UserWalletService;
  11. use Yii;
  12. use yii\db\Exception;
  13. use yii\db\Expression;
  14. /**
  15. * This is the model class for table "{{%addons_double_copy_user}}".
  16. *
  17. * @property int $id
  18. * @property int $mall_id 商城ID
  19. * @property int $user_id 用户ID
  20. * @property int $level 等级
  21. * @property float $commission 累计佣金,单位:元
  22. * @property float $frozen_commission 累计冻结佣金,单位:元
  23. * @property float $frozen_fund 冻结基金,单位:元
  24. * @property int $help_num 助力用户数量
  25. * @property int $team_num 团队数量(代理+老板)
  26. * @property int $pid 上级uid
  27. * @property int $topid 上级老板id
  28. * @property int $pos 相对上级的位置
  29. * @property int $upgrade_status 1条件升级,2购买指定商品升级,3手动升级
  30. * @property int $upgrade_level_at 股东等级升级时间
  31. * @property int|null $is_thaw 解冻状态[0:否,1:全部解冻,2:部分解冻]
  32. * @property float $thaw_percent 上次解冻比例,单位:百分比
  33. * @property float $achievement 业绩,单位:元
  34. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  35. * @property int $created_at 创建时间
  36. * @property int $updated_at 修改时间
  37. * @property float $score 已结算积分
  38. * @property float $frozen_score 未结算积分
  39. * @property float $frozen_fund_score 冻结基金积分
  40. * @property int $is_virtual 是否虚拟用户
  41. * @property int $upgrade_boss_at 成为老板以上等级的时间
  42. */
  43. class DoubleCopyUser extends \common\models\base\BaseActiveRecord
  44. {
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public static function tableName()
  49. {
  50. return '{{%addons_double_copy_user}}';
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function rules()
  56. {
  57. return [
  58. [['mall_id', 'user_id', 'level', 'pid', 'topid', 'pos', 'help_num', 'team_num', 'upgrade_status', 'upgrade_level_at', 'is_thaw', 'status', 'created_at', 'updated_at','is_height_commission', 'is_virtual', 'upgrade_boss_at'], 'integer'],
  59. [['commission', 'frozen_commission', 'frozen_fund', 'achievement', 'thaw_percent','height_commission'], 'number'],
  60. [['score', 'frozen_score', 'frozen_fund_score',], 'number'],
  61. [['goods_info',], 'string'],
  62. ];
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function attributeLabels()
  68. {
  69. return [
  70. 'id' => 'ID',
  71. 'mall_id' => '商城ID',
  72. 'user_id' => '用户ID',
  73. 'level' => '等级',
  74. 'commission' => '累计佣金,单位:元',
  75. 'frozen_commission' => '累计冻结佣金,单位:元',
  76. 'frozen_fund' => '冻结基金,单位:元',
  77. 'pid' => '上级uid',
  78. 'topid' => '上级老板id',
  79. 'pos' => '相对上级的位置',
  80. 'help_num' => '助力用户数量',
  81. 'team_num' => '团队数量(代理+老板)',
  82. 'upgrade_status' => '1条件升级,2购买指定商品升级,3手动升级',
  83. 'upgrade_level_at' => '股东等级升级时间',
  84. 'is_thaw' => '解冻状态[0:否,1:全部解冻,2:部分解冻]',
  85. 'thaw_percent' => '上次解冻比例,单位:百分比',
  86. 'achievement' => '业绩,单位:元',
  87. 'status' => '状态[-1:删除;0:禁用;1启用]',
  88. 'created_at' => '创建时间',
  89. 'updated_at' => '修改时间',
  90. 'goods_info' => '商品信息',
  91. 'is_virtual' => '是否虚拟用户',
  92. 'upgrade_boss_at' => '成为老板以上等级的时间'
  93. ];
  94. }
  95. /**
  96. * 获取用户
  97. * @Author: lun
  98. * @DateTime: 2022/5/14 0014 15:31
  99. * @Copyright: copyright (c) 2021 广东七件事集团
  100. * @return \yii\db\ActiveQuery
  101. */
  102. public function getUser()
  103. {
  104. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,nickname,mobile,avatar_url,parent_id');
  105. }
  106. /**
  107. * 获取用户
  108. * @Author: lun
  109. * @DateTime: 2022/5/14 0014 15:31
  110. * @Copyright: copyright (c) 2021 广东七件事集团
  111. * @return \yii\db\ActiveQuery
  112. */
  113. public function getPuser()
  114. {
  115. return $this->hasOne(User::class, ['id' => 'pid'])->select('id,nickname,mobile,avatar_url,parent_id');
  116. }
  117. /**
  118. * 获取用户
  119. * @Author: lun
  120. * @DateTime: 2022/5/14 0014 15:31
  121. * @Copyright: copyright (c) 2021 广东七件事集团
  122. * @return \yii\db\ActiveQuery
  123. */
  124. public function getTopuser()
  125. {
  126. return $this->hasOne(User::class, ['id' => 'topid'])->select('id,nickname,mobile,avatar_url,parent_id');
  127. }
  128. public function getMarket()
  129. {
  130. return $this->hasMany(self::class, ['pid' => 'user_id'])->where(['status' => StatusEnum::ENABLED])->andWhere(['<=', 'pos', 2])->select('user_id,pid,achievement,is_virtual')->orderBy('pos asc');
  131. }
  132. /**
  133. * 保存数据
  134. * @Author: lun
  135. * @DateTime: 2022/5/14 0014 16:49
  136. * @Copyright: copyright (c) 2021 广东七件事集团
  137. * @param array $params
  138. * @return array|bool
  139. */
  140. public static function setData(array $params){
  141. try{
  142. if(!empty($params['id'])){
  143. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
  144. if(empty($model)) throw new \Exception('等级不存在或已删除');
  145. }else{
  146. $model = new self();
  147. $model->loadDefaultValues();
  148. $model->mall_id = $params['mall_id'];
  149. }
  150. if(!$model->load($params,'') || !$model->save()){
  151. throw new Exception($model->getErrorMessage());
  152. }
  153. return $model->toArray();
  154. }catch(\Exception $e){
  155. self::$static_error = $e->getMessage();
  156. return false;
  157. }
  158. }
  159. /**
  160. * 批量查询
  161. * @Author: lun
  162. * @DateTime: 2022/5/18 0018 11:11
  163. * @Copyright: copyright (c) 2021 广东七件事集团
  164. * @param $offset
  165. * @param $limit
  166. * @param $tree
  167. * @param array $params
  168. * @return bool|mixed
  169. */
  170. public static function batchGetTeamData($offset, $limit, $tree, $params = [])
  171. {
  172. $params['where'][] = ['like', 'tree', "{$tree},%", false];
  173. $params['offset'] = ($offset - 1) * ($limit - 1);
  174. $params['limit'] = $limit;
  175. return UserPartitionRelationship::lists($params);
  176. }
  177. /**
  178. * 更新用户资金
  179. * @Author: lun
  180. * @DateTime: 2022/5/18 0018 11:58
  181. * @Copyright: copyright (c) 2021 广东七件事集团
  182. * @param $users
  183. * @return bool
  184. */
  185. public static function batchAddCommission($users)
  186. {
  187. try {
  188. foreach ($users as $uid => $item) {
  189. $model = self::findOne(['user_id' => $uid, 'status' => StatusEnum::ENABLED]);
  190. if ($model) {
  191. foreach ($item as $field => $commission) {
  192. $model->$field += $commission;
  193. }
  194. if (!$model->save()) throw new \Exception($model->getError());
  195. }
  196. }
  197. return true;
  198. } catch (\Exception $e) {
  199. self::setStaticError($e->getMessage());
  200. return false;
  201. }
  202. }
  203. /**
  204. * 推广中心数据获取
  205. * @Author: lun
  206. * @DateTime: 2022/5/18 0018 16:04
  207. * @Copyright: copyright (c) 2021 广东七件事集团
  208. * @param array $params
  209. * @return mixed
  210. */
  211. public static function getIncomeInfo(array $params)
  212. {
  213. $is_install = MallAddons::isInstall($params['mall_id'], $params['addons_name']);
  214. $data['total_commission'] = 0;
  215. $data['is_install'] = 0;
  216. $basic = \Yii::$app->services->setting->addons->get($params['mall_id'], $params['addons_name'], 'basic');
  217. $data['is_show_at_center'] = 1;
  218. if(isset($basic['is_show_at_center'])) $data['is_show_at_center'] = $basic['is_show_at_center'];
  219. if ($is_install) {
  220. $model = self::findOne([
  221. 'user_id' => $params['user_id'],
  222. 'status' => StatusEnum::ENABLED,
  223. ]);
  224. // 获取分红池收益
  225. $pool_income = DoubleCopyPoolUser::find()->select('pool_income')->where(['user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED])->scalar() ?? 0;
  226. // 获取冻结佣金
  227. CapitalLog::$capitalType = "commission_frozen";
  228. $frozen_commission = CapitalLog::find()->where(['user_id' => $params['user_id'], 'from_type' => DoubleCopyEnum::ADDONS_NAME, 'status' => StatusEnum::DISABLED])->sum('change_num') ?: 0;
  229. $commission = $model['commission'] ?: 0;
  230. $data['total_commission'] = $commission + $frozen_commission + $pool_income;// 累计 = 累计可用 + 冻结 + 分红池收益
  231. $data['is_install'] = $is_install;
  232. }
  233. return $data;
  234. }
  235. /**
  236. * 查询用户是否已达标
  237. * @Author: lun
  238. * @DateTime: 2022/5/18 0018 17:36
  239. * @Copyright: copyright (c) 2021 广东七件事集团
  240. * @param $user_id
  241. * @param $thaw_num
  242. * @return int[]
  243. */
  244. public static function getForzenUser($user_id, $thaw_num)
  245. {
  246. // 数据初始化
  247. $data = [
  248. 'has_data' => false,
  249. 'is_thaw' => 0,
  250. 'boss_num' => 0,
  251. 'agent_num' => 0,
  252. 'help_count' => 0,// 已助力人数
  253. ];
  254. $list = self::find()->select('user_id,level,team_num')->where(['pid' => $user_id, 'pos' => [1,2], 'status' => StatusEnum::ENABLED])->asArray()->all();
  255. if (empty($list)) return $data;
  256. $data['has_data'] = true;
  257. $team_num = 0;
  258. foreach ($list as $item) {
  259. // 查询缺少的老板数量
  260. if ($item['level'] == DoubleCopyEnum::LEVEL_AGENT) {
  261. $data['boss_num']++;
  262. } else {
  263. $data['help_count']++;// 等级大于等于老板则表示老板已助力人数
  264. }
  265. // 查询缺少的经理数量
  266. $team_num += $item['team_num'];
  267. }
  268. if ($team_num < $thaw_num) {
  269. $data['agent_num'] = $thaw_num - $team_num;
  270. $data['help_count'] += $team_num;
  271. } else {
  272. $data['help_count'] += $thaw_num;
  273. }
  274. // 老板+经理数量都达标则此用户达标
  275. if ($data['boss_num'] == 0 && $data['agent_num'] == 0) $data['is_thaw'] = 1;
  276. return $data;
  277. }
  278. //检查额外解冻条件是否达标
  279. public static function checkUnForzen($user_id, $setting)
  280. {
  281. $data = [
  282. 'is_thaw' => 0,
  283. ];
  284. //获取1,2号位数据
  285. $list = self::find()->select('user_id,level')->where(['pid' => $user_id, 'pos' => [1,2], 'status' => StatusEnum::ENABLED])->asArray()->all();
  286. foreach ($list as $key => $value) {
  287. if ($value['level'] < $setting['first_level']) return $data;
  288. }
  289. //获取直推下线符合条件的人数
  290. $push_count = self::find()->select('user_id,level')->where(['pid' => $user_id, 'status' => StatusEnum::ENABLED])->andWhere(['>=', 'level', $setting['second_level']])->count();
  291. if ($push_count < $setting['push_num']) return $data;
  292. $data['is_thaw'] = 1;
  293. return $data;
  294. }
  295. /**
  296. * 保存业绩
  297. * @Author: lun
  298. * @DateTime: 2022/9/8 0008 17:28
  299. * @Copyright: copyright (c) 2021 广东七件事集团
  300. * @param $user_id
  301. * @param $params
  302. * @throws Exception
  303. */
  304. public static function saveAchievement($user_id, $params)
  305. {
  306. $update_data['updated_at'] = time();
  307. foreach ($params as $key => $value) {
  308. $symbol = $value >= 0 ? '+' : '-';
  309. $update_data[$key] = new Expression($key.$symbol.abs($value));
  310. }
  311. $res = self::updateAll($update_data, ['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
  312. if (!$res) throw new Exception("修改用户{$user_id}业绩失败");
  313. }
  314. }