|
- <?php
- require_once '../st-xm/Common/Mysql.php';
- header("Access-Control-Allow-Origin:*");
- //二维数组去除重复
- function array_unique_fb($array2D)
- {
- foreach ($array2D as $v)
- {
- $v = join(",",$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
- $temp[] = $v;
- }
- $temp = array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
- foreach ($temp as $k => $v)
- {
- $array = explode(",",$v); //再将拆开的数组重新组装
- $temp2[$k]['line_id']=$array[0];
- $temp2[$k]['run_id']=$array[1];
- $temp2[$k]['run_date']=$array[2];
- $temp2[$k]['seq_id']=$array[3];
- $temp2[$k]['num1']=$array[4];
- }
- return $temp2;
- }
- //把数组key值改为run_id+seq_id
- function change_key($a)
- {
- $tmp = array();
- foreach ($a as $k => $v) {
- $tmpk = $v['run_id'] . '-' . $v['seq_id'];
- $tmp["$tmpk"] = $v;
- $tmp["$tmpk"]['id'] = $k;
- }
- return $tmp;
- }
-
- //合并数组
- function multi_merge($a, $b) //$a 实际订单数据,$b记录的数值
- {
-
- $arr = array();
-
- foreach ($a as $k => $r) {
- if (isset($b[$k])) {
- $arr[$k] = array_merge($r, $b[$k]);
- } else {
- $arr[$k] = $r;
- $arr[$k]['num2'] = 0;
- }
-
- }
-
- foreach ($b as $k => $r) {
- if (!isset($arr[$k])) {
- $arr[$k] = $r;
- $arr[$k]['num1'] = 0;
- }
-
- }
-
-
- return $arr;
- }
-
- $pdo = conn1();
- //从订单角度得到run_stock表的数据
- $sql1 = "SELECT
- l.line_id,
- m.run_id,
- m.prod_start_station_seq_id,
- m.prod_end_station_seq_id,
- m.run_date,
- count(m.order_id) as num1
- FROM
- order_main m
- LEFT JOIN opera_line l ON m.PARENT_PROD_ID = l.line_id
- LEFT JOIN opera_tickets t on m.prod_id=t.TICKET_ID
- LEFT JOIN opera_line w on t.line_id=w.line_id
- WHERE
- m.PARENT_ORDER_ID <> 0
- AND m.run_date >= '2017-02-05'
- AND m.ORDER_VALID_STATUS = 1
- AND m.CANCEL_FLAG = 0
- AND m.PROD_ID <> 380001
- AND w.line_type in (255,256)
- AND m.ORDER_PROD_TYPE = 82
- group by run_id,prod_start_station_seq_id,prod_end_station_seq_id
- order by m.run_date,m.run_id,m.prod_start_station_seq_id;";
-
- $res1 = $pdo->query($sql1);
- $res1->setFetchMode(PDO::FETCH_ASSOC);
- $arr1 = $res1->fetchAll();
-
-
- //run_stock表本身的数据
- $sql2 = "SELECT
- b.prod_id as line_id,
- b.run_id,
- a.seq_id,
- b.run_date,
- a.saled_count as num2
- FROM
- run_stock a
- LEFT JOIN run_main b ON a.run_id = b.run_id
- WHERE
- b.run_date >= '2017-02-05'
- and a.CANCEL_FLAG=0
- and a.SEAT_TYPE=72
- AND b.PROD_ID <> 0
- and (select sum(SALED_COUNT) as co from run_stock where run_id=b.run_id and CANCEL_FLAG=0)>0
- order by b.run_date,b.run_id;";
-
- $res2 = $pdo->query($sql2);
- $res2->setFetchMode(PDO::FETCH_ASSOC);
- $arr2 = $res2->fetchAll();
-
-
- #对arr1做处理,根据分段合并数据
- function merge($a,$b){
- $arr=array();
- foreach($b as $k=>$v){
-
- //$tmp=$v['run_id'].'-'.$v['prod_end_station_seq_id'];
- $num=0;
- foreach($a as $k2=>$v2){
- if($v['run_id']==$v2['run_id']){
- if($v['seq_id']>=$v2['prod_start_station_seq_id']&&$v['seq_id']<$v2['prod_end_station_seq_id']){
- $num+=$v2['num1'];
- }
- }
- }
- $arr[$k]['line_id']=$v['line_id'];
- $arr[$k]['run_id']=$v['run_id'];
- $arr[$k]['run_date']=$v['run_date'];
- $arr[$k]['seq_id']=$v['seq_id'];
- $arr[$k]['num1']=$num;
-
- }
- return array_unique_fb($arr);//二维数组去重
- }
- $arr1=merge($arr1,$arr2);
-
- /*print_r($arr1);
- print_r($arr3);
- exit;*/
- //切换key值
- $arr1 = change_key($arr1);
- //print_r($arr1);
- $arr2 = change_key($arr2);
- /*print_r($arr1);
- print_r($arr2);
- exit;*/
-
- $arr = multi_merge($arr1, $arr2);//合并二维数组
-
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>run_stock数据检测</title>
- <style type="text/css">
- td {
- border: 1px solid;
- }
- </style>
- </head>
- <body>
- <h2 style="margin-left:20%">run_stock数据检测</h2>
- <table width="60%">
- <tr>
- <td width="10%">ID</td>
- <td width="10%">出发日期run_date</td>
- <td width="10%">班次run_id</td>
- <td width="10%">排序(分段)seq_id</td>
- <td width="10%">实际订单数量</td>
- <td width="10%">记录已售数量</td>
- </tr>
- <?php $i = 1;
- foreach ($arr as $k => $v) { ?>
- <tr style="<?php if ($v['num1'] != $v['num2']) {
- echo 'color:red';
- } ?>">
- <td><?php echo $i; ?></td>
- <td><?php echo $v['run_date']; ?></td>
- <td><?php echo $v['run_id']; ?></td>
- <td><?php echo $v['seq_id']; ?></td>
- <td><?php echo $v['num1']; ?></td>
- <td><?php echo $v['num2']; ?></td>
- </tr>
- <?php $i++;
- } ?>
- </table>
- </body>
- </html>
|