CommissionListController.php 12 KB

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