UserBonus.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace common\models;
  3. use common\helpers\user\Info;
  4. use Yii;
  5. use common\libs\logging\operate\valueType\Config as ValueTypeConfig;
  6. /**
  7. * This is the model class for table "{{%USER_BONUS}}".
  8. *
  9. * @property string $ID
  10. * @property string $USER_ID 会员ID
  11. * @property string $BONUS 奖金总额
  12. * @property string $BONUS_FREEZE 奖金冻结部分
  13. * @property int $VER 乐观锁标识
  14. * @property int $CREATED_AT 创建时间
  15. * @property int $RECONSUME_POINTS 复消积分
  16. * @property int $RECONSUME_POINTS_EXPIRED 复消积分过期
  17. * @property int $RECONSUME_POINTS_TOTAL 复消积分累计
  18. * @property int $MANAGE_TAX 管理费累计
  19. * @property int $BONUS_TOTAL 总奖金累计
  20. */
  21. class UserBonus extends \common\components\ActiveRecord
  22. {
  23. const TYPE = BalanceAudit::TYPE;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%USER_BONUS}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['USER_ID', 'CREATED_AT'], 'required'],
  38. [['BONUS', 'RECONSUME_POINTS', 'MANAGE_TAX', 'BONUS_TOTAL'], 'number'],
  39. [['VER', 'CREATED_AT'], 'integer'],
  40. [['ID','USER_ID'], 'string', 'max' => 32],
  41. [['USER_ID'], 'unique'],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'ID' => 'ID',
  51. 'USER_ID' => '会员ID',
  52. 'BONUS' => '奖金总额',
  53. 'VER' => '乐观锁标识',
  54. 'CREATED_AT' => '创建时间',
  55. 'RECONSUME_POINTS' => '复消积分',
  56. 'RECONSUME_POINTS_TOTAL' => '复消积分累计',
  57. 'RECONSUME_POINTS_EXPIRED' => '复消积分过期',
  58. 'MANAGE_TAX' => '管理费累计',
  59. 'BONUS_TOTAL' => '总奖金累计',
  60. ];
  61. }
  62. /**
  63. * 操作日志记录条件
  64. * @return array
  65. */
  66. public function attrLabelsWithLogType(){
  67. return [
  68. 'USER_ID' => '会员ID',
  69. 'BONUS' => '会员账户',
  70. ];
  71. }
  72. /**
  73. * 乐观锁
  74. * @return string
  75. */
  76. public function optimisticLock()
  77. {
  78. return 'VER';
  79. }
  80. /**
  81. * 获取一名会员的余额
  82. * @param $userId
  83. * @return array|null
  84. */
  85. public static function getBonusByUserId($userId)
  86. {
  87. $data = UserBonus::findOneAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId], 'USER_ID,BONUS');
  88. return $data ?: ['USER_ID' => $userId, 'BONUS' => 0];
  89. }
  90. public function insertOrUpdate()
  91. {
  92. if ($this->USER_ID !== null) {
  93. $pk = $this->USER_ID;
  94. if (!empty($this->$pk)) {
  95. return $this->update();
  96. } else {
  97. return $this->insert();
  98. }
  99. } else {
  100. // 如果模型没有定义主键,则直接插入
  101. return $this->insert();
  102. }
  103. }
  104. }