| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Level;
- use app\models\User;
- use yii\base\Model;
- use yii\helpers\Json;
- class LevelForm extends Model
- {
- public $store_id;
- public $keyword;
- public $model;
- public $level;
- public $name;
- public $money;
- public $status;
- public $promotion_status;
- public $discount;
- public $content;
- public $price;
- public $image;
- public $buy_prompt;
- public $detail;
- public $synopsis;
- public $fybili;
- public $firstProfit;
- public $secondProfit;
- public $thirdProfit;
- public $expires_type;
- public $expires_in;
- public $pages_css;
- public $is_forever;
- public $upgrade_conditions;
- public $growth_value;
- public $growth_refund_value;
- public $downgrade_status;
- public $integral_enlarge;
- public $rules_text;
- public $id;
- public function rules()
- {
- return [
- [['name', 'money', 'synopsis', 'keyword'], 'trim'],
- [['name', 'image', 'keyword'], 'string'],
- [['level', 'name', 'status', 'pages_css', 'is_forever', 'upgrade_conditions','discount', 'integral_enlarge', 'promotion_status', 'rules_text'], 'required', 'on'=>'edit'],
- [['status','is_forever','expires_type', 'promotion_status', 'downgrade_status'], 'in', 'range'=>[0, 1]],
- [['upgrade_conditions'], 'safe'],
- [['discount'], 'number', 'min' => 0.1, 'max' => 10],
- [['money', 'price', 'fybili', 'growth_value', 'growth_refund_value'], 'number', 'min' => 0, 'max' => 99999999],
- [['level'], 'integer', 'min' => 0, 'max' => 100],
- [['content'], 'required', 'on' => 'content'],
- [['name', 'detail', 'buy_prompt'], 'string', 'max' => 255],
- [['firstProfit', 'secondProfit', 'thirdProfit'], 'number'],
- [['expires_in'], 'integer'],
- ['integral_enlarge','default','value'=>1]
- ];
- }
- public function attributeLabels()
- {
- return [
- 'level'=>'会员等级',
- 'name'=>'等级名称',
- 'money'=>'升级条件',
- 'status'=>'状态',
- 'discount'=>'折扣',
- 'content'=>'会员等级说明',
- 'price'=>'升级所需价格',
- 'image'=>'会员图片',
- 'buy_prompt'=>'购买前显示',
- 'detail'=>'会员说明',
- 'synopsis' => '小标题标题',
- 'fybili' => '返佣加速比例',
- 'firstProfit' => '一级分销金额',
- 'secondProfit' => '二级分销金额',
- 'thirdProfit' => '三级分销金额',
- 'expires_type' => '到期类型',
- 'expires_in' => '会员有效期时间'
- ];
- }
- public function search()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => '参数错误'
- ];
- }
- $query = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => Level::NOT_DELETE]);
- if ($this->name) {
- $query->andWhere(['like', 'name', $this->name]);
- }
- $query->select(['id','level','discount','money','price','growth_value','upgrade_conditions','status','is_student','growth_value','name','created_at']);
- $pagination = pagination_make($query,true, 'level DESC');
- $list = $pagination['list'];
- foreach ($list as &$value) {
- $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
- $value['upgrade_conditions'] = json_decode($value['upgrade_conditions'],2);
- $value['upgrade_conditions_text'] = [];
- if(in_array(Level::UPGRADE_CONDITIONS_MONEY,$value['upgrade_conditions']) && !empty($value['money'])){
- $value['upgrade_conditions_text'][] = Level::UPGRADE_CONDITIONS_MAP[Level::UPGRADE_CONDITIONS_MONEY]. ':' .$value['money'];
- }
- if(in_array(Level::UPGRADE_CONDITIONS_GROWTH,$value['upgrade_conditions']) && !empty($value['growth_value'])){
- $value['upgrade_conditions_text'][] = Level::UPGRADE_CONDITIONS_MAP[Level::UPGRADE_CONDITIONS_GROWTH]. ':' .$value['growth_value'];
- }
- if(in_array(Level::UPGRADE_CONDITIONS_PRICE,$value['upgrade_conditions']) && !empty($value['price'])){
- $value['upgrade_conditions_text'][] = Level::UPGRADE_CONDITIONS_MAP[Level::UPGRADE_CONDITIONS_PRICE]. ':' .$value['price'];
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ],
- ];
- }
- /**
- * 获取单条会员等级数据
- * @author: hankaige
- * @Time: 2025/4/16 15:48
- */
- public function getLevelItem(){
- if(empty($this->id)){
- return ['code' => 1,'msg' => 'ID 不能为空'];
- }
- $level = Level::findOne($this->id);
- if(empty($level)){
- return ['code' => 1,'msg' => '会员等级不存在'];
- }
- $level->pages_css = json_decode($level->pages_css,true);
- $level->upgrade_conditions = json_decode($level->upgrade_conditions,true);
- // 兼容一下老数据 之前老数据没有设置购买金额的时候 取消选中购买升级的条件
- if(empty($level->price) && in_array(Level::UPGRADE_CONDITIONS_PRICE,$level->upgrade_conditions)){
- $level->upgrade_conditions = array_diff($level->upgrade_conditions,[Level::UPGRADE_CONDITIONS_PRICE]);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $level
- ];
- }
- public function getAllLevel()
- {
- $list = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => Level::NOT_DELETE])
- ->select('id, level, name, discount')
- ->orderBy('level')
- ->asArray()->all();
- return $list;
- }
- public function getAllLevelList()
- {
- $list = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => Level::NOT_DELETE])
- ->select('id, level, name, discount')
- ->orderBy('level')
- ->asArray()->all();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list,
- ];
- }
- /**
- * @return array
- */
- public function save()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => '参数错误',
- 'data' => $this->getErrors()
- ];
- }
- $this->upgrade_conditions = json_decode($this->upgrade_conditions);
- if ($this->model->isNewRecord) {
- $this->model->is_delete = Level::NOT_DELETE;
- $this->model->created_at = time();
- }
- if ($this->level != $this->model->level) {
- $exit = Level::find()->where(['level' => $this->level, 'store_id' => $this->store_id, 'is_delete' => Level::NOT_DELETE])->exists();
- if ($exit) {
- return [
- 'code' => 1,
- 'msg' => '会员等级已存在'
- ];
- }
- }
- if ($this->name != $this->model->name) {
- $exit_0 = Level::find()->where(['name' => $this->name, 'store_id' => $this->store_id,
- 'is_delete' => Level::NOT_DELETE])->exists();
- if ($exit_0) {
- return [
- 'code' => 1,
- 'msg' => '等级名称重复'
- ];
- }
- }
- if($this->model->id && $this->model->level != $this->level) {
- $count = User::find()->where(['store_id' => $this->store_id, 'level' => $this->model->level])->count();
- if($count > 0) {
- return [
- 'code' => 1,
- 'msg' => '当前会员等级下有会员,禁止修改会员等级'
- ];
- }
- }
- if(in_array(Level::UPGRADE_CONDITIONS_MONEY,$this->upgrade_conditions) && $this->money <= 0){
- return [
- 'code' => 1,
- 'msg' => '累计消费金额升级条件不能小于0'
- ];
- }
- if(in_array(Level::UPGRADE_CONDITIONS_GROWTH,$this->upgrade_conditions) && $this->growth_value <= 0){
- return [
- 'code' => 1,
- 'msg' => '成长值升级条件不能小于0'
- ];
- }
- if (in_array(Level::UPGRADE_CONDITIONS_PRICE,$this->upgrade_conditions) && ($this->firstProfit > 0 || $this->secondProfit > 0 || $this->thirdProfit > 0) && floatval($this->price) <= 0) {
- return [
- 'code' => 1,
- 'msg' => '需要输入会员购买价格'
- ];
- }
- if(in_array(Level::UPGRADE_CONDITIONS_GROWTH,$this->upgrade_conditions) && $this->downgrade_status == 1 && $this->growth_refund_value <= 0){
- return [
- 'code' => 1,
- 'msg' => '开启会员会退后需设置每日消耗成长值数量'
- ];
- }
- if($this->is_forever == 0 && $this->expires_in == 0){
- return [
- 'code' => 1,
- 'msg' => '有效时间不能为空'
- ];
- }
- $this->model->store_id = $this->store_id;
- $this->model->level = $this->level;
- $this->model->name = $this->name;
- $this->model->pages_css = $this->pages_css;
- $this->model->status = $this->status;
- $this->model->is_forever = $this->is_forever;
- $this->model->expires_type = $this->expires_type;
- // 如果设置的是永久 将有效期设置为0 会员等级变动时候 会员有效期计算代码就不用做修改了
- $this->model->expires_in = $this->is_forever == 1 ? 0 : $this->expires_in;
- $this->model->upgrade_conditions = json_encode($this->upgrade_conditions);// 这个字段废弃不用了
- $this->model->money = $this->money;
- $this->model->growth_value = $this->growth_value;
- $this->model->downgrade_status = $this->downgrade_status;
- $this->model->growth_refund_value = $this->downgrade_status == 1 ? $this->growth_refund_value : 0;
- $this->model->price = $this->price;
- $this->model->discount = $this->discount;
- $this->model->integral_enlarge = $this->integral_enlarge ?? 1;// 默认为1倍
- $this->model->promotion_status = $this->promotion_status ?? 0;
- $this->model->firstProfit = $this->firstProfit;
- $this->model->secondProfit = $this->secondProfit;
- $this->model->thirdProfit = $this->thirdProfit;
- $this->model->synopsis = $this->synopsis ? $this->synopsis : '';
- $this->model->rules_text = $this->rules_text ? $this->rules_text : '';
- // $this->model->image = $this->image;
- // $this->model->buy_prompt = $this->buy_prompt;
- // $this->model->detail = $this->detail;
- // $this->model->fybili = $this->fybili;
- if ($this->model->save()) {
- return [
- 'code' => 0,
- 'msg' => '成功'
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '创建失败',
- 'data' => $this->model->getErrors()
- ];
- }
- }
- // /**
- // * 会员等级说明
- // * @return array|mixed
- // */
- // public function saveContent()
- // {
- // if (!$this->validate()) {
- // return $this->errorResponse;
- // }
- //
- // $store = Store::findOne(['id'=>$this->store_id]);
- // $store->member_content = $this->content;
- //
- // if ($store->save()) {
- // return [
- // 'code' => 0,
- // 'msg' => '成功'
- // ];
- // } else {
- // return $this->getErrorResponse($store);
- // }
- // }
- }
|