OrderItem.php 5.9 KB
<?php

namespace app\admin\model\groupon\order;

use think\Model;
use traits\model\SoftDelete;
use addons\groupon\library\traits\model\order\OrderItemStatus;

class OrderItem extends Model
{

    use OrderItemStatus, SoftDelete;

    

    // 表名
    protected $name = 'groupon_order_item';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'dispatch_status_text',
        'comment_status_text',
        'activity_type_text',
        'refund_status_text',
        'status_code',
        'status_name',
        'status_desc',
        'btns',
        'ext_arr'
    ];

    // 发货状态
    const DISPATCH_STATUS_NOSEND = 0;       // 未发货
    const DISPATCH_STATUS_READY = 1;        // 已备货
    const DISPATCH_STATUS_ARRIVE = 2;       // 已到货
    const DISPATCH_STATUS_GETED = 3;        // 已提货


    // 退款状态
    const REFUND_STATUS_NOREFUND = 0;       // 退款状态 未申请
    const REFUND_STATUS_ING = 1;       // 申请中         // 不需要申请(状态不会出现)
    const REFUND_STATUS_OK = 2;       // 已同意
    const REFUND_STATUS_FINISH = 3;       // 退款完成

    // 评价状态
    const COMMENT_STATUS_NO = 0;       // 待评价
    const COMMENT_STATUS_OK = 1;       // 已评价

    public function getExtArrAttr($value, $data)
    {
        return (isset($data['ext']) && $data['ext']) ? json_decode($data['ext'], true) : [];
    }
    
    public function getDispatchStatusList()
    {
        return ['0' => __('Dispatch_status 0'), '1' => __('Dispatch_status 1'), '2' => __('Dispatch_status 2'), '3' => __('Dispatch_status 3')];
    }

    public function getCommentStatusList()
    {
        return ['0' => __('Comment_status 0'), '1' => __('Comment_status 1')];
    }

    public function getRefundStatusList()
    {
        return ['0' => __('Refund_status 0'), '1' => __('Refund_status 1'), '2' => __('Refund_status 2'), '3' => __('Refund_status 3')];
    }


    public function getDispatchStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['dispatch_status']) ? $data['dispatch_status'] : '');
        $list = $this->getDispatchStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getActivityTypeList()
    {
        return ['seckill' => __('Activity_type seckill'), 'groupon' => __('Activity_type groupon')];
    }

    public function getActivityTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['activity_type']) ? $data['activity_type'] : '');
        $list = $this->getActivityTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCommentStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['comment_status']) ? $data['comment_status'] : '');
        $list = $this->getCommentStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRefundStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_status']) ? $data['refund_status'] : '');
        $list = $this->getRefundStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatus($data, $type)
    {
        $btns = [];
        $status_name = '';
        $status_desc = '';

        $status_code = $this->status_code;

        $item_code = 'null';        // 有售后的时候,第二状态
        if (strpos($status_code, '|') !== false) {
            $codes = explode('|', $status_code);
            $status_code = $codes[0] ?? 'null';
            $item_code = $codes[1] ?? 'null';
        }

        switch ($status_code) {
            case 'null':
            case 'cancel':
            case 'invalid':
            case 'nopay':
                // 未支付的返回空
                break;
            case 'nosend':
                $status_name = '待备货';
                $status_desc = '请尽快安排备货';

                $btns[] = 'refund';          // 退款
                break;
            case 'noarrive':
                $status_name = '已备货';
                $status_desc = '平台已备货,等待自提点签收';
                $btns[] = 'refund';          // 退款
                break;
            case 'noget':
                $status_name = '待取货';
                $status_desc = '商品已到达指定自提点,等待用户取货';
                
                $btns[] = 'refund';          // 售后
                break;
            case 'nocomment':
                $status_name = '待评价';
                $status_desc = '等待买家评价';
                $btns[] = 'refund';          // 退款
                break;
            case 'commented':
                $status_name = '已评价';
                $status_desc = '订单已评价';
                $btns[] = 'refund';          // 退款
                break;
            case 'refund_finish':
                $status_name = '退款完成';
                $status_desc = '订单退款完成';
                break;
            case 'refund_ing':      // 不需要申请退款(状态不会出现)
                $status_name = '退款处理中';
                $status_desc = '退款处理中';
                $btns[] = 'refund';           // 退款
                break;
        }

        return $type == 'status_name' ? $status_name : ($type == 'btns' ? $btns : $status_desc);
    }


    // 商家配送, 到店自提的门店
    public function store()
    {
        return $this->belongsTo(\app\admin\model\groupon\store\Store::class, 'store_id');
    }

    public function order()
    {
        return $this->belongsTo(\app\admin\model\groupon\order\Order::class, 'order_id');
    }

}