Business.php 18.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use think\Db;
use think\Exception;

/**
 * 商家端接口
 */
class Business extends Base
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $postParam;
    protected $store_id;
    protected $staff;
    protected $staff_id;
    protected $store;

    public function _initialize()
    {
        $postParam = $this->request->param();
        $this->postParam = $postParam;
        parent::_initialize();
        //$this->auth->id=23 ;//测试用
        $postParam = $this->request->param();
        $this->postParam = $postParam;
        if (empty($postParam['store_id'])) {
            $this->error("门店store_id异常");
        }
        $this->store_id = $postParam['store_id'];
        $store = Db::name("verification_store")->where("id", $this->store_id)->find();
        if (empty($store)) {
            $this->error("门店不存在,请退出登录后重试!");
        }
        $this->store = $store;
        $storewhere['verification_store_id']=$this->store_id;
        $storewhere['user_id']=$this->auth->id;
        $staff = Db::name("verification_staff")->where($storewhere)->find();
        if (empty($staff)) {
            $this->error("暂无权限!");
        }
        $this->staff = $staff;
        $this->staff_id = $staff['id'];
    }


    /**
     * 1、商家首页 获取商家人员信息
     *
     */
    public function getstaff(){
        $data =  $this->staff;
        $store = Db::name('verification_store')->where(['id'=>$data['verification_store_id']])->find();
        $user=Db::name("user")->find($data['user_id']);
        $data['avatar']=$user['avatar'];
        $data['image'] = !empty($store['image'])?request()->domain().$store['image']:'';
        $data['agent_image'] = !empty($store['agent_image'])?request()->domain().$store['agent_image']:'';
        $this->success('获取成功',$data);
    }
    /**
     * 2、核销
     */
    public function checkconsumption(){
        $receive_no=$this->postParam['receive_no'];
        //先查询到领卷记录
        $where["receive_no"] = ["=", $receive_no];
        $where["verification_store_id"] = ["=", $this->store_id];
        $receive=Db::name('verification_receive')->where($where)->find();
        if(empty($receive)){
            $this->error('无效卡卷');
        }
        if($receive['type']==1){
            $this->error('该卡卷已使用');
        }

        if($receive['type']==2){
            $this->error('该卡卷已过期');
        }
        $time=time();
        if($receive['closetime']<$time){
            $this->error('该卡卷已过期');
        }
        Db::startTrans();
        try {
            $iswhere["id"] = ["=", $receive['id']];
            $isdata=[
                'usetime'=>$time,
                'type'=>1
            ];
            //先把领卷记录修改了
            $isreceive=Db::name('verification_receive')->where($iswhere)->update($isdata);
            //添加核销记录
            $canceldata=[
                'verification_store_id'=>$this->store_id,
                'verification_staff_id'=>$this->staff_id,
                'verification_receive_id'=>$receive['id'],
                'createtime'=>$time,
            ];

            $cancel=Db::name('verification_cancel')->insertGetId($canceldata);
            $canceldata=Db::name('verification_cancel')->where('id',$cancel)->find();

            //查询本订单是否所有卡卷都已核销
            $receivewhere['verification_order_id']=['=',$receive['verification_order_id']];
            $receivewhere['type']=['=',0];
            $isuser=Db::name('verification_receive')->where($receivewhere)->count();

            if($isuser==0){
                $isorderuser['id']=['=',$receive['verification_order_id']];
                $isuserdata=[
                    'status'=>1
                ];
                $isorder=Db::name('verification_order')->where($isorderuser)->update($isuserdata);
            }
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        if($cancel){
            $this->success('核销成功',$canceldata);
        }else{
            $this->error('核销失败');
        }
        //核销data

    }

    /**
     *3、核销页面的核销记录 单条
     */
    public function getverification_cancel(){
        $where["a.verification_store_id"] = ["=", $this->store_id];
        $where["a.verification_staff_id"] = ["=", $this->staff_id];
        $field = "a.id,a.createtime,b.name,d.name as coupon_name,c.type,DATE_FORMAT(FROM_UNIXTIME(c.closetime), '%Y-%m-%d %H:%i:%S') AS  closetime,c.type,c.receive_no";
        $cancel=Db::name('verification_cancel')
            ->alias('a')
            ->join('verification_staff b','a.verification_staff_id=b.id')
            ->join('verification_receive c','a.verification_receive_id=c.id')
            ->join('verification_coupon d','c.verification_coupon_id=d.id')
            ->field($field)
            ->join('user e','c.user_id=e.id')
            ->order("a.id","DESC")
            ->where($where)
            ->find();
        $this->success('查询成功',$cancel);

    }

    /**
     *4、核销记录
     */
    public function selectverification_cancel(){
        $keyfield=$this->postParam['keyfield'];
         $page = $this->request->post('page', 1);
        $total = $this->request->post('total', 10);
        $where['a.verification_store_id'] =['=', $this->store_id];
        $wheres=[];
        if($keyfield){
            $wheres['c.receive_no'] =['like', '%' . $keyfield . '%'];
            $wheres['e.mobile']= ['like', '%' . $keyfield . '%'];
        }

        $field = "a.id,a.createtime,b.name,d.name as coupon_name,c.type,DATE_FORMAT(FROM_UNIXTIME(c.closetime), '%Y-%m-%d %H:%i:%S') AS  closetime,c.type,c.receive_no";
        $cancel=Db::name('verification_cancel')
            ->alias('a')
            ->join('verification_staff b','a.verification_staff_id=b.id')
            ->join('verification_receive c','a.verification_receive_id=c.id')
            ->join('verification_coupon d','c.verification_coupon_id=d.id')
            ->field($field)
            ->join('user e','c.user_id=e.id')
            ->whereOr($wheres)
            ->where($where)
            ->order("a.id","DESC")
            ->paginate($total, false, ['page' => $page])
            ->toArray();
        $this->success('查询成功',$cancel);

    }

    /**
     * 5、个人业绩
     */
    public function performance(){
        $where["verification_store_id"] = ["=", $this->store_id];
        $activity=Db::name('verification_activity')->where($where)->order('id DESC')->find();
        //先查询活动
        $this->success('查询成功',$activity);

    }

    /**
     * 6、订单查询
     */
    public function selectorder(){
        $keyfield=$this->postParam['keyfield'];

        $page = $this->request->post('page', 1);
        $total = $this->request->post('total', 10);
        $where["a.verification_store_id"] = ["=", $this->store_id];
        $wheres=[];
        if($keyfield){
            $wheres['a.phone'] =['like', '%' . $keyfield . '%'];
            $wheres['a.order_no'] = ['like', '%' . $keyfield . '%'];
        }
        $field = "a.price,b.image,a.id,a.order_no,a.name,a.phone,a.type,b.title,DATE_FORMAT(FROM_UNIXTIME(a.paytime), '%Y-%m-%d %H:%i:%S') AS paytime,a.type";
        $order=Db::name('verification_order')
            ->alias('a')
            ->join('verification_activity b','a.verification_activity_id=b.id')
            ->whereOr($wheres)
            ->field($field)
            ->order("id","DESC")
            ->where($where)
            ->paginate($total, false, ['page' => $page])
            ->toArray();
        foreach ($order['data'] as $key => $value) {
            if ($order['data'][$key]['image']) {
                $order['data'][$key]['image'] = $this->imgUrl . $order['data'][$key]['image'];
            }
        }
        $this->success('查询成功',$order);
    }

    /**
     * 7、订单详情
     */
    public function orderinfo(){
        $id =$this->postParam['id'];
        $where["a.id"] = ["=", $id];
        $field = "a.id,a.order_no,a.name,a.phone,a.type,b.title";
        $order=Db::name('verification_order')
            ->alias('a')
            ->where($where)
            ->join('verification_store b','a.verification_store_id=b.id')
            ->join('verification_activity c','a.verification_activity_id=c.id')
            ->field('a.id,a.name,a.phone,a.type,a.status,c.image,a.order_no,c.title,b.address,b.lng,b.lat,a.price,a.payMode,DATE_FORMAT(FROM_UNIXTIME(a.paytime), "%Y-%m-%d %H:%i:%S") AS paytime')
            ->find();
        //查询具体卡卷
        if($order){
            $receivewhere['verification_order_id']=$id;

            $receive=Db::name('verification_receive')
                ->alias('a')
                ->join('verification_coupon b','a.verification_coupon_id=b.id')
                ->field("a.id,b.name,b.type,b.voucher_amount,b.gift,b.consumption,b.reduction,b.consumption_name,b.image,DATE_FORMAT(FROM_UNIXTIME(b.closetime), '%Y-%m-%d %H:%i:%S') AS closetime")
                ->where($receivewhere)->select();
            $order['receive']=$receive;


            foreach ($order['receive'] as $key => $value) {
                if($order['receive'][$key]['image']){
                    $order['receive'][$key]['image']=$this->imgUrl.$order['receive'][$key]['image'];
                }
            }
            $order['image']=$this->imgUrl.$order['image'];

            $this->success("查询成功",$order);
        }else{
            $this->error("查询失败");
        }
    }

    /**
     * 7.1修改订单电话 名称
     */
    public function updateorder(){
        $id =$this->postParam['id'];
        $name =$this->postParam['name'];
        $phone =$this->postParam['phone'];
        $res=Db::name('verification_order')->where(['id'=>$id])->update(['name'=>$name,'phone'=>$phone]);
        if($res){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }

    /**
     * 8、手动核销订单中的某个卡卷
     */
    public function ordercheckconsumption(){

        $id=$this->postParam['id'];
        //先查询到领卷记录
        $where["id"] = ["=", $id];
        $where["verification_store_id"] = ["=", $this->store_id];
        $receive=Db::name('verification_receive')->where($where)->find();

        if(empty($receive)){
            $this->error('无效卡卷');
        }
        if($receive['type']==1){
            $this->error('该卡卷已使用');
        }

        if($receive['type']==2){
            $this->error('该卡卷已过期');
        }
        $time=time();
        if($receive['closetime']<$time){
            $this->error('该卡卷已过期');
        }
        Db::startTrans();
        try {
            $iswhere["id"] = ["=", $receive['id']];
            $isdata=[
                'usetime'=>$time,
                'type'=>1
            ];
            //先把领卷记录修改了
            $isreceive=Db::name('verification_receive')->where($iswhere)->update($isdata);
            //添加核销记录
            $canceldata=[
                'verification_store_id'=>$this->store_id,
                'verification_staff_id'=>$this->staff_id,
                'verification_receive_id'=>$receive['id'],
                'createtime'=>$time,
            ];
            $cancel=Db::name('verification_cancel')->insertGetId($canceldata);
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        if($cancel){
            $this->success('核销成功');
        }else{
            $this->error('核销失败');
        }
        //核销data

    }

    /**
     * 9、用户员工管理
     */
    public function userstaff(){
        $store_id=$this->store_id;
        //通过门店id查询到活动
        $time=time();
        $where["verification_store_id"] = ["=", $this->store_id];
        $activity=Db::name('verification_activity')
            ->alias('a')
            ->join('verification_store b','a.verification_store_id=b.id')
            ->field('a.*,b.address,b.name,b.phone')
            ->where($where)->find();

        //查询员工
        $page = $this->request->post('page', 1);
        $total = $this->request->post('total', 10000);
        $where["verification_store_id"] = ["=", $this->store_id];
        $staff=Db::name('verification_staff')
            ->where($where)
            ->paginate($total, false, ['page' => $page])
            ->toArray();
        $staff['activity_name']=$activity['title'];
        $staff['staff_name']=$activity['name'];
        $this->success("查询成功",$staff);
    }

    /**
     *10、修改员工职务
     */
    public function updatestaff(){
        $id=$this->postParam['id'];
        $type=$this->postParam['type'];
        $res=Db::name('verification_staff')
            ->where('id',$id)
            ->update([
                'type'=>$type
            ]);
        if($res){
            $this->success("修改成功");
        }else{
            $this->error("修改异常");
        }
    }


    /**
     * 11、个人业绩 当前门店下的业绩查询
     * page = 1分页
     */
    public function selectPerformance(){
        $store_id=$this->store_id;
        $user_id = $this->auth->id;
        $where["verification_store_id"] = ["=", $this->store_id];
        $activity=Db::name('verification_activity')
            ->alias('a')
            ->join('verification_store b','a.verification_store_id=b.id')
            ->field('a.*,b.address,b.name,b.phone')
            ->where($where)->find();
        //验证
        //$r1 = Db::name('_verification_staff')->where(['user_id'=>$user_id,'type'=>'0','verification_store_id'=>$store_id])->find();
        //if(empty($r1)){ return $this->error('仅店长可查自己店铺信息'); }
        //当前店铺下的代理人,及代理人的推广数量
        $rows = Db::name('_verification_staff')->where(['verification_store_id'=>$store_id,'type'=>['in',['0','1','3']]])->field("name,0 as count,user_id")->paginate(1000)->each(function($item){
            $item['count'] = Db::name('user')->where(['pid'=>$item['user_id']])->count();
            return $item;
        })->toArray();



        $rows['activity_name']=$activity['title'];
        $rows['staff_name']=$activity['name'];
        $this->success("查询成功",$rows);
    }


    /**
     * 12、业绩查询 当前门店下的业绩查询
     * page = 1分页
     * date 日期
     */
    public function select_performance_all(){
        $store_id=$this->store_id;
        $user_id = $this->auth->id;
        $where["verification_store_id"] = ["=", $this->store_id];
        $activity=Db::name('verification_activity')
            ->alias('a')
            ->join('verification_store b','a.verification_store_id=b.id')
            ->field('a.*,b.address,b.name,b.phone')
            ->where($where)->find();
        //参数
        $param = request()->param();
        //验证
        if(empty($param['date'])){$param['date'] = date("Y-m-d"); }
        $dates = [strtotime($param['date']." 00:00:00"),strtotime($param['date']." 23:59:59")];
        //$r1 = Db::name('_verification_staff')->where(['user_id'=>$user_id,'type'=>'0','verification_store_id'=>$store_id])->find();
        //if(empty($r1)){ return $this->error('仅店长可查自己店铺信息'); }
        //当前店铺下的代理人,及代理人的推广数量
        $rows = Db::name('_verification_staff')->where(['verification_store_id'=>$store_id,'type'=>['in',['0','1','3']]])->field("name,0 as count,user_id")->paginate(1000)->each(function($item) use ($dates){
            //一级统计
            $count_1 =  Db::name('user')->where(['pid'=>$item['user_id'],'updatetime'=>['between',$dates]])->field("id")->select();
            $item['count_1'] = count($count_1);
            //二级统计
            $item['count_2'] = 0;
            if(!empty($count_1)){
                $ids = array_column($count_1,'id');
                $item['count_2'] =Db::name('user')->where(['pid'=>['in',$ids],'updatetime'=>['between',$dates]])->field("id")->count();
            }
            //全部
            $item['count_0'] = $item['count_1']+$item['count_2'];
            return $item;
        })->toArray();
        $rows['activity_name']=$activity['title'];
        $rows['staff_name']=$activity['name'];
        $this->success("查询成功",$rows);
    }

    /**
     * 我的红包
    */
    public function red_packet(){
        $store_id=$this->store_id;
        $user_id = $this->auth->id;
//通过门店id查询到活动
        $time=time();
        $where["verification_store_id"] = ["=", $this->store_id];
        $activity=Db::name('verification_activity')
            ->alias('a')
            ->join('verification_store b','a.verification_store_id=b.id')
            ->field('a.*,b.address,b.name,b.phone')
            ->where($where)->find();

        //参数
        $param = request()->param();
        //验证
        $r1 = Db::name('verification_staff')->where(['user_id'=>$user_id,'type'=>'0','verification_store_id'=>$store_id])->find();
        if(empty($r1)){ return $this->error('仅店长可查自己店铺信息'); }
        //当前店铺下的佣金发放记录
        $rows = Db::name('verification_brokerage_record a')
        ->join("user b","a.send_user_id = b.id","left")
        ->join("verification_staff c","c.user_id = b.id")
        ->where(['a.verification_store_id'=>$store_id])
        ->field("a.commission,a.send_createtime,a.send_result, a.send_createtime,a.send_reason,b.nickname,c.phone as mobile")
        ->paginate(20)->each(function($item){
            $item['send_createtime'] = !empty($item['send_createtime'])?date("Y-m-d H:i",$item['send_createtime']):'';
            $item['send_result'] = $item['send_result'] == 1?'成功':'失败';
            return $item;
        });
        $rows['activity_name']=$activity['title'];
        $rows['staff_name']=$activity['name'];
        $this->success("查询成功",$rows);
    }

    /**
     * 14、获取代理人二维码
     */
    public function getagentimg(){
        $res=Db::name('verification_store')->where(['id'=>$this->store_id])->find();
        if($res){
            $img=$this->imgUrl.$res['agent_image'];
        }else{
            return $this->error("请求失败");
        }
        return $this->success("请求成功",$img);
    }


}