AddonsBossLevel.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace addons\BossWeight\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_weight_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 $goods_ids 指定商品id集合
  21. * @property string $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 AddonsBossLevel extends \common\models\base\BaseActiveRecord
  30. {
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function tableName()
  35. {
  36. return '{{%addons_boss_weight_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. public function getSetting()
  78. {
  79. return $this->hasOne(AddonsBossLevelSetting::class, ['level' => 'level']);
  80. }
  81. /**
  82. * 获取等级列表
  83. * @Author: lun
  84. * @DateTime: 2021/10/21 0021 9:20
  85. * @Copyright: copyright (c) 2021 广东七件事集团
  86. * @param array $cond
  87. * @param array $with
  88. * @param bool $is_array
  89. * @param string $select
  90. * @return array|\yii\db\ActiveRecord[]
  91. */
  92. public static function getBossLevel(array $cond=[], array $with=[],bool $is_array=true,string $select='*',$order=""){
  93. $default_cond = [['<>','status',StatusEnum::DELETE]];
  94. $cond = array_merge($default_cond,$cond);
  95. $query = self::find()->select($select);
  96. self::paramPack(['where'=>$cond, 'with'=>$with],$query);
  97. isset($order) && $query->orderBy($order);
  98. $data = $query->asArray($is_array)->all();
  99. return $data;
  100. }
  101. /**
  102. * 获取等级等级和权重
  103. * @Author: lun
  104. * @DateTime: 2021/10/21 0021 9:22
  105. * @Copyright: copyright (c) 2021 广东七件事集团
  106. * @param $mall_id
  107. * @return array
  108. */
  109. public static function getLevelWeights($mall_id) {
  110. $list = self::find()->select('level')->where(['status' => [StatusEnum::DISABLED,StatusEnum::ENABLED], 'mall_id' => $mall_id])->column();
  111. $newList = [];
  112. for ($i = 1; $i <= 10; $i++) {
  113. $newList[] = [
  114. 'name' => '等级' . $i,
  115. 'level' => $i,
  116. 'disabled' => in_array($i, $list),
  117. ];
  118. }
  119. array_unshift($newList, [
  120. 'name' => "默认等级",
  121. 'level' => 0,
  122. 'disabled' => true,
  123. ]);
  124. return $newList;
  125. }
  126. /**
  127. * 保存数据
  128. * @Author: lun
  129. * @DateTime: 2021/10/20 0020 18:01
  130. * @Copyright: copyright (c) 2021 广东七件事集团
  131. * @param array $params
  132. * @return AddonsBossLevel|bool
  133. */
  134. public static function setData(array $params){
  135. try{
  136. if(!empty($params['id'])){
  137. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
  138. if(empty($model)) throw new \Exception('等级不存在或已删除');
  139. // 判断等级是否有被修改
  140. if ($model->level != $params['level']) {
  141. // 判断该等级下是否有股东存在
  142. $boss = AddonsBoss::find()->where(['mall_id' => $model->mall_id, 'level' => $model->level, 'status' => StatusEnum::ENABLED])->asArray()->one();
  143. if ($boss) throw new Exception('该等级下存在股东,不能被修改');
  144. }
  145. }else{
  146. $model = new self();
  147. $model->loadDefaultValues();
  148. $model->mall_id = $params['mall_id'];
  149. }
  150. if(!$model->load($params,'') || !$model->save()){
  151. throw new Exception($model->getErrorMessage());
  152. }
  153. // 插入配置详情
  154. $setting = ['mall_id' => $params['mall_id'], 'level' => $model->level, 'status' => $model->status];
  155. $setting = empty($params['setting']) ? $setting : array_merge($setting, Json::decode($params['setting'], true));
  156. $setting['checked_weight_values'] = json_encode($setting['checked_weight_values']);
  157. $res = AddonsBossLevelSetting::setData($setting);
  158. if ($res == false) throw new Exception(AddonsBossLevelSetting::getStaticError());
  159. return $model;
  160. }catch(\Exception $e){
  161. self::$static_error = $e->getMessage();
  162. return false;
  163. }
  164. }
  165. /**
  166. * 获取升级列表
  167. * @Author: lun
  168. * @DateTime: 2021/10/21 0021 15:05
  169. * @Copyright: copyright (c) 2021 广东七件事集团
  170. * @param $params
  171. * @return array
  172. */
  173. public static function getListByUpgrade($params)
  174. {
  175. $rows = [];
  176. if (!empty($params['mall_id'])) {
  177. $where = ['mall_id' => $params['mall_id'],'status'=>StatusEnum::ENABLED];
  178. $list = self::findAll($where);
  179. foreach ($list as $v) {
  180. $rows[] = ['level' => $v['level'], 'name' => $v['name']];
  181. }
  182. }
  183. return $rows;
  184. }
  185. /**
  186. * 获取等级
  187. * @Author: lun
  188. * @DateTime: 2021/12/16 0016 9:17
  189. * @Copyright: copyright (c) 2021 广东七件事集团
  190. * @param $mall_id
  191. * @return array
  192. */
  193. public static function getLevelByColumn($mall_id){
  194. return self::find()
  195. ->select('name')
  196. ->where(array('mall_id'=>$mall_id,'status'=>StatusEnum::ENABLED))
  197. ->asArray()
  198. ->indexBy('level')
  199. ->orderBy('level desc')
  200. ->column();
  201. }
  202. }