<?php

namespace app\common\model\school\classes\order;

use app\common\model\BaseModel;
use app\common\model\dyqc\ManystoreShop;
use think\Model;
use traits\model\SoftDelete;

class OrderDetail extends BaseModel
{

    use SoftDelete;

    

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

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

    // 追加属性
    protected $append = [
        'add_type_text',
        'type_text',
        'address_type_text'
    ];





    public function getHeadimageAttr($value, $data)
    {
        if (!empty($value)) return cdnurl($value, true);
    }


    public function getImagesAttr($value, $data)
    {
        $imagesArray = [];
        if (!empty($value)) {
            $imagesArray = explode(',', $value);
            foreach ($imagesArray as &$v) {
                $v = cdnurl($v, true);
            }
            return $imagesArray;
        }
        return $imagesArray;
    }


    
    public function getAddTypeList()
    {
        return ['1' => __('Add_type 1'), '2' => __('Add_type 2')];
    }

    public function getTypeList()
    {
        return ['out' => __('Type out'), 'in' => __('Type in')];
    }

    public function getAddressTypeList()
    {
        return ['1' => __('Address_type 1'), '2' => __('Address_type 2')];
    }


    public function getAddTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['add_type']) ? $data['add_type'] : '');
        $list = $this->getAddTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAddressTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['address_type']) ? $data['address_type'] : '');
        $list = $this->getAddressTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


           public function setImagesAttr($value, $data)
    {
        $imagesArray = $value;
        if (!empty($value) && is_array($value)) {
            //转成逗号拼接字符串
            $imagesArray = implode(',', $value);
        }
        return $imagesArray;
    }



    public function classorder()
    {
        return $this->belongsTo(Order::class, 'classes_order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function manystore()
    {
        return $this->belongsTo('app\admin\model\Manystore', 'manystore_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function shop()
    {
        return $this->belongsTo(ManystoreShop::class, 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function teacher()
    {
        return $this->belongsTo('app\common\model\school\classes\Teacher', 'teacher_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
