ShortVideoCommissionItemSetting.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace addons\ShortVideo\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\goods\Goods;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%addons_short_video_commission_item_setting}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $is_enable 是否启用带货奖励:0:否;1是
  12. * @property int $is_alone 是否独立设置:0:否;1是
  13. * @property int $is_extra 是否开启额外分红:0:否;1是
  14. * @property int $item_id 项目id
  15. * @property string $item_type 项目类型:商品goods
  16. * @property string $commission_type 分佣类型:佣金:commission
  17. * @property int $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int $created_at
  19. * @property int $updated_at
  20. */
  21. class ShortVideoCommissionItemSetting extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_short_video_commission_item_setting}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id'], 'required'],
  37. [['mall_id', 'is_enable', 'is_alone', 'item_id', 'status', 'created_at', 'updated_at'], 'integer'],
  38. [['item_type', 'commission_type'], 'string', 'max' => 255],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'mall_id' => 'Mall ID',
  49. 'is_enable' => 'Is Enable',
  50. 'is_alone' => 'Is Alone',
  51. 'item_id' => 'Item ID',
  52. 'item_type' => 'Item Type',
  53. 'commission_type' => 'Commission Type',
  54. 'status' => 'Status',
  55. 'created_at' => 'Created At',
  56. 'updated_at' => 'Updated At',
  57. ];
  58. }
  59. public function getDetail()
  60. {
  61. return $this->hasOne(ShortVideoCommissionItemSettingDetail::class, ['short_video_commission_item_setting_id' => 'id']);
  62. }
  63. public static function setData($params, $data)
  64. {
  65. // dd($params, $data);
  66. try {
  67. $transaction = \Yii::$app->db->beginTransaction();
  68. foreach ($data as $commission_type => $item) {
  69. $detail_params = $item['detail'];
  70. unset($item['detail']);
  71. $model = self::findOne([
  72. 'mall_id' => $params['mall_id'],
  73. 'item_type' => $item['item_type'],
  74. 'item_id' => $params['item_id'],
  75. 'commission_type' => $item['commission_type'],
  76. 'status' => [StatusEnum::ENABLED]
  77. ]);
  78. if (empty($model)) {
  79. $model = new self();
  80. $model->mall_id = $params['mall_id'];
  81. $model->item_id = $params['item_id'];
  82. $model->loadDefaultValues();
  83. }
  84. if(!$model->load($item,'') || !$model->save()){
  85. throw new \Exception($model->getErrorMessage());
  86. }
  87. // 插入配置详情
  88. $detail = array_merge($detail_params, ['mall_id' => $params['mall_id'], 'short_video_commission_item_setting_id' => $model->id]);
  89. $res = ShortVideoCommissionItemSettingDetail::setData($detail);
  90. if ($res == false) {
  91. throw new \Exception(ShortVideoCommissionItemSettingDetail::getStaticError());
  92. }
  93. }
  94. $transaction->commit();
  95. return true;
  96. } catch (\Exception $e) {
  97. $transaction->rollBack();
  98. self::$static_error = $e->getMessage();
  99. return false;
  100. }
  101. }
  102. /**
  103. * @name:
  104. * @msg: 计算带货奖励
  105. * @param {int} $mall_id
  106. * @param {int} $good_id
  107. * @param {int} $good_price 商品售价
  108. * @return {bool | array} commission:带货奖励佣金;award_type:奖励类型 0:固定金额 1:百分比;ratio:奖励比例;is_alone:是否商品里独立设置带货奖励 0:否 1:是;is_reward:是否带货奖励商品,1:是,0:否
  109. */
  110. public static function calculateReward(int $mall_id, int $good_id, float $good_price)
  111. {
  112. $good = Goods::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $good_id]);
  113. if (empty($good)) {
  114. self::$static_error = '商品不存在';
  115. return false;
  116. }
  117. $item_stting = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'item_id' => $good_id]);
  118. $commission = 0;
  119. $award_type = 1;
  120. $ratio = 0;
  121. $is_reward = 1;
  122. if (1 == $item_stting['is_alone']) {
  123. // 独立设置带货奖励
  124. $setting_detail = $item_stting->detail;
  125. if (1 == $setting_detail['calculate_type']) {
  126. // 按照商品售价百分比计算
  127. $commission = bcmul($good_price, $setting_detail['value'] / 100, 2);
  128. $ratio = $setting_detail['value'];
  129. } else {
  130. $award_type = 0;
  131. $commission = $setting_detail['value'];
  132. }
  133. } else {
  134. // 未独立设置
  135. $configs = ShortVideoConfig::getConfig($mall_id);
  136. if (1 == $configs['award_status']) {
  137. if (1 == $configs['award_type']) {
  138. $commission = bcmul($good_price, $configs['award_percentage'] / 100, 2);
  139. $ratio = $configs['award_percentage'];
  140. } else {
  141. $commission = $configs['award_money'];
  142. $award_type = 0;
  143. }
  144. } else {
  145. $is_reward = 0;
  146. }
  147. }
  148. $rst = [
  149. 'commission' => $commission,
  150. 'award_type' => $award_type,
  151. 'ratio' => $ratio,
  152. 'is_alone' => $item_stting['is_alone'] ?? 0,
  153. 'is_reward' => $is_reward,
  154. ];
  155. return $rst;
  156. }
  157. }