DefaultController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace addons\BossWeight\api\modules\v1\controllers;
  3. use addons\BossWeight\common\logic\CommissionLogLogic;
  4. use addons\BossWeight\common\models\AddonsBoss;
  5. use addons\BossWeight\common\models\AddonsBossCommissionLog;
  6. use addons\BossWeight\common\models\AddonsBossLevel;
  7. use addons\BossWeight\common\models\AddonsBossLevelSetting;
  8. use common\enums\OrderStatusEnum;
  9. use common\enums\StatusEnum;
  10. use common\models\order\Order;
  11. use Yii;
  12. use api\controllers\OnAuthController;
  13. use addons\BossWeight\common\enums\BossEnum;
  14. use addons\BossWeight\common\enums\LevelEnum;
  15. /**
  16. * 默认控制器
  17. *
  18. * Class DefaultController
  19. * @package addons\BossWeight\api\modules\v1\controllers
  20. */
  21. class DefaultController extends OnAuthController
  22. {
  23. public $modelClass = '';
  24. public $enableCsrfValidation = false;
  25. /**
  26. * 不用进行登录验证的方法
  27. *
  28. * 例如: ['index', 'update', 'create', 'view', 'delete']
  29. * 默认全部需要验证
  30. *
  31. * @var array
  32. */
  33. // protected $authOptional = ['index'];
  34. /**
  35. * 股东分红首页
  36. *
  37. * @return string
  38. */
  39. public function actionIndex()
  40. {
  41. if (Yii::$app->request->isGet) {
  42. $get = Yii::$app->request->get();
  43. // 检查提交的参数
  44. $res = Yii::$app->services->validate->validate($get,[
  45. [['page','limit'], 'integer']
  46. ]);
  47. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  48. $data['user_id'] = $this->user_id;
  49. $data['nickname'] = $this->user->nickname ?? '';
  50. $data['avatar_url'] = $this->user->avatar_url ?? '';
  51. // 获取股东信息
  52. $boss = AddonsBoss::getOne([['mall_id' => $this->mall_id,'user_id' => $this->user_id,'status'=>1]]);
  53. if(!$boss){
  54. return $this->error('您还不是股东,无法查看股东信息');
  55. }
  56. // 数据初始化
  57. $amounts = 0;
  58. $commission = 0;
  59. // 获取配置
  60. $config = \Yii::$app->services->setting->addons->get($this->mall_id, BossEnum::ADDONS_NAME, 'basic');
  61. $data['total_turnover_name'] = $config['total_turnover_name'] ?? '平台营业额';
  62. $data['amounts_name'] = $config['amounts_name'] ?? '上期股东收益';
  63. $data['user_weight_name'] = $config['user_weight_name'] ?? '持有权重数量';
  64. $data['total_weight_name'] = $config['total_weight_name'] ?? '平台总权重数量';
  65. $data['is_customize'] = $config['is_customize'] ?? 0;
  66. // 获取收益列表
  67. $where = [];
  68. $where[] = ['=', 'mall_id', $this->mall_id];
  69. $where[] = ['=', 'user_id', $this->user_id];
  70. $where[] = ['=', 'status', StatusEnum::ENABLED];
  71. $order = 'created_at desc,end_time desc';
  72. $select = 'amounts,commission,type,compute_period,start_time,end_time';
  73. $params = array('where' => $where, 'order' => $order, 'select' => $select , 'limit' => $get['limit']);
  74. $list = AddonsBossCommissionLog::listPage($params, false, true);
  75. $commonission_type = LevelEnum::getCommissionType();// 分红类型
  76. foreach ($list['list'] as $key => &$item) {
  77. // if ($get['page'] == 1) {
  78. // // 循环获取本期收益
  79. // if (in_array($key, [1])) {
  80. // if ($item['type'] == 1) {
  81. //// $amounts = $item['amounts'];
  82. // $commission += $item['commission'];
  83. // }
  84. // if ($item['type'] == 2) $commission += $item['commission'];
  85. // if ($item['type'] == 4) $commission += $item['commission'];
  86. // }
  87. // }
  88. if($get['page'] == 1){
  89. if($item['type'] == 4 && $item['start_time'] == date("Ymd",strtotime('-1 day'))){
  90. $commission += $item['commission'];
  91. }
  92. }
  93. $item['start_time'] = date_format(date_create($item['start_time']),"Y-m-d");
  94. $item['end_time'] = date_format(date_create($item['end_time']),"Y-m-d");
  95. $item['type_desc'] = $commonission_type[$item['type']] ?? '';
  96. if($item['type'] == 4 && $data['is_customize']){
  97. $item['type_desc'] = '权重鼎分红';
  98. }
  99. }
  100. if(isset($config['compute_show_type']) && $config['compute_show_type'] == 2){
  101. $computeTimeArr = [strtotime(date('Y-m-d 00:00:00')),strtotime(date('Y-m-d 00:00:00'))];
  102. }else{
  103. //获取结算时间
  104. $computeTimeArr = CommissionLogLogic::getComputeTime($this->mall_id);
  105. }
  106. $totalTurnover = Order::find()->where([
  107. 'pay_status' => [ OrderStatusEnum::PAY],
  108. 'mall_id' => $this->mall_id,
  109. 'status' => 1,
  110. ])->andWhere([
  111. 'and',
  112. ['>=','pay_time',$computeTimeArr[0]],
  113. ['<','pay_time',$computeTimeArr[1] + 86400],
  114. ])->sum('pay_money');
  115. $totalTurnover = $totalTurnover?:0;
  116. //昨日营业额
  117. $startDate = date('Y-m-d',strtotime("-1 day"));
  118. $yesterdayTurnover = Order::find()->where([
  119. 'pay_status' => [ OrderStatusEnum::PAY],
  120. 'mall_id' => $this->mall_id,
  121. 'status' => 1,
  122. ])->andWhere([
  123. 'and',
  124. ['>=','pay_time',strtotime($startDate)],
  125. ['<','pay_time',strtotime(date("Y-m-d"))],
  126. ])->sum('pay_money');
  127. $yesterdayTurnover = $yesterdayTurnover?:0;
  128. $data['is_boss'] = $boss ? 1 : 0;// 是否是股东,0=否,1=是
  129. // $data['amounts'] = sprintf("%.2f",substr(sprintf("%.3f", $amounts), 0, -2));// 分红池
  130. $data['commission'] = sprintf("%.2f",substr(sprintf("%.3f", $commission), 0, -2));// 本期收益
  131. $total_weight = CommissionLogLogic::getTotalWeight($this->mall_id);//平台权重
  132. if(!empty($boss)){
  133. $level = $boss->level;
  134. $levelData = AddonsBossLevelSetting::find()->where(["level" => $level,"status" => AddonsBossLevelSetting::STATUS_ENABLE,'mall_id'=>$this->mall_id])->one();
  135. if(!empty($levelData)){
  136. $totalTurnover = round($totalTurnover * $levelData->weight_turnover_ratio / 100,2);
  137. $yesterdayTurnover = round($yesterdayTurnover * $levelData->weight_turnover_ratio / 100,2);
  138. }
  139. }else{
  140. $totalTurnover = 0;
  141. $yesterdayTurnover = 0;
  142. $total_weight = 0;
  143. }
  144. //加权分红
  145. $data['total_turnover'] = $totalTurnover;//平台营业额
  146. $data['amounts'] = $yesterdayTurnover;// 分红池,昨日营业额
  147. $data['total_weight'] = $total_weight;//平台权重
  148. $bossArr = AddonsBoss::find()->where(['mall_id' => $this->mall_id,'user_id' => $this->user_id,'status'=>1])->asArray()->one();
  149. $data['user_weight'] = 0;
  150. if($bossArr){
  151. $data['user_weight'] = CommissionLogLogic::getBossWeight($bossArr);//用户权重
  152. }
  153. $data['able_withdrawal_commission'] = $boss->commission;// 可提现收益
  154. $data['list'] = $list;
  155. $this->success('获取成功', $data);
  156. }
  157. }
  158. public function actionRights()
  159. {
  160. if (Yii::$app->request->isGet) {
  161. // 获取股东信息
  162. $boss = AddonsBoss::getOne([['mall_id' => $this->mall_id,'user_id' => $this->user_id]]);
  163. // 获取股东等级
  164. $list = AddonsBossLevel::getBossLevel([['mall_id' => $this->mall_id]],[],true,'level,name,explain','level asc');
  165. foreach ($list as $level) {
  166. if ($boss->level == $level['level']) {
  167. $level_name = $level['name'];
  168. break;
  169. }
  170. }
  171. $data['user_id'] = $this->user_id;
  172. $data['nickname'] = $this->user->nickname ?? '';
  173. $data['avatar_url'] = $this->user->avatar_url ?? '';
  174. $data['level_name'] = $level_name ?? '';
  175. $data['list'] = $list;
  176. $this->success('获取成功', $data);
  177. }
  178. }
  179. }