<?php
declare (strict_types = 1);

namespace app\model\system;

use Closure;
use library\basic\BaseModel;
use library\traits\JwtAuthModelTrait;
use library\traits\ModelTrait;
use think\Model;

/**
 * @mixin \think\Model
 */
class Member extends BaseModel
{
    use ModelTrait;
    use JwtAuthModelTrait;


    /**
     * 获取列表数据
     * @param $page
     * @param $where
     * @param $pageCount
     * @param $desc
     */
    public function getList($page,$where = [],$pageCount = 20,$filed = '*',$desc = ''){
        $data =  $this
            ->field("m.*,s.name as site_name,l.name as level_name,
            (select count(*) from table_order where uid = m.uid and status > 0) as order_count,
            (select sum(v) from table_recharge where status = 1 and uid = m.uid ) as recharge_count,
            (select count(*) from table_order_info where status > 0 and uid = m.uid ) as order_info_count,
            (select nickname from table_member where uid = m.i_uid) as i_nickname
            "
            )
            ->alias("m")
            ->join('site s','s.sassid=m.sassid')
            ->leftJoin("member_level l","l.id=m.levelid")
            ->when(!empty($where),function ($query) use($where){
                //keyword
                if(!empty($where['keyword'])) {
                    $query->whereLike('m.nickname',"%{$where['keyword']}%");
                }

                if(!empty($where['timeVal'][0]) && !empty($where['timeVal'][1])) {
                    $query->where('regtime','>',strtotime($where['timeVal'][0]));
                    $query->where('regtime','<',strtotime($where['timeVal'][1]));
                }

                if(!empty($where['not_buy'])) {

                    //今日消费
                    if($where['not_buy'] == 'today') {
                        $query->whereTime("last_con_time",'today');
                    }

                    //7天内消费
                    if($where['not_buy'] == '1_7day') {
                        $time = time() - 6*24*3600;
                        $query->where("last_con_time",'>',$time);
                        $query->order("last_con_time","desc");
                    }

                    //7-13天未消费
                    if($where['not_buy'] == '7day') {
                        $time = time() - 13*24*3600;
                        $times = time() - 6*24*3600;
                        $query->where("last_con_time",'>',$time);
                        $query->where("last_con_time",'<',$times);
                        $query->order("last_con_time","desc");
                    }

                    //14天未消费
                    if($where['not_buy'] == '14day') {
                        $time = time() - 13*24*3600;
                        $query->where("last_con_time",'<',$time);
                        $query->where("last_con_time",'<>',0);
                        $query->order("last_con_time","desc");
                    }

                    //1个月未消费
                    if($where['not_buy'] == 'month') {
                        $time = strtotime('-1 month');
                        $query->where("last_con_time",'<',$time);
                        $query->where("last_con_time",'<>',0);
                        $query->order("last_con_time","desc");
                    }

                    //从无消费
                    if($where['not_buy'] == 'nothing') {
                        $query->where("last_con_time",0);
                    }
                }

                if(!empty($where['mobile'])) {
                    $query->whereLike('m.mobile',"%{$where['mobile']}%");
                }

                if(!empty($where['level'])) {
                    $query->where('m.levelid',$where['level']);
                }

                if(strlen($where['follow_type']) > 0) {
                    $query->where('m.follow_type',$where['follow_type']);
                }


                if(!empty($where['sassid'])) {
                    //判断是否多个
                    $sassIds = explode(',',(string)$where['sassid']);
                    if(count($sassIds) > 1)
                        $query->whereIn('m.sassid',$sassIds);
                    else
                        $query->where('m.sassid',$where['sassid']);
                }

                if(!empty($where['i_uid'])) {
                    $query->where('m.i_uid',$where['i_uid']);
                }

                if(!empty($where['uid'])) {
                    $query->where('m.uid',$where['uid']);
                }

                if(!empty($where['label'])) {
                    $query->where('m.label',$where['label']);
                }

                if(!empty($where['admin_id'])) {
                    $query->where('m.admin_id',$where['admin_id']);
                }

                if(!empty($where['not_follow'])) {
                    if($where['not_follow']=='3day'){
                        $time = time() - 3*24*3600;
                        $query->where("last_follow_time",'<',$time);
                    }else{
                        $time = time() - 7*24*3600;
                        $query->where("last_follow_time",'<',$time);
                    }
                }

                if(!empty($where['lately_login'])) {
                    $time = time() - 3*24*3600;
                    $query->where("lasttime",'>',$time);
                }

                if(!empty($where['type'])) {
                    if($where['type']=='new'){
                        $query->where("last_con_time",'=',0);
                    }else{
                        $query->where("last_con_time",'<>',0);
                    }
                }

            })
            ->order($desc)
            ->paginate(['list_rows'=>$pageCount,'page'=>$page])
            ->toArray();
       //  echo $this->getLastSql();
        return [$data['total'],$data['data']];
    }




    /**
     * 获取列表数据
     * @param $page
     * @param $where
     * @param $pageCount
     * @param $desc
     */
    public function getItem($uid){
        $data =  $this

            ->field("*,(SELECT count(*) from table_order where uid = u.uid and status > 0) as order_count,
                (select name from table_member_level where id = u.levelid) as lavel_name,
            (select sum(v) from table_recharge where status = 1 and uid = u.uid ) as recharge_count,
            (select count(*) from table_order_info where status >= 1 and uid = u.uid ) as order_info_count
            ")
            ->alias("u")
            ->where('uid',$uid)
            ->find();
        return $data;
    }

}
