MemberAccountService.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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\api\member;
  12. use app\dict\member\MemberAccountTypeDict;
  13. use app\model\member\Member;
  14. use app\model\member\MemberAccountLog;
  15. use core\base\BaseApiService;
  16. use think\db\exception\DbException;
  17. /**
  18. * 会员账户流水服务层(会员个人账户通过会员服务层查询)
  19. * Class MemberAccountService
  20. * @package app\service\admin\member
  21. */
  22. class MemberAccountService extends BaseApiService
  23. {
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. $this->model = new MemberAccountLog();
  28. }
  29. /**
  30. * 会员账户流水列表
  31. * @param array $where
  32. * @return array
  33. */
  34. public function getPage(array $where = [])
  35. {
  36. $where['member_id'] = $this->member_id;
  37. $field = 'id, member_id, account_type, account_data, from_type, related_id, create_time, memo';
  38. $search_model = $this->model->withSearch(['member_id','account_type', 'from_type', 'create_time','account_data_gt', 'account_data_lt','keyword'],$where)->field($field)->order('create_time desc')->append(['from_type_name', 'account_type_name']);
  39. return $this->pageQuery($search_model);
  40. }
  41. /**
  42. * 会员积分流水列表
  43. * @param array $where
  44. * @return array
  45. */
  46. public function getPointPage(array $where = [])
  47. {
  48. $type_where = [];
  49. switch ($where['amount_type']){
  50. case 'income':
  51. $type_where = [
  52. ['account_data', '>', 0 ],
  53. ];
  54. break;
  55. case 'disburse':
  56. $type_where = [
  57. ['account_data', '<', 0 ],
  58. ];
  59. break;
  60. default:
  61. break;
  62. }
  63. $where['member_id'] = $this->member_id;
  64. $field = 'id, member_id, account_type, account_data, from_type, related_id, create_time, memo';
  65. $search_model = $this->model->where($type_where)->withSearch(['member_id','account_type', 'from_type', 'create_time','account_data_gt', 'account_data_lt'],$where)->field($field)->order('create_time desc')->append(['from_type_name', 'account_type_name']);
  66. $list = $this->pageQuery($search_model);
  67. $list['data'] = $this->monthlyGrouping($list['data']);
  68. return $list;
  69. }
  70. /**
  71. * 会员余额流水列表
  72. * @param array $where
  73. * @return array
  74. */
  75. public function getBalancePage(array $data = [])
  76. {
  77. switch ($data['trade_type']){
  78. case 'income':
  79. $type_where = [
  80. ['account_type', 'in', [MemberAccountTypeDict::BALANCE,MemberAccountTypeDict::MONEY]],
  81. ['account_data', '>', 0 ],
  82. ['from_type', '<>', 'cash_out']
  83. ];
  84. break;
  85. case 'disburse':
  86. $type_where = [
  87. ['account_type', 'in', [MemberAccountTypeDict::BALANCE,MemberAccountTypeDict::MONEY]],
  88. ['account_data', '<', 0 ],
  89. ['from_type', '<>', 'cash_out']
  90. ];
  91. break;
  92. case 'cash_out':
  93. $type_where = [
  94. ['account_type', '=', MemberAccountTypeDict::MONEY],
  95. ['from_type', '=', 'cash_out']
  96. ];
  97. break;
  98. default:
  99. $type_where = [
  100. ['account_type', 'in', [MemberAccountTypeDict::BALANCE,MemberAccountTypeDict::MONEY]],
  101. ];
  102. break;
  103. }
  104. $where['member_id'] = $this->member_id;
  105. $where['create_time'] = $data['create_time'];
  106. $field = 'id, member_id, account_type, account_data, account_sum, from_type, related_id, create_time, memo';
  107. $search_model = $this->model->where($type_where)->withSearch(['member_id', 'create_time'],$where)->field($field)->order('create_time desc')->append(['from_type_name', 'account_type_name']);
  108. return $this->pageQuery($search_model);
  109. }
  110. /**
  111. * 账户流水详情
  112. * @param int $id
  113. * @return array
  114. */
  115. public function getInfo(int $id)
  116. {
  117. $field = 'id, member_id, account_type, account_data, from_type, related_id, create_time, memo';
  118. return $this->model->where([['id', '=', $id], ['member_id', '=', $this->member_id]])->field($field)->append(['from_type_name', 'account_type_name'])->findOrEmpty()->toArray();
  119. }
  120. /**
  121. * 会员账户统计数量
  122. * @param array $where
  123. * @return int
  124. * @throws DbException
  125. */
  126. public function getCount(array $where = []){
  127. $where['member_id'] = $this->member_id;
  128. return $this->model->withSearch(['member_id','account_type', 'from_type', 'create_time'],$where)->count();
  129. }
  130. /**
  131. * 会员积分统计数量
  132. * @param array $where
  133. * @return int
  134. * @throws DbException
  135. */
  136. public function getPointCount(){
  137. $data = [
  138. 'point' => 0,
  139. 'point_get' => 0,
  140. 'use'=> 0,//使用
  141. ];
  142. $info = (new Member())->where([['member_id', '=', $this->member_id]])->field('point,point_get')->findOrEmpty()->toArray();
  143. $data['point'] = $info['point'] ?? 0;
  144. $data['point_get'] = $info['point_get'] ?? 0;
  145. $data['use'] = abs($this->model->where([
  146. ['member_id', '=', $this->member_id],
  147. ['account_type', '=', MemberAccountTypeDict::POINT],
  148. ['account_data', '<', 0]
  149. ])->sum('account_data'));
  150. return $data;
  151. }
  152. /**
  153. * 按年月分组数据(例如账单)
  154. * @param array $arr_data 分组的数组
  155. * @param string $time_field 时间分组字段
  156. * @return array
  157. */
  158. function monthlyGrouping($arr_data, $time_field = 'create_time')
  159. {
  160. if (empty($time_field)) {
  161. return $arr_data;
  162. }
  163. //按月份分组
  164. $arr_month = [];
  165. //全部年月数据
  166. $arr_return_data = [];
  167. foreach ($arr_data as $data) {
  168. //按月份分组
  169. $year_month = mb_substr($data[$time_field], 0, 7);
  170. $arr_month[$year_month]['month_data'][] = $data;
  171. if (!isset($arr_month[$year_month]['month_info']))
  172. {
  173. $arr_month[$year_month]['month_info'] =
  174. [
  175. 'year' => mb_substr($year_month, 0, 4),
  176. 'month' => mb_substr($year_month, 5, 2),
  177. ];
  178. }
  179. }
  180. foreach ($arr_month as $month) {
  181. $arr_return_data[] = $month ?? [];
  182. }
  183. return $arr_return_data;
  184. }
  185. }