CommissionListController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace app\admin\controller\finance;
  3. use app\admin\service\goods\GoodsSkuService;
  4. use app\admin\service\member\MemberService;
  5. use app\admin\service\order\PayDetailService;
  6. use app\controller\Curd;
  7. use app\model\Member;
  8. use app\model\MemberAccountList;
  9. use app\model\Order;
  10. use app\model\OrderSheet;
  11. use app\model\PayDetail;
  12. use support\Request;
  13. use support\Response;
  14. class CommissionListController extends Curd
  15. {
  16. public function __construct()
  17. {
  18. $this->model = new MemberAccountList();
  19. }
  20. public function select(Request $request): Response
  21. {
  22. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  23. $order = $request->get('order', 'desc');
  24. $field = $field ?? 'member_account_list_addtimes';
  25. $query = $this->doSelect($where, $field, $order);
  26. return $this->doFormat($query, $format, $limit);
  27. }
  28. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  29. {
  30. $model = $this->model->with([
  31. 'member' => function ($query) {
  32. $query->select('member_id', 'member_mobile', 'member_is_owner', 'member_is_vip', 'member_is_partner', 'member_is_referrer');
  33. },
  34. 'memberInfo' => function ($query) {
  35. $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
  36. },
  37. 'memberCert' => function ($query) {
  38. $query->select('join_cert_member_id', 'member_cert_name');
  39. }
  40. ]);
  41. foreach ($where as $column => $value) {
  42. if (is_array($value)) {
  43. if ($value[0] === 'like' || $value[0] === 'not like') {
  44. $model = $model->where($column, $value[0], "%$value[1]%");
  45. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  46. $model = $model->where($column, $value[0], $value[1]);
  47. } elseif ($value[0] == 'in' && !empty($value[1])) {
  48. $valArr = $value[1];
  49. if (is_string($value[1])) {
  50. $valArr = explode(",", trim($value[1]));
  51. }
  52. $model = $model->whereIn($column, $valArr);
  53. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  54. $valArr = $value[1];
  55. if (is_string($value[1])) {
  56. $valArr = explode(",", trim($value[1]));
  57. }
  58. $model = $model->whereNotIn($column, $valArr);
  59. } elseif ($value[0] == 'null') {
  60. $model = $model->whereNull($column);
  61. } elseif ($value[0] == 'not null') {
  62. $model = $model->whereNotNull($column);
  63. } elseif ($value[0] !== '' || $value[1] !== '') {
  64. $model = $model->whereBetween($column, $value);
  65. }
  66. } else {
  67. $model = $model->where($column, $value);
  68. }
  69. }
  70. if ($field) {
  71. $model = $model->orderBy($field, $order);
  72. }
  73. return $model;
  74. }
  75. public function afterQuery($items)
  76. {
  77. foreach ($items as &$item) {
  78. $item['member_mobile'] = isset($item['member']) ? $item['member']['member_mobile'] : '';
  79. $item['member_info_nickname'] = isset($item['memberInfo']) && !empty($item['memberInfo']['member_info_nickname']) ? $item['memberInfo']['member_info_nickname'] : '';
  80. $item['member_cert_name'] = isset($item['memberCert']) && !empty($item['memberCert']['member_cert_name']) ? $item['memberCert']['member_cert_name'] : '';
  81. $item['member_name'] = MemberService::getMemberName($item['member_mobile'], $item['member_cert_name'], $item['member_info_nickname']);
  82. $item['member_info_headimg'] = isset($item['memberInfo']) && !empty($item['memberInfo']['member_info_headimg']) ? $item['memberInfo']['member_info_headimg'] : "";
  83. $item['member_is_owner'] = isset($item['member']) ? $item['member']['member_is_owner'] : '';
  84. $item['member_is_vip'] = isset($item['member']) ? $item['member']['member_is_vip'] : '';
  85. $item['member_is_partner'] = isset($item['member']) ? $item['member']['member_is_partner'] : '';
  86. $item['member_is_referrer'] = isset($item['member']) ? $item['member']['member_is_referrer'] : '';
  87. unset($item['member'], $item['memberInfo'], $item['memberCert']);
  88. if (!empty($item['member_account_list_json'])) {
  89. $memberAccountListJson = json_decode($item['member_account_list_json'], true);
  90. $member = Member::with([
  91. 'cert' => function ($query) {
  92. $query->select('join_cert_member_id', 'member_cert_name');
  93. }
  94. ])->where('member_id', $memberAccountListJson['master_member_id'])
  95. ->first();
  96. $mobile = !empty($member->member_mobile) ? $member->member_mobile : '';
  97. $certName = !empty($member->cert) ? $member->cert->member_cert_name : '';
  98. $item['master_name'] = MemberService::getMemberCertName($mobile, $certName, '');
  99. }
  100. }
  101. return $items;
  102. }
  103. /**
  104. * @Desc 佣金管理顶部统计
  105. * @Author Gorden
  106. * @Date 2024/10/31 15:33
  107. *
  108. * @param Request $request
  109. * @return Response
  110. */
  111. public function statistics(Request $request)
  112. {
  113. $todayTimeUnixStart = strtotime(date('Y-m-d'));
  114. $todayTimeUnixEnd = strtotime(date('Y-m-d 23:59:59'));
  115. $yesterdayStart = strtotime(date('Y-m-d', strtotime("-1 days")));
  116. $yesterdayEnd = strtotime(date('Y-m-d 23:59:59', strtotime("-1 days")));
  117. $monthStart = strtotime(date('Y-m-01'));
  118. $monthEnd = strtotime(date('Y-m-t 23:59:59'));
  119. $commissionModel = MemberAccountList::where('member_account_list_attr', 'IN')
  120. ->where('member_account_list_status','ACTIVED')
  121. ->selectRaw("SUM(member_account_list_amount) as amount, COUNT(member_account_list_id) as total");
  122. $totalModel = clone $commissionModel;
  123. $total = $totalModel->first()->toArray();
  124. $todayTotalModel = clone $commissionModel;
  125. $todayTotal = $todayTotalModel->whereBetween('member_account_list_addtimes', [$todayTimeUnixStart, $todayTimeUnixEnd])
  126. ->first()
  127. ->toArray();
  128. $yesterdayTotalModel = clone $commissionModel;
  129. $yesterdayTotal = $yesterdayTotalModel->whereBetween('member_account_list_addtimes', [$yesterdayStart, $yesterdayEnd])
  130. ->first()
  131. ->toArray();
  132. $monthTotalModel = clone $commissionModel;
  133. $monthTotal = $monthTotalModel->whereBetween('member_account_list_addtimes', [$monthStart, $monthEnd])
  134. ->first()
  135. ->toArray();
  136. return json_success('success', [
  137. 'total' => $total,
  138. 'todayTotal' => $todayTotal,
  139. 'yesterdayTotal' => $yesterdayTotal,
  140. 'monthTotal' => $monthTotal
  141. ]);
  142. }
  143. /**
  144. * @Desc 记录详情
  145. * @Author Gorden
  146. * @Date 2024/9/28 17:15
  147. *
  148. * @param Request $request
  149. * @return Response
  150. */
  151. public function info(Request $request): Response
  152. {
  153. $id = $request->get('member_account_list_id');
  154. if (!$id) {
  155. return json_fail("参数异常");
  156. }
  157. $info = MemberAccountList::where('member_account_list_id', $id)->first();
  158. $infoMember = Member::with([
  159. 'role' => function ($query) {
  160. $query->select('member_role_id', 'member_role_name');
  161. }
  162. ])->where('member_id', $info->join_member_account_list_member_id)
  163. ->first();
  164. $info->member_role_name = !empty($infoMember->role) ? $infoMember->role->member_role_name : '';
  165. if (!empty($info->member_account_list_json)) {
  166. $accountListJson = json_decode($info->member_account_list_json, true);
  167. // 下单人
  168. if (isset($accountListJson['master_member_id'])) {
  169. $member = Member::with([
  170. 'info' => function ($query) {
  171. $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
  172. },
  173. 'cert' => function ($query) {
  174. $query->select('join_cert_member_id', 'member_cert_name');
  175. },
  176. 'role' => function ($query) {
  177. $query->select('member_role_id', 'member_role_name');
  178. }
  179. ])->where('member_id', $accountListJson['master_member_id'])
  180. ->first();
  181. $info->master_nickname = isset($member->info) && !empty($member->info->member_info_nickname) ? $member->info->member_info_nickname : MemberService::getMemberNickname($member->member_mobile ?? '');
  182. $info->master_headimg = MemberService::getAvatarUrl(!empty($member->info) ? $member->info->member_info_headimg : '');
  183. $info->master_certname = isset($member->cert) && !empty($member->cert->member_cert_name) ? $member->cert->member_cert_name : '';
  184. $info->master_mobile = isset($member) && !empty($member->member_mobile) ? $member->member_mobile : '';
  185. $info->master_member_name = MemberService::getMemberName($info->master_mobile, $info->master_certname, $info->master_nickname);
  186. $info->master_role_name = !empty($member->role) ? $member->role->member_role_name : '';
  187. $info->join_member_role_id = $member->join_member_role_id ?? '';
  188. $info->member_is_owner = $member->member_is_owner ?? '';
  189. $info->member_is_vip = $member->member_is_vip ?? '';
  190. $info->member_is_partner = $member->member_is_partner ?? '';
  191. $info->member_is_referrer = $member->member_is_referrer ?? '';
  192. $info->member_is_franchisee = $member->member_is_franchisee ?? '';
  193. }
  194. // 订单
  195. if (isset($accountListJson['order_id'])) {
  196. $info->order = Order::where('order_id', $accountListJson['order_id'])
  197. ->select('order_id', 'order_groupby', 'order_amount_total', 'order_amount_pay', 'order_addtimes')
  198. ->first();
  199. $sheets = OrderSheet::with([
  200. 'goods' => function ($query) {
  201. $query->select('goods_id', 'goods_name', 'goods_sales_price', 'goods_cover');
  202. },
  203. 'sku' => function ($query) {
  204. $query->select('goods_sku_id', 'goods_sku_specs_json');
  205. }
  206. ])->where('join_sheet_order_id', $accountListJson['order_id'])
  207. ->select('join_sheet_order_id', 'join_sheet_goods_id', 'join_sheet_goods_sku_id', 'order_sheet_num', 'order_sheet_price', 'order_sheet_amount', 'order_sheet_addtimes')
  208. ->get()
  209. ->toArray();
  210. foreach ($sheets as &$sheet) {
  211. if (!empty($sheet['goods']) && !empty($sheet['goods']['goods_cover'])) {
  212. $sheet['goods']['goods_cover'] = getenv('STORAGE_DOMAIN') . $sheet['goods']['goods_cover'];
  213. }
  214. if (!empty($sheet['sku'])) {
  215. $sheet['sku']['sku_title'] = GoodsSkuService::getSkuTitle($sheet['sku']['goods_sku_specs_json']);
  216. }
  217. }
  218. $info->sheet = $sheets;
  219. // 支付记录
  220. $payDetails = PayDetail::whereJsonContains('join_pay_object_json->order_id', $accountListJson['order_id'])
  221. ->select('pay_amount', 'pay_status', 'pay_paytimes', 'pay_prepayid')
  222. ->where('pay_status', 'SUCCESS')
  223. ->orderBy('pay_addtimes', 'DESC')
  224. ->get()
  225. ->toArray();
  226. foreach ($payDetails as &$detail) {
  227. $detail['pay_category'] = PayDetailService::getPayWayByPrepayId($detail['pay_prepayid']);
  228. }
  229. $info->payDetails = $payDetails;
  230. }
  231. }
  232. return json_success('success', $info);
  233. }
  234. }