<?php
/**
 * @package merchant
 *
 * @author xaboy
 * @day 2020-04-16
 *
 * 
 */

namespace app\common\repositories\serviceprovider;
use app\common\model\store\product\ProductReply;
use app\common\repositories\BaseRepository;
use function Symfony\Component\String\s;
use think\facade\Db;
use think\facade\Route;

/**
 * Class MerchantRepository
 * @package app\common\repositories\system\merchant
 * @mixin MerchantDao
 * @author xaboy
 * @day 2020-04-16
 */
class ServiceproviderRepository extends BaseRepository
{
    /**
     * MerchantRepository constructor.
     * @param MerchantDao $dao
     */
    public function __construct()
    {

    }
    //统计
    public function report_form($uid,$day){
        if ($day=="today"){
            $day_where=[
                date("Y-m-d")." 0:0:0",
                date("Y-m-d h:i:s")
            ];
        }elseif ($day=="yesterday"){
            $day_where=[
                date("Y-m-d",strtotime("-1 day"))." 0:0:0",
                date("Y-m-d",strtotime("-1 day"))." 23:23:23"
            ];
        }else{
            $day_where=[
                $day." 0:0:0",
                $day." 23:23:23"
            ];
        }
        $mer_ids=self::get_down_mer($uid);
        //新增商户数
            $countmer=Db::name("merchant")->where("mer_id","in",$mer_ids)->where("create_time","between time",$day_where)->count();
        //新增锁定会员数
//            $usercount=Db::name("user")->where("locktime","between time",$day_where)->where("mer_id","in",$mer_ids)->count();
//            $userlock=Db::name("user")
//                ->alias("u")
//                ->join("user_certification uc","uc.uid=u.uid")
//                ->where("uc.create_time","between time",$day_where)
//                ->where(["spread_uid"=>$uid])
//                ->count();
        //待锁定会员数
        $userlockno=Db::name("store_order")
            ->alias("so")
            ->join("user u","so.uid=u.uid")
            ->where("so.status","in","0,1,2,3")
            ->where("so.mer_id","in",$mer_ids)
            ->where("u.mer_id",0)
            ->group("so.uid")
            ->where("so.create_time","between time",$day_where)
            ->count();
        $userlock= Db::name("user")->where("locktime","between time",$day_where)->where("mer_id","in",$mer_ids)->count();
        //新增收益
            $userbill=Db::name("user_bill")->where(["uid"=>$uid,"commission_type"=>3])->where("create_time","between time",$day_where)->count();

//            $userlockno=$userlockno;
        //产生分佣
            $userfen=Db::name("user_bill")->where(["uid"=>$uid])->where("create_time","between time",$day_where)->where(["type"=>"commission","commission_type"=>["in","1,2,3,4"]])->sum("number");
        //产生消费养老金
            $useryanglao=Db::name("user_bill")->where("create_time","between time",$day_where)->where(["uid"=>$uid,"commission_type"=>5])->count();
        //产生消费
            $userxiaofei=Db::name("user_bill")->where(["uid"=>$uid])->where("create_time","between time",$day_where)->group("link_id")->count();
        return ["countmer"=>$countmer,"userlock"=>$userlock,"userbill"=>$userbill,"userlockno"=>$userlockno,"userfen"=>$userfen,"useryanglao"=>$useryanglao,"userxiaofei"=>$userxiaofei];
    }
    //商户
    public function index($uid,$where)
    {
        if ($where["mer_id"]){
//            $where["branch"]=1;
        }else{
            $where=[];
            $where["branch"]=0;
            $where["uid"]=$uid;
        }
        $mer=Db::name("merchant")
            ->alias("m")
            ->join("merchant_category mc","mc.merchant_category_id=m.category_id","left")
            ->where($where)
            ->field("mer_id,mer_name,mer_avatar,m.create_time,mc.category_name")->find();
        if ($mer){
            $mer["paycode"]="";
            $base_commission=Db::name("system_config_value")
                ->where(["mer_id"=>$mer["mer_id"],"config_key"=>"base_commission"])
                ->value("value");
            $mer["base_commission"]=(int)$base_commission?$base_commission:0;
            return $mer;
        }else{
            return $uid;
        }
    }

    public function news_mer($uid,$data){
//        $where=["recommender"=>$uid];
        $mer_ids=self::get_down_mer($uid);
        $page=empty($data["page"])?1:$data["page"];
        $limit=empty($data["limit"])?10:$data["limit"];
        $list=Db::name("merchant")
            ->where("mer_id","in",$mer_ids)
            ->page($page,$limit)
            ->field("mer_avatar,mer_name,addr,mer_phone")
            ->select()->toArray();
        $count=Db::name("merchant")
            ->where("mer_id","in",$mer_ids)
            ->count();
        if ($list){
            return ["list"=>$list,"count"=>$count];
        }else{
            return [];
        }
    }
    public function news_lockuser($uid,$data){
//        $where=["spread_uid"=>$uid];
        $mer_ids=self::get_down_mer($uid);
        $page=empty($data["page"])?1:$data["page"];
        $limit=empty($data["limit"])?10:$data["limit"];
        $list=Db::name("user")
//            ->where($where)
            ->where("mer_id","in",$mer_ids)
            ->page($page,$limit)
            ->field("nickname,uid")
            ->select()->toArray();
        $count=Db::name("user")
//            ->where($where)
            ->where("mer_id","in",$mer_ids)
            ->count();
        foreach ($list as &$v){
//            $v["countprice"]=Db::name("store_order")->where(["uid"=>$v["uid"],"paid"=>1,"status"=>["<>",-1]])->sum("pay_price");
            $v["countprice"]=Db::name("store_order")->where(["paid"=>1,"uid"=>$v["uid"]])->where("status","in","0,1,2,3")->sum("pay_price");
        }
        if ($list){
            return ["list"=>$list,"count"=>$count];
        }else{
            return [];
        }
    }
    public function user_consume($uid,$data){
        $mer_ids=self::get_down_mer($uid);
        $where=["paid"=>1];

        $page=empty($data["page"])?1:$data["page"];
        $limit=empty($data["limit"])?10:$data["limit"];
        $list=Db::name("store_order")
            ->alias("so")
            ->join("user u","so.uid=u.uid")
            ->where($where)
            ->where("so.mer_id","in",$mer_ids)
            ->where("u.mer_id","in",$mer_ids)
            ->where("so.status","in","0,1,2,3")
            ->page($page,$limit)
            ->group("so.order_sn")
            ->field("order_sn,so.pay_price,nickname,so.create_time")
            ->select()->toArray();
        $count=Db::name("store_order")
            ->alias("so")
            ->join("user u","so.uid=u.uid")
            ->where("so.status","in","0,1,2,3")
            ->where($where)
            ->group("so.order_sn")
            ->where("so.mer_id","in",$mer_ids)
            ->count();
        if ($list){
            return ["list"=>$list,"count"=>$count];
        }else{
            return [];
        }
    }
    public function get_bill_list($uid,$data){
        $page=empty($data["page"])?1:$data["page"];
        $limit=empty($data["limit"])?10:$data["limit"];
        $where=["uid"=>$uid];
        if ($data["type"]==1){
            $where["commission_type"]=2;
        }
        if ($data["type"]==2){
            $where["commission_type"]=1;
        }
        if ($data["type"]==3){
            $where["commission_type"]=4;
        }
        if ($data["type"]==4){
            $where["commission_type"]=5;
        }

        $list=Db::name("user_bill")
            ->where($where)
            ->field("bill_id,title,number,pm,create_time")
            ->page($page,$limit)
            ->select()->toArray();
        $count=Db::name("user_bill")->where($where)->count();
        return ["list"=>$list,"count"=>$count];
    }

    public function benefit_contribution($uid,$data){
        $mer_ids=self::get_down_mer($uid);
        if(empty($mer_ids)){
            return [];
        }
        $list=Db::name("user")->where("mer_id","in",$mer_ids)->field("uid,nickname")->select()->toArray();
        if (!$list){
            return [];
        }
//        $list=Db::name("user_bill")
//            ->alias("ub")
//            ->join("user u","u.uid=ub.uid")
//            ->where("u.uid","in",implode(",",$xiaofeiren))
//            ->where(["type"=>"commission"])
//            ->group("u.uid")
//            ->field("sum(number) as price,nickname")
//            ->select()->toArray();
        foreach ($list as &$v){
//            $v["price"]=Db::name("user_bill")->where("",$v["uid"])->sum("number");
            $v["price"]=Db::name("store_order")
                ->alias("so")
                ->join("user_bill ub","ub.order_sn=so.order_sn")
                ->where(["so.uid"=>$v["uid"],"ub.uid"=>$uid])
                ->sum("number");

        }
        return $list;
    }
    public function get_mer_info($uid,$data){
        $mer_ids=self::get_down_mer($uid);
        //商户数
        $mer_count=$mer_ids?count(explode(",",$mer_ids)):0;

        //锁定会员数
        $lock_user=Db::name("user")->where("mer_id","in",$mer_ids)->count();
        //消费金额
//        $xf_mer=Db::name("merchant")->where(["recommender"=>$uid])->column("mer_id");
        $xf_price=Db::name("store_order")->where("mer_id","in",$mer_ids)->where(["paid"=>1])->where("mer_id","in",$mer_ids)->where("status","in","0,1,2,3")->sum("pay_price");
        //产生分润
        $fenrun=Db::name("user_bill")
            ->where(["type"=>"commission","uid"=>$uid])
            ->where("commission_type","in","2,3,4")
            ->sum("number");
//            Db::name("user_bill")->where(["uid"=>$uid,"type"=>"commission"])->sum("number");
        //待锁定会员
//        $lock_userno=0;
        $lock_userno=Db::name("store_order")
            ->alias("so")
            ->join("user u","u.uid=so.uid")
            ->where("so.mer_id","in",$mer_ids)
            ->where("u.mer_id",0)
            ->group("so.uid")
            ->count();
        //产生养老金
        $yanglaojin=Db::name("store_order")
            ->alias("so")
            ->join("user_bill ub","ub.order_sn=so.order_sn")
            ->where("so.mer_id","in",$mer_ids)
            ->where(["commission_type"=>5,"type"=>"commission"])
            ->sum("number");
        return [
            "mer_count"=>$mer_count,
            "lock_user"=>$lock_user,
            "xf_price"=>$xf_price,
            "fenrun"=>$fenrun,
            "lock_userno"=>$lock_userno,
            "yanglaojin"=>$yanglaojin
        ];
    }
    public function get_user_info($uid,$data){

//        //锁定会员数
//        $lock_user=Db::name("user")->where(["spread_uid"=>$uid])->count();
        $mer_ids=self::get_down_mer($uid);
        //消费金额
//        $xf_mer=Db::name("merchant")->where(["recommender"=>$uid])->column("mer_id");
        $xf_price=Db::name("user_bill")
            ->where(["type"=>"commission","uid"=>$uid])
            ->where("commission_type","in","2,3,4")
            ->sum("number");
        if(empty($mer_ids)){
            return [
                "lock_user"=>0,
                "xiaofei"=>0,
                "xf_price"=>$xf_price,
                "yanglaojin"=>0,
                "liushui"=>0

            ];
        }
        //锁定会员数
        $lock_user=Db::name("user")->where("mer_id","in",$mer_ids)->count();
        $xiaofeiren=Db::name("user")->where("mer_id","in",$mer_ids)->column("uid");
        if ($xiaofeiren){
            $xiaofei=Db::name("store_order")->where("uid","in",implode(",",$xiaofeiren))->where(["paid"=>1])->where("mer_id","in",$mer_ids)->where("status","in","0,1,2,3")->count();
        }else{
            $xiaofei=0;
        }


//        $yanglaojin=Db::name("store_order")
//            ->alias("so")
//            ->join("user_bill ub","ub.order_sn=so.order_sn")
//            ->where("so.mer_id","in",$mer_ids)
//            ->where(["commission_type"=>5,"type"=>"commission"])
//            ->sum("number");
//        $yanglaojin=Db::name("user_bill")->where("")
        $yanglaojin=Db::name("user")->where("mer_id","in",$mer_ids)->sum("annuity");
//        $liushui= Db::name("store_order")->where("mer_id","in",$mer_ids)->where(["paid"=>1])->where("mer_id","in",$mer_ids)->where("status","in","0,1,2,3")->sum("pay_price");
        $liushui=Db::name("store_order")->where("uid","in",implode(",",$xiaofeiren))->where(["paid"=>1])->where("mer_id","in",$mer_ids)->where("status","in","0,1,2,3")->sum("pay_price");
        return [
            "lock_user"=>$lock_user,
            "xiaofei"=>$xiaofei,
            "xf_price"=>$xf_price,
            "yanglaojin"=>$yanglaojin,
            "liushui"=>$liushui

        ];
    }
    public function get_mer_list($uid,$data){
        $page=empty($data["page"])?1:$data["page"];
        $limit=empty($data["limit"])?10:$data["limit"];

        $mer_ids=self::get_down_mer($uid);
        $page=empty($data["page"])?1:$data["page"];
        $limit=empty($data["limit"])?10:$data["limit"];
        if ($mer_ids){
            $userorder=Db::name("merchant")
                ->alias("m")
                ->join("merchant_category mc","mc.merchant_category_id=m.category_id","left")
                ->where("mer_id","in",$mer_ids)
                ->field("m.mer_id,mer_avatar,postage_score,m.mer_name,mc.category_name,district_addr,m.create_time")
                ->page($page,$limit)
                ->select()->toArray();
            $usercount=Db::name("merchant")
                ->alias("m")
                ->join("merchant_category mc","mc.merchant_category_id=m.category_id","left")
                ->where("mer_id","in",$mer_ids)
                ->count();
            foreach ($userorder as &$v){
                $v["usercount"]=Db::name("user")->where("mer_id",$v["mer_id"])->count();
                $v["distance"]="1km";
            }
            return  ["list"=>$userorder,"count"=>$usercount];
        }else{
            return ["list"=>[],"count"=>0];

        }


//        $merchant=Db::name("merchant")
//            ->alias("m")
//            ->join("user u","u.mer_id=m.mer_id","left")
//            ->join("merchant_category mc","mc.merchant_category_id=m.category_id","left")
//            ->where(["recommender"=>$uid])
//            ->group("m.mer_id")
//            ->field("m.mer_id,count(u.uid) as usercount,mer_avatar,postage_score,mer_name,mc.category_name,district_addr,m.create_time")
//            ->order("usercount desc")
//            ->page($page,$limit)
//            ->select()->toArray();
//        $count=Db::name("merchant")
//            ->alias("m")
//            ->join("user u","u.mer_id=m.mer_id","left")
//            ->join("merchant_category mc","mc.merchant_category_id=m.category_id","left")
//            ->group("m.mer_id")
//            ->where(["recommender"=>$uid])
//            ->count();
//        foreach ($merchant as &$v){
//            $v["distance"]="1km";
//        }
//        if ($merchant){
//            return ["list"=>$merchant,"count"=>$count];
//        }else{
//            return [];
//        }
    }
    public function get_my_profit($uid){
        $zong=Db::name("user_bill")->where("uid",$uid)->where("type","commission")->where("commission_type","<>",5)->sum("number");
        $yidao=Db::name("user_extract")->where("uid",$uid)->where("type","commission")->where("status",1)->sum("extract_price");
        $weidao=Db::name("user_extract")->where("uid",$uid)->where("type","commission")->where("status",0)->sum("extract_price");
        $weijiesuan=Db::name("user_bill")->where("uid",$uid)->where("type","commission")->where("status",0)->where("commission_type","<>",5)->sum("number");
        return ["zong"=>$zong,"yidao"=>$yidao,"weidao"=>$weidao,"weijiesuan"=>$weijiesuan];
    }
    public function lock_user_profit($uid){
        $zong=Db::name("user_bill")->where("type","commission")->where("uid",$uid)->where("commission_type",2)->sum("number");
        $yidao=Db::name("user_extract")->where("uid",$uid)->where("status",1)->sum("extract_price");
        $weidao=Db::name("user_extract")->where("uid",$uid)->where("status",0)->sum("extract_price");
        $weijiesuan=Db::name("user_bill")->where("type","commission")->where("uid",$uid)->where("status",0)->where("commission_type",2)->sum("number");
        return ["zong"=>$zong,"yidao"=>$yidao,"weidao"=>$weidao,"weijiesuan"=>$weijiesuan];
    }
    public function channel_service_fee($uid){
        $zong=Db::name("user_bill")->where("uid",$uid)->where("commission_type",1)->sum("number");
        $yidao=Db::name("user_extract")->where("uid",$uid)->where("status",1)->sum("extract_price");
        $weidao=Db::name("user_extract")->where("uid",$uid)->where("status",0)->sum("extract_price");
        $weijiesuan=Db::name("user_bill")->where("uid",$uid)->where("status",0)->where("commission_type",1)->sum("number");

        return ["zong"=>$zong,"yidao"=>$yidao,"weidao"=>$weidao,"weijiesuan"=>$weijiesuan];
    }
    public function rebate_within_jurisdiction($uid){
        $zong=Db::name("user_bill")->where("uid",$uid)->where("commission_type",4)->sum("number");
        $yidao=Db::name("user_extract")->where("uid",$uid)->where("status",1)->sum("extract_price");
        $weidao=Db::name("user_extract")->where("uid",$uid)->where("status",0)->sum("extract_price");
        $weijiesuan=Db::name("user_bill")->where("uid",$uid)->where("status",0)->where("commission_type",4)->sum("number");
        return ["zong"=>$zong,"yidao"=>$yidao,"weidao"=>$weidao,"weijiesuan"=>$weijiesuan];
    }
    public function advertising_revenue($uid){
        $zong=Db::name("user_bill")->where("uid",$uid)->where("commission_type",4)->sum("number");
        $yidao=Db::name("user_extract")->where("uid",$uid)->where("status",5)->sum("extract_price");
        $weidao=Db::name("user_extract")->where("uid",$uid)->where("status",0)->sum("extract_price");
        $weijiesuan=Db::name("user_bill")->where("uid",$uid)->where("status",0)->where("commission_type",5)->sum("number");
        return ["zong"=>$zong,"yidao"=>$yidao,"weidao"=>$weidao,"weijiesuan"=>$weijiesuan];
    }

    public function user_list($uid,$data){
        $mer_ids=self::get_down_mer($uid);
        $page=empty($data["page"])?1:$data["page"];
        $limit=empty($data["limit"])?10:$data["limit"];
        if ($mer_ids){
//            $userorder=Db::name("store_order")
//                ->alias("so")
//                ->join("user u","u.uid=so.uid")
//                ->where("so.mer_id","in",$mer_ids)
////                ->where("u.mer_id","in","0,".$mer_ids)
//                ->where(["paid"=>1])
//                ->where("so.status","in","0,1,2,3")
//                ->group("u.uid")
//                ->field("u.nickname,count(so.order_id) as countorder,annuity,u.mer_id,u.create_time")
//                ->page($page,$limit)
//                ->order("countorder desc")
//                ->select()->toArray();
            $userorder=Db::name("user")->where("mer_id","in",$mer_ids)->page($page,$limit)->field("nickname,create_time,annuity,mer_id,uid")->select()->toArray();

//            $usercount=Db::name("store_order")
//                ->alias("so")
//                ->join("user u","u.uid=so.uid")
//                ->where("so.mer_id","in",$mer_ids)
//                ->where("u.mer_id","in","0,".$mer_ids)
//                ->group("u.uid")
//                ->count();
            $usercount=Db::name("user")->where("mer_id","in",$mer_ids)->count();
            foreach ($userorder as &$v){
                $mer_name=Db::name("merchant")->where("mer_id", $v["mer_id"])->value("mer_name");
                $v["mer_name"]=$mer_name;
                $v["mer_lock"]=$mer_name?1:0;
                $v["countorder"]=Db::name("store_order") ->where(["mer_id"=>$v["mer_id"],"uid"=>$v["uid"]])->count();
            }
            return  ["list"=>$userorder,"count"=>$usercount];
        }else{
            return ["list"=>[],"count"=>0];
        }

    }
    public function mer_list($uid,$data){
        $mer_ids=self::get_down_mer($uid);
        $page=empty($data["page"])?1:$data["page"];
        $limit=empty($data["limit"])?10:$data["limit"];
        if ($mer_ids){
            $userorder=Db::name("merchant")
                ->where("mer_id","in",$mer_ids)
                ->field("mer_id,mer_name,create_time")
                ->page($page,$limit)
                ->select()->toArray();
            $usercount=Db::name("merchant")
                ->where("mer_id","in",$mer_ids)
                ->count();
            foreach ($userorder as &$v){
                $v["lockcount"]=Db::name("user")->where("mer_id",$v["mer_id"])->count();
//                $v["commission"]=Db::name("user_bill")
//                    ->alias("ub")
//                    ->join("store_order so","ub.order_sn=so.order_sn")
//                    ->where(["so.mer_id"=>$v["mer_id"],"ub.commission_type"=>["in","1,2,3,4"]])
//                    ->sum("number");
//                $v["yanglao"]=Db::name("user_bill")
//                    ->alias("ub")
//                    ->join("store_order so","ub.order_sn=so.order_sn")
//                    ->where(["so.mer_id"=>$v["mer_id"],"ub.commission_type"=>5])
//                    ->sum("number");
//                $v["lockcountNo"]=Db::name("user")->where("mer_id",$v["mer_id"])->count();

                //产生分润
//                $v["commission"]=Db::name("store_order")
//                    ->alias("so")
//                    ->join("user_bill ub","ub.order_sn=so.order_sn")
//                    ->where("so.mer_id",$v["mer_id"])
//                    ->where(["type"=>"commission"])
//                    ->where("commission_type","in","2,3,4")
//                    ->sum("number");
                $v["commission"]=Db::name("user_bill")
                    ->where(["type"=>"commission","mer_id"=>$v["mer_id"]])
                    ->where("commission_type","in","2,3,4")
                    ->sum("number");
                $v["lockcountNo"]=Db::name("store_order")
                    ->alias("so")
                    ->join("user u","u.uid=so.uid")
                    ->where("so.mer_id",$v["mer_id"])
                    ->where("u.mer_id",0)
                    ->group("so.uid")
                    ->count();
                //产生养老金
                $v["yanglao"]=Db::name("store_order")
                    ->alias("so")
                    ->join("user_bill ub","ub.order_sn=so.order_sn")
                    ->where("so.mer_id",$v["mer_id"])
                    ->where(["commission_type"=>5,"type"=>"commission"])
                    ->sum("number");


            }
            return  ["list"=>$userorder,"count"=>$usercount];
        }else{
            return ["list"=>[],"count"=>0];
        }
    }
    //根据身份查名下商户id  名下代理id
    public static function get_down_mer($uid,$returnarea=0){
        $user=Db::name("user")->where("uid",$uid)->field("identity")->find();
        $area=0;
        $areas=[];
//        if ($user_identity==1){
//            $area=Db::name("user_street")->where("uid",$uid)->value("street_id");
//        }
//        if ($user_identity==2){
//            $area=Db::name("user_area")->where("uid",$uid)->value("area_id");
//        }
//        if ($user_identity==3){
//            $area=Db::name("user_city")->where("uid",$uid)->value("city_id");
//        }
        if ($user["identity"]==1){
            $data=Db::name("user_street")->where("uid",$uid)->find();
            $area=$data["street_id"];
            $areas[]=$data["street_id"];
        }
        if ($user["identity"]==2){
            $data=Db::name("user_area")->where("uid",$uid)->find();
            $area=$data["area_id"];
            $datas=Db::name("user_street")->where("area_code",$data["area_code"])->column("street_id");
            $areas[]=$data["area_id"];
            foreach ($datas as $vs){
                $areas[]= $vs;
            }
        }
        if ($user["identity"]==3){
            $data= Db::name("user_city")->where("uid",$uid)->find();
            $area=$data["city_id"];
            $areas[]=$data["city_id"];

            $list=Db::name("user_area")->where("city_code",$data["city_code"])->select()->toArray();
            foreach ($list as $k=>&$v){
                $vlist=Db::name("user_street")->where(["area_code"=>$v["area_code"]])->where("uid",">",0)->select()->toArray();
                if ($v["uid"]==0&&!$vlist){
                    unset($list[$k]);
                }else{
                    $areas[]=$v["area_id"];
//                    $v["list"]=$vlist;
                    foreach ($vlist as $v){
                        $areas[]= $v["street_id"];
                    }
                }
            }

        }



        if ($areas){
//            if ($dengji){
//                $uid_ids=Db::name("user")->where("adr_id",$area)->column("uid");
//                $uid_ids=implode(",",$uid_ids);
//                return $uid_ids;
//            }else{
//                $mer_ids=Db::name("merchant")->where("city_id|area_id|street_id",$area)->column("mer_id");
//                $mer_ids=implode(",",$mer_ids);
//                return $mer_ids;
//            }
            if ($returnarea){
                return $area;
            }else{
                $mer_ids=[];
                foreach ($areas as $q){
                    $mer_id=Db::name("merchant")->where("city_id|area_id|street_id",$q)->column("mer_id");
                    if ($mer_id){
//                        $mer_ids[]=implode(",",$mer_id);
                        $mer_ids= array_merge($mer_id,$mer_ids);
//                        if ($mer_ids){
//                            $mer_ids.=",".implode(",",$mer_id);
//                        }else{
//                            $mer_ids.=implode(",",$mer_id);
//                        }
                    }
                }
//                array_unique($mer_ids)
//                var_dump($mer_ids);exit;
                return implode(",",array_unique($mer_ids));
            }
        }else{
            return '';
        }
    }
    public function means($uid,$data){
        $page=empty($data["page"])?1:$data["page"];
        $limit=empty($data["limit"])?10:$data["limit"];
        $list=Db::name("article")
            ->where("cid",$data["cid"])
            ->field("article_id,create_time,title,synopsis")
            ->page($page,$limit)
            ->select()->toArray();
        $count=Db::name("article")
            ->where("cid",$data["cid"])
            ->count();
        if ($list){
            return ["list"=>$list,"count"=>$count];
        }else{
            return [];
        }
    }
    public function means_info($uid,$data){
        $res=Db::name("article")
            ->alias("a")
            ->join("article_content ac","ac.article_content_id=a.article_id")
            ->where("article_id",$data["article_id"])
            ->field("a.article_id,ac.content,a.create_time,a.title")
            ->find();
        return $res;
    }
    public function countcommission($uid,$data){
        $brokerage_price=Db::name("user")->where("uid",$uid)->value("brokerage_price");
        $daili=Db::name("user_bill")->where(["commission_type"=>1,"uid"=>$uid])->sum("number");
        $yongjin=Db::name("user_bill")->where(["commission_type"=>2,"uid"=>$uid])->sum("number");
        $tuiguang=Db::name("user_bill")->where(["commission_type"=>3,"uid"=>$uid])->sum("number");
        $xiaqu=Db::name("user_bill")->where(["commission_type"=>4,"uid"=>$uid])->sum("number");
        return ["brokerage_price"=>$brokerage_price,"daili"=>$daili,"yongjin"=>$yongjin,"tuiguang"=>$tuiguang,"xiaqu"=>$xiaqu];
    }
    public function commissionlist($uid,$data){
        $page=empty($data["page"])?1:$data["page"];
        $limit=empty($data["limit"])?10:$data["limit"];
        $list=Db::name("user_bill")
            ->where(["uid"=>$uid,'type'=>"commission"])
            ->order('bill_id desc')
            ->page($page,$limit)
            ->select();
        $count=Db::name("user_bill")
            ->where(["uid"=>$uid])
            ->count();
        return ["list"=>$list,"count"=>$count];
    }

    public function add_agent($uid,$data){
        $user= Db::name("user")->where("account",$data["phone"])->find();
        $repository = app()->make(\app\common\repositories\user\UserRepository::class);
        if ($user){
            return "用户已存在";
        }else{
            $user = $repository->registr($data['phone'], time(),'APP',"",$data["name"]);
            $spread_account=Db::name("user")->where("uid",$uid)->value("account");
            $uid=$user["uid"];
        }
        Db::name("user_certification")->insert([
            "user_name"=>$data["name"],
            "mobile"=>$data['phone'],
            "status"=>1,
            "audit_time"=>time(),
            "create_time"=>time(),
            "update_time"=>time(),
            "uid"=>$uid
        ]);
        $data= [
            "name" => $data["name"],
            "identity" => $data["identity"],
            "license" => $data["license"],
            "user_id"=>$uid,
            "province_id"=>$data["province_id"],
            "city_id"=>$data["city_id"],
            "area_id"=>$data["area_id"],
            "street_id"=>$data["street_id"],
            'spread_account'=>$spread_account,
            "add_time"=>time(),
            "update_time"=>time()
        ];
//        $repository = app()->make(\app\common\repositories\user\UserRepository::class);
        $res = $repository->agent($data);
        if($res['code'] ==  400){
            return $res['msg'];
        }
        return "申请成功";

    }
    public function get_agent($uid){
        $uids=self::get_down_mer($uid,1);
        $page=empty($data["page"])?1:$data["page"];
        $limit=empty($data["limit"])?10:$data["limit"];
//        var_dump($uids);exit;
        $user=Db::name("user")
            ->alias("u")
            ->join("user_agent ua","ua.user_id=u.uid")
            ->where("adr_id",$uids)
            ->field("u.nickname,u.identity,u.phone,u.uid,ua.street_id,ua.area_id,ua.city_id")
            ->page($page,$limit)
            ->select()
            ->toArray();


        foreach ($user as &$v){
            $v["phone"]=substr($v["phone"], 0, 3) . '****' . substr($v["phone"], 7, 4);
            $street=  Db::name("user_street")->where("street_id",$v["street_id"])->value("street_name");
            $area_id =  Db::name("user_area")->where("area_id",$v["area_id"])->value("area_name");
            $city  =   Db::name("user_city")->where("city_id",$v["city_id"])->value("city_name");
            $v["addres"]=$city."-".$area_id."-".$street;
            $v["identity"]=$v["identity"]==3?"市级代理":(($v["identity"]==2)?"县级代理":"镇级代理");
        }
        $count=Db::name("user")
            ->where("adr_id",$uids)
            ->count();

        return ["list"=>$user,"count"=>$count];
    }
    public function edit_agent($uid,$data){
        $user= Db::name("user")->where("account",$data["phone"])->find();
        $spread_account=Db::name("user")->where("uid",$uid)->value("account");
        $areaid=0;
        if ($data["identity"]==1){
            $areaid=Db::name("user_street")->where("street_id",$data["street_id"])->value("uid");
        }
        if ($data["identity"]==2){
            $areaid=Db::name("user_area")->where("area_id",$data["area_id"])->value("uid");
        }
        if ($data["identity"]==3){
            $areaid=Db::name("user_city")->where("city_id",$data["city_id"])->value("uid");
        }
        if ($areaid){
            return ["code"=>0,"msg"=>"该区域已经有代理了"];
        }
        $datas= [
            "name" => $data["name"],
            "identity" => $data["identity"],
            "license" => $data["license"],
//            "user_id"=>$user["uid"],
            "province_id"=>$data["province_id"],
            "city_id"=>$data["city_id"],
            "area_id"=>$data["area_id"],
            "street_id"=>$data["street_id"],
            'spread_account'=>$spread_account,
            "update_time"=>time()
        ];

        Db::transaction(function () use ($datas,$user) {
            $res=Db::name("user_agent")->where("user_id",$user["uid"])->update($datas);

            if ($res){
                if ($datas["identity"]!=$user["identity"]){
                    Db::name("user")->where("account",$user["account"])->update(["identity"=>$datas["identity"]]);
                    if ($datas["identity"]==1){
                        Db::name("user_street")->where("street_id",$datas["street_id"])->update(["uid"=>$user["uid"]]);
                    }
                    if ($datas["identity"]==2){
                        Db::name("user_area")->where("area_id",$datas["area_id"])->update(["uid"=>$user["uid"]]);
                    }
                    if ($datas["identity"]==3){
                        Db::name("user_city")->where("city_id",$datas["city_id"])->update(["uid"=>$user["uid"]]);
                    }
                }
//                return  ["code"=>1,"msg"=>"修改成功"];
            }

        });
        return  ["code"=>1,"msg"=>"修改成功"];
    }
    public function get_one_agent($uid,$data){
        $user=Db::name("user_agent")
            ->where("user_id",$data["uid"])
            ->find();
        if($user){
            $user["street_id"]=  Db::name("user_street")->where("street_id",$user["street_id"])->field("street_id,street_code,street_name")->find();
            $user["area_id"]=  Db::name("user_area")->where("area_id",$user["area_id"])->field("area_id,area_code,area_name")->find();
            $user["city_id"]=   Db::name("user_city")->where("city_id",$user["city_id"])->field("city_id,city_code,city_name")->find();

            $user["province_id"]=   Db::name("user_province")->where("province_id",$user["province_id"])->field("province_id,province_code,province_name")->find();
        }
        return $user;
    }
    public function mer_distribution($uid){
        $area=self::get_down_mer($uid,1);
        $mer=Db::name("merchant")
            ->where("city_id|area_id|street_id",$area)
            ->field("mer_name,mer_phone,doorhead,lat,lng,mer_avatar,addr")
            ->select()->toArray();
        return $mer;
    }
    public function get_area($uid){
        $user= Db::name("user")->where("uid",$uid)->find();
        $data=[];
        if ($user["identity"]==1){
            $data=Db::name("user_street")->where("uid",$uid)->find();
        }
        if ($user["identity"]==2){
            $data=Db::name("user_area")->where("uid",$uid)->find();
            $data["list"]=Db::name("user_street")->where("area_code",$data["area_code"])->select();
        }
        if ($user["identity"]==3){
            $data= Db::name("user_city")->where("uid",$uid)->find();
            $list=Db::name("user_area")->where("city_code",$data["city_code"])->select()->toArray();
            foreach ($list as $k=>&$v){
                $vlist=Db::name("user_street")->where(["area_code"=>$v["area_code"]])->where("uid",">",0)->select()->toArray();
                if ($v["uid"]==0&&!$vlist){
                    unset($list[$k]);
                }else{
                    $v["list"]=$vlist;
                }
            }
            $data["list"]=$list;
        }
        return $data;

    }














}
