UserLevel.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace common\models\user;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use common\models\common\MallSetting;
  6. use addons\EqualSubsidy\common\models\EqualSubsidyUserLevelConfig;
  7. use PHPUnit\Util\Exception;
  8. use Yii;
  9. /**
  10. * This is the model class for table "{{%user_level}}".
  11. *
  12. *
  13. * @property int $id
  14. * @property int $mall_id
  15. * @property int $level 会员等级
  16. * @property string $name 等级名称
  17. * @property string $pic_url 图标
  18. * @property string $discount 折扣
  19. * @property int $status 状态[-1:删除;0:禁用;1启用]
  20. * @property int $created_at
  21. * @property int $updated_at
  22. * @property int $is_specified_level 指定等级会员价展示[0否;1是]
  23. * @property string $specified_level 指定等级
  24. */
  25. class UserLevel extends BaseActiveRecord
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function tableName()
  31. {
  32. return '{{%user_level}}';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['mall_id', 'level'], 'required'],
  41. [['mall_id', 'level', 'status', 'created_at', 'updated_at','is_open_expired','expired_day','member_price_is_show', 'is_specified_level'], 'integer'],
  42. [['name', 'pic_url', 'specified_level'], 'string', 'max' => 255],
  43. [['discount'], 'number', 'min' => 0, 'max' => 10],
  44. ];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => 'ID',
  53. 'mall_id' => 'Mall ID',
  54. 'level' => '会员等级',
  55. 'name' => '等级名称',
  56. 'pic_url' => '图标',
  57. 'discount' => '折扣',
  58. 'status' => '状态[-1:删除;0:禁用;1启用]',
  59. 'created_at' => 'Created At',
  60. 'updated_at' => 'Updated At',
  61. 'expired_day' => 'Expired Day',
  62. 'is_open_expired' => 'Is Open Expired',
  63. 'member_price_is_show' => 'Member Price Is Show',
  64. 'is_specified_level' => '指定等级会员价展示[0否;1是]',
  65. 'specified_level' => '指定等级'
  66. ];
  67. }
  68. public function getMemberBenefitList()
  69. {
  70. return $this->hasMany(MemberBenefit::class, ['level_id' => 'id']);
  71. }
  72. public function getUpgradeCondition()
  73. {
  74. return $this->hasOne(UserLevelUpgradeCondition::class, ['user_level_id'=>'id']);
  75. }
  76. public function getEqualSubsidy()
  77. {
  78. return $this->hasOne(EqualSubsidyUserLevelConfig::class, ['user_level_id'=>'id']);
  79. }
  80. /**
  81. * 保存数据
  82. * @param array $params
  83. * @return bool|UserLevel
  84. */
  85. public static function setData(array $params)
  86. {
  87. $t = Yii::$app->db->beginTransaction();
  88. try {
  89. if (!empty($params['id'])) {
  90. $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  91. if (empty($model)) throw new Exception('服务不存在或已删除');
  92. $where[] = ['<>', 'id', $params['id']];
  93. } else {
  94. $model = new self();
  95. $model->loadDefaultValues();
  96. }
  97. $where[] = ['mall_id' => $params['mall_id']];
  98. $where[] = ['level' => $params['level']];
  99. $res = UserLevel::getUserLevelOne($where);
  100. if (!empty($res)) throw new Exception('会员等级出现重复');
  101. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  102. $params['user_level_id'] = $model->id;
  103. $res = UserLevelUpgradeCondition::setData($params);
  104. if ($res === false) throw new Exception(UserLevelUpgradeCondition::getStaticError());
  105. if(!empty($params['benefits'])){
  106. $params['level_id'] = $model->id;
  107. $res = MemberBenefit::setData($params);
  108. if ($res === false) throw new Exception(MemberBenefit::getStaticError());
  109. }else{
  110. $old_data = MemberBenefit::findAll(['level_id' => $model->id, 'status' => StatusEnum::ENABLED]);
  111. foreach($old_data as $benefit){
  112. $benefit->status = StatusEnum::DELETE;
  113. $benefit->save();
  114. }
  115. }
  116. $t->commit();
  117. return $model;
  118. } catch (Exception $e) {
  119. $t->rollBack();
  120. self::$static_error = $e->getMessage();
  121. return false;
  122. }
  123. }
  124. /**
  125. * 删除
  126. * @param $params
  127. * @param $status
  128. * @return bool
  129. */
  130. public static function del($params, $status)
  131. {
  132. $t = Yii::$app->db->beginTransaction();
  133. try {
  134. $query = self::find()->where(['and',
  135. ['id' => $params['id']],
  136. ['<>', 'status', StatusEnum::DELETE]
  137. ]);
  138. $model = $query->one();
  139. $res = User::findOne(['level'=>$model->level,'mall_id'=>$params['mall_id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  140. if($res) throw new Exception('该等级关联了会员');
  141. $model->status = $status;
  142. $res = $model->save();
  143. if ($res === false) throw new Exception('删除失败');
  144. $where[] = ['mall_id' => $params['mall_id']];
  145. $where[] = ['user_level_id' => $params['id']];
  146. $res = UserLevelUpgradeCondition::del($where, $status);
  147. if ($res === false) throw new Exception(UserLevelUpgradeCondition::getStaticError());
  148. $t->commit();
  149. return true;
  150. } catch (\Exception $e) {
  151. $t->rollBack();
  152. self::$static_error = $e->getMessage();
  153. return false;
  154. }
  155. }
  156. /**
  157. * 获取等级列表
  158. * @return array
  159. */
  160. public static function getLevelList($mall_id)
  161. {
  162. $levelList = [];
  163. $list = self::find()->select('level')->where(['and',['<>', 'status', StatusEnum::DELETE],['mall_id'=>$mall_id]])->column();
  164. for ($i = 1; $i <= 50; $i++) {
  165. $levelList[] = [
  166. 'name' => '权重' . $i,
  167. 'level' => $i,
  168. 'disabled' => in_array($i, $list),
  169. ];
  170. }
  171. return $levelList;
  172. }
  173. /**
  174. * 获取会员等级列表
  175. * @param array $cond
  176. * @param array $with
  177. * @param int $is_array
  178. * @param string $select
  179. * @return array|UserLevel[]|\yii\db\ActiveRecord[]
  180. */
  181. public static function getUserLevel(array $cond, array $with = [], int $is_array = 0, string $select = '*', $orderBy = 'level asc')
  182. {
  183. $default_cond = [
  184. ['<>', 'status', StatusEnum::DELETE]
  185. ];
  186. $cond = array_merge($cond, $default_cond);
  187. $query = self::find()->select($select);
  188. self::paramPack(['where' => $cond, 'with' => $with], $query);
  189. return $query->asArray($is_array)->orderBy($orderBy)->all();
  190. }
  191. /**
  192. * 获取会员等级列表
  193. * @param array $cond
  194. * @param array $with
  195. * @param int $is_array
  196. * @param string $select
  197. * @return array|UserLevel[]|\yii\db\ActiveRecord[]
  198. */
  199. public static function getUserLevelOne(array $cond, array $with = [], int $is_array = 0, string $select = '*')
  200. {
  201. $default_cond = [
  202. ['<>', 'status', StatusEnum::DELETE]
  203. ];
  204. $cond = array_merge($cond, $default_cond);
  205. $query = self::find()->select($select);
  206. self::paramPack(['where' => $cond, 'with' => $with], $query);
  207. return $query->asArray($is_array)->one();
  208. }
  209. /**
  210. * 会员等级数组
  211. * @param $mall_id
  212. * @param $type
  213. * @return array
  214. */
  215. public static function getUserLevelArr($mall_id, $type = 1, $is_array = false)
  216. {
  217. $list = self::getUserLevel([['mall_id' => $mall_id]], [], $is_array, 'name,level');
  218. if ($type == 1) {
  219. return array_column($list, 'name', 'level');
  220. } else {
  221. return $list;
  222. }
  223. }
  224. /**
  225. * 用户列表获取
  226. * @param $mall_id
  227. * @param string $select
  228. * @return array|UserLevel[]|\yii\db\ActiveRecord[]
  229. */
  230. public static function getUserLevelList($mall_id, string $select = 'name,level')
  231. {
  232. $list = self::getUserLevel([['mall_id' => $mall_id]], [], true, $select);
  233. if (!empty($list)) {
  234. $list = array_column($list, null, 'level');
  235. }
  236. return $list;
  237. }
  238. /**
  239. * 条件升级数据源使用
  240. * @param $params
  241. * @return array
  242. */
  243. public static function getUserListByUpgrade($params)
  244. {
  245. $rows = [];
  246. if (!empty($params['mall_id'])) {
  247. $where[] = ['mall_id' => $params['mall_id']];
  248. $list = self::getUserLevel($where, [], 0);
  249. foreach ($list as $v) {
  250. $rows[] = ['level' => $v['level'], 'name' => $v['name']];
  251. }
  252. }
  253. return $rows;
  254. }
  255. public static function getListIndexByLevelColumn($mall_id,$status=StatusEnum::ENABLED){
  256. $where = ['mall_id'=>$mall_id];
  257. if($status) $where['status'] = $status;
  258. return self::find()
  259. ->select('name')
  260. ->where($where)
  261. ->asArray()
  262. ->indexBy('level')
  263. ->orderBy('level desc')
  264. ->column();
  265. }
  266. /**
  267. * 获取包含默认等级的会员等级列表
  268. *
  269. * @param integer $mall_id
  270. * @return array
  271. */
  272. public static function getList(int $mall_id)
  273. {
  274. $list = static::find()->where(['mall_id' => $mall_id])
  275. ->andWhere(['>', 'status', StatusEnum::DELETE])
  276. ->select('level, name')
  277. ->asArray()
  278. ->all();
  279. array_unshift($list, ['level' => 0, 'name' => MallSetting::getDefaultLevelName($mall_id)]);
  280. return $list;
  281. }
  282. /**
  283. * 获取包含默认等级的会员等级列表 只返回启用的等级
  284. *
  285. * @param integer $mall_id
  286. * @return array
  287. */
  288. public static function getEnableLevelList(int $mall_id)
  289. {
  290. $list = static::find()->where(['mall_id' => $mall_id])
  291. ->andWhere(['=', 'status', StatusEnum::ENABLED])
  292. ->select('level, name')
  293. ->orderBy('level asc')
  294. ->asArray()
  295. ->all();
  296. array_unshift($list, ['level' => 0, 'name' => MallSetting::getDefaultLevelName($mall_id)]);
  297. return $list;
  298. }
  299. }