如果一些复杂的数据查询不好用数组,那就用字符串拼接,灵活方便

/**
 * 查询订单是否过期
 */
public function checkOrderStatus() {
    if (!$group_order_id = $_POST['group_order_id']) {
        $this->json->printOutError('缺少参数', 10001);
    }
    $group_order      = M('group_order');
    $group_order_info = $group_order->where(['id' => $group_order_id])->find();
    if (!$group_order_info) {
        $this->json->printOutError('订单信息不存在', 10002);
    }
    if ((int)$group_order_info['ugroup_inc_id'] === 0) { // 发起团
        $leader_order_id = $group_order_id;
    } else {
        $leader_order_id = $group_order_info['ugroup_inc_id'];
    }
    // 获取相关团的数据
    $capture_group_order = $group_order
        ->where('is_pay = 1 and status = 1 and expired_time < '.time().' and ( id = '.$leader_order_id.' or ugroup_inc_id ='.$leader_order_id.' )')
        ->select();
//        setlog($group_order->getLastSql());

    // todo改造,成功一条,commit一条
    $error_msg_arr = [];
    $success_count = 0;
    $fail_count = 0;
    if ($capture_group_order) {
        $group_purchase = M('group_purchase');
        $group_reward   = M('group_reward');
        vendor('Func.WxPay');
        foreach ($capture_group_order as $k=>$v) {
            M()->startTrans();
            // step1 修改订单状态
            $edit_data = [
                'status' => -2,
                'updatetime' => time()
            ];
            $edit_group_order_flag = $group_order
                ->where(['id'=>$v['id']])
                ->save($edit_data);
            if ($edit_group_order_flag === false) {
                M()->rollback();
                $error_msg_arr[] = date('Y-m-d H:i:s').'-清理过期时间未拼团成功订单:修改状态不成功';
                $fail_count++;
                continue;
            }

            // step2 撤回已占用库存
            $group_flag = $group_purchase->where(['id'=>$v['group_id']])->setDec('buyed_stock');
            if ($group_flag === false) {
                M()->rollback();
                $error_msg_arr[] = date('Y-m-d H:i:s').'-清理未支付订单:修改库存失败';
                $fail_count++;
                continue;
            }

            // step3 撤回已占用惊喜金额
            if ((int)$v['reward_id'] > 0) {
                $reward_edit_data  = [
                    'status' => 1
                ];
                $group_reward_flag = $group_reward->where(['id' => $v['reward_id']])->save($reward_edit_data);
                if ($group_reward_flag === false) {
                    M()->rollback();
                    $error_msg_arr[] = date('Y-m-d H:i:s').'-清理未支付订单:修改惊喜失败';
                    $fail_count++;
                    continue;
                }
            }
            // step4 退回付款金额
            $refund_result = WxPay::refundOrder($v['billno'], $v['payed_price'], $v['payed_price']);
            if ((int)$refund_result['num'] === 1) {
                $refund_order_edit_data = [
                    'refund_transaction_id' => $refund_result['refund_id'],
                    'refund_order_sn'       => $refund_result['refund_no'],
                    'is_refund'             => 1,
                ];
                $refund_order_edit_flag = $group_order->where(['id' => $v['id']])->save($refund_order_edit_data);
                if ($refund_order_edit_flag === false) { // 如果失败,再改一次
                    $refund_order_edit_data = [
                        'is_refund'             => 1,
                    ];
                    $group_order->where(['id' => $v['id']])->save($refund_order_edit_data);
                }
            } else {
                M()->rollback();
                $error_msg_arr[] = date('Y-m-d H:i:s').'-清理未支付订单:退款失败'.$refund_result['desc'];
                $fail_count++;
                continue;
            }
            M()->commit();
            $success_count++;
            $error_msg_arr[] = date('Y-m-d H:i').'-清理未支付订单:订单号,'.$v['billno'].'拼团失败,退款成功';
        }
        $error_msg_arr[] = date('Y-m-d H:i').'-清理过期时间未拼团成功订单:本次共清理'.count($capture_group_order).'个拼团失败订单'.',成功了'.$success_count.',失败了'.$fail_count.'个'.PHP_EOL;
        setlog($_POST,$error_msg_arr,__METHOD__);
        // 再次查询状态
        $group_order_info = $group_order->where(['id' => $group_order_id])->find();
    }
    $this->json->printOutSuccess($group_order_info);
}

多尝试sql拼接,很好的解决复杂的业务问题。记得多打log测试。

posted @ 2019-05-29 20:06  TBHacker  阅读(267)  评论(0编辑  收藏  举报