<?php

namespace app\common\model;


use think\Model;

class TicketOrder extends Model
{

    protected $name = 'ticket_order';

    /**
     * 取单条订单信息
     * @access public
     * @author csdeshang
     * @param type $condition 条件
     * @param type $fields 字段
     * @return type
     */
    public function detail($condition = [], $fields = '*')
    {
        $order_info = $this->field($fields)->where($condition)->find();
        if (empty($order_info)) {
            return [];
        }
        if (isset($order_info['order_state'])) {
            $state_desc                     = $this->_state($order_info['order_state']);
            $order_info['state_desc']       = $state_desc[0];
            $order_info['order_state_text'] = $state_desc[1];
        }
        if (isset($order_info['payment_code'])) {
            $order_info['payment_name'] = get_order_payment_name($order_info['payment_code']);
        }
        return $order_info;
    }

    /**
     * 更改订单信息
     * @access public
     * @author csdeshang
     * @param type $data 数据
     * @param type $condition 条件
     * @param type $limit 限制
     * @return type
     */
    public function edit($data, $condition, $limit = 0)
    {
        return $this->where($condition)->limit($limit)->update($data);
    }

    /**
     * 取得订单状态文字输出形式
     * @access public
     * @author csdeshang
     * @param type $order_state 订单状态
     * @return type
     */
    private function _state($order_state)
    {
        switch ($order_state) {
            case ORDER_STATE_CANCEL:
                $order_state      = '<span style="color:#999">已取消</span>';
                $order_state_text = '已取消';
                break;
            case ORDER_STATE_NEW:
                $order_state      = '<span style="color:#36C">待付款</span>';
                $order_state_text = '待付款';
                break;
            case ORDER_STATE_PAY:
                $order_state      = '<span style="color:#999">已支付</span>';
                $order_state_text = '已支付';
                break;
            case ORDER_STATE_SUCCESS:
                $order_state      = '<span style="color:#999">已完成</span>';
                $order_state_text = '已完成';
                break;
        }
        return [$order_state, $order_state_text];;
    }
}
