<?php
declare (strict_types = 1);

namespace app\model\admin;

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 = 1 and uid = m.uid ) as order_info_count,
            m2.mobile as i_mobile,m2.nickname as i_nickname
            "
            )
            ->alias("m")
            ->join('site s','s.sassid=m.sassid')
            ->leftJoin("member_level l","l.id=m.levelid")
            ->leftJoin('member m2',"m2.uid=m.i_uid")
            ->where('m.sassid',$where['sassid'])
            ->when(!empty($where),function ($query) use($where){
                //keyword
                if(!empty($where['keyword'])) {
                    $query->whereLike('m.nickname',"%{$where['keyword']}%");
                }

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

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

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

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

            })
            ->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,$sassid){
        $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)
            ->where('sassid',$sassid)
            ->find();
        return $data;
    }
}
