AreaCommissionItemSetting.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace addons\Area\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_area_commission_item_setting".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $is_enable 是否独立设置:0:否;1是
  12. * @property int $item_id 项目id
  13. * @property string $item_type 项目类型:商品goods
  14. * @property string $commission_type 分佣类型:积分score;佣金:commission
  15. * @property int $is_percent_team 团队奖类型 0百分比 1 固定金额
  16. * @property int $is_percent_equal 平级奖类型 0百分比 1 固定金额
  17. * @property int $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int $created_at
  19. * @property int $updated_at
  20. */
  21. class AreaCommissionItemSetting extends BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_area_commission_item_setting}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id'], 'required'],
  37. [['mall_id', 'is_enable', 'item_id', 'is_percent_team', 'is_percent_equal', '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_enable' => '是否独立设置:0:否;1是',
  50. 'item_id' => '项目id',
  51. 'item_type' => '项目类型:商品goods',
  52. 'commission_type' => '分佣类型:积分score;佣金:commission',
  53. 'is_percent_team' => '团队奖类型 0百分比 1 固定金额',
  54. 'is_percent_equal' => '平级奖类型 0百分比 1 固定金额',
  55. 'status' => '状态[-1:删除;0:禁用;1启用]',
  56. 'created_at' => 'Created At',
  57. 'updated_at' => 'Updated At',
  58. ];
  59. }
  60. public function getDetail()
  61. {
  62. return $this->hasMany(AreaCommissionItemSettingDetail::class, ['area_commission_item_setting_id' => 'id'])->indexBy('level');
  63. }
  64. public static function setData($params, $data)
  65. {
  66. try{
  67. foreach ($data as $item) {
  68. $model = self::findOne([
  69. 'mall_id' => $params['mall_id'],
  70. 'item_type' => $item['item_type'],
  71. 'commission_type' => $item['commission_type'],
  72. 'item_id' => $params['item_id'],
  73. 'status' => [StatusEnum::ENABLED]
  74. ]);
  75. if (empty($model)) {
  76. $model = new self();
  77. $model->mall_id = $params['mall_id'];
  78. $model->item_id = $params['item_id'];
  79. $model->loadDefaultValues();
  80. }
  81. if(!$model->load($item,'') || !$model->save()){
  82. throw new \Exception($model->getErrorMessage());
  83. }
  84. // 插入配置详情
  85. $detail = ['mall_id' => $params['mall_id'], 'area_commission_item_setting_id' => $model->id];
  86. $res = AreaCommissionItemSettingDetail::setData($detail, $item['detail']);
  87. if ($res == false) throw new \Exception(AreaCommissionItemSettingDetail::getStaticError());
  88. }
  89. return true;
  90. }catch(\Exception $e){
  91. self::$static_error = $e->getMessage();
  92. return false;
  93. }
  94. }
  95. }