Currency.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\models;
  3. use Exception;
  4. use Yii;
  5. use yii\behaviors\TimestampBehavior;
  6. use yii\db\ActiveRecord;
  7. /**
  8. * This is the model class for table "{{%currency}}".
  9. *
  10. * @property int $id ID
  11. * @property int $store_id 商城ID
  12. * @property string $name 币种名称
  13. * @property string $scale 精度
  14. * @property string $unit 币种单位
  15. * @property string $code 币种代码
  16. * @property string $symbol
  17. * @property string $plugin_code 插件代码
  18. * @property float $exchange_rate 兑换比例
  19. * @property float $guid_price 指导价
  20. * @property float $cny_price
  21. * @property float $exchange_price 兑换价
  22. * @property float $draw_fee_ratio
  23. * @property int $enable 是否启用 【1是 0否】
  24. * @property int $enable_draw 是否启用 【1是 0否】
  25. * @property int $enable_bussiness_mode 是否支持商业模式【1是 0否】
  26. * @property int $enable_goods_deduct 是否支持商品抵扣【1是 0否】
  27. * @property string $remark 备注
  28. * @property int $created_at 创建时间
  29. * @property int $updated_at 更新时间
  30. * @property int $enable_exchange
  31. * @property int $ts_cardinal_number
  32. * @property int $ex_cardinal_number
  33. * @property string $remainder
  34. */
  35. class Currency extends \yii\db\ActiveRecord
  36. {
  37. // 贡献积分
  38. const CURRENCY_COIN = 'coin';
  39. // 串码红包
  40. const CURRENCY_STRING_CODE = 'string_code';
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public static function tableName()
  45. {
  46. return '{{%currency}}';
  47. }
  48. public function behaviors()
  49. {
  50. return [
  51. [
  52. 'class' => TimestampBehavior::class,
  53. 'attributes' => [
  54. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
  55. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
  56. ]
  57. ]
  58. ];
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function rules()
  64. {
  65. return [
  66. [['store_id', 'code', 'name', 'unit', 'exchange_rate', 'enable', 'enable_bussiness_mode', 'enable_goods_deduct'], 'required'],
  67. [['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'],
  68. [['exchange_rate', 'transfer_fee_rate', 'balance_rate', 'transmit_rate', 'balance_fee_ratio', 'draw_fee_ratio', 'remainder', 'guid_price','cny_price', 'exchange_price'], 'number'],
  69. [['name', 'unit'], 'string', 'max' => 64],
  70. ['code', 'checkCode'],
  71. [['code'], 'match', 'pattern' => '/^[a-z][a-z_0-9]{1,}$/', 'message' => '币种码只能以小写字母、数字和下划线构成,并且只能以字母开头'],
  72. [['code', 'symbol', 'plugin_code', 'type'], 'string', 'max' => 32],
  73. [['logo'], 'string', 'max' => 128],
  74. [['remark'], 'string', 'max' => 255],
  75. ];
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function attributeLabels()
  81. {
  82. return [
  83. 'id' => 'ID',
  84. 'store_id' => '店铺ID',
  85. 'name' => '币种名称',
  86. 'scale' => '精度',
  87. 'unit' => '币种单位',
  88. 'code' => '币种代码',
  89. 'plugin_code' => '插件代码',
  90. 'logo' => '币种图标',
  91. 'exchange_rate' => '兑换比例',
  92. 'exchange_price' => '兑换价',
  93. 'enable' => '是否启用 【1是 0否】',
  94. 'enable_bussiness_mode' => '是否支持商业模式【1是 0否】',
  95. 'enable_goods_deduct' => '是否支持商品抵扣【1是 0否】',
  96. 'remark' => '备注',
  97. 'is_transfer' => '是否支持转账',
  98. 'transfer_fee_rate' => '转账手续费',
  99. 'type' => '货币类型',
  100. 'created_at' => '创建时间',
  101. 'updated_at' => '更新时间',
  102. 'ts_cardinal_number' => '转送基数',
  103. 'ex_cardinal_number' => '兑换基数',
  104. 'remainder' => '无质押需要剩余',
  105. ];
  106. }
  107. /**
  108. * 验证code合法性
  109. */
  110. public function checkCode($attribute, $params)
  111. {
  112. if (in_array($this->code, ['balance', 'score', 'commission'])) $this->addError('code', '此币种代码已被使用');
  113. $currency = self::find()->where(array('code' => $this->code, 'store_id' => $this->store_id))->one();
  114. /* @var Currency[] $currency */
  115. if (!empty($currency)) {
  116. if ($this->exchange_rate != $currency->exchange_rate) {
  117. $this->addError('code', '此币种代码已被使用,兑换比例不能修改');
  118. }
  119. }
  120. }
  121. public static function getCurrencyByCode($store_id, $code)
  122. {
  123. return self::find()->where(array('store_id' => $store_id, 'code' => $code, 'enable' => 1))->one();
  124. }
  125. }