MemberAccountService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\member;
  12. use app\dict\member\MemberAccountChangeTypeDict;
  13. use app\dict\member\MemberAccountTypeDict;
  14. use app\model\member\Member;
  15. use app\model\member\MemberAccountLog;
  16. use app\service\core\member\CoreMemberAccountService;
  17. use core\base\BaseAdminService;
  18. use core\exception\AdminException;
  19. /**
  20. * 会员账户流水服务层(会员个人账户通过会员服务层查询)
  21. * Class MemberAccountService
  22. * @package app\service\admin\member
  23. */
  24. class MemberAccountService extends BaseAdminService
  25. {
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. $this->model = new MemberAccountLog();
  30. }
  31. /**
  32. * 会员账户流水列表
  33. * @param array $where
  34. * @return array
  35. */
  36. public function getPage(array $where = [])
  37. {
  38. $field = 'member_account_log.id, member_account_log.member_id, member_account_log.site_id, member_account_log.account_type, member_account_log.account_data,member_account_log.account_sum, member_account_log.from_type, member_account_log.related_id, member_account_log.create_time, member_account_log.memo';
  39. $member_where = [];
  40. if (!empty($where[ 'keywords' ])) {
  41. $member_where[] = [ "member.member_no|member.nickname|member.mobile", 'like', '%' . $where[ 'keywords' ] . '%' ];
  42. }
  43. $search_model = $this->model->where([ [ 'member_account_log.site_id', '=', $this->site_id ] ])->withSearch([ 'join_member_id' => 'member_id', 'account_type', 'from_type', 'join_create_time' => 'create_time' ], $where)
  44. ->withJoin(
  45. [ 'member' => ['member_id','member_no', 'username', 'mobile', 'nickname', 'headimg']
  46. ])->where($member_where)->field($field)->order('create_time desc')->append([ 'from_type_name', 'account_type_name' ]);
  47. return $this->pageQuery($search_model);
  48. }
  49. /**
  50. * 账户流水详情
  51. * @param int $id
  52. * @return array
  53. */
  54. public function getInfo(int $id)
  55. {
  56. $field = 'id, member_id, site_id, account_type, account_data, from_type, related_id, create_time, memo';
  57. return $this->model->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->with('memberInfo')->field($field)->append([ 'from_type_name', 'account_type_name' ])->findOrEmpty()->toArray();
  58. }
  59. /**
  60. * 添加调整积分数据
  61. * @param array $data
  62. * @return mixed
  63. */
  64. public function adjustPoint(array $data)
  65. {
  66. return ( new CoreMemberAccountService() )->addLog($data[ 'member_id' ], 'point', $data[ 'account_data' ], 'adjust', $data[ 'memo' ]);
  67. }
  68. /**
  69. * 添加调整余额账户
  70. * @param array $data
  71. * @return bool
  72. */
  73. public function adjustBalance(array $data)
  74. {
  75. return ( new CoreMemberAccountService() )->addLog($data[ 'member_id' ], 'balance', $data[ 'account_data' ], 'adjust', $data[ 'memo' ]);
  76. }
  77. public function adjustMoney(array $data)
  78. {
  79. return ( new CoreMemberAccountService() )->addLog($data[ 'member_id' ], MemberAccountTypeDict::MONEY, $data[ 'account_data' ], 'adjust', $data[ 'memo' ]);
  80. }
  81. /**
  82. * 获取账户类型的变动方式
  83. * @param $account_type
  84. * @return array|mixed|string
  85. */
  86. public function getFromType($account_type)
  87. {
  88. if (!array_key_exists($account_type, MemberAccountTypeDict::getType())) throw new AdminException('MEMBER_TYPE_NOT_EXIST');
  89. return MemberAccountChangeTypeDict::getType($account_type);
  90. }
  91. /**
  92. * 获取账户数据和
  93. * @param string $account_type (注意查询对应账户)
  94. */
  95. public function getSumAccount(string $account_type)
  96. {
  97. return $this->model->where([ [ 'site_id', '=', $this->site_id ], [ 'account_type', '=', $account_type ] ])->sum('account_data');
  98. }
  99. /**
  100. * 会员账户详情
  101. * @param int $member_id
  102. * @return array
  103. */
  104. public function getMemberAccountInfo(int $member_id)
  105. {
  106. $field = 'point, point_get, balance, balance_get, growth, growth_get, money, money_get, commission, commission_get, commission_cash_outing';
  107. return ( new Member() )->where([ [ 'member_id', '=', $member_id ], [ 'site_id', '=', $this->site_id ] ])->field($field)->findOrEmpty()->toArray();
  108. }
  109. /**
  110. * 已提现佣金
  111. * @return float
  112. */
  113. public function getWithdrawnCommission(int $member_id = 0)
  114. {
  115. $condition = [
  116. [ 'site_id', '=', $this->site_id ],
  117. [ 'account_type', '=', MemberAccountTypeDict::COMMISSION ],
  118. [ 'from_type', '=', 'cash_out' ]
  119. ];
  120. if (!empty($member_id)) $condition[] = [ 'member_id', '=', $member_id ];
  121. return $this->model->where($condition)->sum('account_data');
  122. }
  123. /**
  124. * 账户支出总额
  125. * @return float
  126. */
  127. public function getExpensesSumAccount(string $account_type, int $member_id = 0)
  128. {
  129. $condition = [
  130. [ 'site_id', '=', $this->site_id ],
  131. [ 'account_type', '=', $account_type ],
  132. [ 'account_data', '<', '0' ]
  133. ];
  134. if (!empty($member_id)) $condition[] = [ 'member_id', '=', $member_id ];
  135. return $this->model->where($condition)->sum('account_data');
  136. }
  137. }