AddonsBossLevel.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace addons\Boss\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_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. * @property int $upgrade_type_store_goods 购买门店商品升级,0关闭,1开启
  29. * @property int $buy_store_goods_type 购买门店商品触发升级:0订单完成 1下单完成
  30. * @property int $store_goods_type 门店商品升级类型,1- 任意商品 2- 指定商品
  31. * @property int $store_goods_ids 门店商品id集合
  32. * @property int $store_goods_list 门店商品集合
  33. */
  34. class AddonsBossLevel extends \common\models\base\BaseActiveRecord
  35. {
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function tableName()
  40. {
  41. return '{{%addons_boss_level}}';
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function rules()
  47. {
  48. return [
  49. [['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'],
  50. [['checked_condition_values', 'checked_condition_keys', 'explain'], 'string'],
  51. [['name'], 'string', 'max' => 45],
  52. [['goods_ids'], 'string', 'max' => 3000],
  53. [['goods_list'], 'string'],
  54. [['upgrade_type_store_goods','buy_store_goods_type','store_goods_type'],'integer'],
  55. [['store_goods_ids','store_goods_list'],'string']
  56. ];
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function attributeLabels()
  62. {
  63. return [
  64. 'id' => 'ID',
  65. 'mall_id' => '商城ID',
  66. 'level' => '股东等级',
  67. 'name' => '等级名称',
  68. 'is_auto_upgrade' => '是否自动升级【0否,1是】',
  69. 'upgrade_type_condition' => '是否开启条件升级【0否,1是】',
  70. 'condition_type' => '满足条件:1满足其一,2满足所有',
  71. 'checked_condition_values' => '升级条件的值集合',
  72. 'checked_condition_keys' => '被选中的升级条件的key集合',
  73. 'upgrade_type_goods' => '是否开启购买商品升级【0否,1是】',
  74. 'goods_ids' => '指定商品id集合',
  75. 'goods_list' => '指定商品信息列表json',
  76. 'goods_type' => '商品升级类型:1任意商品,2指定商品',
  77. 'buy_goods_type' => '购买商品升级条件:0订单完成,1支付完成',
  78. 'explain' => '等级说明',
  79. 'status' => '状态[-1:删除;0:禁用;1启用]',
  80. 'created_at' => 'Created At',
  81. 'updated_at' => 'Updated At',
  82. ];
  83. }
  84. public function getSetting()
  85. {
  86. return $this->hasOne(AddonsBossLevelSetting::class, ['level' => 'level','mall_id' => 'mall_id'])->andWhere(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]);
  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 getBossLevel(array $cond=[], array $with=[],bool $is_array=true,string $select='*',$order=""){
  100. $default_cond = [['<>','status',StatusEnum::DELETE]];
  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 AddonsBossLevel|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 = AddonsBoss::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' => $model->level, 'status' => $model->status];
  163. $setting = empty($params['setting']) ? $setting : array_merge($setting, Json::decode($params['setting'], true));
  164. if (!empty($setting)) { // 配置详情状态根据权重状态变更
  165. $setting['status'] = $model->status;
  166. }
  167. $res = AddonsBossLevelSetting::setData($setting);
  168. if ($res == false) throw new Exception(AddonsBossLevelSetting::getStaticError());
  169. $trans->commit();
  170. return $model;
  171. }catch(\Exception $e){
  172. $trans->rollBack();
  173. self::$static_error = $e->getMessage();
  174. return false;
  175. }
  176. }
  177. /**
  178. * 获取升级列表
  179. * @Author: lun
  180. * @DateTime: 2021/10/21 0021 15:05
  181. * @Copyright: copyright (c) 2021 广东七件事集团
  182. * @param $params
  183. * @return array
  184. */
  185. public static function getListByUpgrade($params)
  186. {
  187. $rows = [];
  188. if (!empty($params['mall_id'])) {
  189. $where = ['mall_id' => $params['mall_id'],'status'=>StatusEnum::ENABLED];
  190. $list = self::findAll($where);
  191. foreach ($list as $v) {
  192. $rows[] = ['level' => $v['level'], 'name' => $v['name']];
  193. }
  194. }
  195. return $rows;
  196. }
  197. /**
  198. * 获取等级
  199. * @Author: lun
  200. * @DateTime: 2021/12/16 0016 9:17
  201. * @Copyright: copyright (c) 2021 广东七件事集团
  202. * @param $mall_id
  203. * @return array
  204. */
  205. public static function getLevelByColumn($mall_id){
  206. return self::find()
  207. ->select('name')
  208. ->where(array('mall_id'=>$mall_id,'status'=>StatusEnum::ENABLED))
  209. ->asArray()
  210. ->indexBy('level')
  211. ->orderBy('level desc')
  212. ->column();
  213. }
  214. }