123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\main\constants\AdminConstants;
- use app\main\constants\ApiConstants;
- use app\main\helper\ArrayHelper;
- use app\main\service\AdminService;
- use app\main\service\AppGuideService;
- use app\main\service\OfficialAccountsService;
- use think\Model;
- /**
- * 订单管理
- *
- * @icon fa fa-circle-o
- */
- class Orders extends Backend
- {
- /**
- * Orders模型对象
- */
- protected $model = null;
- protected $searchFields = 'payid';
- protected $authId;
- protected $groupId;
- protected $path;
- protected $exportPath;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('Orders');
- $this->view->assign("typeList", $this->model->getTypeList());
- $this->view->assign("stateList", $this->model->getStateList());
- $finishtime ='0';
- $aid = null;
- $this->assignconfig('searchFormVisibleStatus',1);
- // 此处为打款管理->渠道商查看详情->筛选参数
- if(isset($_GET['t']) && $_GET['t'] != ''){
- $t = date('Y-m-d',strtotime($_GET['t']));
- $finishtime = $t.' 00:00:00 _ '.''.$t.' 23:59:59';
- $aid = intval($_GET['aid']);
- $this->assignconfig('searchFormVisibleStatus',0);
- }
- $group = model('AuthGroupAccess')->where('uid',$this->auth->id)->find();
- $truegroupId = $group->group_id;
- if(isset($_GET['channel_id']) && intval($_GET['channel_id'])>0){
- $groupObj = model('AuthGroupAccess')->where('uid',$_GET['channel_id'])->find();
- if($groupObj){
- $thisGroup = $groupObj->group_id;
- if($thisGroup > 2){ //判断传过来的不是管理员的id
- if($thisGroup==3){ //如果是渠道商
- if($truegroupId <3){ //只有管理员才有此操作
- $this->authId = $_GET['channel_id'];
- $thisChannel = model('Admin')->where('id',$this->authId)->find();
- $this->groupId = 3;
- $this->assign('channelTitle','当前是渠道商('.$thisChannel->nickname.')的数据');
- $this->assignconfig('channelstr','?channel_id='.$this->authId);
- $pay = empty($this->request->get('pay'))? 0:intval($this->request->get('pay'));
- if(isset($_GET['ids'])){
- $this->path = '/admin/orders/index?pay='.$pay.'&ids='.$_GET['ids'].'&ref=addtabs';
- $this->exportPath = '/admin/orders/ordersexport?pay='.$pay.'&ids='.$_GET['ids'];
- }else{
- $this->path = '/admin/orders/index?pay='.$pay.'&channel_id='.$this->authId.'&ref=addtabs';
- $this->exportPath = '/admin/orders/ordersexport?pay='.$pay.'&channel_id='.$this->authId;
- }
- }
- }else{
- if($truegroupId <4){ //管理员或渠道商才有此操作
- if($truegroupId == 3){ //如果是从渠道商查来的,判断当前代理商是不是渠道商下属
- $isCh = model('AdminExtend')->where(['admin_id'=>$_GET['channel_id'],'create_by'=>$group->uid])->find();
- if($isCh){ //如果是当前渠道商的下属
- $this->authId = $_GET['channel_id'];
- $thisChannel = model('Admin')->where('id',$this->authId)->find();
- $this->groupId = 4;
- $this->assign('channelTitle','当前是代理商('.$thisChannel->nickname.')的数据');
- $this->assignconfig('channelstr','?channel_id='.$this->authId);
- $pay = empty($this->request->get('pay'))? 0:intval($this->request->get('pay'));
- if(isset($_GET['ids'])){
- $this->path = '/admin/orders/index?pay='.$pay.'&ids='.$_GET['ids'].'&ref=addtabs';
- $this->exportPath = '/admin/orders/ordersexport?pay='.$pay.'&ids='.$_GET['ids'];
- }else{
- $this->path = '/admin/orders/index?pay='.$pay.'&channel_id='.$this->authId.'&ref=addtabs';
- $this->exportPath = '/admin/orders/ordersexport?pay='.$pay.'&channel_id='.$this->authId;
- }
- }
- }else{ //管理员查过来的
- $this->authId = $_GET['channel_id'];
- $thisChannel = model('Admin')->where('id',$this->authId)->find();
- $this->groupId = 4;
- $this->assign('channelTitle','当前是代理商('.$thisChannel->nickname.')的数据');
- $this->assignconfig('channelstr','?channel_id='.$this->authId);
- $pay = empty($this->request->get('pay'))? 0:intval($this->request->get('pay'));
- if(isset($_GET['ids'])){
- $this->path = '/admin/orders/index?pay='.$pay.'&ids='.$_GET['ids'].'&ref=addtabs';
- $this->exportPath = '/admin/orders/ordersexport?pay='.$pay.'&ids='.$_GET['ids'];
- }else{
- $this->path = '/admin/orders/index?pay='.$pay.'&channel_id='.$this->authId.'&ref=addtabs';
- $this->exportPath = '/admin/orders/ordersexport?pay='.$pay.'&channel_id='.$this->authId;
- }
- }
- }
- }
- }
- }
- }else{
- $this->authId = $this->auth->id;
- $this->groupId = $truegroupId;
- $pay = empty($this->request->get('pay'))? 0:intval($this->request->get('pay'));
- if(isset($_GET['ids'])){
- $this->path = '/admin/orders/index?pay='.$pay.'&ids='.$_GET['ids'].'&ref=addtabs';
- $this->exportPath = '/admin/orders/ordersexport?pay='.$pay.'&ids='.$_GET['ids'];
- }else{
- $this->path = '/admin/orders/index?aid='.$aid.'&t='.$finishtime.'&pay='.$pay.'&ref=addtabs';
- $this->exportPath = '/admin/orders/ordersexport?aid='.$aid.'&t='.$finishtime.'&pay='.$pay;
- }
- }
- if($this->request->has('mark') || $this->request->param('push_id') || $this->request->param('push_idx')){
- $mark_lock['mark'] = $this->request->param('mark');
- $mark_lock['push_id'] = $this->request->param('push_id');
- $mark_lock['push_idx'] = $this->request->param('push_idx');
- $this->path .= '&'.http_build_query($mark_lock);
- $this->exportPath .= '&'.http_build_query($mark_lock);
- }
- $this->assignconfig('orderpath',$this->path);
- $this->assignconfig('exportpath',$this->exportPath);
- }
- /**
- * 导出csv
- * @author 李聪聪
- */
- public function ordersexport(){
- ini_set('memory_limit', '512M'); //内存限制
- $filter = $this->request->get("filter", '{}');
- $filter = json_decode($filter, true);
- if (array_key_exists('orders.createtime', $filter) || array_key_exists('createtime', $filter)) {
- $sort = 'orders.createtime';
- //判断时间区间
- if (isset($filter['orders.createtime'])) {
- $timeRange = explode(' - ',$filter['orders.createtime']);
- } else if (isset($filter['createtime'])) {
- $timeRange = explode(' - ',$filter['createtime']);
- }
- $stime = strtotime($timeRange[0]);
- $etime = strtotime($timeRange[1]);
- if (abs($etime-$stime) > (86400*31)) {
- $this->error("查询时间范围超过一个月", null, 13);
- }
- }
- //提现详情页筛选时间
- $groupId = $this->groupId; //用户组
- $condition_finishtime = [];
- if(isset($_GET['t']) & !empty($_GET['t']) ){
- $time = explode('_',$_GET['t']);
- $condition_finishtime['orders.finishtime'] = array(array('egt',strtotime($time[0])),array('elt',strtotime($time[1])));
- $conStr= intval($_GET['aid']);// 覆盖掉前面的admin_id
- //为管理员筛选渠道商订单详情
- if($groupId==2){
- $collect = model('AdminExtend')->where('create_by',$conStr)->field('admin_id')->select();
- $condition['orders.admin_id'] = '';
- if($collect){
- foreach($collect as $val){
- $search_id[]=$val->admin_id;
- }
- $search_id[]= $conStr;
- $condition['orders.admin_id'] = array('in',implode(',',$search_id));
- }else{
- $condition['orders.admin_id'] = array('in',$conStr);
- }
- }
- }
- $columns = [ '商户单号', '交易单号', '用户','用户关注时间','用户注册时间','类型','充值金额','支付状态','下单时间','完成时间','推广链接ID','推广链接渠道', '是否分账'];
- if (in_array($this->group, [
- AdminConstants::ADMIN_SUPER_MANAGER_ID,
- AdminConstants::ADMIN_GROUP_ID_ADMIN,
- AdminConstants::ADMIN_GROUP_ID_SETTLEMENT,
- AdminConstants::ADMIN_GROUP_ID_CHANNEL,
- ]
- )) {
- array_push($columns,'书名');
- }
- if (in_array($this->group, [
- AdminConstants::ADMIN_SUPER_MANAGER_ID,
- AdminConstants::ADMIN_GROUP_ID_ADMIN,
- AdminConstants::ADMIN_GROUP_ID_CHANNEL,
- AdminConstants::ADMIN_GROUP_ID_CUSTOMER_SERVICE,
- AdminConstants::ADMIN_GROUP_ID_SETTLEMENT,
- ])) {
- array_push($columns, '代理商');
- }
- if (in_array($this->group, [
- AdminConstants::ADMIN_SUPER_MANAGER_ID,
- AdminConstants::ADMIN_GROUP_ID_ADMIN,
- AdminConstants::ADMIN_GROUP_ID_CUSTOMER_SERVICE,
- AdminConstants::ADMIN_GROUP_ID_SETTLEMENT,
- ])) {
- array_push($columns,'渠道商');
- array_push($columns,'渠道商Id');
- array_push($columns,'渠道商用户名');
- }
- $pay = empty($this->request->get('pay'))? 0:intval($this->request->get('pay'));
- $condition = []; //条件数组
- $condition_referral = []; //推广条件
- $condition_referral_or = []; //推广条件
- if(isset($_GET['ids'])){
- $ids = $_GET['ids'];
- $condition_referral_or = function ($query) use ($ids) {
- $query->where('orders.referral_id', $ids)
- ->whereOr('orders.referral_id_permanent', $ids);
- return $query;
- };
- $condition_referral['orders.resource_id'] = [['exp','is null'],['=',0],'or'];//资源id
- }
- $ext_var = '';
- if ($this->request->has('mark') || $this->request->param('push_id') || $this->request->param('push_idx')) {
- $ext_var = "orders.ext != '' AND JSON_VALID(ext)";
- if ($this->request->param('mark')) {
- $condition_referral['JSON_EXTRACT(`orders`.ext,"$.mark") + 0'] = (int)$this->request->param('mark');
- }
- if ($this->request->param('push_id')) {
- $condition_referral['JSON_EXTRACT(`orders`.ext,"$.push_id") + 0'] = (int)$this->request->param('push_id');
- }
- if ($this->request->param('push_idx')) {
- $condition_referral['JSON_EXTRACT(`orders`.ext,"$.push_idx") + 0'] = (int)$this->request->param('push_idx');
- }
- }
- if($groupId==3){ //渠道商
- $collect = model('AdminExtend')->where('create_by',$this->authId)->select();
- $conStr = '';
- if($collect){
- foreach($collect as $val){
- $conStr.=$val->admin_id.',';
- }
- $conStr .= $this->authId;
- }else{
- $conStr = $this->authId;
- }
- }elseif($groupId==4){ //代理商
- $condition['orders.admin_id'] = $this->authId;
- }
- //取代理商和渠道商的昵称
- $channels = model('admin')->join('auth_group_access a','a.uid= admin.id')->where('a.group_id','>',2)->select();
- $cNames = [];
- $qrImages = [];
- foreach ($channels as $channel) {
- if($channel->group_id == 3){
- $cNames['channel'][$channel->id] = $channel->nickname;
- $qrImages[$channel['id']]['cusername'] = $channel['username'];
- }else{
- $cNames['agent'][$channel->id] = $channel->nickname;
- }
- }
- //取渠道商的公众号二维码及admin_id
- $qrchannnels = model('admin_config')->join('auth_group_access a','a.uid = admin_config.admin_id')->where('a.group_id',3)->select();
- foreach ($qrchannnels as $qrchannnel){
- $qrImages[$qrchannnel['admin_id']]['qrimage'] = empty($qrchannnel['qrcode_image']) ? 'https://open.weixin.qq.com/qr/code?username='.$qrchannnel['json']['authorizer_info']['user_name'] : $qrchannnel['qrcode_image'];
- $qrImages[$qrchannnel['admin_id']]['cchannelid'] = $qrchannnel['admin_id'];
- }
- //取代理商和渠道商的对应关系
- $extend = model('adminExtend')->alias('e')->field('e.admin_id,e.create_by')->join('auth_group_access a',
- 'a.uid= e.admin_id')->where('a.group_id', '>', 2)->select();
- $agentBy = [];
- foreach ($extend as $key => $value) {
- $agentBy[$value['admin_id']] = $value['create_by'];
- }
- if($pay > 0){
- if($pay == 1){
- $condition['orders.state'] = '1';
- }elseif($pay == 2){
- $condition['orders.state'] = '0';
- }
- }
- $orderId = 0;
- [$where, $sort, $order, $offset, $limit] = $this->buildparams();
- // $sort = 'admin_id';
- $limit = 2000;
- $data = [];
- mb_convert_variables('GBK', 'UTF-8', $columns);
- $data[] = $columns;
- if($groupId == 3){
- $total = $this->model
- ->where($where)
- ->where($ext_var)
- ->where($condition_referral)
- ->where($condition_referral_or)
- ->where($condition_finishtime)
- ->where('admin_id','in',$conStr)
- ->count();
- $pages = ceil($total / $limit);
- for($i = 1; $i <= $pages; $i++) {
- $offset = 0;
- if ($i == 1){
- $list = $this->model
- //->join('referral r', 'r.id=orders.referral_id', 'left')
- //->join('book b', 'orders.book_id = b.id', 'left')
- ->field('orders.*')
- ->force('index_admin_deduct_state')
- ->where($where)
- ->where($ext_var)
- ->where($condition_referral)
- ->where($condition_referral_or)
- ->where($condition_finishtime)
- ->where('orders.admin_id', 'in', $conStr)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- }else{
- $list = $this->model
- //->join('referral r', 'r.id=orders.referral_id', 'left')
- //->join('book b', 'orders.book_id = b.id', 'left')
- ->field('orders.*')
- ->force('index_admin_deduct_state')
- ->where($where)
- ->where($ext_var)
- ->where('orders.id','<',$orderId)
- ->where($condition_referral)
- ->where($condition_referral_or)
- ->where($condition_finishtime)
- ->where('orders.admin_id', 'in', $conStr)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- }
- if ($list){
- $this->getBookReferralInfo($list);
- }
- $endArr = end($list);
- $orderId = $endArr['id'];
- $result = $this->getUserData($list,$cNames,$qrImages,$agentBy);
- $data = array_merge($data, $result);
- }
- }else{
- $total = $this->model
- ->where($where)
- ->where($ext_var)
- ->where($condition_referral)
- ->where($condition_referral_or)
- ->where($condition_finishtime)
- ->where($condition)
- ->count();
- $pages = ceil($total / $limit);
- for($i = 1; $i <= $pages; $i++) {
- $offset = 0;
- if ($i == 1){
- $list = $this->model
- //->join('referral r', 'r.id=orders.referral_id', 'left')->join('book b', 'orders.book_id = b.id', 'left')
- ->field('orders.*')
- ->force('index_admin_deduct_state')
- ->where($where)
- ->where($ext_var)
- ->where($condition_referral)
- ->where($condition_referral_or)
- ->where($condition_finishtime)
- ->where($condition)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- }else{
- $list = $this->model
- //->join('referral r', 'r.id=orders.referral_id', 'left')->join('book b', 'orders.book_id = b.id', 'left')
- ->field('orders.*')
- ->force('index_admin_deduct_state')
- ->where($where)
- ->where($ext_var)
- ->where($condition_referral)
- ->where($condition_referral_or)
- ->where($condition_finishtime)
- ->where($condition)
- ->where('orders.id','<',$orderId)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- }
- if ($list){
- $this->getBookReferralInfo($list);
- }
- $endArr = end($list);
- $orderId = $endArr['id'];
- $result = $this->getUserData($list,$cNames,$qrImages,$agentBy);
- $data = array_merge($data, $result);
- }
- }
- $fileName = "导出订单数据-".date('YmdHis', time()).".csv";
- // 头部标题
- $content = '';
- foreach ($data as $k => $v) {
- $content .= implode(',', $v) . PHP_EOL;
- }
- header('Expires: 0');
- header('Content-Encoding: utf-8');
- header("Content-type:text/csv; charset=utf-8");
- Header("Accept-Length: " . strlen($content));
- header("Content-Disposition:attachment;filename=" . $fileName);
- header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
- header('Pragma:public');
- echo $content;exit;
- }
- private function getBookReferralInfo(&$list)
- {
- $bookIds = array_unique( array_column($list,'book_id') );
- $booksInfo = [];
- $referralInfo = [];
- if ($bookIds){
- $booksInfo = model('book')->getBooksInfo($bookIds);
- }
- $referralIds = array_unique(array_column($list,'referral_id'));
- if ($referralIds){
- $referralInfo = model("Referral")->field('id,name,admin_id')->where('id', 'in', $referralIds)->select();
- $referralInfo = ArrayHelper::index($referralInfo, 'id');
- }
- foreach($list as $k => &$v){
- $v['pdqd'] = isset($referralInfo[$v['referral_id']])?$referralInfo[$v['referral_id']]['name']:'';
- $v['radmin_id'] = isset($referralInfo[$v['referral_id']])?$referralInfo[$v['referral_id']]['admin_id']:1;
- $v['bookname'] = isset($booksInfo[$v['book_id']])?$booksInfo[$v['book_id']]['name']:'';
- }
- }
- /**
- * @param $fp
- * @param $list
- * @param null $agentBy
- * @author 李聪聪
- */
- public function getUserData($list,$cNames=null,$qrImages=null,$agentBy=null){
- //支付状态
- $payStatus = ['0'=>'未支付','1'=>'已支付'];
- $orderIds = array_column($list, 'out_trade_no');
- $splitRows = model("PalmpaySplitAccountLog")->field('order_id')->where('order_id', 'in', $orderIds)->where('channel_amount', '>', 0)->select();
- if ($splitRows) {
- $splitRows = ArrayHelper::index($splitRows, 'order_id');
- }
- $mUser = model('user');
- $result = [];
- foreach ($list as $k => $v)
- {
- $newUser = $mUser->setConnect($v['user_id'])
- ->where(['id'=>$v['user_id']])
- ->field('id,channel_id,nickname,createtime,avatar,subscribe_time')
- ->select();
- if($newUser){
- $aUser = $newUser[0]->getData();
- unset($newUser);
- if(empty($cNames['agent'][$v['admin_id']])){
- $aUser['agent_id'] = '无';
- if(empty($cNames['channel'][$v['admin_id']])){
- $aUser['channel_id'] = '';
- }else{
- $aUser['channel_id'] = $cNames['channel'][$v['admin_id']];
- }
- //二维码图片
- if(empty($qrImages[$v['admin_id']])){
- $aUser['qrimage'] = '';
- $aUser['cusername'] = '';
- $aUser['cchannelid'] = '';
- }else{
- $aUser['qrimage'] = empty($qrImages[$v['admin_id']]['qrimage']) ? '':$qrImages[$v['admin_id']]['qrimage'];
- $aUser['cusername'] = empty($qrImages[$v['admin_id']]['cusername'])?'':$qrImages[$v['admin_id']]['cusername'];
- $aUser['cchannelid'] = empty($qrImages[$v['admin_id']]['cchannelid'])?'':$qrImages[$v['admin_id']]['cchannelid'];
- }
- }else{
- $aUser['agent_id'] = $cNames['agent'][$v['admin_id']];
- $aUser['channel_id'] = $cNames['channel'][$agentBy[$v['admin_id']]];
- if(empty($qrImages[$agentBy[$v['admin_id']]])){
- $aUser['qrimage'] = '';
- $aUser['cusername'] = '';
- $aUser['cchannelid'] = '';
- }else{
- $aUser['cusername'] = empty($qrImages[$agentBy[$v['admin_id']]]['cusername'])? '':$qrImages[$agentBy[$v['admin_id']]]['cusername'];
- $aUser['cchannelid'] = empty($qrImages[$agentBy[$v['admin_id']]]['cchannelid']) ? '':$qrImages[$agentBy[$v['admin_id']]]['cchannelid'];
- $aUser['qrimage'] = empty($qrImages[$agentBy[$v['admin_id']]]['qrimage'])? '': $qrImages[$agentBy[$v['admin_id']]]['qrimage'];
- }
- }
- //获取每列数据,转换处理成需要导出的数据
- $groupId = $this->groupId; //用户组
- //是否分账
- $split = '否';
- if ($splitRows && isset($splitRows[$v['out_trade_no']])) {
- $split = '是';
- }
- // '\t' 可以解决科学技术法问题
- $rowData = [
- '"'.$v['out_trade_no'].'"',
- empty($v['transaction_id'])?"无":"\t".$v['transaction_id']."\t",
- str_replace(["\n", "\t", ","], ["", "", ""], $aUser['nickname']."({$v['user_id']})"),
- $aUser['subscribe_time'] ? date('Y-m-d H:i:s', $aUser['subscribe_time']) : '-',
- date('Y-m-d H:i:s',$aUser['createtime']),
- $v['type_text'],
- $v['money'],
- $payStatus[$v['state']],
- date('Y-m-d H:i:s',$v['createtime']),
- empty($v['finishtime'])?'无':date('Y-m-d H:i:s',$v['finishtime']),
- $v['referral_id'],
- $v['pdqd'],
- $split,
- ];
- if (in_array($groupId, [
- AdminConstants::ADMIN_SUPER_MANAGER_ID,
- AdminConstants::ADMIN_GROUP_ID_ADMIN,
- AdminConstants::ADMIN_GROUP_ID_SETTLEMENT,
- AdminConstants::ADMIN_GROUP_ID_CHANNEL,
- ])) {
- array_push($rowData, $v['bookname']);
- }
- if (in_array($this->group, [
- AdminConstants::ADMIN_SUPER_MANAGER_ID,
- AdminConstants::ADMIN_GROUP_ID_ADMIN,
- AdminConstants::ADMIN_GROUP_ID_CHANNEL,
- AdminConstants::ADMIN_GROUP_ID_CUSTOMER_SERVICE,
- AdminConstants::ADMIN_GROUP_ID_SETTLEMENT,
- ])) {
- array_push($rowData,$aUser['agent_id'] ?? '无');
- }
- if (in_array($this->group, [
- AdminConstants::ADMIN_SUPER_MANAGER_ID,
- AdminConstants::ADMIN_GROUP_ID_ADMIN,
- AdminConstants::ADMIN_GROUP_ID_CUSTOMER_SERVICE,
- AdminConstants::ADMIN_GROUP_ID_SETTLEMENT,
- ])) {
- array_push($rowData,$aUser['channel_id'] ?? '');
- array_push($rowData,$aUser['cchannelid'] ?? '');
- array_push($rowData,$aUser['cusername'] ?? '');
- }
- //需要格式转换,否则会乱码
- mb_convert_variables('GBK', 'UTF-8', $rowData);
- // fputcsv($fp, $rowData);
- $result[] = $rowData;
- unset($rowData);
- }
- }
- unset($list);
- unset($mUser);
- unset($splitRows);
- unset($cNames);
- unset($qrImages);
- unset($agentBy);
- return $result;
- }
- /**
- * 查单
- */
- public function searchorder(){
- header("Cache-control:private");
- if($this->request->isPost()){
- $oid = $this->request->post('oid');
- $patten = array("\r\n", "\n", "\r");
- if(empty($oid)){
- $this->error('请填写数据');
- }
- $oid = str_replace($patten,',',$oid);
- $oidArr = explode(',',$oid);
- foreach ($oidArr as $key=>$val){
- $oidArr[$key] = trim($val);
- if(empty($val)){
- unset($oidArr[$key]);
- }
- }
- $orderRes = collection(model('Orders')->where('transaction_id','in',$oidArr)->select())->toArray();
- $wxpayList = OfficialAccountsService::instance()->getWxpayModel()->column("id,mcid,quartet_merchant_id");
- $wxpay = [];
- foreach($wxpayList as $item){
- $wxpay[$item['id']] = $item;
- }
- $res = [];
- if ($orderRes) {
- foreach ($orderRes as $val) {
- if (array_key_exists($val['wxpay_id'], $wxpay)) {
- $val['mcid'] = $wxpay[$val['wxpay_id']]['mcid'];
- if (array_key_exists($val['payment_method'], ApiConstants::$allPaymentMethods)) {
- $val['payinfo'] = ApiConstants::$allPaymentMethods[$val['payment_method']];
- } else {
- $val['payinfo'] = '微信支付 ';
- }
- if ($val['payment_method'] != ApiConstants::PAYMENT_METHOD_WECHAT) {
- $val['mcid'] = $wxpay[$val['wxpay_id']]['quartet_merchant_id'];
- }
- } else {
- $val['mcid'] = '';
- $val['payinfo'] = '';
- }
- array_push($res, $val);
- }
- }
- if (count($res) !== count($oidArr)){
- $tranArr = array_column($res, 'transaction_id');
- $arrDiff = array_diff($oidArr,$tranArr);
- foreach ($arrDiff as $val){
- array_push($res,['transaction_id'=>$val,'user_id'=>'未查到','wxpay_id'=>'未查到','mcid'=>'未查到','payinfo'=>'未查到']);
- }
- }
- $this->assign('res',$res);
- }else{
- $this->assign('res','');
- }
- return $this->view->fetch();
- }
- /**
- * 查看
- */
- public function index()
- {
- ini_set('memory_limit', '256M'); //内存限制
- //当前是否为关联查询
- $this->relationSearch = true;
- $pay = empty($this->request->get('pay'))? 0:intval($this->request->get('pay'));
- $groupId = $this->groupId; //用户组
- $condition = []; //条件数组
- $condition_referral_or = $condition_referral = []; //推广条件
- if(isset($_GET['ids'])){
- $ids = $_GET['ids'];
- // $condition_referral['orders.referral_id'] = $_GET['ids'];//推广id
- $condition_referral['orders.resource_id'] = [['exp','is null'],['=',0],'or'];//资源id
- $condition_referral_or = function ($query) use ($ids) {
- $query->where('orders.referral_id', $ids)
- ->whereOr('orders.referral_id_permanent', $ids);
- return $query;
- };
- }
- if($groupId==3){ //渠道商
- $collect = model('AdminExtend')->where('create_by',$this->authId)->select();
- $conStr = '';
- if($collect){
- foreach($collect as $val){
- $conStr.=$val->admin_id.',';
- }
- $conStr .= $this->authId;
- }else{
- $conStr = $this->authId;
- }
- }elseif($groupId==4){ //代理商
- $condition['orders.admin_id'] = $this->authId;
- }
- if ($this->request->isAjax())
- {
- $ext_var = '';
- if ($this->request->has('mark') || $this->request->param('push_id') || $this->request->param('push_idx')) {
- $ext_var = "orders.ext != '' AND JSON_VALID(ext)";
- if ($this->request->param('mark')) {
- $condition_referral['JSON_EXTRACT(`orders`.ext,"$.mark") + 0'] = (int)$this->request->param('mark');
- }
- if ($this->request->param('push_id')) {
- $condition_referral['JSON_EXTRACT(`orders`.ext,"$.push_id") + 0'] = (int)$this->request->param('push_id');
- }
- if ($this->request->param('push_idx')) {
- $condition_referral['JSON_EXTRACT(`orders`.ext,"$.push_idx") + 0'] = (int)$this->request->param('push_idx');
- }
- }
- if($pay > 0){
- if($pay == 1){
- $condition['orders.state'] = '1';
- }elseif($pay == 2){
- $condition['orders.state'] = '0';
- }
- }
- //提现详情页筛选时间
- $condition_finishtime = [];
- if(isset($_GET['t']) & !empty($_GET['t']) ){
- $time = explode('_',$_GET['t']);
- $condition_finishtime['orders.finishtime'] = array(array('egt',strtotime($time[0])),array('elt',strtotime($time[1])));
- if (abs(strtotime($time[1])-strtotime($time[0])) > (86400*31)) {
- $this->error("查询时间范围超过一个月", null, 13);
- }
- $conStr= intval($_GET['aid']);// 覆盖掉前面的admin_id
- //为管理员筛选渠道商订单详情
- if($groupId==2){
- $collect = model('AdminExtend')->where('create_by',$conStr)->field('admin_id')->select();
- $condition['orders.admin_id'] = '';
- if($collect){
- foreach($collect as $val){
- $search_id[]=$val->admin_id;
- }
- $search_id[]= $conStr;
- $condition['orders.admin_id'] = array('in',implode(',',$search_id));
- }else{
- $condition['orders.admin_id'] = array('in',$conStr);
- }
- }
- }
- [$where, $sort, $order, $offset, $limit] = $this->buildparams();
- $limit = 10;
- $filter = $this->request->get("filter", '{}');
- $filter = json_decode($filter, true);
- if (array_key_exists('orders.createtime', $filter) || array_key_exists('createtime', $filter)) {
- //判断时间区间
- if (isset($filter['orders.createtime'])) {
- $timeRange = explode(' - ',$filter['orders.createtime']);
- } else if (isset($filter['createtime'])) {
- $timeRange = explode(' - ',$filter['createtime']);
- }
- $stime = strtotime($timeRange[0]);
- $etime = strtotime($timeRange[1]);
- if (abs($etime-$stime) > (86400*31)) {
- $this->error("查询时间范围超过一个月", null, 13);
- }
- }
- if (in_array($groupId, [AdminConstants::ADMIN_GROUP_ID_CHANNEL, AdminConstants::ADMIN_GROUP_ID_AGENT])){
- $sort = 'orders.updatetime';
- } else {
- if ($filter) {
- $sort = 'orders.updatetime';
- } else {
- $sort = 'orders.createtime';
- }
- }
- $order = 'desc';
- if($groupId ==3){
- $total = $this->model
- ->where($where)
- ->where($ext_var)
- ->where($condition_referral)
- ->where($condition_referral_or)
- ->where($condition_finishtime)
- ->where('admin_id','in',$conStr)
- ->count();
- $list = $this->model
- ->where($where)
- ->where($ext_var)
- ->where($condition_referral)
- ->where($condition_referral_or)
- ->where($condition_finishtime)
- ->where('orders.admin_id','in',$conStr)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- }else{
- $total = $this->model
- ->where($where)
- ->where($ext_var)
- ->where($condition_referral)
- ->where($condition_referral_or)
- ->where($condition_finishtime)
- ->where($condition)
- ->count();
- $list = $this->model
- ->where($where)
- ->where($ext_var)
- ->where($condition_referral)
- ->where($condition_referral_or)
- ->where($condition_finishtime)
- ->where($condition)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- }
- $referralRows = [];
- $booksInfo = [];
- if ($list) {
- $referralIds = array_column($list, 'referral_id');
- $preferralIds = array_column($list, 'referral_id_permanent');
- $referralIds = array_unique(array_merge($referralIds, $preferralIds));
- if ($referralIds) {
- $referralRows = model("Referral")->field('id,name,admin_id')->where('id', 'in', $referralIds)->select();
- $referralRows = ArrayHelper::index($referralRows, 'id');
- }
- $bookIds = array_column($list, 'book_id');
- $bookIds = array_filter($bookIds);
- if (!empty($bookIds)) {
- $booksInfo = model('book')->getBooksInfo($bookIds);
- }
- $adminIds = array_column($list, 'admin_id');
- $agentRef = AdminService::instance()->getAdminExtendModel()
- ->alias('a')
- ->whereIn('admin_id', $adminIds)
- ->join('auth_group_access aga', 'aga.uid = a.create_by')
- ->where('aga.group_id', AdminConstants::ADMIN_GROUP_ID_CHANNEL)
- ->column('a.admin_id,a.create_by');
- $agentIds = array_keys($agentRef);
- $channelIds = array_merge(array_diff($adminIds, $agentIds), array_values($agentRef));
- $channelList = AdminService::instance()->getAdminConfigModel()
- ->alias('ac')
- ->join('admin a','a.id=admin_config.admin_id', 'RIGHT')
- ->whereIn('a.id', array_merge($agentIds, $channelIds))
- ->field('a.id,ac.json,a.nickname,a.username')
- ->select();
- $cNames = [];
- foreach ($channelList as $adminInfo) {
- if (in_array($adminInfo['id'], $channelIds)) {
- if ($adminInfo['qrcode_image']) {
- $qrImages[$adminInfo['id']]['qrimage'] = $adminInfo['qrcode_image'];
- } elseif ($adminInfo['json']['authorizer_info']['user_name']) {
- $qrImages[$adminInfo['id']]['qrimage'] = 'https://open.weixin.qq.com/qr/code?username=' . $adminInfo['json']['authorizer_info']['user_name'];
- } else {
- $qrImages[$adminInfo['id']]['qrimage'] = '';
- }
- $qrImages[$adminInfo['id']]['cchannelid'] = $adminInfo['id'];
- $qrImages[$adminInfo['id']]['cusername'] = $adminInfo['username'];
- }
- $cNames[$adminInfo['id']] = $adminInfo['nickname'];
- }
- }
- foreach ($list as $k => &$v)
- {
- unset($v['deduct']);
- $user = model('user')
- ->setConnect($v['user_id'])
- ->where(['id'=>$v['user_id']])
- ->field('id,channel_id,nickname,createtime,avatar,subscribe_time')
- ->select();
- if ($user) {
- $user = $user[0];
- }
- if(in_array($v['admin_id'], $channelIds)){
- $user['agent_id'] = '无';
- $user['channel_id'] = $cNames[$v['admin_id']];
- //二维码图片
- if(empty($qrImages[$v['admin_id']])){
- $user['qrimage'] = '';
- $user['cusername'] = '';
- $user['cchannelid'] = '';
- }else{
- $user['qrimage'] = empty($qrImages[$v['admin_id']]['qrimage']) ? '':$qrImages[$v['admin_id']]['qrimage'];
- $user['cusername'] = empty($qrImages[$v['admin_id']]['cusername'])?'':$qrImages[$v['admin_id']]['cusername'];
- $user['cchannelid'] = empty($qrImages[$v['admin_id']]['cchannelid'])?'':$qrImages[$v['admin_id']]['cchannelid'];
- }
- }else{
- $user['agent_id'] = $cNames[$v['admin_id']];
- $channelId = $agentRef[$v['admin_id']];
- $user['channel_id'] = $cNames[$channelId];
- if(empty($qrImages[$channelId])){
- $user['qrimage'] = '';
- $user['cusername'] = '';
- $user['cchannelid'] = '';
- }else{
- $user['cusername'] = empty($qrImages[$channelId]['cusername'])? '':$qrImages[$channelId]['cusername'];
- $user['cchannelid'] = empty($qrImages[$channelId]['cchannelid']) ? '':$qrImages[$channelId]['cchannelid'];
- $user['qrimage'] = empty($qrImages[$channelId]['qrimage'])? '': $qrImages[$channelId]['qrimage'];
- }
- }
- $v['user'] = $user;
- //组装推广链接信息
- if ($v['referral_id'] && isset($referralRows[$v['referral_id']])) {
- $v['pdqd'] = $referralRows[$v['referral_id']]['name'];
- $v['radmin_id'] = $referralRows[$v['referral_id']]['admin_id'];
- } elseif ($v['referral_id_permanent'] && $referralRows[$v['referral_id_permanent']]) {
- $v['pdqd'] = $referralRows[$v['referral_id_permanent']]['name'];
- $v['radmin_id'] = $referralRows[$v['referral_id_permanent']]['admin_id'];
- } else {
- $v['pdqd'] = null;
- $v['radmin_id'] = null;
- }
- //组装书籍名称
- if ($v['book_id'] && isset($booksInfo[$v['book_id']])) {
- $v['bookname'] = $booksInfo[$v['book_id']]['name'];
- } else {
- $v['bookname'] = '';
- }
- }
- unset($v);
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- $this->view->assign('pay',$pay);
- $isShowBusinessLine = true;
- if (in_array($this->group, [3, 4, 7, 8])) {
- $isShowBusinessLine = false;
- if (AppGuideService::instance()->isShowAdminMenu($this->auth->id)) {
- $isShowBusinessLine = true;
- }
- }
- $this->assignconfig('isShowBusinessLine', $isShowBusinessLine);
- return $this->view->fetch();
- }
- //管理员查kl单
- public function klorder(){
- if ($this->request->isAjax()) {
- [$where, $sort, $order, $offset, $limit] = $this->buildparams();
- $total = $this->model
- ->where($where)
- ->where('deduct', 1)
- ->count();
- $list = $this->model
- ->where($where)
- ->where('deduct', 1)
- ->order('id desc')
- ->limit($offset, $limit)
- ->select();
- foreach ($list as $k => &$v)
- {
- $adminData = model('Admin')->field('nickname')->where(['id' => $v['admin_id']]) ->find() -> toarray();
- $v['cnickname'] = $adminData['nickname'] ?? '';
- $v['user'] = model('user')->setConnect($v['user_id'])->where(['id'=>$v['user_id']])->find();
- }
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- $this->assignconfig('orderType',1);
- return $this->fetch();
- }
- }
|