| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace app\models;
- use Exception;
- use Yii;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- /**
- * This is the model class for table "{{%currency}}".
- *
- * @property int $id ID
- * @property int $store_id 商城ID
- * @property string $name 币种名称
- * @property string $scale 精度
- * @property string $unit 币种单位
- * @property string $code 币种代码
- * @property string $symbol
- * @property string $plugin_code 插件代码
- * @property float $exchange_rate 兑换比例
- * @property float $guid_price 指导价
- * @property float $cny_price
- * @property float $exchange_price 兑换价
- * @property float $draw_fee_ratio
- * @property int $enable 是否启用 【1是 0否】
- * @property int $enable_draw 是否启用 【1是 0否】
- * @property int $enable_bussiness_mode 是否支持商业模式【1是 0否】
- * @property int $enable_goods_deduct 是否支持商品抵扣【1是 0否】
- * @property string $remark 备注
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- * @property int $enable_exchange
- * @property int $ts_cardinal_number
- * @property int $ex_cardinal_number
- * @property string $remainder
- */
- class Currency extends \yii\db\ActiveRecord
- {
- // 贡献积分
- const CURRENCY_COIN = 'coin';
- // 串码红包
- const CURRENCY_STRING_CODE = 'string_code';
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%currency}}';
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- 'attributes' => [
- ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
- ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
- ]
- ]
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['store_id', 'code', 'name', 'unit', 'exchange_rate', 'enable', 'enable_bussiness_mode', 'enable_goods_deduct'], 'required'],
- [['id', 'store_id', 'ts_cardinal_number', 'ex_cardinal_number', 'enable', 'enable_bussiness_mode', 'enable_exchange', 'enable_goods_deduct', 'is_transfer', 'created_at', 'updated_at', 'enable_balance', 'enable_draw', 'enable_transmit', 'transmit_user_type', 'scale'], 'integer'],
- [['exchange_rate', 'transfer_fee_rate', 'balance_rate', 'transmit_rate', 'balance_fee_ratio', 'draw_fee_ratio', 'remainder', 'guid_price','cny_price', 'exchange_price'], 'number'],
- [['name', 'unit'], 'string', 'max' => 64],
- ['code', 'checkCode'],
- [['code'], 'match', 'pattern' => '/^[a-z][a-z_0-9]{1,}$/', 'message' => '币种码只能以小写字母、数字和下划线构成,并且只能以字母开头'],
- [['code', 'symbol', 'plugin_code', 'type'], 'string', 'max' => 32],
- [['logo'], 'string', 'max' => 128],
- [['remark'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => '店铺ID',
- 'name' => '币种名称',
- 'scale' => '精度',
- 'unit' => '币种单位',
- 'code' => '币种代码',
- 'plugin_code' => '插件代码',
- 'logo' => '币种图标',
- 'exchange_rate' => '兑换比例',
- 'exchange_price' => '兑换价',
- 'enable' => '是否启用 【1是 0否】',
- 'enable_bussiness_mode' => '是否支持商业模式【1是 0否】',
- 'enable_goods_deduct' => '是否支持商品抵扣【1是 0否】',
- 'remark' => '备注',
- 'is_transfer' => '是否支持转账',
- 'transfer_fee_rate' => '转账手续费',
- 'type' => '货币类型',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'ts_cardinal_number' => '转送基数',
- 'ex_cardinal_number' => '兑换基数',
- 'remainder' => '无质押需要剩余',
- ];
- }
- /**
- * 验证code合法性
- */
- public function checkCode($attribute, $params)
- {
- if (in_array($this->code, ['balance', 'score', 'commission'])) $this->addError('code', '此币种代码已被使用');
- $currency = self::find()->where(array('code' => $this->code, 'store_id' => $this->store_id))->one();
- /* @var Currency[] $currency */
- if (!empty($currency)) {
- if ($this->exchange_rate != $currency->exchange_rate) {
- $this->addError('code', '此币种代码已被使用,兑换比例不能修改');
- }
- }
- }
- public static function getCurrencyByCode($store_id, $code)
- {
- return self::find()->where(array('store_id' => $store_id, 'code' => $code, 'enable' => 1))->one();
- }
- }
|