OrderModel.php 3.9 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];
        }
        $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)
    {
        $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';
        if ($orderInfo['payment'] == 0) {
            if (Db::name('user')->where(['id' => $orderInfo['user_id']])->setDec('balance', $orderInfo['order_amount'])) {
                $wallet = [
                    'user_id' => $orderInfo['user_id'],
                    'type' => 0,
                    'cost' => $orderInfo['order_amount'],
                    'create_time' => time()
                ];
                if (Db::name('my_wallet')->insert($wallet)) {
                    $go = 'commit';
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        if ($orderInfo['is_use_discount_coupon'] == 1 && !empty($orderInfo['discount_coupon_id'])) {
            $coupons = [
                'id' => $orderInfo['discount_coupon_id'],
                'use_time' => time(),
                'status' => 2
            ];
            if (Db::name('discount_coupon')->update($coupons)) {
                $go = 'commit';
            } else {
                return false;
            }
        }
        //订单状态无定金直接变为3,有定金变为1
        if ($orderInfo['status'] == 0) {
            if ($orderInfo['order_type'] == 0) {
                $status = 3;
            } elseif ($orderInfo['order_type'] == 1) {
                $status = 1;
            } 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 ($go == 'commit') {
            Db::commit();
            return true;
        } else {
            Db::rollback();
            return false;
        }
    }

}