| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use yii\base\Model;
- use app\models\Coupon;
- use app\models\VerifyCard;
- use app\models\BusinessMember;
- use app\models\BusinessRightInfo;
- use app\models\BusinessMemberOrder;
- use app\models\SaasUser;
- use app\models\Store;
- use app\models\BusinessRightDuration;
- class BusinessMemberForm extends Model
- {
- public $id;
- public $name;
- public $image;
- public $bg_image;
- public $get_type;
- public $give;
- public $give_coupon;
- public $give_verify;
- public $give_self;
- public $coupon_info;
- public $verify_info;
- public $self_info;
- public $sale_info;
- public $commission_type;
- public $commission_one;
- public $commission_two;
- public $commission_three;
- public $right_status;
- public $right_ids;
- public $right_info;
- public $right_default;
- public $sort;
- public $is_delete;
- public $search_type; //0.全部 1.使用中 2.已过期
- public $start_time;
- public $end_time;
- public $page;
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['name','image','bg_image','give'], 'required'],
- [['id','sort', 'give', 'give_coupon', 'give_verify', 'give_self', 'commission_type', 'right_status','is_delete','get_type','page','search_type','self_info','give_self'], 'integer'],
- [['image', 'bg_image','coupon_info','verify_info','right_ids','right_info','right_default','sale_info','self_info','start_time','end_time'], 'string'],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'name' => '会员卡名称',
- 'image' => '图片',
- 'bg_image' => '背景图片',
- 'get_type' => '获取方式',
- 'sale_info' => '购买详情',
- 'give' => '赠送开关',
- 'give_coupon' => '优惠券赠送开关',
- 'give_verify' => '核销卡赠送开关',
- 'give_self' => '开启自购返利',
- 'coupon_info' => '赠送优惠券信息',
- 'verify_info' => '赠送卡券信息',
- 'self_info' => '自购返利',
- 'commission_type' => '佣金类型:1百分比 2固定金额',
- 'commission_one' => '一级佣金',
- 'commission_two' => '二级佣金',
- 'commission_three' => '三级佣金',
- 'right_status' => '权益开关',
- 'right_ids' => '权利ids',
- 'right_info' => '权益信息',
- 'right_default' => '权益详情',
- ];
- }
- public function list($status=''){
- $query = BusinessMember::find()->where(['is_delete'=>0]);
- if( strlen($status) > 0 && ($status==1 || $status==0) ){
- $query->andWhere(['status'=>$status]);
- }
- $countQuery = clone $query;
- $count = $countQuery->count();
- $page = post_params('page', 1);
- $pageSize = post_params('pageSize', 10);
- $query->offset(($page - 1) * $pageSize)->limit($pageSize);
- $list = $query->asArray()->all();
- foreach($list as $k=>$v){
- //计算售卖总数
- $list[$k]['num'] = 0;
- //计算售卖金额
- $list[$k]['total_amount'] = 0;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $page,
- 'totalCount' => $count,
- ],
- ];
- }
- public function save(){
- if($this->attributes['id']){
- $member = BusinessMember::find()->where(['id'=>$this->attributes['id']])->one();
- }else{
- $member = new BusinessMember();
- }
- $member->attributes = $this->attributes;
- $member->sort = (empty($member->attributes['sort'])) ? 0 : $member->attributes['sort'];
- $member->coupon_info = (is_null($member->attributes['coupon_info'])) ? '' : json_encode($member->attributes['coupon_info']);
- $member->right_info = (is_null($member->attributes['right_info'])) ? '' : json_encode($member->attributes['right_info']);
- $member->verify_info = (is_null($member->attributes['verify_info'])) ? '' : json_encode($member->attributes['verify_info']);
- $member->right_ids = (is_null($member->attributes['right_ids'])) ? '' : json_encode($member->attributes['right_ids']);
- $member->sale_info = (is_null($member->attributes['sale_info'])) ? '' : json_encode($member->attributes['sale_info']);
- $member->self_info = (empty($member->attributes['self_info'])) ? 0 : $member->attributes['self_info'];
- $member->right_default = (is_null($member->attributes['right_default'])) ? '' : $member->attributes['right_default'];
- $member->commission_one = empty($member->attributes['commission_one']) ? 0.00 : $member->attributes['commission_one'];
- $member->commission_two = empty($member->attributes['commission_two']) ? 0.00 : $member->attributes['commission_two'];
- $member->commission_three = empty($member->attributes['commission_three']) ? 0.00 : $member->attributes['commission_three'];
- $member->create_time = time();
- $memberInfo = $member->save();
- if($memberInfo){
- return [
- 'code' => 0,
- 'msg' => '成功',
- ];
- }else{
- return [
- 'code' => 1,
- 'msg' => '失败',
- ];
- }
- }
- //上下架
- public function editStatusById($id,$status){
- $memberInfo = BusinessMember::find()->where(['id'=>$id,'is_delete'=>0])->one();
- if(!$memberInfo || $memberInfo['status'] == $status ){
- return [
- 'code' => 1,
- 'msg' => '数据信息有误',
- ];
- }
- $memberInfo->status = $status;
- if($memberInfo->save()){
- return [
- 'code' => 0,
- 'msg' => '成功',
- ];
- }else{
- return [
- 'code' => 1,
- 'msg' => '失败',
- ];
- }
- }
- //删除信息
- public function delInfoById($id){
- $memberInfo = BusinessMember::find()->where(['id'=>$id,'is_delete'=>0])->one();
- if(!$memberInfo){
- return [
- 'code' => 1,
- 'msg' => '数据不存在',
- ];
- }
- $memberInfo->is_delete = 1;
- if($memberInfo->save()){
- return [
- 'code' => 0,
- 'msg' => '成功',
- ];
- }else{
- return [
- 'code' => 1,
- 'msg' => '失败',
- ];
- }
- }
- //获取优惠券详情
- public function getInfoById($id){
- $memberInfo = BusinessMember::find()->where(['id'=>$id,'is_delete'=>0])->asArray()->one();
- if(!$memberInfo){
- return [
- 'code' => 1,
- 'msg' => '失败',
- ];
- }
- if($memberInfo['sale_info']){
- $memberInfo['sale_info'] = json_decode($memberInfo['sale_info'],true);
- }
- //优惠券
- $couponIds = json_decode($memberInfo['coupon_info'],true);
- if($couponIds != "[]" && $couponIds){
- $couponInfo = Coupon::find()->alias('c')->leftJoin(["s"=>Store::tableName()], 'c.store_id=s.id')
- ->where(['in','c.id',$couponIds])->andWhere(['c.is_delete'=>0,'c.is_business'=>1])->select('c.*,s.name store_name')->asArray()->all();
- $memberInfo['coupon_info_detail'] = $couponInfo;
- $memberInfo['coupon_info'] = $couponIds;
- }else{
- $memberInfo['coupon_info_detail'] = [];
- }
- //核销卡详情
- $verifyIds = json_decode($memberInfo['verify_info'],true);
- if($verifyIds != "[]" && $verifyIds){
- $verifyInfo = VerifyCard::find()->alias('v')->leftJoin(["s"=>Store::tableName()], 'v.store_id=s.id')
- ->where(['in','v.id',$verifyIds])->andWhere(['v.is_delete'=>0,'v.is_business'=>1])->select('v.*,s.name store_name')->asArray()->all();
- $memberInfo['verify_info_detail'] = $verifyInfo;
- $memberInfo['verify_info'] = $verifyIds;
- }else{
- $memberInfo['verify_info_detail'] = [];
- }
- //权利信息
- if($memberInfo['right_info']){
- // $info = BusinessRightInfo::find()->where(['is_delete'=>0])->select("id,name,explanation")->asArray()->all();
- $info = BusinessRightInfo::resetRightInfo([], "id,name,explanation");
- $rightInfo = json_decode($memberInfo['right_info'],true);
- $rightInfo = array_column($rightInfo,NULL,'id');
- foreach($info as $key=>$val){
- if(isset($rightInfo[$val['id']]['icon']) && $rightInfo[$val['id']]['icon']){
- $info[$key]['icon'] = $rightInfo[$val['id']]['icon'];
- }else{
- $info[$key]['icon'] = '';
- }
- if(isset($rightInfo[$val['id']]['choosed']) && $rightInfo[$val['id']]['choosed']){
- $info[$key]['choosed'] = $rightInfo[$val['id']]['choosed'];
- }else{
- $info[$key]['choosed'] = false;
- }
- if(isset($rightInfo[$val['id']]['custom_name']) && $rightInfo[$val['id']]['custom_name']){
- $info[$key]['custom_name'] = $rightInfo[$val['id']]['custom_name'];
- }else{
- $info[$key]['custom_name'] = '';
- }
- if(isset($rightInfo[$val['id']]['custom_explanation']) && $rightInfo[$val['id']]['custom_explanation']){
- $info[$key]['custom_explanation'] = $rightInfo[$val['id']]['custom_explanation'];
- }else{
- $info[$key]['custom_explanation'] = '';
- }
- }
- $memberInfo['right_info'] = $info;
- }
- return [
- 'code' => 0,
- 'msg' => '成功',
- 'data' => $memberInfo
- ];
- }
- //获取权限列表
- public function getBusinessRightInfo(){
- // $info = BusinessRightInfo::find()->where(['is_delete'=>0])->select("id,name,explanation")->asArray()->all();
- $info = BusinessRightInfo::resetRightInfo([], "id,name,explanation");
- return [
- 'code' => 0,
- 'msg' => '成功',
- 'data' => $info
- ];
- }
- //获取购买卡片记录
- public function getBusinessBuyCartList(){
- $search_type = $this->search_type;
- $saas_name = $this->name;
- $start_time = $this->start_time;
- $end_time = $this->end_time;
- $page = ($this->page)?$this->page:1;
- $pageSize = 10;
- $query = BusinessMemberOrder::find()->alias('o')->leftJoin(['b'=>BusinessMember::tableName()],'o.member_id=b.id')->leftJoin(['s' => SaasUser::tableName()], 'o.saas_id = s.id')->where(['o.is_delete'=>0,'o.pay_type'=>1]);
- if($search_type == 1){
- $query->andWhere(['>=','o.end_time',time()]);
- }elseif($search_type == 2){
- $query->andWhere(['<=','o.end_time',time()]);
- }
- if($start_time){
- $query->andWhere(['>=','o.pay_time',strtotime($start_time)]);
- }
- if($end_time){
- $query->andWhere(['<=','o.pay_time',strtotime($end_time)]);
- }
- if($saas_name){
- $query->andWhere(['like','s.name',$saas_name]);
- }
- $countQuery = clone $query;
- $count = $countQuery->count();
- $query->offset(($page - 1) * $pageSize)->limit($pageSize);
- $list = $query->select("o.id,s.name userName,b.name cartName,b.sale_info,o.pay_price,o.member_type,o.pay_time,o.end_time,o.pay_type")->orderBy("o.pay_time desc")->asArray()->all();
- foreach($list as $k=>$v){
- $saleInfo = json_decode($v['sale_info'],true);
- $saleInfo = array_column($saleInfo,NULL ,'type');
- $list[$k]['alias_name'] = $saleInfo[$v['member_type']]['name'];
- if($v['member_type'] == 'perpetual'){
- $list[$k]['end_time'] = 9999999999;
- }
- }
- return [
- 'code' =>0,
- 'data' => [
- 'totalCount' => (int)$count,
- 'list' => $list,
- 'page' => $page,
- 'pageSize' => (int)$pageSize
- ]
- ];
- }
- //权限时间
- public function rightDuration($saas_id,$rid,$addTime,$from = 1){
- $duration = BusinessRightDuration::find()->where(['right_id'=>$rid,'saas_id'=>$saas_id,'is_delete'=>0])->one();
- if(!$duration){
- $duration = new BusinessRightDuration();
- $duration->saas_id = $saas_id;
- $duration->right_id = $rid;
- $duration->created_at = time();
- $duration->expire_time = ($addTime == 0) ? 0 : time()+ $addTime;
- $duration->is_delete = 0;
- $duration->from = $from;
- return $duration->save();
- }else{
- if($duration->expire_time > 0){
- $expire_time = $duration->expire_time;
- //如果已经过期从当前时间开始
- if($duration->expire_time <= time()){
- $expire_time = time();
- }
- $duration->expire_time = ($addTime == 0) ? 0 : $expire_time + $addTime;
- return $duration->save();
- }else{
- $duration->expire_time = ($addTime == 0) ? 0 : time()+ $addTime;;
- return $duration->save();
- }
- }
- }
- }
|