<?php

namespace backend\modules\goods\models\ars;

use Yii;
use yii\behaviors\TimestampBehavior;
use backend\modules\goods\models\ars\Category;
use backend\modules\goods\models\ars\ShopCategory;
use backend\modules\file\models\ars\File;
use backend\modules\goods\models\ars\Brand;
use backend\modules\goods\models\ars\Supplier;

/**
 * This is the model class for table "atg_goods".
 *
 * @property int $id
 * @property int $pid 父级id
 * @property int $cat_id 后台商品类别id
 * @property int $brand_id 品牌id
 * @property int $shop_cat_id 前端商品类别id
 * @property string $name 商品名称
 * @property string $sn 商品唯一货号
 * @property string $code 商品货码
 * @property int $supplier_id 供应商id
 * @property int $weight 重量
 * @property int $length 长度
 * @property int $width 宽度
 * @property int $height 高度
 * @property int $diameter 直径
 * @property string $unit 单位
 * @property int $sold_count 已售数量
 * @property int $limit_count 限购数量
 * @property int $stock 库存
 * @property int $stock_warn 库存警告
 * @property int $market_price 市场价
 * @property int $price 销售价
 * @property string $brief 简介
 * @property string $description 详细介绍
 * @property int $image 图片id
 * @property int $model_id 模型id
 * @property int $is_sale 该商品是否开放销售,1为是,0为否
 * @property int $sort_order 排序
 * @property int $bouns_points 奖励积分
 * @property int $experience_points 经验值
 * @property int $is_delete 是否删除,1为已删除
 * @property int $express_template 配送详情id
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $sku_mode sku类型
 * @property int $is_taking 是否自提
 * @property int $is_express 是否快递发货
 * @property int $express_type 快递运费方式
 * @property int $uniform_postage 统一邮费
 */
class Goods extends \yii\db\ActiveRecord
{
    //商品封面图
    public $coverImagePath;
    public $coverImageId;
    //商品详情图
    public $detailImagePath;
    public $detailImageId;
    //是否删除is_delete
    const IS_DELETE_NO = 0;//未删除
    const IS_DELETE_YES = 1;//已删除
    //该商品是否开放销售is_sale
    const IS_SALE_NO = 0;//否
    const IS_SALE_YES = 1;//是
    //类型sku
    const SKU_MODE_ATTR = 1;//SKU类型属性
    const SKU_MODE_MANUAL = 2;//SKU类型手写
    //是否自提is_taking
    const IS_TAKING_NO = 0; //否
    const IS_TAKING_YES = 1;    //是
    //是否快递发货is_express
    const IS_EXPRESS_NO = 0;    //否
    const IS_EXPRESS_YES = 1;   //是
    //快递运费计算方式express_type
    const EXPRESS_TYPE_UNIFORM_POSTAGE = 1; //统一邮费
    const EXPRESS_TYPE_EXPRESS_TEMPLATE = 2;    //运费模板

    public static $isTaking = [
        self::IS_TAKING_NO => '否',
        self::IS_TAKING_YES => '是'
    ];
    public static $isExpress = [
        self::IS_EXPRESS_NO => '否',
        self::IS_EXPRESS_YES => '是'
    ];
    public static $expressType = [
        self::EXPRESS_TYPE_UNIFORM_POSTAGE => '统一邮费',
        self::EXPRESS_TYPE_EXPRESS_TEMPLATE => '运费模板'
    ];
    public static $isSale = [
        self::IS_SALE_NO => '不在售',
        self::IS_SALE_YES => '在售'
    ];
    public $ruleVerify = 0;//规则验证是否通过
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'atg_goods';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['pid', 'cat_id', 'brand_id', 'shop_cat_id', 'supplier_id', 'weight', 'length', 'width', 'height', 'diameter', 'sold_count', 'limit_count', 'stock', 'stock_warn', 'image', 'model_id', 'is_sale', 'sort_order', 'bouns_points', 'experience_points', 'is_delete', 'express_template', 'sku_mode', 'is_taking', 'is_express', 'express_type'], 'integer'],
            [['cat_id', 'brand_id', 'shop_cat_id', 'name'], 'required'],
            [['sn'], 'checkExist'],
            [['description', 'coverImageId', 'detailImageId'], 'string'],
            [['name'], 'string', 'max' => 120],
            [['sn'], 'string', 'max' => 60],
            [['code'], 'string', 'max' => 50],
            [['unit'], 'string', 'max' => 16],
            [['brief'], 'string', 'max' => 255],
            [['weight', 'length', 'width', 'height', 'diameter', 'sold_count', 'market_price', 'price', 'uniform_postage'], 'checkNegative'],
            [['uniform_postage', 'market_price', 'price'], 'safe']
        ];
    }

    /**
     * @param $attribute
     * @param $params
     * 验证是否为负数
     */
    public function checkNegative($attribute, $params)
    {
        if ($this->$attribute < 0) {
            $this->addError($attribute, "不得为负数");
        }
    }

    /**
     * @param $attribute
     * @param $params
     * 验证商品编号唯一
     */
    public function checkExist($attribute, $params)
    {
        $goods = self::find()->where([$attribute => $this->$attribute, 'is_delete' => 0])->one();
        if ($this->isNewRecord) {
            if ($goods) {
                $this->addError($attribute, "该商品编号已经存在");
            }
        } else {
            if ($goods && $goods->id != $this->id) {
                $this->addError($attribute, "该商品编号已经存在");
            }
        }
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'id',
            'pid' => '父级id',
            'cat_id' => '后台商品类别',
            'brand_id' => '品牌',
            'shop_cat_id' => '前端商品类别',
            'name' => '商品名称',
            'sn' => '商品唯一货号',
            'code' => '商品货码',
            'supplier_id' => '供应商',
            'weight' => '重量',
            'length' => '长度',
            'width' => '宽度',
            'height' => '高度',
            'diameter' => '直径',
            'unit' => '单位',
            'sold_count' => '已售数量',
            'limit_count' => '限购数量',
            'stock' => '库存',
            'stock_warn' => '库存警告',
            'market_price' => '市场价',
            'price' => '销售价',
            'brief' => '简介',
            'description' => '详细介绍',
            'image' => '首页图片',
            'model_id' => '模型id',
            'is_sale' => '销售状态',
            'sort_order' => '排序',
            'bouns_points' => '奖励积分',
            'experience_points' => '经验值',
            'is_delete' => '是否删除,1为已删除',
            'express_template' => '配送详情',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'sku_mode' => 'sku类型',
            'is_taking' => '是否自提',
            'is_express' => '是否快递发货',
            'express_type' => '快递运费方式',
            'uniform_postage' => '统一邮费',
        ];
    }
   

    /**
    * @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();
                },
            ],
        ];
    }

    /**
     * @param bool $insert
     * @return bool
     * 自动填入参数
     */
    public function beforeSave($insert)
    {
        if (!$this->sn) {
            $this->sn = time() . rand(1111, 9999);
        }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }

    public function getCategory()
    {
        return $this->hasOne(Category::className(), ['id' => 'cat_id']);
    }

    public function getShopCategory()
    {
        return $this->hasOne(ShopCategory::className(), ['id' => 'shop_cat_id']);
    }

    public function getImageFile()
    {
        return $this->hasOne(File::className(), ['id' => 'image']);
    }

    public function getBrand()
    {
        return $this->hasOne(Brand::className(), ['id' => 'brand_id']);
    }

    public function getSupplier()
    {
        return $this->hasOne(Supplier::className(), ['id' => 'supplier_id']);
    }
}