订单数据量很大的情况临时需求加上定时统计数据

<?php
/**
 * Created by PhpStorm.
 * User: 捉泥鳅
 * Date: 2019/3/4
 * Time: 14:42
 */

namespace app\api\controller;
use think\Db;



class EmployOrderCount
{

    function index()
    {
        $select_where['order_pay_status'] = 2;
        $select_where['update_time'] = null;
        $order_arr['order_id'] = 'asc';
        $limit_str = 500;
        $stime = '2018-09-01';
        $info_update = Db::query('SELECT train_company_id,FROM_UNIXTIME(UNIX_TIMESTAMP(order_create_date) , "%Y-%m-%d") as date,order_id,order_create_date,order_emp_name,order_emp_id,order_pay,train_company_name  FROM `d_emp_order`   WHERE `order_pay_status` = ? AND update_time IS  NULL  AND order_create_date > ? ORDER BY order_id asc limit ?' ,[2,$stime,$limit_str]);
       $order_id_arr =  array_map(function($v){return $v['order_id'];},$info_update);
       Db::startTrans();
       try {
            db('d_emp_order')->whereIn("order_id", $order_id_arr)->update(['update_time' => time()]);
            $order_emp = [];
            // echo json_encode($info_update);exit;
            foreach ($info_update as $order) {
                // db('d_emp_order')->where(['order_id'=>$order['order_id']])->update(['update_time'=>time()]);
                $date_form = $order['date'];
                $emp_id = $order['order_emp_id'];
                if (empty($order_emp[$emp_id][$date_form])) {
                    $order_emp[$emp_id][$date_form] = $order;
                    $order_emp[$emp_id][$date_form]['order_num'] = 1;
                    $order_emp[$emp_id][$date_form]['date'] = $date_form;

                } else {
                    $order_emp[$emp_id][$date_form]['order_num'] += 1;
                    $order_emp[$emp_id][$date_form]['order_pay'] = $order_emp[$emp_id][$date_form]['order_pay'] + $order['order_pay'];
                }
            }
            foreach ($order_emp as $key => $val) {
                foreach ($val as $k => $v) {
                    //  echo $k.$key;exit;
                    $is_exit = Db::table('order_sum')->where(['date' => $k, 'emp_id' => $key, 'emp_name' => $v['order_emp_name']])->find();
                    if (!empty($is_exit)) {
                        // echo 99999;
                        $data['order_num'] = $is_exit['order_num'] + $v['order_num'];
                        $data['sum_pay'] = $is_exit['sum_pay'] + $v['order_pay'];
                        $data['company_name'] = $v['train_company_name'];
                        $data['company_id'] = $v['train_company_id'];
                        $data['order_create_date'] = $v['order_create_date'];
                        $data['update_time'] = time();
                        $data['emp_id'] = $v['order_emp_id'];
                        $data['emp_name'] = $v['order_emp_name'];
                        $data['date'] = $v['date'];
                        $data['last_order_id'] = $v['order_id'];
                        Db::table('order_sum')->where(['id' => $is_exit['id']])->update($data);
                    } else {
                        $data['order_num'] = $v['order_num'];
                        $data['emp_id'] = $v['order_emp_id'];
                        $data['emp_name'] = $v['order_emp_name'];
                        $data['sum_pay'] = $v['order_pay'];
                        $data['company_name'] = $v['train_company_name'];
                        $data['company_id'] = $v['train_company_id'];
                        $data['order_create_date'] = $v['order_create_date'];
                        $data['update_time'] = time();
                        $data['add_time'] = time();
                        $data['date'] = $v['date'];
                        $data['last_order_id'] = $v['order_id'];
                        Db::table('order_sum')->insert($data);
                    }
                }
            }
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
        }

    }

    

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值