1, 'data' => [ 'total' => 0, 'rows' => [], ], ]; try { $channel_ids = AdminService::instance()->getVipAdminBindModel()->getChannelIds($admin_id); if (!$channel_ids) { return $default; } $fields = [ 'LEFT(createdate,6)' => 'create_month', 'IFNULL(SUM(o.recharge_money),0)' => 'recharge_money', 'IFNULL(SUM(o.normal_recharge_money),0)' => 'normal_recharge_money', 'IFNULL(SUM(o.vip_recharge_orders_count),0)' => 'vip_recharge_orders_count', 'IFNULL(SUM(o.normal_recharge_orders),0)' => 'normal_recharge_orders', 'IFNULL(SUM(o.vip_recharge_money),0)' => 'vip_recharge_money', 'IFNULL(SUM(o.vip_recharge_orders),0)' => 'vip_recharge_orders', 'IFNULL(SUM(o.recharge_money_benefit),0)' => 'recharge_money_benefit', 'o.type', 'o.flag', ]; $total = $this->getRechargeOptions($date_range, $fields, false) ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL) ->where('o.business_line', $business_line) ->whereIn('o.admin_id', $channel_ids) ->group('o.type, o.flag, create_month') ->count(); $all_list = $this->getRechargeOptions($date_range, $fields, false) ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL) ->where('o.business_line', $business_line) ->whereIn('o.admin_id', $channel_ids) ->field($fields) ->group('o.type, o.flag, create_month') ->limit($offset, $limit) ->select(); $current_page_createdate = []; foreach ($all_list as $channel_order) { $current_page_createdate[] = $channel_order['create_month']; } $agent_list = $this->getRechargeOptions($date_range, $fields, false) ->where('o.flag', Order::ORDER_COLLECT_FLAG_BRANCH) ->where('o.business_line', $business_line) ->whereIn('LEFT(createdate,6)', $current_page_createdate) ->whereIn('o.admin_id', $channel_ids) ->order(['create_month' => 'asc']) ->field($fields) ->group('o.type, o.flag, create_month') ->select(); $channel_list = $this->getRechargeOptions($date_range, $fields, false) ->where('o.flag', Order::ORDER_COLLECT_FLAG_SELF) ->where('o.business_line', $business_line) ->whereIn('LEFT(createdate,6)', $current_page_createdate) ->whereIn('o.admin_id', $channel_ids) ->order(['create_month' => 'asc']) ->field($fields) ->group('o.type, o.flag, create_month') ->select(); $channel_data = $this->processChannelResult($all_list, $channel_list, $agent_list); return [ 'code' => 0, 'data' => [ 'total' => $total, 'rows' => $channel_data, ], ]; } catch (\Exception $e) { Log::log($e->getMessage() . $e->getFile() . $e->getLine()); return $default; } } public function getDayReport($admin_id, $date_range, $offset, $limit, $business_line = PayConstants::BUSINESS_WECHAT) { $default = [ 'code' => 1, 'data' => [ 'total' => 0, 'rows' => [], ], ]; try { $channel_ids = AdminService::instance()->getVipAdminBindModel()->getChannelIds($admin_id); if (!$channel_ids) { return $default; } $fields = [ 'createdate', 'IFNULL(SUM(o.recharge_money),0)' => 'recharge_money', 'IFNULL(SUM(o.normal_recharge_money),0)' => 'normal_recharge_money', 'IFNULL(SUM(o.vip_recharge_orders_count),0)' => 'vip_recharge_orders_count', 'IFNULL(SUM(o.normal_recharge_orders),0)' => 'normal_recharge_orders', 'IFNULL(SUM(o.vip_recharge_money),0)' => 'vip_recharge_money', 'IFNULL(SUM(o.vip_recharge_orders),0)' => 'vip_recharge_orders', 'IFNULL(SUM(o.recharge_money_benefit),0)' => 'recharge_money_benefit', 'o.type', 'o.flag', ]; $total = $this->getRechargeOptions($date_range, $fields) ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL) ->where('o.business_line', $business_line) ->whereIn('o.admin_id', $channel_ids) ->group('o.type, o.flag, createdate') ->count(); $all_list = $this->getRechargeOptions($date_range, $fields) ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL) ->where('o.business_line', $business_line) ->whereIn('o.admin_id', $channel_ids) ->field($fields) ->group('o.type, o.flag, createdate') ->limit($offset, $limit) ->select(); $current_page_createdate = []; foreach ($all_list as $channel_order) { $current_page_createdate[] = $channel_order['createdate']; } $agent_list = $this->getRechargeOptions($date_range, $fields) ->where('o.flag', Order::ORDER_COLLECT_FLAG_BRANCH) ->where('o.business_line', $business_line) ->whereIn('o.createdate', $current_page_createdate) ->whereIn('o.admin_id', $channel_ids) ->order(['createdate' => 'asc']) ->field($fields) ->group('o.type, o.flag, createdate') ->select(); $channel_list = $this->getRechargeOptions($date_range, $fields) ->where('o.flag', Order::ORDER_COLLECT_FLAG_SELF) ->where('o.business_line', $business_line) ->whereIn('o.createdate', $current_page_createdate) ->whereIn('o.admin_id', $channel_ids) ->order(['createdate' => 'asc']) ->field($fields) ->group('o.type, o.flag, createdate') ->select(); $channel_data = $this->processChannelResult($all_list, $channel_list, $agent_list); return [ 'code' => 0, 'data' => [ 'total' => $total, 'rows' => $channel_data, ], ]; } catch (\Exception $e) { Log::log($e->getMessage() . $e->getFile() . $e->getLine()); return $default; } } /** * @param $admin_id * @param $date_range * @param $offset * @param $limit * @return array */ public function getMonthServiceReport($admin_id, $date_range, $offset, $limit, $filter=[], $business_line = PayConstants::BUSINESS_WECHAT) { $default = [ 'code' => 1, 'data' => [ 'total' => 0, 'rows' => [], ], ]; try { $channel_ids = AdminService::instance()->getVipAdminBindModel()->getChannelIds($admin_id); if (!$channel_ids) { return $default; } $fields = [ 'admin.username', 'admin.id', 'LEFT(createdate,6)' => 'create_month', 'IFNULL(SUM(o.recharge_money),0)' => 'recharge_money', 'IFNULL(SUM(o.normal_recharge_money),0)' => 'normal_recharge_money', 'IFNULL(SUM(o.vip_recharge_orders_count),0)' => 'vip_recharge_orders_count', 'IFNULL(SUM(o.normal_recharge_orders),0)' => 'normal_recharge_orders', 'IFNULL(SUM(o.vip_recharge_money),0)' => 'vip_recharge_money', 'IFNULL(SUM(o.vip_recharge_orders),0)' => 'vip_recharge_orders', 'IFNULL(SUM(o.recharge_money_benefit),0)' => 'recharge_money_benefit', 'replace(JSON_EXTRACT(json,"$.authorizer_info.nick_name"),\'"\',\'\')' => 'wx_nickname', 'o.type', 'o.flag', ]; $total = $this->getRechargeOptions($date_range, $fields, false, $filter) ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL) ->where('o.business_line', $business_line) ->whereIn('o.admin_id', $channel_ids) ->group('admin.id, o.type, o.flag, create_month, wx_nickname') ->count(); $all_list = $this->getRechargeOptions($date_range, $fields, false, $filter) ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL) ->where('o.business_line', $business_line) ->whereIn('o.admin_id', $channel_ids) ->order(['LEFT(createdate,6)' => 'asc']) ->field($fields) ->group('admin.id, o.type, o.flag, create_month, wx_nickname') ->limit($offset, $limit) ->select(); $current_page_channel_ids = $current_page_createdate = []; foreach ($all_list as $channel_order) { $current_page_channel_ids[] = $channel_order['id']; $current_page_createdate[] = $channel_order['create_month']; } $agent_list = $this->getRechargeOptions($date_range, $fields, false, $filter) ->where('o.flag', Order::ORDER_COLLECT_FLAG_BRANCH) ->where('o.business_line', $business_line) ->whereIn('o.admin_id', $current_page_channel_ids) ->whereIn('LEFT(createdate,6)', $current_page_createdate) ->order(['create_month' => 'asc']) ->field($fields) ->group('admin.id, o.type, create_month, wx_nickname') ->select(); $channel_list = $this->getRechargeOptions($date_range, $fields, false, $filter) ->where('o.flag', Order::ORDER_COLLECT_FLAG_SELF) ->where('o.business_line', $business_line) ->whereIn('o.admin_id', $current_page_channel_ids) ->whereIn('LEFT(createdate,6)', $current_page_createdate) ->order(['create_month' => 'asc']) ->field($fields) ->group('admin.id, o.type, create_month, wx_nickname') ->select(); $channel_data = $this->processChannelResult($all_list, $channel_list, $agent_list); return [ 'code' => 0, 'data' => [ 'total' => $total, 'rows' => $channel_data, ], ]; } catch (\Exception $e) { Log::log($e->getMessage()); return $default; } } /** * @param $admin_id * @param $date_range * @param $offset * @param $limit * @return array */ public function getDayServiceReport($admin_id, $date_range, $offset, $limit, $filter=[], $business_line = PayConstants::BUSINESS_WECHAT) { $default = [ 'code' => 1, 'data' => [ 'total' => 0, 'rows' => [], ], ]; try { $channel_ids = AdminService::instance()->getVipAdminBindModel()->getChannelIds($admin_id); if (!$channel_ids) { return $default; } $fields = [ 'admin.username', 'admin.id', 'o.createdate', 'replace(JSON_EXTRACT(json,"$.authorizer_info.nick_name"),\'"\',\'\')' => 'wx_nickname', 'o.recharge_money', 'o.normal_recharge_orders', 'o.vip_recharge_orders', 'o.normal_recharge_money', 'o.vip_recharge_money', 'o.recharge_money', 'o.recharge_money_benefit', 'o.flag', 'o.type', 'o.createdate', ]; $total = $this->getRechargeOptions($date_range, $fields, true, $filter ) ->whereIn('o.admin_id', $channel_ids) ->where('o.business_line', $business_line) ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL)->count(); $all_list = $this->getRechargeOptions($date_range, $fields, true, $filter) ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL) ->where('o.business_line', $business_line) ->whereIn('o.admin_id', $channel_ids) ->order(['o.createdate' => 'desc', 'recharge_money' => 'desc', 'o.admin_id' => 'asc']) ->limit($offset, $limit) ->select(); $current_page_channel_ids = $current_page_createdate = []; foreach ($all_list as $channel_order) { $current_page_channel_ids[] = $channel_order['id']; $current_page_createdate[] = $channel_order['createdate']; } $agent_list = $this->getRechargeOptions($date_range, $fields, true, $filter) ->where('o.flag', Order::ORDER_COLLECT_FLAG_BRANCH) ->where('o.business_line', $business_line) ->whereIn('o.admin_id', $current_page_channel_ids) ->whereIn('o.createdate', $current_page_createdate) ->select(); $channel_list = $this->getRechargeOptions($date_range, $fields,true, $filter) ->where('o.flag', Order::ORDER_COLLECT_FLAG_SELF) ->where('o.business_line', $business_line) ->whereIn('o.admin_id', $current_page_channel_ids) ->whereIn('o.createdate', $current_page_createdate) ->select(); $channel_data = $this->processChannelResult($all_list, $channel_list, $agent_list); return [ 'code' => 0, 'data' => [ 'total' => $total, 'rows' => $channel_data, ], ]; } catch (\Exception $e) { Log::alert($e->getMessage() . $e->getFile() . $e->getLine()); return $default; } } /** * @param $item * @return string */ public function processUniqueKey($item) { $item = $item->toArray(); $return_keys = []; $keys = ['createdate', 'create_month', 'id']; foreach ($keys as $key) { if (array_key_exists($key, $item)) { $return_keys[] = $item[$key]; } } return implode('-', $return_keys); } public function processChannelResult($all_list, $channel_list, $agent_list) { $result = []; if ($all_list) { $agent_data = []; //代理商 foreach ($agent_list as $item) { $key = $this->processUniqueKey($item); $agent_data[$key] = [ 'recharge_money' => $item['recharge_money'], 'recharge_money_benefit' => $item['recharge_money_benefit'], ]; } $channel_data = []; //渠道商 foreach ($channel_list as $item) { $key = $this->processUniqueKey($item); $channel_data[$key] = [ 'recharge_money' => $item['recharge_money'], 'recharge_money_benefit' => $item['recharge_money_benefit'], ]; } //统计结果 foreach ($all_list as $item) { $key = $this->processUniqueKey($item); //self_recharge->渠道商本身的充值金额 //self_recharge_benefit->渠道商的分成收益:渠道商本身的充值 * 渠道商分成比例1 if(array_key_exists($key, $channel_data)){ $item['self_recharge'] = $channel_data[$key]['recharge_money']; $item['self_recharge_benefit'] = $channel_data[$key]['recharge_money_benefit']; }else{ $item['self_recharge'] = 0; $item['self_recharge_benefit'] = 0; } $item['total_benefit'] = $item['recharge_money_benefit']; if (array_key_exists($key, $agent_data)) { $item['total_benefit'] = $item['recharge_money_benefit'] - $agent_data[$key]['recharge_money_benefit']; $item['agent_benefit'] = $agent_data[$key]['recharge_money_benefit']; $item['agent_recharge'] = $agent_data[$key]['recharge_money']; } else { $item['agent_benefit'] = 0; $item['agent_recharge'] = 0; } $result[] = $item; } } return $result; } /** * @param $date_range * @param $fields * @param $day * @return Query */ public function getRechargeOptions($date_range, $fields, $day = true, $filter=[], $business_line = PayConstants::BUSINESS_WECHAT) { $time = explode(',', $date_range); $where = []; if ( isset($filter['username']) && !empty($filter['username'])){ $where['admin.username'] = $filter['username']; } if (!$day) { $time[0] .= '00'; //获取前一天的日期格式 if ($time[1] == date('Ym')) { $time[1] = $time[1] . str_pad(date('d') - 1, 2, '0', STR_PAD_LEFT); }else{ $time[1] .= '31'; } } $query = $this->getOrderCollectModel() ->alias('o') ->where('o.business_line', $business_line) ->join('admin', "o.admin_id=admin.id", "LEFT") ->join('admin_config', "admin.id=admin_config.admin_id", "LEFT") ->where("type", '=', Order::ORDER_COLLECT_TYPE_DAY) ->where($where) ->where("createdate>=$time[0]") ->where("createdate<=$time[1]") ->field($fields); return $query; } /** * @param $channelIds * @param $date * @return int|mixed */ public function getVipDataRangeTime($channelIds, $date, $business_line = PayConstants::BUSINESS_WECHAT) { try{ $result = $this->getOrderCollectModel() ->where('business_line', $business_line) ->field(['sum(recharge_money)' => 'recharge_money']) ->where(['type' => 1, 'flag' => 3, 'admin_id' => ['in', $channelIds], 'createdate' => $date]) ->find(); if($result){ return $result['recharge_money']; } return 0; }catch (\Exception $e){ Log::alert($e->getMessage() . $e->getFile() . $e->getLine()); return 0; } } /** * 分账参数 * @param $money * @param $channelId * @return \app\common\model\object\ReturnObject */ public function getPalmPayCmd($money, $channelId) { if($money==0) return false; $dzAccount = $this->getDzAccount(get_host_no_port())->data; $default = ['dz_palmpay_sub_account_id'=>'','dz_amount'=>0,'channel_palmpay_sub_account_id'=>'','channel_amount'=>0]; if ($dzAccount) { $cmd = [ 'SFJOrdertype' => '1', 'remarktype' => 'JHS0100000', 'plantCode' => '4032', ]; $channelAccount = $this->getChannelAccount($dzAccount['palmpay_company_id'], $channelId)->data; if ($channelAccount && $dzAccount['palmpay_company_id'] == $channelAccount['palmpay_company_id']) { //分账主体一致才分账 $adminExtend = \app\main\service\AdminService::instance()->getAdminExtendModel()->getInfo($channelId); $rate = $adminExtend['benefit']; $channelMoney = number_format(floatval($rate) * floatval($money),2,'.',''); $dzMoney = number_format(floatval($money) - $channelMoney,2,'.',''); if($channelMoney > 0) { $default['channel_palmpay_sub_account_id'] = $channelAccount['sub_account_code']; $default['channel_amount'] = $channelMoney; $cmd['oderlist'][] = $this->getOrderItem($channelMoney, $channelAccount['sub_account_code'])->data; } if($dzMoney > 0) { $default['dz_palmpay_sub_account_id'] = $dzAccount['sub_account_code']; $default['dz_amount'] = $dzMoney; $cmd['oderlist'][] = $this->getOrderItem($dzMoney, $dzAccount['sub_account_code'])->data; } } else { //主体不一致,钱款打点众子账户 $cmd['oderlist'][] = $this->getOrderItem($money, $dzAccount['sub_account_code'])->data; $default['dz_palmpay_sub_account_id'] = $dzAccount['sub_account_code']; $default['dz_amount'] = $money; } $json = json_encode([$cmd]); $return = urlencode($json); } else { $return = false; } return $this->setData(['cmd'=>$return,'default'=>$default])->getReturn(); } /** * 获取分账单 * @param $dzMoney * @param $SubAccNo * @return \app\common\model\object\ReturnObject */ public function getOrderItem($dzMoney, $SubAccNo) { $tmp = [ 'SubAccNo' => $SubAccNo, 'PayModel' => '1', 'TranFee' => '0.00', 'subamount' => (string)$dzMoney, 'suborderId' => $this->getUniqueOrder()->data, 'object' => '支付-' . $SubAccNo, ]; return $this->setData($tmp)->getReturn(); } /** * 获取分账订单唯一值 * @return \app\common\model\object\ReturnObject */ public function getUniqueOrder() { $data = substr(md5(substr_replace(uniqid() ,getmypid(),0,strlen(getmypid())). substr(microtime(),2,6) . uniqid()), 0, 22); return $this->setData($data)->getReturn(); } /** * 获取点众账号子商户 * @param $host * @return \app\common\model\object\ReturnObject */ public function getDzAccount($host) { $dzAccount = OfficialAccountsService::instance()->getWxpayModel() ->alias('wx') ->join('palmpay_sub_account', 'palmpay_sub_account.id=wx.palmpay_sub_account_ids', 'LEFT') ->where('wx.pay_host', $host) ->whereNotNull('palmpay_sub_account.id') ->find(); return $this->setData($dzAccount)->getReturn(); } /** * 获取渠道账号子商户 * @param $palmpay_company_id * @return \app\common\model\object\ReturnObject */ public function getChannelAccount($palmpay_company_id, $channelId) { $adminConfig = \app\main\service\AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channelId); $channelAccount = false; if ($adminConfig && $adminConfig['palmpay_sub_account_ids']) { $subAccountMap = json_decode($adminConfig['palmpay_sub_account_ids'], true); $subAccountArr=array_column($subAccountMap,'palmpay_sub_account_id','company_id'); if(array_key_exists($palmpay_company_id,$subAccountArr)){ $subAccountId = $subAccountArr[$palmpay_company_id]; $palmpaySubAccount = new PalmpaySubAccount(); $channelAccount = $palmpaySubAccount->where('id', $subAccountId)->find(); } } return $this->setData($channelAccount)->getReturn(); } /** * 获取订单扣量账号 * @param $groupId * @param $adminId * @param $channelId * @param $insertData * @param $isActivity * @return \app\common\model\object\ReturnObject * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getKlAdminInfo($groupId, $adminId, $channelId, $insertData, $isActivity = false) { $orderDeduction = new OrderDeduction(); $adminInfo = false; if ($channelId) { if ($rule_config = KlService::instance()->getKlRule($channelId)) { $adminInfo = AdminKlUpdateService::instance()->getKlAdmin($channelId, $insertData, $rule_config)->data; }else{ $adminInfo = $orderDeduction->orderKL($adminId, $channelId, $insertData, $isActivity); } } //订单转移 if ($groupId == 4 && !$adminInfo) { $adminInfo = $orderDeduction->orderTransfer($adminId, $channelId, $insertData, $isActivity); } if($adminInfo){ $this->setData($adminInfo); } return $this->getReturn(); } }