<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\common\model\coupon;


use app\common\model\BaseModel;
use app\common\model\goods\Goods;
use app\common\model\goods_category\GoodsCategory;


/**
 * CouponRules模型
 * Class CouponRules
 * @package app\common\model\coupon
 */
class CouponRules extends BaseModel
{

    protected $name = 'coupon_rules';

    public function couponWithCategory()
    {
        return $this->belongsToMany(GoodsCategory::class,CouponCategory::class,'goods_category_id','coupon_id');
    }

    public function couponWithGoods()
    {
        return $this->belongsToMany(Goods::class,CouponGoods::class,'goods_id','coupon_id');
    }

    public function couponCategory()
    {
        return $this->hasMany(CouponCategory::class, 'coupon_id', 'goods_category_id');
    }

    public function getDiscountRatioTextAttr($value,$data)
    {
        return !empty($data['discount_ratio'])?$data['discount_ratio']*10:'';
    }

    public function couponCategoryOne()
    {
        return $this->hasOne(CouponCategory::class, 'coupon_id', 'id');
    }
    public function getLabelsAttr($value)
    {
        return $value?array_map(function($item){ return (int)$item; },explode(',',$value)):[];
    }
}