BossAchievementLevel.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace addons\BossAchievement\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. use yii\db\Exception;
  6. use yii\helpers\Json;
  7. /**
  8. * This is the model class for table "{{%addons_boss_achievement_level}}".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城ID
  12. * @property int $level 股东等级
  13. * @property string $name 等级名称
  14. * @property int $is_auto_upgrade 是否自动升级【0否,1是】
  15. * @property int $upgrade_type_condition 是否开启条件升级【0否,1是】
  16. * @property int $condition_type 满足条件:1满足其一,2满足所有
  17. * @property string|null $checked_condition_values 升级条件的值集合
  18. * @property string|null $checked_condition_keys 被选中的升级条件的key集合
  19. * @property int $upgrade_type_goods 是否开启购买商品升级【0否,1是】
  20. * @property string|null $goods_ids 指定商品id集合
  21. * @property string|null $goods_list 指定商品信息列表json
  22. * @property int $goods_type 商品升级类型:1任意商品,2指定商品
  23. * @property int $buy_goods_type 购买商品升级条件:0订单完成,1支付完成
  24. * @property string|null $explain 等级说明
  25. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  26. * @property int $created_at
  27. * @property int $updated_at
  28. */
  29. class BossAchievementLevel extends \common\models\base\BaseActiveRecord
  30. {
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function tableName()
  35. {
  36. return '{{%addons_boss_achievement_level}}';
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['mall_id', 'level', 'is_auto_upgrade', 'upgrade_type_condition', 'condition_type', 'upgrade_type_goods', 'goods_type', 'buy_goods_type', 'status', 'created_at', 'updated_at'], 'integer'],
  45. [['checked_condition_values', 'checked_condition_keys', 'explain'], 'string'],
  46. [['name'], 'string', 'max' => 45],
  47. [['goods_ids'], 'string', 'max' => 3000],
  48. [['goods_list'], 'string', 'max' => 5000],
  49. ];
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function attributeLabels()
  55. {
  56. return [
  57. 'id' => 'ID',
  58. 'mall_id' => '商城ID',
  59. 'level' => '股东等级',
  60. 'name' => '等级名称',
  61. 'is_auto_upgrade' => '是否自动升级【0否,1是】',
  62. 'upgrade_type_condition' => '是否开启条件升级【0否,1是】',
  63. 'condition_type' => '满足条件:1满足其一,2满足所有',
  64. 'checked_condition_values' => '升级条件的值集合',
  65. 'checked_condition_keys' => '被选中的升级条件的key集合',
  66. 'upgrade_type_goods' => '是否开启购买商品升级【0否,1是】',
  67. 'goods_ids' => '指定商品id集合',
  68. 'goods_list' => '指定商品信息列表json',
  69. 'goods_type' => '商品升级类型:1任意商品,2指定商品',
  70. 'buy_goods_type' => '购买商品升级条件:0订单完成,1支付完成',
  71. 'explain' => '等级说明',
  72. 'status' => '状态[-1:删除;0:禁用;1启用]',
  73. 'created_at' => 'Created At',
  74. 'updated_at' => 'Updated At',
  75. ];
  76. }
  77. /**
  78. * 关联等级设置
  79. * @Author: lun
  80. * @DateTime: 2022/7/10 0010 10:24
  81. * @Copyright: copyright (c) 2021 广东七件事集团
  82. * @return \yii\db\ActiveQuery
  83. */
  84. public function getSetting()
  85. {
  86. return $this->hasOne(BossAchievementLevelSetting::class, ['level' => 'level']);
  87. }
  88. /**
  89. * 获取等级列表
  90. * @Author: lun
  91. * @DateTime: 2021/10/21 0021 9:20
  92. * @Copyright: copyright (c) 2021 广东七件事集团
  93. * @param array $cond
  94. * @param array $with
  95. * @param bool $is_array
  96. * @param string $select
  97. * @return array|\yii\db\ActiveRecord[]
  98. */
  99. public static function getBossAchievementLevel(array $cond=[], array $with=[],bool $is_array=true,string $select='*',$order=""){
  100. $default_cond = [['=','status',StatusEnum::ENABLED]];
  101. $cond = array_merge($default_cond,$cond);
  102. $query = self::find()->select($select);
  103. self::paramPack(['where'=>$cond, 'with'=>$with],$query);
  104. isset($order) && $query->orderBy($order);
  105. $data = $query->asArray($is_array)->all();
  106. return $data;
  107. }
  108. /**
  109. * 获取等级等级和权重
  110. * @Author: lun
  111. * @DateTime: 2021/10/21 0021 9:22
  112. * @Copyright: copyright (c) 2021 广东七件事集团
  113. * @param $mall_id
  114. * @return array
  115. */
  116. public static function getLevelWeights($mall_id) {
  117. $list = self::find()->select('level')->where(['status' => [StatusEnum::DISABLED,StatusEnum::ENABLED], 'mall_id' => $mall_id])->column();
  118. $newList = [];
  119. for ($i = 1; $i <= 10; $i++) {
  120. $newList[] = [
  121. 'name' => '等级' . $i,
  122. 'level' => $i,
  123. 'disabled' => in_array($i, $list),
  124. ];
  125. }
  126. array_unshift($newList, [
  127. 'name' => "默认等级",
  128. 'level' => 0,
  129. 'disabled' => true,
  130. ]);
  131. return $newList;
  132. }
  133. /**
  134. * 保存数据
  135. * @Author: lun
  136. * @DateTime: 2021/10/20 0020 18:01
  137. * @Copyright: copyright (c) 2021 广东七件事集团
  138. * @param array $params
  139. * @return BossAchievementLevel|bool
  140. */
  141. public static function setData(array $params){
  142. $trans = Yii::$app->db->beginTransaction();
  143. try{
  144. if(!empty($params['id'])){
  145. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
  146. if(empty($model)) throw new \Exception('等级不存在或已删除');
  147. // 判断等级是否有被修改
  148. if ($model->level != $params['level']) {
  149. // 判断该等级下是否有股东存在
  150. $boss = BossAchievementUser::find()->where(['mall_id' => $model->mall_id, 'level' => $model->level, 'status' => StatusEnum::ENABLED])->asArray()->one();
  151. if ($boss) throw new \Exception('该等级下存在股东,不能被修改');
  152. }
  153. }else{
  154. $model = new self();
  155. $model->loadDefaultValues();
  156. $model->mall_id = $params['mall_id'];
  157. }
  158. if(!$model->load($params,'') || !$model->save()){
  159. throw new Exception($model->getErrorMessage());
  160. }
  161. // 插入配置详情
  162. $setting = ['mall_id' => $params['mall_id'], 'level_id' => $model->id, 'level' => $model->level, 'status' => $model->status];
  163. $setting = empty($params['setting']) ? $setting : array_merge($setting, Json::decode($params['setting'], true));
  164. $res = BossAchievementLevelSetting::setData($setting);
  165. if ($res == false) throw new Exception(BossAchievementLevelSetting::getStaticError());
  166. $trans->commit();
  167. return $model;
  168. }catch(\Exception $e){
  169. $trans->rollBack();
  170. self::$static_error = $e->getMessage();
  171. return false;
  172. }
  173. }
  174. }