EarningController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /****************************************************************
  3. * Author: libaojia
  4. * Date: 2018/1/9 下午1:44
  5. * Email: livsyitian@163.com
  6. * QQ: 995265288
  7. * User: 芸众商城 www.yunzshop.com
  8. ****************************************************************/
  9. namespace app\frontend\modules\finance\controllers;
  10. use app\common\components\ApiController;
  11. use app\common\exceptions\AppException;
  12. use app\common\models\Income;
  13. /**
  14. * 收入接口重构 2018-01-09 YITIAN
  15. * Class EarningController
  16. * @package app\frontend\modules\finance\controllers
  17. */
  18. class EarningController extends ApiController
  19. {
  20. private $incomeModel;
  21. public function preAction()
  22. {
  23. parent::preAction();
  24. $this->incomeModel = Income::uniacid()->where('member_id', $this->getMemberId());
  25. }
  26. /**
  27. * 收入页面数据显示接口
  28. *
  29. * @return \Illuminate\Http\JsonResponse
  30. */
  31. public function earningCount()
  32. {
  33. $result = [
  34. 'total' => [
  35. 'grand_total' => $this->grandTotal(),
  36. 'can_withdraw' => $this->canWithdrawTotal()
  37. ],
  38. 'data' => $this->earningDetail(),
  39. ];
  40. return $this->successJson('ok', $result);
  41. }
  42. //累计收入
  43. private function grandTotal()
  44. {
  45. return $this->incomeModel->sum('amount');
  46. }
  47. //可提现收入
  48. private function canWithdrawTotal()
  49. {
  50. return $this->incomeModel->where('status', 0)->sum('amount');
  51. }
  52. //收入明细
  53. private function earningDetail()
  54. {
  55. $config = \app\common\modules\shop\ShopConfig::current()->get('plugin');
  56. $array = [];
  57. foreach ($config as $key => $item) {
  58. //$typeModel = $this->incomeModel->where('incometable_type', $item['class']);
  59. $typeModel = Income::uniacid()->where('member_id', $this->getMemberId())->whereStatus(0)->where('incometable_type', $item['class']);
  60. $array[] = [
  61. 'title' => $item['title'],
  62. 'ico' => $item['ico'],
  63. 'type' => $item['type'],
  64. 'type_name' => $item['title'],
  65. 'income' => $typeModel->sum('amount'),
  66. 'can' => $this->itemIsShow($item)
  67. ];
  68. }
  69. return $array;
  70. }
  71. private function itemIsShow($item)
  72. {
  73. $result = true;
  74. if ($item['agent_class']) {
  75. $agentModel = $item['agent_class']::{$item['agent_name']}($this->getMemberId());
  76. if ($item['agent_status']) {
  77. $agentModel = $agentModel->where('status', 1);
  78. }
  79. //推广中心显示
  80. if (!$agentModel) {
  81. $result = false;
  82. } else {
  83. $agent = $agentModel->first();
  84. if ($agent) {
  85. $result = true;
  86. } else {
  87. $result = false;
  88. }
  89. }
  90. }
  91. return $result;
  92. }
  93. //
  94. private function getMemberId()
  95. {
  96. $member_id = \YunShop::app()->getMemberId();
  97. if (!$member_id) {
  98. throw new AppException('未获取到会员数据');
  99. }
  100. return $member_id;
  101. }
  102. }