RebateUserLevel.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace addons\Rebate\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. /**
  7. *
  8. * @property int $id
  9. * @property int $mall_id 商城ID
  10. * @property int $level
  11. * @property string $name 等级名称
  12. * @property int $is_auto_upgrade 是否开启自动升级
  13. * @property int $upgrade_type_condition 是否开启条件升级
  14. * @property string $checked_condition_values 升级条件
  15. * @property string $checked_condition_keys 升级条件选中的key集合
  16. * @property int $condition_type 0 未选择、1满足其一 2、满足所有
  17. * @property int $upgrade_type_goods 购买升级商品,0关闭,1开启
  18. * @property int $buy_goods_type 购买升级商品触发升级:0订单完成 1下单完成
  19. * @property int $goods_type 商品升级类型,1- 任意商品 2- 指定商品
  20. * @property string $goods_ids 商品id集合
  21. * @property string $goods_list 商品列表json
  22. * @property string $explain 等级说明
  23. * @property int $status 状态[-1:删除;0:禁用;1启用]
  24. * @property int $created_at
  25. * @property int $updated_at
  26. */
  27. class RebateUserLevel extends BaseActiveRecord
  28. {
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public static function tableName()
  33. {
  34. return '{{%addons_rebate_user_level}}';
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['mall_id', 'checked_condition_values'], 'required'],
  43. [['mall_id', 'level', 'is_auto_upgrade', 'upgrade_type_condition', 'condition_type', 'upgrade_type_goods',
  44. 'buy_goods_type', 'goods_type', 'status', 'created_at', 'updated_at'], 'integer'],
  45. [['checked_condition_values'], 'string'],
  46. [['name'], 'string', 'max' => 45],
  47. [['checked_condition_keys'], 'string', 'max' => 1000],
  48. [['goods_ids'], 'string', 'max' => 3000],
  49. [['goods_list', 'explain'], 'string'],
  50. ];
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function attributeLabels()
  56. {
  57. return [
  58. 'id' => 'ID',
  59. 'mall_id' => '商城ID',
  60. 'level' => 'Level',
  61. 'name' => '等级名称',
  62. 'is_auto_upgrade' => '是否开启自动升级',
  63. 'upgrade_type_condition' => '是否开启条件升级',
  64. 'checked_condition_values' => '升级条件',
  65. 'checked_condition_keys' => '升级条件选中的key集合',
  66. 'condition_type' => '0 未选择、1满足其一 2、满足所有',
  67. 'upgrade_type_goods' => '购买升级商品,0关闭,1开启',
  68. 'buy_goods_type' => '购买升级商品触发升级:0订单完成 1下单完成',
  69. 'goods_type' => '商品升级类型,1- 任意商品 2- 指定商品',
  70. 'goods_ids' => '商品id集合',
  71. 'goods_list' => '商品列表json',
  72. 'explain' => '等级说明',
  73. 'status' => '状态[-1:删除;0:禁用;1启用]',
  74. 'created_at' => 'Created At',
  75. 'updated_at' => 'Updated At',
  76. ];
  77. }
  78. public static function setData(array $params)
  79. {
  80. $t = \Yii::$app->db->beginTransaction();
  81. try {
  82. if(!empty($params['id'])){
  83. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  84. if (empty($model)) throw new \Exception('服务不存在或已删除');
  85. }else{
  86. $model = new self();
  87. $model->loadDefaultValues();
  88. }
  89. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  90. if (empty($params['checked_condition_keys'])) $params['checked_condition_keys'] = [];
  91. if (empty($params['checked_condition_values'])) $params['checked_condition_values'] = [];
  92. if (empty($params['goods_ids'])) $params['goods_ids'] = [];
  93. if (empty($params['goods_list'])) $params['goods_list'] = [];
  94. $model->checked_condition_keys = json_encode($params['checked_condition_keys'], JSON_NUMERIC_CHECK);
  95. $model->checked_condition_values = json_encode($params['checked_condition_values'], JSON_NUMERIC_CHECK);
  96. $model->goods_ids = json_encode($params['goods_ids'], JSON_NUMERIC_CHECK);
  97. $model->goods_list = json_encode($params['goods_list'], JSON_NUMERIC_CHECK);
  98. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  99. $t->commit();
  100. return $model;
  101. } catch (\Exception $e) {
  102. $t->rollBack();
  103. self::$static_error = $e->getMessage();
  104. return false;
  105. }
  106. }
  107. public static function del($params){
  108. try{
  109. $model = self::findOne(['id'=>$params['id']]);
  110. if(!empty($model)){
  111. $res = RebateUser::findOne(['level'=>$model->level,'mall_id'=>$params['mall_id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  112. if(!empty($res)) throw new \Exception('该等级关联了推三返一用户');
  113. $model->status = StatusEnum::DELETE;
  114. $res = $model->save();
  115. if($res==false) throw new \Exception($model->getErrorMessage());
  116. }
  117. return true;
  118. }catch (\Exception $e){
  119. self::$static_error = $e->getMessage();
  120. return false;
  121. }
  122. }
  123. public static function getLevelWeights($mall_id)
  124. {
  125. $list = self::find()->select('level')->where(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED], 'mall_id' => $mall_id])->column();
  126. $newList = [];
  127. for ($i = 1; $i <= 10; $i++) {
  128. $newList[] = [
  129. 'name' => '等级' . $i,
  130. 'level' => $i,
  131. 'disabled' => in_array($i, $list),
  132. ];
  133. }
  134. return $newList;
  135. }
  136. public static function getLevelList(array $cond = [], array $with = [], bool $is_array = true, string $select = '*')
  137. {
  138. $default_cond = [['<>', 'status', StatusEnum::DELETE]];
  139. $cond = array_merge($default_cond, $cond);
  140. $query = self::find()->select($select);
  141. self::paramPack(['where' => $cond, 'with' => $with], $query);
  142. return $query->asArray($is_array)->all();
  143. }
  144. public static function getLevelByColumn($mall_id){
  145. return self::find()
  146. ->select('name')
  147. ->where(array('mall_id'=>$mall_id,'status'=>StatusEnum::ENABLED))
  148. ->asArray()
  149. ->indexBy('level')
  150. ->orderBy('level desc')
  151. ->column();
  152. }
  153. public static function getRebateUserLevelArr($mall_id, $type = 1)
  154. {
  155. $rebate_user_level_arr = RebateUserLevel::lists(['where'=>[['mall_id'=>$mall_id], ['status'=>StatusEnum::ENABLED]], 'select'=>'level,name']);
  156. if ($type == 1) {
  157. return array_column($rebate_user_level_arr, 'name', 'level');
  158. } else {
  159. return $rebate_user_level_arr;
  160. }
  161. }
  162. }