LuckyGroupLevel.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace addons\LuckyGroup\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_lucky_group_level".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $level 等级权重
  12. * @property string $name 等级名称
  13. * @property float $team_commission 渠道奖佣金
  14. * @property int $is_percent_team 渠道奖类型 0百分比 1 固定金额
  15. * @property float $equal_commission 平级奖佣金
  16. * @property int $is_percent_equal 平级奖类型 0百分比 1 固定金额
  17. * @property float $over_commission 越级奖佣金
  18. * @property int $is_percent_over 越级奖类型 0百分比 1 固定金额
  19. * @property int $is_auto_upgrade 是否开启升级设置【1是 0否】
  20. * @property int $upgrade_type_goods 开启商品升级【1是 0否】
  21. * @property int $upgrade_type_condition 开启条件升级【1是 0否】
  22. * @property string|null $checked_condition_values 升级条件设置值列表
  23. * @property string|null $checked_condition_keys 选中条件的key值集合
  24. * @property int $condition_type 条件升级选项【0未选择 1满足其一 2满足所有】
  25. * @property int $goods_type 商品升级类型,1- 任意商品 2- 指定商品
  26. * @property string|null $goods_ids 指定升级商品的ID集合
  27. * @property int $buy_goods_type 购买商品升级时间场景【1订单完成 2支付】
  28. * @property int $init_attend_times 每日参团次数
  29. * @property string|null $desc 等级权益描述
  30. * @property int $status 状态[-1:删除;0:禁用;1启用]
  31. * @property int $created_at 创建时间
  32. * @property int $updated_at 更新时间
  33. */
  34. class LuckyGroupLevel extends BaseActiveRecord
  35. {
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function tableName()
  40. {
  41. return '{{%addons_lucky_group_level}}';
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function rules()
  47. {
  48. return [
  49. [['mall_id'], 'required'],
  50. [['mall_id', 'level', 'is_percent_team', 'is_percent_equal', 'is_percent_over', 'is_auto_upgrade', 'upgrade_type_goods', 'upgrade_type_condition', 'condition_type', 'goods_type', 'buy_goods_type', 'init_attend_times', 'status', 'created_at', 'updated_at'], 'integer'],
  51. [['team_commission', 'equal_commission', 'over_commission'], 'number'],
  52. [['checked_condition_values', 'checked_condition_keys', 'goods_ids', 'desc'], 'string'],
  53. [['name'], 'string', 'max' => 32],
  54. ];
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function attributeLabels()
  60. {
  61. return [
  62. 'id' => 'ID',
  63. 'mall_id' => '商城ID',
  64. 'level' => '等级权重',
  65. 'name' => '等级名称',
  66. 'team_commission' => '渠道奖佣金',
  67. 'is_percent_team' => '渠道奖类型 0百分比 1 固定金额',
  68. 'equal_commission' => '平级奖佣金',
  69. 'is_percent_equal' => '平级奖类型 0百分比 1 固定金额',
  70. 'over_commission' => '越级奖佣金',
  71. 'is_percent_over' => '越级奖类型 0百分比 1 固定金额',
  72. 'is_auto_upgrade' => '是否开启升级设置【1是 0否】',
  73. 'upgrade_type_goods' => '开启商品升级【1是 0否】',
  74. 'upgrade_type_condition' => '开启条件升级【1是 0否】',
  75. 'checked_condition_values' => '升级条件设置值列表',
  76. 'checked_condition_keys' => '选中条件的key值集合',
  77. 'condition_type' => '条件升级选项【0未选择 1满足其一 2满足所有】',
  78. 'goods_type' => '商品升级类型,1- 任意商品 2- 指定商品',
  79. 'goods_ids' => '指定升级商品的ID集合',
  80. 'buy_goods_type' => '购买商品升级时间场景【1订单完成 2支付】',
  81. 'init_attend_times' => '每日参团次数',
  82. 'desc' => '等级权益描述',
  83. 'status' => '状态[-1:删除;0:禁用;1启用]',
  84. 'created_at' => '创建时间',
  85. 'updated_at' => '更新时间',
  86. ];
  87. }
  88. public static function setData(int $mall_id, array $data)
  89. {
  90. if (!empty($data['id'])) {
  91. $model = self::findOne(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED], 'id' => $data['id']]);
  92. if (empty($model)) {
  93. self::$static_error = '分类数据异常';
  94. return false;
  95. }
  96. } else {
  97. $model = new self();
  98. $model->loadDefaultValues();
  99. $model->status = StatusEnum::ENABLED;
  100. $model->mall_id = $mall_id;
  101. unset($data['id']);
  102. }
  103. foreach ($data as $key => $val) {
  104. if (
  105. ('checked_condition_values' === $key || 'checked_condition_keys' == $key || 'goods_ids' == $key) &&
  106. !empty($val) &&
  107. is_array($val)
  108. ) {
  109. $val = json_encode($val);
  110. }
  111. $model->$key = $val;
  112. }
  113. if (!$model->save()) {
  114. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  115. return false;
  116. }
  117. return $model;
  118. }
  119. public static function getListByUpgrade($params = null)
  120. {
  121. if (!empty($params['mall_id'])) {
  122. $lists = self::find()
  123. ->select('id,level,name')
  124. ->where(['mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED])
  125. ->orderBy('level asc')
  126. ->asArray()
  127. ->all();
  128. // 不要权重为 0 的等级
  129. // if (!in_array(0, array_column($lists, 'level'))) {
  130. // array_unshift($lists, [
  131. // 'name' => "默认等级",
  132. // 'level' => 0,
  133. // 'id' => 0,
  134. // ]);
  135. // }
  136. } else {
  137. $lists = [];
  138. // array_unshift($lists, [
  139. // 'name' => "默认等级",
  140. // 'level' => 0,
  141. // 'id' => 0,
  142. // ]);
  143. }
  144. return $lists;
  145. }
  146. public static function getLevelWeights(int $mall_id)
  147. {
  148. $lists = self::find()
  149. ->select('level')
  150. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])
  151. ->orderBy('level asc')
  152. ->asArray()
  153. ->column();
  154. // dd($lists);
  155. if (!empty($lists)) {
  156. for ($i = 1; $i <= 10; $i++) {
  157. $levels[] = [
  158. 'name' => '权重' . $i,
  159. 'level' => $i,
  160. 'disabled' => in_array($i, $lists),
  161. ];
  162. }
  163. } else {
  164. for ($i = 1; $i <= 10; $i++) {
  165. $levels[] = [
  166. 'name' => '权重' . $i,
  167. 'level' => $i,
  168. 'disabled' => false,
  169. ];
  170. }
  171. }
  172. return $levels;
  173. }
  174. }