OrderModel.php 4.1 KB
<?php
/**
 * Created by PhpStorm.
 * User: yhbr
 * Date: 2018/9/5
 * Time: 15:03
 */
namespace app\order\model;
use think\Model;
use think\Db;

class OrderModel extends Model
{
    //暂定
    public function getOrderListByUserId($status, $orderSn, $userId)
    {
        $where['user_id'] = ['eq', $userId];
        if ($orderSn != null) {
            $where['order_sn'] = ['like', "%$orderSn%"];
        }
        if ($status != null) {
            $where['status'] = ['eq', $status];
        }else {
            $where['status'] = ['neq', 7];
        }
        $res = Db::name('order_info')->alias('o')
            ->field('o.id as oid,o.order_sn,a.name,a.thumb,o.status,a.down_price,s.price')
            ->join('activity a', 'a.id=o.activity_id')
            ->join('activity_schedule s', 's.id=o.schedule_id')
            ->where($where)
            ->order('add_time DESC')
            ->select()
            ->toArray();
        foreach ($res as $k => $v) {
            $v['status_text'] = getOrderStatusText($v['status']);
            $v['count'] = Db::name('order_detail')->where(['oid' => $v['oid']])->count();
            $res[$k] = $v;
        }
        return $res;
    }

    //通用订单回调类(未完待续。。。)
    //余额扣除(如果余额支付->钱包记录)->改变订单状态(必然)->相应批次库存减少(必然)
    public function orderCallBack($oid, $order_amount = null)
    {
        $orderInfo = Db::name('order_info')->alias('o')
            ->field('o.user_id,o.is_use_discount_coupon,discount_coupon_id,o.status,o.payment,o.order_type,o.activity_id,o.schedule_id,l.order_amount')
            ->join('order_log l', 'l.oid=o.id')
            ->where(['o.id' => $oid])
            ->find();
        Db::startTrans();
        $go = 'rollback';
        $amount = ($order_amount == null) ? $orderInfo['order_amount'] : $order_amount;
        if ($orderInfo['payment'] == 0) {
            if (Db::name('user')->where(['id' => $orderInfo['user_id']])->setDec('balance', $amount)) {
                $wallet = [
                    'user_id' => $orderInfo['user_id'],
                    'type' => 0,
                    'cost' => $amount,
                    'create_time' => time()
                ];
                if (Db::name('my_wallet')->insert($wallet)) {
                    $go = 'commit';
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        //订单状态无定金直接变为4,有定金变为2
        if ($orderInfo['status'] == 1) {
            if ($orderInfo['order_type'] == 0) {
                $status = 4;
            } elseif ($orderInfo['order_type'] == 1) {
                $status = 2;
            } else {
                $status = 0;
            }
            $order = [
                'id' => $oid,
                'status' => $status
            ];
            if (Db::name('order_info')->update($order)) {
                //相应批次报名数增加
                $dec = Db::name('order_detail')->where(['oid' => $oid])->count();
                if (Db::name('activity_schedule')->where(['id' => $orderInfo['schedule_id']])->setInc('real_join_num', $dec)) {
                    $go = 'commit';
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        //待付尾款订单
        if($orderInfo['status'] == 3) {
            $status = 4;
            $order = [
                'id' => $oid,
                'status' => $status
            ];
            if(Db::name('order_info')->update($order)) {
                $go = 'commit';
            }else {
                return false;
            }
        }
        if ($go == 'commit') {
            Db::commit();
            if($orderInfo['payment'] == 1) {
                echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
                exit();
            }
            return true;
        } else {
            Db::rollback();
            return false;
        }
    }
}