Orders.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\main\constants\AdminConstants;
  5. use app\main\constants\ApiConstants;
  6. use app\main\helper\ArrayHelper;
  7. use app\main\service\AdminService;
  8. use app\main\service\AppGuideService;
  9. use app\main\service\OfficialAccountsService;
  10. use think\Model;
  11. /**
  12. * 订单管理
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Orders extends Backend
  17. {
  18. /**
  19. * Orders模型对象
  20. */
  21. protected $model = null;
  22. protected $searchFields = 'payid';
  23. protected $authId;
  24. protected $groupId;
  25. protected $path;
  26. protected $exportPath;
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = model('Orders');
  31. $this->view->assign("typeList", $this->model->getTypeList());
  32. $this->view->assign("stateList", $this->model->getStateList());
  33. $finishtime ='0';
  34. $aid = null;
  35. $this->assignconfig('searchFormVisibleStatus',1);
  36. // 此处为打款管理->渠道商查看详情->筛选参数
  37. if(isset($_GET['t']) && $_GET['t'] != ''){
  38. $t = date('Y-m-d',strtotime($_GET['t']));
  39. $finishtime = $t.' 00:00:00 _ '.''.$t.' 23:59:59';
  40. $aid = intval($_GET['aid']);
  41. $this->assignconfig('searchFormVisibleStatus',0);
  42. }
  43. $group = model('AuthGroupAccess')->where('uid',$this->auth->id)->find();
  44. $truegroupId = $group->group_id;
  45. if(isset($_GET['channel_id']) && intval($_GET['channel_id'])>0){
  46. $groupObj = model('AuthGroupAccess')->where('uid',$_GET['channel_id'])->find();
  47. if($groupObj){
  48. $thisGroup = $groupObj->group_id;
  49. if($thisGroup > 2){ //判断传过来的不是管理员的id
  50. if($thisGroup==3){ //如果是渠道商
  51. if($truegroupId <3){ //只有管理员才有此操作
  52. $this->authId = $_GET['channel_id'];
  53. $thisChannel = model('Admin')->where('id',$this->authId)->find();
  54. $this->groupId = 3;
  55. $this->assign('channelTitle','当前是渠道商('.$thisChannel->nickname.')的数据');
  56. $this->assignconfig('channelstr','?channel_id='.$this->authId);
  57. $pay = empty($this->request->get('pay'))? 0:intval($this->request->get('pay'));
  58. if(isset($_GET['ids'])){
  59. $this->path = '/admin/orders/index?pay='.$pay.'&ids='.$_GET['ids'].'&ref=addtabs';
  60. $this->exportPath = '/admin/orders/ordersexport?pay='.$pay.'&ids='.$_GET['ids'];
  61. }else{
  62. $this->path = '/admin/orders/index?pay='.$pay.'&channel_id='.$this->authId.'&ref=addtabs';
  63. $this->exportPath = '/admin/orders/ordersexport?pay='.$pay.'&channel_id='.$this->authId;
  64. }
  65. }
  66. }else{
  67. if($truegroupId <4){ //管理员或渠道商才有此操作
  68. if($truegroupId == 3){ //如果是从渠道商查来的,判断当前代理商是不是渠道商下属
  69. $isCh = model('AdminExtend')->where(['admin_id'=>$_GET['channel_id'],'create_by'=>$group->uid])->find();
  70. if($isCh){ //如果是当前渠道商的下属
  71. $this->authId = $_GET['channel_id'];
  72. $thisChannel = model('Admin')->where('id',$this->authId)->find();
  73. $this->groupId = 4;
  74. $this->assign('channelTitle','当前是代理商('.$thisChannel->nickname.')的数据');
  75. $this->assignconfig('channelstr','?channel_id='.$this->authId);
  76. $pay = empty($this->request->get('pay'))? 0:intval($this->request->get('pay'));
  77. if(isset($_GET['ids'])){
  78. $this->path = '/admin/orders/index?pay='.$pay.'&ids='.$_GET['ids'].'&ref=addtabs';
  79. $this->exportPath = '/admin/orders/ordersexport?pay='.$pay.'&ids='.$_GET['ids'];
  80. }else{
  81. $this->path = '/admin/orders/index?pay='.$pay.'&channel_id='.$this->authId.'&ref=addtabs';
  82. $this->exportPath = '/admin/orders/ordersexport?pay='.$pay.'&channel_id='.$this->authId;
  83. }
  84. }
  85. }else{ //管理员查过来的
  86. $this->authId = $_GET['channel_id'];
  87. $thisChannel = model('Admin')->where('id',$this->authId)->find();
  88. $this->groupId = 4;
  89. $this->assign('channelTitle','当前是代理商('.$thisChannel->nickname.')的数据');
  90. $this->assignconfig('channelstr','?channel_id='.$this->authId);
  91. $pay = empty($this->request->get('pay'))? 0:intval($this->request->get('pay'));
  92. if(isset($_GET['ids'])){
  93. $this->path = '/admin/orders/index?pay='.$pay.'&ids='.$_GET['ids'].'&ref=addtabs';
  94. $this->exportPath = '/admin/orders/ordersexport?pay='.$pay.'&ids='.$_GET['ids'];
  95. }else{
  96. $this->path = '/admin/orders/index?pay='.$pay.'&channel_id='.$this->authId.'&ref=addtabs';
  97. $this->exportPath = '/admin/orders/ordersexport?pay='.$pay.'&channel_id='.$this->authId;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }else{
  105. $this->authId = $this->auth->id;
  106. $this->groupId = $truegroupId;
  107. $pay = empty($this->request->get('pay'))? 0:intval($this->request->get('pay'));
  108. if(isset($_GET['ids'])){
  109. $this->path = '/admin/orders/index?pay='.$pay.'&ids='.$_GET['ids'].'&ref=addtabs';
  110. $this->exportPath = '/admin/orders/ordersexport?pay='.$pay.'&ids='.$_GET['ids'];
  111. }else{
  112. $this->path = '/admin/orders/index?aid='.$aid.'&t='.$finishtime.'&pay='.$pay.'&ref=addtabs';
  113. $this->exportPath = '/admin/orders/ordersexport?aid='.$aid.'&t='.$finishtime.'&pay='.$pay;
  114. }
  115. }
  116. if($this->request->has('mark') || $this->request->param('push_id') || $this->request->param('push_idx')){
  117. $mark_lock['mark'] = $this->request->param('mark');
  118. $mark_lock['push_id'] = $this->request->param('push_id');
  119. $mark_lock['push_idx'] = $this->request->param('push_idx');
  120. $this->path .= '&'.http_build_query($mark_lock);
  121. $this->exportPath .= '&'.http_build_query($mark_lock);
  122. }
  123. $this->assignconfig('orderpath',$this->path);
  124. $this->assignconfig('exportpath',$this->exportPath);
  125. }
  126. /**
  127. * 导出csv
  128. * @author 李聪聪
  129. */
  130. public function ordersexport(){
  131. ini_set('memory_limit', '512M'); //内存限制
  132. $filter = $this->request->get("filter", '{}');
  133. $filter = json_decode($filter, true);
  134. if (array_key_exists('orders.createtime', $filter) || array_key_exists('createtime', $filter)) {
  135. $sort = 'orders.createtime';
  136. //判断时间区间
  137. if (isset($filter['orders.createtime'])) {
  138. $timeRange = explode(' - ',$filter['orders.createtime']);
  139. } else if (isset($filter['createtime'])) {
  140. $timeRange = explode(' - ',$filter['createtime']);
  141. }
  142. $stime = strtotime($timeRange[0]);
  143. $etime = strtotime($timeRange[1]);
  144. if (abs($etime-$stime) > (86400*31)) {
  145. $this->error("查询时间范围超过一个月", null, 13);
  146. }
  147. }
  148. //提现详情页筛选时间
  149. $groupId = $this->groupId; //用户组
  150. $condition_finishtime = [];
  151. if(isset($_GET['t']) & !empty($_GET['t']) ){
  152. $time = explode('_',$_GET['t']);
  153. $condition_finishtime['orders.finishtime'] = array(array('egt',strtotime($time[0])),array('elt',strtotime($time[1])));
  154. $conStr= intval($_GET['aid']);// 覆盖掉前面的admin_id
  155. //为管理员筛选渠道商订单详情
  156. if($groupId==2){
  157. $collect = model('AdminExtend')->where('create_by',$conStr)->field('admin_id')->select();
  158. $condition['orders.admin_id'] = '';
  159. if($collect){
  160. foreach($collect as $val){
  161. $search_id[]=$val->admin_id;
  162. }
  163. $search_id[]= $conStr;
  164. $condition['orders.admin_id'] = array('in',implode(',',$search_id));
  165. }else{
  166. $condition['orders.admin_id'] = array('in',$conStr);
  167. }
  168. }
  169. }
  170. $columns = [ '商户单号', '交易单号', '用户','用户关注时间','用户注册时间','类型','充值金额','支付状态','下单时间','完成时间','推广链接ID','推广链接渠道', '是否分账'];
  171. if (in_array($this->group, [
  172. AdminConstants::ADMIN_SUPER_MANAGER_ID,
  173. AdminConstants::ADMIN_GROUP_ID_ADMIN,
  174. AdminConstants::ADMIN_GROUP_ID_SETTLEMENT,
  175. AdminConstants::ADMIN_GROUP_ID_CHANNEL,
  176. ]
  177. )) {
  178. array_push($columns,'书名');
  179. }
  180. if (in_array($this->group, [
  181. AdminConstants::ADMIN_SUPER_MANAGER_ID,
  182. AdminConstants::ADMIN_GROUP_ID_ADMIN,
  183. AdminConstants::ADMIN_GROUP_ID_CHANNEL,
  184. AdminConstants::ADMIN_GROUP_ID_CUSTOMER_SERVICE,
  185. AdminConstants::ADMIN_GROUP_ID_SETTLEMENT,
  186. ])) {
  187. array_push($columns, '代理商');
  188. }
  189. if (in_array($this->group, [
  190. AdminConstants::ADMIN_SUPER_MANAGER_ID,
  191. AdminConstants::ADMIN_GROUP_ID_ADMIN,
  192. AdminConstants::ADMIN_GROUP_ID_CUSTOMER_SERVICE,
  193. AdminConstants::ADMIN_GROUP_ID_SETTLEMENT,
  194. ])) {
  195. array_push($columns,'渠道商');
  196. array_push($columns,'渠道商Id');
  197. array_push($columns,'渠道商用户名');
  198. }
  199. $pay = empty($this->request->get('pay'))? 0:intval($this->request->get('pay'));
  200. $condition = []; //条件数组
  201. $condition_referral = []; //推广条件
  202. $condition_referral_or = []; //推广条件
  203. if(isset($_GET['ids'])){
  204. $ids = $_GET['ids'];
  205. $condition_referral_or = function ($query) use ($ids) {
  206. $query->where('orders.referral_id', $ids)
  207. ->whereOr('orders.referral_id_permanent', $ids);
  208. return $query;
  209. };
  210. $condition_referral['orders.resource_id'] = [['exp','is null'],['=',0],'or'];//资源id
  211. }
  212. $ext_var = '';
  213. if ($this->request->has('mark') || $this->request->param('push_id') || $this->request->param('push_idx')) {
  214. $ext_var = "orders.ext != '' AND JSON_VALID(ext)";
  215. if ($this->request->param('mark')) {
  216. $condition_referral['JSON_EXTRACT(`orders`.ext,"$.mark") + 0'] = (int)$this->request->param('mark');
  217. }
  218. if ($this->request->param('push_id')) {
  219. $condition_referral['JSON_EXTRACT(`orders`.ext,"$.push_id") + 0'] = (int)$this->request->param('push_id');
  220. }
  221. if ($this->request->param('push_idx')) {
  222. $condition_referral['JSON_EXTRACT(`orders`.ext,"$.push_idx") + 0'] = (int)$this->request->param('push_idx');
  223. }
  224. }
  225. if($groupId==3){ //渠道商
  226. $collect = model('AdminExtend')->where('create_by',$this->authId)->select();
  227. $conStr = '';
  228. if($collect){
  229. foreach($collect as $val){
  230. $conStr.=$val->admin_id.',';
  231. }
  232. $conStr .= $this->authId;
  233. }else{
  234. $conStr = $this->authId;
  235. }
  236. }elseif($groupId==4){ //代理商
  237. $condition['orders.admin_id'] = $this->authId;
  238. }
  239. //取代理商和渠道商的昵称
  240. $channels = model('admin')->join('auth_group_access a','a.uid= admin.id')->where('a.group_id','>',2)->select();
  241. $cNames = [];
  242. $qrImages = [];
  243. foreach ($channels as $channel) {
  244. if($channel->group_id == 3){
  245. $cNames['channel'][$channel->id] = $channel->nickname;
  246. $qrImages[$channel['id']]['cusername'] = $channel['username'];
  247. }else{
  248. $cNames['agent'][$channel->id] = $channel->nickname;
  249. }
  250. }
  251. //取渠道商的公众号二维码及admin_id
  252. $qrchannnels = model('admin_config')->join('auth_group_access a','a.uid = admin_config.admin_id')->where('a.group_id',3)->select();
  253. foreach ($qrchannnels as $qrchannnel){
  254. $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'];
  255. $qrImages[$qrchannnel['admin_id']]['cchannelid'] = $qrchannnel['admin_id'];
  256. }
  257. //取代理商和渠道商的对应关系
  258. $extend = model('adminExtend')->alias('e')->field('e.admin_id,e.create_by')->join('auth_group_access a',
  259. 'a.uid= e.admin_id')->where('a.group_id', '>', 2)->select();
  260. $agentBy = [];
  261. foreach ($extend as $key => $value) {
  262. $agentBy[$value['admin_id']] = $value['create_by'];
  263. }
  264. if($pay > 0){
  265. if($pay == 1){
  266. $condition['orders.state'] = '1';
  267. }elseif($pay == 2){
  268. $condition['orders.state'] = '0';
  269. }
  270. }
  271. $orderId = 0;
  272. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  273. // $sort = 'admin_id';
  274. $limit = 2000;
  275. $data = [];
  276. mb_convert_variables('GBK', 'UTF-8', $columns);
  277. $data[] = $columns;
  278. if($groupId == 3){
  279. $total = $this->model
  280. ->where($where)
  281. ->where($ext_var)
  282. ->where($condition_referral)
  283. ->where($condition_referral_or)
  284. ->where($condition_finishtime)
  285. ->where('admin_id','in',$conStr)
  286. ->count();
  287. $pages = ceil($total / $limit);
  288. for($i = 1; $i <= $pages; $i++) {
  289. $offset = 0;
  290. if ($i == 1){
  291. $list = $this->model
  292. //->join('referral r', 'r.id=orders.referral_id', 'left')
  293. //->join('book b', 'orders.book_id = b.id', 'left')
  294. ->field('orders.*')
  295. ->force('index_admin_deduct_state')
  296. ->where($where)
  297. ->where($ext_var)
  298. ->where($condition_referral)
  299. ->where($condition_referral_or)
  300. ->where($condition_finishtime)
  301. ->where('orders.admin_id', 'in', $conStr)
  302. ->order($sort, $order)
  303. ->limit($offset, $limit)
  304. ->select();
  305. }else{
  306. $list = $this->model
  307. //->join('referral r', 'r.id=orders.referral_id', 'left')
  308. //->join('book b', 'orders.book_id = b.id', 'left')
  309. ->field('orders.*')
  310. ->force('index_admin_deduct_state')
  311. ->where($where)
  312. ->where($ext_var)
  313. ->where('orders.id','<',$orderId)
  314. ->where($condition_referral)
  315. ->where($condition_referral_or)
  316. ->where($condition_finishtime)
  317. ->where('orders.admin_id', 'in', $conStr)
  318. ->order($sort, $order)
  319. ->limit($offset, $limit)
  320. ->select();
  321. }
  322. if ($list){
  323. $this->getBookReferralInfo($list);
  324. }
  325. $endArr = end($list);
  326. $orderId = $endArr['id'];
  327. $result = $this->getUserData($list,$cNames,$qrImages,$agentBy);
  328. $data = array_merge($data, $result);
  329. }
  330. }else{
  331. $total = $this->model
  332. ->where($where)
  333. ->where($ext_var)
  334. ->where($condition_referral)
  335. ->where($condition_referral_or)
  336. ->where($condition_finishtime)
  337. ->where($condition)
  338. ->count();
  339. $pages = ceil($total / $limit);
  340. for($i = 1; $i <= $pages; $i++) {
  341. $offset = 0;
  342. if ($i == 1){
  343. $list = $this->model
  344. //->join('referral r', 'r.id=orders.referral_id', 'left')->join('book b', 'orders.book_id = b.id', 'left')
  345. ->field('orders.*')
  346. ->force('index_admin_deduct_state')
  347. ->where($where)
  348. ->where($ext_var)
  349. ->where($condition_referral)
  350. ->where($condition_referral_or)
  351. ->where($condition_finishtime)
  352. ->where($condition)
  353. ->order($sort, $order)
  354. ->limit($offset, $limit)
  355. ->select();
  356. }else{
  357. $list = $this->model
  358. //->join('referral r', 'r.id=orders.referral_id', 'left')->join('book b', 'orders.book_id = b.id', 'left')
  359. ->field('orders.*')
  360. ->force('index_admin_deduct_state')
  361. ->where($where)
  362. ->where($ext_var)
  363. ->where($condition_referral)
  364. ->where($condition_referral_or)
  365. ->where($condition_finishtime)
  366. ->where($condition)
  367. ->where('orders.id','<',$orderId)
  368. ->order($sort, $order)
  369. ->limit($offset, $limit)
  370. ->select();
  371. }
  372. if ($list){
  373. $this->getBookReferralInfo($list);
  374. }
  375. $endArr = end($list);
  376. $orderId = $endArr['id'];
  377. $result = $this->getUserData($list,$cNames,$qrImages,$agentBy);
  378. $data = array_merge($data, $result);
  379. }
  380. }
  381. $fileName = "导出订单数据-".date('YmdHis', time()).".csv";
  382. // 头部标题
  383. $content = '';
  384. foreach ($data as $k => $v) {
  385. $content .= implode(',', $v) . PHP_EOL;
  386. }
  387. header('Expires: 0');
  388. header('Content-Encoding: utf-8');
  389. header("Content-type:text/csv; charset=utf-8");
  390. Header("Accept-Length: " . strlen($content));
  391. header("Content-Disposition:attachment;filename=" . $fileName);
  392. header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
  393. header('Pragma:public');
  394. echo $content;exit;
  395. }
  396. private function getBookReferralInfo(&$list)
  397. {
  398. $bookIds = array_unique( array_column($list,'book_id') );
  399. $booksInfo = [];
  400. $referralInfo = [];
  401. if ($bookIds){
  402. $booksInfo = model('book')->getBooksInfo($bookIds);
  403. }
  404. $referralIds = array_unique(array_column($list,'referral_id'));
  405. if ($referralIds){
  406. $referralInfo = model("Referral")->field('id,name,admin_id')->where('id', 'in', $referralIds)->select();
  407. $referralInfo = ArrayHelper::index($referralInfo, 'id');
  408. }
  409. foreach($list as $k => &$v){
  410. $v['pdqd'] = isset($referralInfo[$v['referral_id']])?$referralInfo[$v['referral_id']]['name']:'';
  411. $v['radmin_id'] = isset($referralInfo[$v['referral_id']])?$referralInfo[$v['referral_id']]['admin_id']:1;
  412. $v['bookname'] = isset($booksInfo[$v['book_id']])?$booksInfo[$v['book_id']]['name']:'';
  413. }
  414. }
  415. /**
  416. * @param $fp
  417. * @param $list
  418. * @param null $agentBy
  419. * @author 李聪聪
  420. */
  421. public function getUserData($list,$cNames=null,$qrImages=null,$agentBy=null){
  422. //支付状态
  423. $payStatus = ['0'=>'未支付','1'=>'已支付'];
  424. $orderIds = array_column($list, 'out_trade_no');
  425. $splitRows = model("PalmpaySplitAccountLog")->field('order_id')->where('order_id', 'in', $orderIds)->where('channel_amount', '>', 0)->select();
  426. if ($splitRows) {
  427. $splitRows = ArrayHelper::index($splitRows, 'order_id');
  428. }
  429. $mUser = model('user');
  430. $result = [];
  431. foreach ($list as $k => $v)
  432. {
  433. $newUser = $mUser->setConnect($v['user_id'])
  434. ->where(['id'=>$v['user_id']])
  435. ->field('id,channel_id,nickname,createtime,avatar,subscribe_time')
  436. ->select();
  437. if($newUser){
  438. $aUser = $newUser[0]->getData();
  439. unset($newUser);
  440. if(empty($cNames['agent'][$v['admin_id']])){
  441. $aUser['agent_id'] = '无';
  442. if(empty($cNames['channel'][$v['admin_id']])){
  443. $aUser['channel_id'] = '';
  444. }else{
  445. $aUser['channel_id'] = $cNames['channel'][$v['admin_id']];
  446. }
  447. //二维码图片
  448. if(empty($qrImages[$v['admin_id']])){
  449. $aUser['qrimage'] = '';
  450. $aUser['cusername'] = '';
  451. $aUser['cchannelid'] = '';
  452. }else{
  453. $aUser['qrimage'] = empty($qrImages[$v['admin_id']]['qrimage']) ? '':$qrImages[$v['admin_id']]['qrimage'];
  454. $aUser['cusername'] = empty($qrImages[$v['admin_id']]['cusername'])?'':$qrImages[$v['admin_id']]['cusername'];
  455. $aUser['cchannelid'] = empty($qrImages[$v['admin_id']]['cchannelid'])?'':$qrImages[$v['admin_id']]['cchannelid'];
  456. }
  457. }else{
  458. $aUser['agent_id'] = $cNames['agent'][$v['admin_id']];
  459. $aUser['channel_id'] = $cNames['channel'][$agentBy[$v['admin_id']]];
  460. if(empty($qrImages[$agentBy[$v['admin_id']]])){
  461. $aUser['qrimage'] = '';
  462. $aUser['cusername'] = '';
  463. $aUser['cchannelid'] = '';
  464. }else{
  465. $aUser['cusername'] = empty($qrImages[$agentBy[$v['admin_id']]]['cusername'])? '':$qrImages[$agentBy[$v['admin_id']]]['cusername'];
  466. $aUser['cchannelid'] = empty($qrImages[$agentBy[$v['admin_id']]]['cchannelid']) ? '':$qrImages[$agentBy[$v['admin_id']]]['cchannelid'];
  467. $aUser['qrimage'] = empty($qrImages[$agentBy[$v['admin_id']]]['qrimage'])? '': $qrImages[$agentBy[$v['admin_id']]]['qrimage'];
  468. }
  469. }
  470. //获取每列数据,转换处理成需要导出的数据
  471. $groupId = $this->groupId; //用户组
  472. //是否分账
  473. $split = '否';
  474. if ($splitRows && isset($splitRows[$v['out_trade_no']])) {
  475. $split = '是';
  476. }
  477. // '\t' 可以解决科学技术法问题
  478. $rowData = [
  479. '"'.$v['out_trade_no'].'"',
  480. empty($v['transaction_id'])?"无":"\t".$v['transaction_id']."\t",
  481. str_replace(["\n", "\t", ","], ["", "", ""], $aUser['nickname']."({$v['user_id']})"),
  482. $aUser['subscribe_time'] ? date('Y-m-d H:i:s', $aUser['subscribe_time']) : '-',
  483. date('Y-m-d H:i:s',$aUser['createtime']),
  484. $v['type_text'],
  485. $v['money'],
  486. $payStatus[$v['state']],
  487. date('Y-m-d H:i:s',$v['createtime']),
  488. empty($v['finishtime'])?'无':date('Y-m-d H:i:s',$v['finishtime']),
  489. $v['referral_id'],
  490. $v['pdqd'],
  491. $split,
  492. ];
  493. if (in_array($groupId, [
  494. AdminConstants::ADMIN_SUPER_MANAGER_ID,
  495. AdminConstants::ADMIN_GROUP_ID_ADMIN,
  496. AdminConstants::ADMIN_GROUP_ID_SETTLEMENT,
  497. AdminConstants::ADMIN_GROUP_ID_CHANNEL,
  498. ])) {
  499. array_push($rowData, $v['bookname']);
  500. }
  501. if (in_array($this->group, [
  502. AdminConstants::ADMIN_SUPER_MANAGER_ID,
  503. AdminConstants::ADMIN_GROUP_ID_ADMIN,
  504. AdminConstants::ADMIN_GROUP_ID_CHANNEL,
  505. AdminConstants::ADMIN_GROUP_ID_CUSTOMER_SERVICE,
  506. AdminConstants::ADMIN_GROUP_ID_SETTLEMENT,
  507. ])) {
  508. array_push($rowData,$aUser['agent_id'] ?? '无');
  509. }
  510. if (in_array($this->group, [
  511. AdminConstants::ADMIN_SUPER_MANAGER_ID,
  512. AdminConstants::ADMIN_GROUP_ID_ADMIN,
  513. AdminConstants::ADMIN_GROUP_ID_CUSTOMER_SERVICE,
  514. AdminConstants::ADMIN_GROUP_ID_SETTLEMENT,
  515. ])) {
  516. array_push($rowData,$aUser['channel_id'] ?? '');
  517. array_push($rowData,$aUser['cchannelid'] ?? '');
  518. array_push($rowData,$aUser['cusername'] ?? '');
  519. }
  520. //需要格式转换,否则会乱码
  521. mb_convert_variables('GBK', 'UTF-8', $rowData);
  522. // fputcsv($fp, $rowData);
  523. $result[] = $rowData;
  524. unset($rowData);
  525. }
  526. }
  527. unset($list);
  528. unset($mUser);
  529. unset($splitRows);
  530. unset($cNames);
  531. unset($qrImages);
  532. unset($agentBy);
  533. return $result;
  534. }
  535. /**
  536. * 查单
  537. */
  538. public function searchorder(){
  539. header("Cache-control:private");
  540. if($this->request->isPost()){
  541. $oid = $this->request->post('oid');
  542. $patten = array("\r\n", "\n", "\r");
  543. if(empty($oid)){
  544. $this->error('请填写数据');
  545. }
  546. $oid = str_replace($patten,',',$oid);
  547. $oidArr = explode(',',$oid);
  548. foreach ($oidArr as $key=>$val){
  549. $oidArr[$key] = trim($val);
  550. if(empty($val)){
  551. unset($oidArr[$key]);
  552. }
  553. }
  554. $orderRes = collection(model('Orders')->where('transaction_id','in',$oidArr)->select())->toArray();
  555. $wxpayList = OfficialAccountsService::instance()->getWxpayModel()->column("id,mcid,quartet_merchant_id");
  556. $wxpay = [];
  557. foreach($wxpayList as $item){
  558. $wxpay[$item['id']] = $item;
  559. }
  560. $res = [];
  561. if ($orderRes) {
  562. foreach ($orderRes as $val) {
  563. if (array_key_exists($val['wxpay_id'], $wxpay)) {
  564. $val['mcid'] = $wxpay[$val['wxpay_id']]['mcid'];
  565. if (array_key_exists($val['payment_method'], ApiConstants::$allPaymentMethods)) {
  566. $val['payinfo'] = ApiConstants::$allPaymentMethods[$val['payment_method']];
  567. } else {
  568. $val['payinfo'] = '微信支付 ';
  569. }
  570. if ($val['payment_method'] != ApiConstants::PAYMENT_METHOD_WECHAT) {
  571. $val['mcid'] = $wxpay[$val['wxpay_id']]['quartet_merchant_id'];
  572. }
  573. } else {
  574. $val['mcid'] = '';
  575. $val['payinfo'] = '';
  576. }
  577. array_push($res, $val);
  578. }
  579. }
  580. if (count($res) !== count($oidArr)){
  581. $tranArr = array_column($res, 'transaction_id');
  582. $arrDiff = array_diff($oidArr,$tranArr);
  583. foreach ($arrDiff as $val){
  584. array_push($res,['transaction_id'=>$val,'user_id'=>'未查到','wxpay_id'=>'未查到','mcid'=>'未查到','payinfo'=>'未查到']);
  585. }
  586. }
  587. $this->assign('res',$res);
  588. }else{
  589. $this->assign('res','');
  590. }
  591. return $this->view->fetch();
  592. }
  593. /**
  594. * 查看
  595. */
  596. public function index()
  597. {
  598. ini_set('memory_limit', '256M'); //内存限制
  599. //当前是否为关联查询
  600. $this->relationSearch = true;
  601. $pay = empty($this->request->get('pay'))? 0:intval($this->request->get('pay'));
  602. $groupId = $this->groupId; //用户组
  603. $condition = []; //条件数组
  604. $condition_referral_or = $condition_referral = []; //推广条件
  605. if(isset($_GET['ids'])){
  606. $ids = $_GET['ids'];
  607. // $condition_referral['orders.referral_id'] = $_GET['ids'];//推广id
  608. $condition_referral['orders.resource_id'] = [['exp','is null'],['=',0],'or'];//资源id
  609. $condition_referral_or = function ($query) use ($ids) {
  610. $query->where('orders.referral_id', $ids)
  611. ->whereOr('orders.referral_id_permanent', $ids);
  612. return $query;
  613. };
  614. }
  615. if($groupId==3){ //渠道商
  616. $collect = model('AdminExtend')->where('create_by',$this->authId)->select();
  617. $conStr = '';
  618. if($collect){
  619. foreach($collect as $val){
  620. $conStr.=$val->admin_id.',';
  621. }
  622. $conStr .= $this->authId;
  623. }else{
  624. $conStr = $this->authId;
  625. }
  626. }elseif($groupId==4){ //代理商
  627. $condition['orders.admin_id'] = $this->authId;
  628. }
  629. if ($this->request->isAjax())
  630. {
  631. $ext_var = '';
  632. if ($this->request->has('mark') || $this->request->param('push_id') || $this->request->param('push_idx')) {
  633. $ext_var = "orders.ext != '' AND JSON_VALID(ext)";
  634. if ($this->request->param('mark')) {
  635. $condition_referral['JSON_EXTRACT(`orders`.ext,"$.mark") + 0'] = (int)$this->request->param('mark');
  636. }
  637. if ($this->request->param('push_id')) {
  638. $condition_referral['JSON_EXTRACT(`orders`.ext,"$.push_id") + 0'] = (int)$this->request->param('push_id');
  639. }
  640. if ($this->request->param('push_idx')) {
  641. $condition_referral['JSON_EXTRACT(`orders`.ext,"$.push_idx") + 0'] = (int)$this->request->param('push_idx');
  642. }
  643. }
  644. if($pay > 0){
  645. if($pay == 1){
  646. $condition['orders.state'] = '1';
  647. }elseif($pay == 2){
  648. $condition['orders.state'] = '0';
  649. }
  650. }
  651. //提现详情页筛选时间
  652. $condition_finishtime = [];
  653. if(isset($_GET['t']) & !empty($_GET['t']) ){
  654. $time = explode('_',$_GET['t']);
  655. $condition_finishtime['orders.finishtime'] = array(array('egt',strtotime($time[0])),array('elt',strtotime($time[1])));
  656. if (abs(strtotime($time[1])-strtotime($time[0])) > (86400*31)) {
  657. $this->error("查询时间范围超过一个月", null, 13);
  658. }
  659. $conStr= intval($_GET['aid']);// 覆盖掉前面的admin_id
  660. //为管理员筛选渠道商订单详情
  661. if($groupId==2){
  662. $collect = model('AdminExtend')->where('create_by',$conStr)->field('admin_id')->select();
  663. $condition['orders.admin_id'] = '';
  664. if($collect){
  665. foreach($collect as $val){
  666. $search_id[]=$val->admin_id;
  667. }
  668. $search_id[]= $conStr;
  669. $condition['orders.admin_id'] = array('in',implode(',',$search_id));
  670. }else{
  671. $condition['orders.admin_id'] = array('in',$conStr);
  672. }
  673. }
  674. }
  675. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  676. $limit = 10;
  677. $filter = $this->request->get("filter", '{}');
  678. $filter = json_decode($filter, true);
  679. if (array_key_exists('orders.createtime', $filter) || array_key_exists('createtime', $filter)) {
  680. //判断时间区间
  681. if (isset($filter['orders.createtime'])) {
  682. $timeRange = explode(' - ',$filter['orders.createtime']);
  683. } else if (isset($filter['createtime'])) {
  684. $timeRange = explode(' - ',$filter['createtime']);
  685. }
  686. $stime = strtotime($timeRange[0]);
  687. $etime = strtotime($timeRange[1]);
  688. if (abs($etime-$stime) > (86400*31)) {
  689. $this->error("查询时间范围超过一个月", null, 13);
  690. }
  691. }
  692. if (in_array($groupId, [AdminConstants::ADMIN_GROUP_ID_CHANNEL, AdminConstants::ADMIN_GROUP_ID_AGENT])){
  693. $sort = 'orders.updatetime';
  694. } else {
  695. if ($filter) {
  696. $sort = 'orders.updatetime';
  697. } else {
  698. $sort = 'orders.createtime';
  699. }
  700. }
  701. $order = 'desc';
  702. if($groupId ==3){
  703. $total = $this->model
  704. ->where($where)
  705. ->where($ext_var)
  706. ->where($condition_referral)
  707. ->where($condition_referral_or)
  708. ->where($condition_finishtime)
  709. ->where('admin_id','in',$conStr)
  710. ->count();
  711. $list = $this->model
  712. ->where($where)
  713. ->where($ext_var)
  714. ->where($condition_referral)
  715. ->where($condition_referral_or)
  716. ->where($condition_finishtime)
  717. ->where('orders.admin_id','in',$conStr)
  718. ->order($sort, $order)
  719. ->limit($offset, $limit)
  720. ->select();
  721. }else{
  722. $total = $this->model
  723. ->where($where)
  724. ->where($ext_var)
  725. ->where($condition_referral)
  726. ->where($condition_referral_or)
  727. ->where($condition_finishtime)
  728. ->where($condition)
  729. ->count();
  730. $list = $this->model
  731. ->where($where)
  732. ->where($ext_var)
  733. ->where($condition_referral)
  734. ->where($condition_referral_or)
  735. ->where($condition_finishtime)
  736. ->where($condition)
  737. ->order($sort, $order)
  738. ->limit($offset, $limit)
  739. ->select();
  740. }
  741. $referralRows = [];
  742. $booksInfo = [];
  743. if ($list) {
  744. $referralIds = array_column($list, 'referral_id');
  745. $preferralIds = array_column($list, 'referral_id_permanent');
  746. $referralIds = array_unique(array_merge($referralIds, $preferralIds));
  747. if ($referralIds) {
  748. $referralRows = model("Referral")->field('id,name,admin_id')->where('id', 'in', $referralIds)->select();
  749. $referralRows = ArrayHelper::index($referralRows, 'id');
  750. }
  751. $bookIds = array_column($list, 'book_id');
  752. $bookIds = array_filter($bookIds);
  753. if (!empty($bookIds)) {
  754. $booksInfo = model('book')->getBooksInfo($bookIds);
  755. }
  756. $adminIds = array_column($list, 'admin_id');
  757. $agentRef = AdminService::instance()->getAdminExtendModel()
  758. ->alias('a')
  759. ->whereIn('admin_id', $adminIds)
  760. ->join('auth_group_access aga', 'aga.uid = a.create_by')
  761. ->where('aga.group_id', AdminConstants::ADMIN_GROUP_ID_CHANNEL)
  762. ->column('a.admin_id,a.create_by');
  763. $agentIds = array_keys($agentRef);
  764. $channelIds = array_merge(array_diff($adminIds, $agentIds), array_values($agentRef));
  765. $channelList = AdminService::instance()->getAdminConfigModel()
  766. ->alias('ac')
  767. ->join('admin a','a.id=admin_config.admin_id', 'RIGHT')
  768. ->whereIn('a.id', array_merge($agentIds, $channelIds))
  769. ->field('a.id,ac.json,a.nickname,a.username')
  770. ->select();
  771. $cNames = [];
  772. foreach ($channelList as $adminInfo) {
  773. if (in_array($adminInfo['id'], $channelIds)) {
  774. if ($adminInfo['qrcode_image']) {
  775. $qrImages[$adminInfo['id']]['qrimage'] = $adminInfo['qrcode_image'];
  776. } elseif ($adminInfo['json']['authorizer_info']['user_name']) {
  777. $qrImages[$adminInfo['id']]['qrimage'] = 'https://open.weixin.qq.com/qr/code?username=' . $adminInfo['json']['authorizer_info']['user_name'];
  778. } else {
  779. $qrImages[$adminInfo['id']]['qrimage'] = '';
  780. }
  781. $qrImages[$adminInfo['id']]['cchannelid'] = $adminInfo['id'];
  782. $qrImages[$adminInfo['id']]['cusername'] = $adminInfo['username'];
  783. }
  784. $cNames[$adminInfo['id']] = $adminInfo['nickname'];
  785. }
  786. }
  787. foreach ($list as $k => &$v)
  788. {
  789. unset($v['deduct']);
  790. $user = model('user')
  791. ->setConnect($v['user_id'])
  792. ->where(['id'=>$v['user_id']])
  793. ->field('id,channel_id,nickname,createtime,avatar,subscribe_time')
  794. ->select();
  795. if ($user) {
  796. $user = $user[0];
  797. }
  798. if(in_array($v['admin_id'], $channelIds)){
  799. $user['agent_id'] = '无';
  800. $user['channel_id'] = $cNames[$v['admin_id']];
  801. //二维码图片
  802. if(empty($qrImages[$v['admin_id']])){
  803. $user['qrimage'] = '';
  804. $user['cusername'] = '';
  805. $user['cchannelid'] = '';
  806. }else{
  807. $user['qrimage'] = empty($qrImages[$v['admin_id']]['qrimage']) ? '':$qrImages[$v['admin_id']]['qrimage'];
  808. $user['cusername'] = empty($qrImages[$v['admin_id']]['cusername'])?'':$qrImages[$v['admin_id']]['cusername'];
  809. $user['cchannelid'] = empty($qrImages[$v['admin_id']]['cchannelid'])?'':$qrImages[$v['admin_id']]['cchannelid'];
  810. }
  811. }else{
  812. $user['agent_id'] = $cNames[$v['admin_id']];
  813. $channelId = $agentRef[$v['admin_id']];
  814. $user['channel_id'] = $cNames[$channelId];
  815. if(empty($qrImages[$channelId])){
  816. $user['qrimage'] = '';
  817. $user['cusername'] = '';
  818. $user['cchannelid'] = '';
  819. }else{
  820. $user['cusername'] = empty($qrImages[$channelId]['cusername'])? '':$qrImages[$channelId]['cusername'];
  821. $user['cchannelid'] = empty($qrImages[$channelId]['cchannelid']) ? '':$qrImages[$channelId]['cchannelid'];
  822. $user['qrimage'] = empty($qrImages[$channelId]['qrimage'])? '': $qrImages[$channelId]['qrimage'];
  823. }
  824. }
  825. $v['user'] = $user;
  826. //组装推广链接信息
  827. if ($v['referral_id'] && isset($referralRows[$v['referral_id']])) {
  828. $v['pdqd'] = $referralRows[$v['referral_id']]['name'];
  829. $v['radmin_id'] = $referralRows[$v['referral_id']]['admin_id'];
  830. } elseif ($v['referral_id_permanent'] && $referralRows[$v['referral_id_permanent']]) {
  831. $v['pdqd'] = $referralRows[$v['referral_id_permanent']]['name'];
  832. $v['radmin_id'] = $referralRows[$v['referral_id_permanent']]['admin_id'];
  833. } else {
  834. $v['pdqd'] = null;
  835. $v['radmin_id'] = null;
  836. }
  837. //组装书籍名称
  838. if ($v['book_id'] && isset($booksInfo[$v['book_id']])) {
  839. $v['bookname'] = $booksInfo[$v['book_id']]['name'];
  840. } else {
  841. $v['bookname'] = '';
  842. }
  843. }
  844. unset($v);
  845. $result = array("total" => $total, "rows" => $list);
  846. return json($result);
  847. }
  848. $this->view->assign('pay',$pay);
  849. $isShowBusinessLine = true;
  850. if (in_array($this->group, [3, 4, 7, 8])) {
  851. $isShowBusinessLine = false;
  852. if (AppGuideService::instance()->isShowAdminMenu($this->auth->id)) {
  853. $isShowBusinessLine = true;
  854. }
  855. }
  856. $this->assignconfig('isShowBusinessLine', $isShowBusinessLine);
  857. return $this->view->fetch();
  858. }
  859. //管理员查kl单
  860. public function klorder(){
  861. if ($this->request->isAjax()) {
  862. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  863. $total = $this->model
  864. ->where($where)
  865. ->where('deduct', 1)
  866. ->count();
  867. $list = $this->model
  868. ->where($where)
  869. ->where('deduct', 1)
  870. ->order('id desc')
  871. ->limit($offset, $limit)
  872. ->select();
  873. foreach ($list as $k => &$v)
  874. {
  875. $adminData = model('Admin')->field('nickname')->where(['id' => $v['admin_id']]) ->find() -> toarray();
  876. $v['cnickname'] = $adminData['nickname'] ?? '';
  877. $v['user'] = model('user')->setConnect($v['user_id'])->where(['id'=>$v['user_id']])->find();
  878. }
  879. $result = array("total" => $total, "rows" => $list);
  880. return json($result);
  881. }
  882. $this->assignconfig('orderType',1);
  883. return $this->fetch();
  884. }
  885. }