VirtualCoin.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2017/9/10
  6. * Time: 上午11:30
  7. */
  8. namespace app\common\models;
  9. /**
  10. * Class VirtualCoin
  11. * @package app\common\models
  12. * @property float amountOfCoin
  13. * @property float amountOfMoney
  14. */
  15. abstract class VirtualCoin extends BaseModel
  16. {
  17. protected $table = 'yz_virtual_coin';
  18. protected $attributes = [
  19. 'amountOfCoin' => 0,
  20. 'amountOfMoney' => 0,
  21. ];
  22. protected $_name;
  23. protected $code;
  24. protected $appends = ['name'];
  25. protected $exchange_rate;
  26. function __construct($attribute = [])
  27. {
  28. parent::__construct($attribute);
  29. $this->exchange_rate = $this->getExchangeRate();
  30. $this->_name = $this->getName();
  31. $this->code = $this->getCode();
  32. }
  33. public function getNameAttribute()
  34. {
  35. return $this->getName();
  36. }
  37. public function getCode()
  38. {
  39. return isset($this->code) ? $this->code : $this->code = $this->_getCode();
  40. }
  41. public function getName()
  42. {
  43. return isset($this->_name) ? $this->_name : $this->_name = $this->_getName();
  44. }
  45. public function getExchangeRate()
  46. {
  47. return isset($this->exchange_rate) ? $this->exchange_rate : ($this->exchange_rate = $this->_getExchangeRate() ?: 1);
  48. }
  49. abstract protected function _getExchangeRate();
  50. abstract protected function _getName();
  51. abstract protected function _getCode();
  52. /**
  53. * @param VirtualCoin $coin
  54. * @return VirtualCoin
  55. */
  56. public function plus(VirtualCoin $coin)
  57. {
  58. return (new static())->setMoney($this->amountOfMoney + $coin->getMoney());
  59. }
  60. public function setCoin($amount)
  61. {
  62. $this->amountOfMoney = $amount * $this->exchange_rate;
  63. //四舍六入五成双
  64. // $this->amountOfMoney = round($amount * $this->exchange_rate,2,PHP_ROUND_HALF_EVEN);
  65. return $this;
  66. }
  67. public function setMoney($amount)
  68. {
  69. // $this->amountOfMoney = round($amount,4,PHP_ROUND_HALF_EVEN);
  70. $this->amountOfMoney = $amount;
  71. return $this;
  72. }
  73. public function toArray()
  74. {
  75. $this->amountOfCoin = sprintf('%.2f', $this->getCoin());
  76. $this->amountOfMoney = sprintf('%.2f', $this->getMoney());
  77. return parent::toArray();
  78. }
  79. /**
  80. * @return float|int
  81. */
  82. public function getCoin()
  83. {
  84. //四舍六入五成双
  85. return $this->amountOfCoin = round($this->amountOfMoney / $this->exchange_rate,2,PHP_ROUND_HALF_EVEN);
  86. // return $this->amountOfCoin = sprintf('%.2f', bcdiv($this->amountOfMoney,$this->exchange_rate,4));
  87. }
  88. /**
  89. * @return mixed
  90. */
  91. public function getMoney()
  92. {
  93. return $this->amountOfMoney;
  94. }
  95. public function save(array $options = [])
  96. {
  97. return true;
  98. }
  99. }