ServeBonusOrderLog.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace addons\ServeBonus\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. use yii\db\Exception;
  6. /**
  7. * This is the model class for table "{{%addons_servebonus_order_log}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $user_id 用户ID
  12. * @property int $buy_uid 购买者uid
  13. * @property int $level 出单用户等级
  14. * @property int $order_id 订单ID
  15. * @property int $order_no 订单编号
  16. * @property int $prize 出单奖励,单位:元
  17. * @property int $average_count 结算人数
  18. * @property int $date 出单日期,格式:20220124
  19. * @property int $is_settlement 是否已结算【0否,1是】
  20. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  21. * @property int $created_at
  22. * @property int $updated_at
  23. */
  24. class ServeBonusOrderLog extends \common\models\base\BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%addons_servebonus_order_log}}';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['mall_id', 'order_id', 'order_detail_id', 'date','status', 'created_at', 'updated_at','order_type','goods_id','num'], 'integer'],
  40. [['order_no'], 'string'],
  41. [['money'], 'number'],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'mall_id' => '商城ID',
  52. 'user_id' => '用户ID',
  53. 'order_id' => '订单ID',
  54. 'order_no' => '订单编号',
  55. 'money' => '订单金额,单位:元',
  56. 'order_detail_id' => '订单详情ID',
  57. 'date' => '出单日期,格式:20220124',
  58. 'status' => '状态[-1:删除;0:禁用;1启用]',
  59. 'created_at' => 'Created At',
  60. 'updated_at' => 'Updated At',
  61. ];
  62. }
  63. /**
  64. * 保存数据
  65. * @Author: lun
  66. * @DateTime: 2022/1/25 0025 17:28
  67. * @Copyright: copyright (c) 2021 广东七件事集团
  68. * @param $params
  69. * @return bool
  70. */
  71. public static function setData($params)
  72. {
  73. try {
  74. $model = self::findOne(['mall_id' => $params['mall_id'], 'order_id' => $params['order_id'], 'status' => StatusEnum::ENABLED]);
  75. if (!empty($model)) return true;
  76. $model = new self();
  77. $model->loadDefaultValues();
  78. if (!$model->load($params, '') || !$model->save()) {
  79. throw new Exception($model->getErrorMessage());
  80. }
  81. return true;
  82. } catch (\Exception $e) {
  83. self::setStaticError($e->getMessage());
  84. return false;
  85. }
  86. }
  87. /**
  88. * 获取当前期数指定等级业绩
  89. */
  90. public static function getTotalAmountsNum($mall_id,$level,$start_day,$end_day){
  91. if(empty($level)) return 0;
  92. $levelSetting = ServeBonusLevelSetting::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED,'level'=>$level])->asArray()->one();
  93. if(empty($levelSetting) || empty($levelSetting['cloud_stock_achievement_prize'])) return 0;
  94. $list = self::find()->select('goods_id,num,money')
  95. ->where(['mall_id' => $mall_id])
  96. ->andFilterWhere(['between', 'date', $start_day, $end_day])
  97. ->asArray()
  98. ->all();
  99. $goods_ids = array_column($list,'goods_id');
  100. $goods_setting_list = ServeBonusCommissionItemSetting::find()
  101. ->where(['mall_id' => $mall_id,'item_id' => $goods_ids, 'is_enable' => StatusEnum::ENABLED])
  102. ->with(['detail'])
  103. ->indexBy('item_id')
  104. ->asArray()
  105. ->all();
  106. $total = 0;
  107. foreach($list as $item){
  108. $goods_setting = $goods_setting_list[$item['goods_id']];
  109. if(empty($goods_setting)) continue;
  110. if (!empty($goods_setting['is_alone']) && $goods_setting['is_alone'] == 1) {
  111. // 商品独立分佣配置
  112. $level_calculate = $goods_setting['detail'];
  113. $percent = $goods_setting['cloud_stock_achievement_percent'];
  114. } else {
  115. // 等级分佣配置
  116. $level_calculate[$level] = $levelSetting;
  117. $percent = $levelSetting['cloud_stock_achievement_percent'];
  118. }
  119. if(empty($percent)){
  120. $total += $level_calculate[$level]['cloud_stock_achievement_prize'] * $item['money'] / 100;
  121. }else{
  122. $total += $level_calculate[$level]['cloud_stock_achievement_prize'] * $item['num'];
  123. }
  124. }
  125. return $total;
  126. }
  127. }