0, 'amountOfMoney' => 0, 'is_point' => 0, 'use_point' => 0, 'use_money' => 0, ]; protected $_name; protected $code; protected $appends = ['name']; protected $exchange_rate; function __construct($attribute = []){ parent::__construct($attribute); $this->exchange_rate = $this->getExchangeRate(); $this->_name = $this->getName(); $this->code = $this->getCode(); } public function getNameAttribute(){ return $this->getName(); } public function getCode(){ return isset($this->code) ? $this->code : $this->code = $this->_getCode(); } public function getName(){ return isset($this->_name) ? $this->_name : $this->_name = $this->_getName(); } public function getExchangeRate(){ return isset($this->exchange_rate) ? $this->exchange_rate : ($this->exchange_rate = $this->_getExchangeRate() ?: 1); } abstract protected function _getExchangeRate(); abstract protected function _getName(); abstract protected function _getCode(); /** * @param VirtualCoin $coin * @return VirtualCoin */ public function plus(VirtualCoin $coin){ $result = (new static())->setMoney($this->amountOfMoney + $coin->getMoney());// 积分最低抵扣金额 if($coin->is_point == 1){ $result->setPoint([ 'is_point' => 1, 'use_point' => $this->use_point + $coin->use_point, 'use_money' => $this->use_money + $coin->use_money, ])->setMoney(0); } return $result; } public function setCoin($amount){ $this->amountOfMoney = $amount * $this->exchange_rate; //四舍六入五成双 // $this->amountOfMoney = round($amount * $this->exchange_rate,2,PHP_ROUND_HALF_EVEN); return $this; } public function setMoney($amount){ // $this->amountOfMoney = round($amount,4,PHP_ROUND_HALF_EVEN); $this->amountOfMoney = $amount; return $this; } public function toArray(){ $this->amountOfCoin = sprintf('%.2f',$this->getCoin()); $this->amountOfMoney = sprintf('%.2f',$this->getMoney()); return parent::toArray(); } /** * @return float|int */ public function getCoin(){ //四舍六入五成双 if($this->is_point == 1) return (float)$this->use_point; return $this->amountOfCoin = round($this->amountOfMoney / $this->exchange_rate,2,PHP_ROUND_HALF_EVEN); // return $this->amountOfCoin = sprintf('%.2f', bcdiv($this->amountOfMoney,$this->exchange_rate,4)); } /** * @return mixed */ public function getMoney(){ if($this->is_point == 1) return (float)0; return $this->amountOfMoney; } public function save(array $options = []){ return TRUE; } // 积分商品 - 设置内容 public function setPoint($field){ if(array_key_exists('is_point',$field)) $this->is_point = $field['is_point']; if(array_key_exists('use_point',$field)) $this->use_point = $field['use_point']; if(array_key_exists('use_money',$field)) $this->use_money = $field['use_money']; return $this; } }