<?php

namespace backend\modules\shop\models\ars;

use common\models\User;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "ats_order".
 *
 * @property int $id
 * @property int $user_id 用户id
 * @property string $order_sn 订单号
 * @property string $invoice_id 发票单号
 * @property int $status 状态
 * @property int $type 类型
 * @property int $goods_count 商品数量
 * @property int $goods_amount 商品金额
 * @property int $shipping_amount 物流金额
 * @property int $shipping_type 物流类型
 * @property string $consignee 收件人
 * @property string $phone 手机号码
 * @property string $province 省份
 * @property string $city 城市
 * @property string $area 区域
 * @property int $taking_site 自提点
 * @property int $pay_type 支付方式
 * @property int $pay_at 支付时间
 * @property string $payment_sn 付款单号
 * @property int $payment_amount 支付金额
 * @property int $receivables 应收款
 * @property string $remarks 备注
 * @property int $discount_amount 折扣金额
 * @property string $discount_description 折扣说明
 * @property int $updated_at 更新时间
 * @property int $created_at 创建时间
 * @property string $address 详细地址
 */
class Order extends \yii\db\ActiveRecord
{
    const TYPE_SHOPPING = 1;//普通购物订单

    const SHIPPING_TYPE_VIRTUAL_GOODS = 0;//虚拟货物
    const SHIPPING_TYPE_PICKED_UP = 1;//自提
    const SHIPPING_TYPE_EXPRESS = 2;//快递物流

    const STATUS_UNCONFIRMED = 0;
    const STATUS_NONPAYMENT = 1;
    const STATUS_CANCEL = 2;
    const STATUS_PAYMENT_TO_BE_CONFIRMED = 3;
    const STATUS_TO_BE_SHIPPING = 6;
    const STATUS_APPLY_REFUND = 4;
    const STATUS_REFUND = 5;
    const STATUS_SHIPMENT_ALL = 7;
    const STATUS_SHIPMENT_PORTION = 8;
    const STATUS_FINISH = 9;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'ats_order';
    }

    public function fields()
    {
        $fields = parent::fields();
        unset($fields['user_id']);
        unset($fields['payment_sn']);
        return $fields;
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_id', 'status', 'type', 'goods_count', 'goods_amount', 'shipping_amount', 'shipping_type', 'taking_site', 'pay_type', 'pay_at', 'payment_amount', 'receivables', 'discount_amount'], 'integer'],
            [['discount_description', 'address'], 'string'],
            [['order_sn', 'invoice_id'], 'string', 'max' => 64],
            [['consignee', 'phone'], 'string', 'max' => 20],
            [['province', 'city', 'area'], 'string', 'max' => 10],
            [['payment_sn'], 'string', 'max' => 120],
            [['remarks'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'id',
            'user_id' => '用户id',
            'order_sn' => '订单号',
            'invoice_id' => '发票单号',
            'status' => '状态',
            'type' => '类型',
            'goods_count' => '商品数量',
            'goods_amount' => '商品金额',
            'shipping_amount' => '物流金额',
            'shipping_type' => '物流类型',
            'consignee' => '收件人',
            'phone' => '手机号码',
            'province' => '省份',
            'city' => '城市',
            'area' => '区域',
            'taking_site' => '自提点',
            'pay_type' => '支付方式',
            'pay_at' => '支付时间',
            'payment_sn' => '付款单号',
            'payment_amount' => '支付金额',
            'receivables' => '应收款',
            'remarks' => '备注',
            'discount_amount' => '折扣金额',
            'discount_description' => '折扣说明',
            'updated_at' => '更新时间',
            'created_at' => '创建时间',
            'address' => '详细地址',
        ];
    }

    public function beforeSave($insert)
    {
        $address = Yii::createObject([
            'class' => 'api\logic\AddressLogic',
        ]);
        if ($insert) {
            $default = $address->getDefault();
            $this->province = $default['province'] ?? '';
            $this->city = $default['city'] ?? '';
            $this->area = $default['area'] ?? '';
        }
        if (!$insert && $this->status == self::STATUS_UNCONFIRMED && $this->type == self::TYPE_SHOPPING) {
            $express = Yii::createObject([
                'class' => 'api\logic\ExpressLogic',
            ]);
            $this->shipping_amount = $express->countShippingAmount($this);
            $this->receivables = $this->goods_amount + $this->shipping_amount;
            $this->payment_amount = $this->receivables - $this->discount_amount;
        }
        return parent::beforeSave($insert);
    }

    /**
     * @author linyao
     * @email 602604991@qq.com
     * @created Nov 8, 2019
     *
     * 行为存储创建时间和更新时间
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => 'updated_at',
                'value' => function () {
                    return time();
                },
            ],
        ];
    }

    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    public function getProvinceId()
    {
        return $this->hasOne(Province::className(), ['province_id' => 'province']);
    }

    public function getCityId()
    {
        return $this->hasOne(City::className(), ['city_id' => 'city']);
    }
    public function getAreaId()
    {
        return $this->hasOne(Area::className(), ['area_id' => 'area']);
    }

    /**
     * @param $column
     * @param null $value
     * @return bool
     * 获取各状态数组
     */
    public static function dropDown($column, $value = null)
    {
        $dropDownList = [
            'shipping_type' => [
                self::SHIPPING_TYPE_VIRTUAL_GOODS => '虚拟货物',
                self::SHIPPING_TYPE_PICKED_UP => '自提',
                self::SHIPPING_TYPE_EXPRESS => '快递物流',
            ],
            'type' => [
                self::TYPE_SHOPPING => '普通购物订单',
            ],
            'status' => [
                self::STATUS_UNCONFIRMED => '待确认',
                self::STATUS_NONPAYMENT => '待支付',
                self::STATUS_CANCEL => '已取消',
                self::STATUS_PAYMENT_TO_BE_CONFIRMED => '支付待确认',
                self::STATUS_APPLY_REFUND => '申请退款',
                self::STATUS_REFUND => '已退款',
                self::STATUS_TO_BE_SHIPPING => '待发货',
                self::STATUS_SHIPMENT_ALL => '全部发货',
                self::STATUS_SHIPMENT_PORTION => '部分发货',
                self::STATUS_FINISH => '已完成',
            ]
        ];

        //根据具体值显示对应的值
        if ($value !== null)
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column][$value] : false;
        //返回关联数组,用户下拉的filter实现
        else
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column] : false;
    }

}