AddonsBossRewardLog.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace addons\Boss\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\user\User;
  5. use Yii;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "{{%addons_boss_reward_log}}".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城ID
  12. * @property int $order_id 订单ID
  13. * @property string $order_no 订单编号
  14. * @property int $user_id 用户ID
  15. * @property int $type 结算类型:1永久分红,2额外分红
  16. * @property int $level 结算时用户的股东等级
  17. * @property int $average_count 结算人数
  18. * @property float $amounts 结算的总金额,单位:元
  19. * @property float $commission 结算的佣金,单位:元
  20. * @property string|null $settlement_config 结算时的配置
  21. * @property string|null $settlement_data 结算时的数据
  22. * @property string $from_type 结算时的数据
  23. * @property int $is_settlement 是否已结算【0否,1是】
  24. * @property int $date 数据生成日期
  25. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  26. * @property int $created_at
  27. * @property int $updated_at
  28. */
  29. class AddonsBossRewardLog extends \common\models\base\BaseActiveRecord
  30. {
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function tableName()
  35. {
  36. return '{{%addons_boss_reward_log}}';
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['mall_id', 'order_id', 'user_id', 'type', 'level', 'average_count', 'is_settlement', 'date', 'status', 'created_at', 'updated_at'], 'integer'],
  45. [['amounts', 'commission'], 'number'],
  46. [['settlement_config', 'settlement_data'], 'string'],
  47. [['order_no'], 'string', 'max' => 100],
  48. [['from_type'], 'string', 'max' => 50],
  49. ];
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function attributeLabels()
  55. {
  56. return [
  57. 'id' => 'ID',
  58. 'mall_id' => '商城ID',
  59. 'order_id' => '订单ID',
  60. 'order_no' => '订单编号',
  61. 'user_id' => '用户ID',
  62. 'type' => '结算类型:1永久分红,2额外分红',
  63. 'level' => '结算时用户的股东等级',
  64. 'average_count' => '结算人数',
  65. 'amounts' => '结算的总金额,单位:元',
  66. 'commission' => '结算的佣金,单位:元',
  67. 'settlement_config' => '结算时的配置',
  68. 'settlement_data' => '结算时的数据',
  69. 'is_settlement' => '是否已结算【0否,1是】',
  70. 'date' => '数据生成日期',
  71. 'status' => '状态[-1:删除;0:禁用;1启用]',
  72. 'created_at' => 'Created At',
  73. 'updated_at' => 'Updated At',
  74. ];
  75. }
  76. /**
  77. * 关联用户表
  78. * @Author: lun
  79. * @DateTime: 2023/7/18 0018 17:37
  80. * @Copyright: copyright (c) 2021 广东七件事集团
  81. * @return \yii\db\ActiveQuery
  82. */
  83. public function getUser()
  84. {
  85. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,nickname,mobile,username,avatar_url');
  86. }
  87. /**
  88. * 生成沉淀池数据
  89. * @Author: lun
  90. * @DateTime: 2022/9/21 0021 15:43
  91. * @Copyright: copyright (c) 2021 广东七件事集团
  92. * @param $params
  93. * @param $sediment_percent
  94. * @param $job_id
  95. * @return array
  96. * @throws Exception
  97. */
  98. public static function createSediment($mall_id, $params, $sediment_percent, $job_id)
  99. {
  100. // 按照比例计算沉淀池金额,不四舍五入保留两位小数
  101. $sedimen_money = sprintf("%.2f",substr(sprintf("%.3f", ($params['commission'] * $sediment_percent / 100)), 0, -1));
  102. // 剩余分红金额,不四舍五入保留两位小数
  103. $surplus_commission = sprintf("%.2f",substr(sprintf("%.3f", ($params['commission'] - $sedimen_money)), 0, -1));
  104. // 必须大于一分钱才可以进入
  105. $data = [];$time = time();
  106. if ($sedimen_money >= 0.01) {
  107. $data['mall_id'] = $mall_id;
  108. $data['user_id'] = $params['user_id'];
  109. $data['type'] = $params['type'];
  110. $data['level'] = $params['level'];
  111. $data['amounts'] = $params['commission'];
  112. $data['commission'] = $sedimen_money;
  113. $data['settlement_config'] = json_encode(['sediment_percent' => $sediment_percent]);
  114. $data['settlement_data'] = json_encode(['job_id' => $job_id]);
  115. $data['is_settlement'] = StatusEnum::DISABLED;
  116. $data['date'] = date('Ymd', $time);
  117. $data['created_at'] = $time;
  118. $data['updated_at'] = $time;
  119. }
  120. return ['commission' => $surplus_commission, 'sedimen_money' => $sedimen_money, 'reward' => $data];
  121. }
  122. /**
  123. * 获取未结算的分红
  124. *
  125. * @param int $user_id
  126. * @param int $mall_id
  127. * @return float
  128. */
  129. public static function getUnsettledCommission(int $user_id, int $mall_id)
  130. {
  131. return self::find()
  132. ->where(['user_id' => $user_id, 'mall_id' => $mall_id, 'is_settlement' => 0, 'status' => StatusEnum::ENABLED])
  133. ->sum('commission') ?: 0.00;
  134. }
  135. }