CommissionItemSetting.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace addons\OperationCenter\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%addons_operation_center_commission_item_setting}}".
  7. *
  8. * @property int $id
  9. * @property int $mall_id 商城ID
  10. * @property int|null $is_partake 是否参与运营中心:0:否;1是
  11. * @property int|null $is_enable 是否独立设置:0:否;1是
  12. * @property int|null $item_id 项目id
  13. * @property string|null $item_type 项目类型:商品goods
  14. * @property int|null $settings 分销佣金设置:0:按商品设置;1:按规格设置
  15. * @property string|null $commission_type 分佣类型:积分score;佣金:commission
  16. * @property int|null $is_percent 类型 0百分比 1 固定金额
  17. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int|null $created_at
  19. * @property int|null $updated_at
  20. */
  21. class CommissionItemSetting extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_operation_center_commission_item_setting}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id'], 'required'],
  37. [['mall_id', 'is_partake', 'is_enable', 'item_id', 'settings', 'is_percent', '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' => '商城ID',
  49. 'is_partake' => '是否参与运营中心:0:否;1是',
  50. 'is_enable' => '是否独立设置:0:否;1是',
  51. 'item_id' => '项目id',
  52. 'item_type' => '项目类型:商品goods',
  53. 'settings' => '分销佣金设置:0:按商品设置;1:按规格设置',
  54. 'commission_type' => '分佣类型:积分score;佣金:commission',
  55. 'is_percent' => '类型 0百分比 1 固定金额',
  56. 'status' => '状态[-1:删除;0:禁用;1启用]',
  57. 'created_at' => 'Created At',
  58. 'updated_at' => 'Updated At',
  59. ];
  60. }
  61. /**
  62. * 关联设置
  63. * @Author: lun
  64. * @DateTime: 2023/8/16 0016 16:34
  65. * @Copyright: copyright (c) 2021 广东七件事集团
  66. * @return \yii\db\ActiveQuery
  67. */
  68. public function getDetail()
  69. {
  70. return $this->hasMany(CommissionItemSettingDetail::class, ['commission_item_setting_id' => 'id'])->where(['status' => StatusEnum::ENABLED])->indexBy('level');
  71. }
  72. /**
  73. * 保存数据
  74. * @Author: lun
  75. * @DateTime: 2023/8/16 0016 16:35
  76. * @Copyright: copyright (c) 2021 广东七件事集团
  77. * @param $params
  78. * @param $data
  79. * @return bool
  80. */
  81. public static function setData($params, $data)
  82. {
  83. try{
  84. foreach ($data as $item) {
  85. $model = self::findOne([
  86. 'mall_id' => $params['mall_id'],
  87. 'item_type' => $item['item_type'],
  88. 'commission_type' => $item['commission_type'],
  89. 'item_id' => $params['item_id'],
  90. 'status' => [StatusEnum::ENABLED]
  91. ]);
  92. if (empty($model)) {
  93. $model = new self();
  94. $model->mall_id = $params['mall_id'];
  95. $model->item_id = $params['item_id'];
  96. $model->loadDefaultValues();
  97. }
  98. if(!$model->load($item,'') || !$model->save()){
  99. throw new \Exception($model->getErrorMessage());
  100. }
  101. // 插入配置详情
  102. $detail = ['mall_id' => $params['mall_id'], 'commission_item_setting_id' => $model->id];
  103. $res = CommissionItemSettingDetail::setData($detail, $item['detail']);
  104. if ($res == false) throw new \Exception(CommissionItemSettingDetail::getStaticError());
  105. }
  106. return true;
  107. }catch(\Exception $e){
  108. self::$static_error = $e->getMessage();
  109. return false;
  110. }
  111. }
  112. /**
  113. * 通过等级获取详情
  114. *
  115. * @return CommissionItemSettingDetail
  116. */
  117. public function getCommissionDetailByLevel(int $level)
  118. {
  119. return CommissionItemSettingDetail::find()
  120. ->andWhere(['level' => $level, 'commission_item_setting_id' => $this->id, 'status' => 1])
  121. ->one();
  122. }
  123. }