Index.php 36.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 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
<?php

namespace app\api\controller;

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

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页接口
     * @ApiTitle    (服务商申请)
     * @ApiSummary  (服务商申请)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/SellerStatus)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="CompanyMain", type="integer", required=true, description="公司主营业务")
     * @ApiParams   (name="CompanyType", type="string", required=true, description="申请业务类型:1=换电商,2=经营商,3=维修商,4=代理商")
     * @ApiParams   (name="Person", type="string", required=true, description="负责人")
     * @ApiParams   (name="Mobile", type="string", required=true, description="联系电话")
     * @ApiParams   (name="PersonEmail", type="string", required=true, description="负责人邮箱地址")
     * @ApiParams   (name="BusinessPeople", type="string", required=true, description="业务专员")
     * @ApiParams   (name="BusinessPeopleEmail", type="string", required=true, description="业务专员邮箱地址")
     * @ApiParams   (name="OpenBank", type="string", required=true, description="开户银行")
     * @ApiParams   (name="Number", type="string", required=true, description="账号")
     * @ApiParams   (name="LastYearMoney", type="string", required=true, description="上年营业额")
     * @ApiParams   (name="CompanyPeopleNum", type="string", required=true, description="公司员工数")
     * @ApiParams   (name="image", type="string", required=true, description="营业执照或负责人身份证照片")
     * @ApiParams   (name="CompanyName", type="string", required=true, description="公司或商号名称")
     * @ApiParams   (name="fa_address", type="string", required=true, description="法定地址")
     * @ApiParams   (name="yingye_address", type="string", required=true, description="营业地址")
     * @ApiParams   (name="mianji", type="string", required=true, description="营业场地面积")
     * @ApiParams   (name="tel", type="string", required=true, description="通信地址")
     * @ApiParams   (name="web", type="string", required=true, description="万维地址")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    'data':{
    })
     */
    public function SellerStatus()
    {
        $UserId = $this->IsToken($this->request->header());
        //验证申请是否唯一
        $this->OnlySeller($UserId);
        $params = $this->MyParams();
        $params['user_id'] = $UserId;
        $params['Status'] = 0;
        $params['createtime'] = time();
        $params['updatetime'] = time();
        try {
            $Res = Db::name('seller')->insert($params);
        } catch (\Exception  $e) {
            $this->error($e->getMessage());
        }
        $this->Res($Res);
    }


    /**
     * 首页接口
     * @ApiTitle    (新增/更改门店)
     * @ApiSummary  (新增/更改门店)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/InsertUpdateStor)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="integer", required=true, description="类型:1=新增,2=更改")
     * @ApiParams   (name="id", type="integer", required=true, description="门店ID/新增时不传")
     * @ApiParams   (name="URL", type="string", required=true, description="路径/新增时传")
     * @ApiParams   (name="avatar", type="string", required=true, description="门牌照")
     * @ApiParams   (name="name", type="string", required=true, description="店铺名称")
     * @ApiParams   (name="address", type="string", required=true, description="店铺位置")
     * @ApiParams   (name="lng", type="string", required=true, description="经度")
     * @ApiParams   (name="lat", type="string", required=true, description="纬度")
     * @ApiParams   (name="address_con", type="string", required=true, description="详细地址")
     * @ApiParams   (name="mobile", type="string", required=true, description="联系电话")
     * @ApiParams   (name="hours", type="string", required=true, description="营业时间")
     * @ApiParams   (name="Battery", type="string", required=true, description="电池分类Json数组type_id.num)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    'data':{
    })
     */
    public function InsertUpdateStor()
    {
        $UserId = $this->IsToken($this->request->header());
        $params = $this->request->param();
        if ($params['type'] == 1) {
            $data = [
                'user_id' => $UserId,
                'createtime' => time(),
                'updatetime' => time(),
                'avatar' => $params['avatar'],
                'name' => $params['name'],
                'address' => $params['address'],
                'lng' => $params['lng'],
                'lat' => $params['lat'],
                'address_con' => $params['address_con'],
                'mobile' => $params['mobile'],
                'hours' => $params['hours'],
                'Battery' => htmlspecialchars_decode($params['Battery']),
            ];
            $Res = Db::name('stor')->insert($data);
            $StorID = Db::name('stor')->where(['user_id' => $UserId,
                'createtime' => time(),
                'updatetime' => time(),
                'avatar' => $params['avatar'],
                'name' => $params['name'],
                'address' => $params['address'],
                'lng' => $params['lng'],
                'lat' => $params['lat'],
                'address_con' => $params['address_con'],
                'mobile' => $params['mobile'],
                'hours' => $params['hours'],
                'Battery' => htmlspecialchars_decode($params['Battery'])])->value('id');
            $this->NameCarQrCode('storID' . $StorID, $params['url'] . '?stor_id=' . $StorID);
            Db::name('stor')->where('user_id', $UserId)->where('id', $StorID)->update(['stor_image' => '/images/storID' . $StorID . '.png']);
        } else {
            $data = [
                'updatetime' => time(),
                'avatar' => $params['avatar'],
                'name' => $params['name'],
                'address' => $params['address'],
                'lng' => $params['lng'],
                'lat' => $params['lat'],
                'address_con' => $params['address_con'],
                'mobile' => $params['mobile'],
                'hours' => $params['hours'],
                'Battery' => htmlspecialchars_decode($params['Battery']),
            ];
            $Res = Db::name('stor')->where('id', $params['id'])->update($data);
        }
        $this->Res($Res);
    }


    /**
     * 首页接口
     * @ApiTitle    (门店更改回显)
     * @ApiSummary  (门店更改回显)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/UpdateStorBack)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="门店ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    "avatar": "http://kcd.qiniu.bronet.cn/uploads/20201027/lhOJtzf7HMlBaF2HRJXbDb9OaaZ_.jpg",
    "name": "王二蛋的店",
    "address": "天津市河东区大直沽街道汇贤里安教小区",
    "lng": "117.244942",
    "lat": "39.119204",
    "address_con": "天津市",
    "mobile": "13292442918", //联系方式
    "hours": "1213", 营业时间
    "stor_image": "", 店铺吗
    "Battery": [  //电池分类
    {
    "type_id": "1",
    "num": "10"
    },
    {
    "type_id": "2",
    "num": "10"
    }
    ]
    }
    })
     */
    public function UpdateStorBack()
    {
        $UserId = $this->IsToken($this->request->header());
        $ID = input('id');
        $Arr = Db::name('stor')->where('id', $ID)->where('user_id', $UserId)->find();
        if (empty($Arr)) {
            $this->error('参数错误', 0);
        }
        $BatteryArr = json_decode($Arr['Battery'], true);
        foreach ($BatteryArr as $k => $v) {
            $BatteryArr[$k]['title'] = Db::name('battery_code')->where('id', $v['type_id'])->value('title');
        }
        $data = [
            'avatar' => cdnurl($Arr['avatar']),
            'name' => $Arr['name'],
            'address' => $Arr['address'],
            'lng' => $Arr['lng'],
            'lat' => $Arr['lat'],
            'address_con' => $Arr['address_con'],
            'mobile' => $Arr['mobile'],
            'hours' => $Arr['hours'],
            'stor_image' => $this->request->domain() . $Arr['stor_image'],
            'Battery' => $BatteryArr,
        ];
        $this->success('成功', $data);
    }


    /**
     * 首页接口
     * @ApiTitle    (我的门店)
     * @ApiSummary  (我的门店)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/MyStor)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="keywords", type="string", required=true, description="搜索关键字")
     * @ApiParams   (name="start", type="string", required=true, description="开始日期")
     * @ApiParams   (name="end", type="string", required=true, description="结束日期")
     * @ApiParams   (name="pages", type="string", required=true, description="pages")
     * @ApiParams   (name="rows", type="string", required=true, description="rows")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    'data':{
    })
     */
    public function MyStor()
    {
        $UserId = $this->IsToken($this->request->header());
        $params = $this->request->param();
        $mapA = [];
        $mapB = [];
        $mapC = [];
        if (!empty($params['keywords']) || $params['keywords'] != '' || $params['keywords'] != "" || $params['keywords'] != null) {
            $mapA['name'] = ['LIKE', '%' . $params['keywords'] . '%'];
        }
        if (!empty($params['start']) || $params['start'] != '' || $params['start'] != "" || $params['start'] != null) {
            $StartTime = strtotime($params['start']);
            $mapB['createtime'] = ['GT', $StartTime];
        }
        if (!empty($params['end']) || $params['end'] != '' || $params['end'] != "" || $params['end'] != null) {
            $EndTime = strtotime($params['end']);
            $mapC['createtime'] = ['LT', $EndTime];
        }
        $Arr = Db::name('stor')->where('user_id', $UserId)->where($mapA)->where($mapB)->where($mapC)->order('createtime desc')->page($params['pages'], $params['rows'])->select();
        $Count = Db::name('stor')->where('user_id', $UserId)->where($mapA)->where($mapB)->where($mapC)->order('createtime desc')->select();
        $StorCount = Db::name('stor')->where('user_id', $UserId)->select();
        if (empty($Arr)) {
            $data = [
                'StorCount' => count($StorCount),
                'Count' => 0,
                'List' => []
            ];
        } else {
            foreach ($Arr as $k => $v) {
                $List[$k]['id'] = $v['id'];
                $List[$k]['name'] = $v['name'];
                $List[$k]['createtime'] = date('Y.m.d', $v['createtime']);
            }
            $data = [
                'StorCount' => count($StorCount),
                'Count' => count($Count),
                'List' => $List
            ];
        }
        $this->success('成功', $data);
    }


    /**
     * 首页接口
     * @ApiTitle    (签订协议)
     * @ApiSummary  (签订协议)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/Agreement)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="name", type="string", required=true, description="姓名")
     * @ApiParams   (name="seller_id", type="string", required=true, description="服务商ID")
     * @ApiParams   (name="battery_id", type="string", required=true, description="电池类型")
     * @ApiParams   (name="card_image", type="string", required=true, description="身份证照片")
     * @ApiParams   (name="mobile", type="string", required=true, description="联系方式")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": "202010290928196016917223" //订单号 用订单号去调支付
    })
     */
    public function Agreement()
    {
        $UserId = $this->IsToken($this->request->header());
        $params = $this->request->param();
        //订单号
        $OrderSn = $this->order_sn();
        //押金
        $MoneyArr = Db::name('money_config')->where('battery_id', $params['battery_id'])->find();
        $res111 = Db::name('agreement')->where('user_id', $UserId)->where('seller_id', $params['seller_id'])->where('status', 'IN', '0,1')->find();
        if (!empty($res111)) {
            $this->error('禁止重复签订协议', 0);
            die;
        }
        $data = [
            'OrderSn' => $OrderSn,
            'user_id' => $UserId,
            'name' => $params['name'],
            'battery_id' => $params['battery_id'],
            'seller_id' => $params['seller_id'],
            'mobile' => $params['mobile'],
            'UpMoney' => $MoneyArr['UpMoney'],
            'MonthMoney' => $MoneyArr['MonthMoney'] * 3,
            'Money' => $MoneyArr['MonthMoney'] * 2,
            'card_image' => $params['card_image'],
            'status' => 0,
            'refind_status' => 3,
            'createtime' => time(),
            'updatetime' => time(),
            'EXP_time' => time() + 86400 * 30
        ];
        $res = Db::name('agreement')->insert($data);
        if (!$res) {
            $this->error('失败', 0);
        } else {
            $this->success('成功', $OrderSn);
        }
    }


    /**
     * 首页接口
     * @ApiTitle    (服务商空间)
     * @ApiSummary  (服务商空间)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/SellerQzone)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="stor_id", type="string", required=true, description="门店ID /首次渲染 传0")
     * @ApiParams   (name="start", type="string", required=true, description="开始日期 /首次渲染 传空字符串''")
     * @ApiParams   (name="end", type="string", required=true, description="结束日期 /首次渲染 传空字符串''")
     * @ApiParams   (name="pages", type="string", required=true, description="pages")
     * @ApiParams   (name="rows", type="string", required=true, description="rows")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    'data':{
    })
     */
    public function SellerQzone()
    {
        $UserId = $this->IsToken($this->request->header());
        $params = $this->request->param();
        $mapA = [];
        $mapB = [];
        $mapC = [];
        if ($params['stor_id'] != 0) {
            $mapA['o.stor_id'] = ['eq', $params['stor_id']];
        }
        if (!empty($params['start']) || $params['start'] != '' || $params['start'] != "" || $params['start'] != null) {
            $StartTime = strtotime($params['start']);
            $mapB['o.createtime'] = ['GT', $StartTime];
        }
        if (!empty($params['end']) || $params['end'] != '' || $params['end'] != "" || $params['end'] != null) {
            $EndTime = strtotime($params['end']);
            $mapC['o.createtime'] = ['LT', $EndTime];
        }
        $Arr = Db::name('order')
            ->alias('o')
            ->join('stor s', 's.id=o.stor_id')
            ->where('s.user_id', $UserId)
            ->join('user u', 'u.id=o.user_id')
            ->where($mapA)
            ->where($mapB)
            ->where($mapC)
            ->order('id desc')
            ->page($params['pages'], $params['rows'])
            ->field('o.id,u.nickname,u.avatar,o.createtime')
            ->select();
        $Count = Db::name('order')
            ->alias('o')
            ->join('stor s', 's.id=o.stor_id')
            ->where('s.user_id', $UserId)
            ->join('user u', 'u.id=o.user_id')
            ->where($mapA)
            ->where($mapB)
            ->where($mapC)
            ->order('id desc')
            ->page($params['pages'], $params['rows'])
            ->field('o.id,u.nickname,u.avatar,o.createtime')
            ->select();
        $OrderCount = Db::name('order')
            ->alias('o')
            ->join('stor s', 's.id=o.stor_id')
            ->where('s.user_id', $UserId)
            ->select();
        if (empty($Arr)) {
            $data = [
                'OrderCount' => count($OrderCount),
                'Count' => count($Count),
                'List' => []
            ];
        } else {
            foreach ($Arr as $k => $v) {
                $List[$k]['id'] = $v['id'];
                $List[$k]['nickname'] = $v['nickname'];
                $List[$k]['avatar'] = $v['avatar'];
                $List[$k]['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
            }
            $data = [
                'OrderCount' => count($OrderCount),
                'Count' => count($Count),
                'List' => $List
            ];
        }
        $this->success('成功', $data);
    }


    /**
     * 首页接口
     * @ApiTitle    (我的客户)
     * @ApiSummary  (我的客户)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/MyCustomer)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="string", required=true, description="协议审核状态:0=待审核,1=协议中,2=已终止,3=已欠费")
     * @ApiParams   (name="pages", type="string", required=true, description="pages")
     * @ApiParams   (name="rows", type="string", required=true, description="rows")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    "Count": 1, //数量
    "List": [
    {
    "id": 1,  //id
    "nickname": "王叨叨",
    "avatar": "https://thirdwx.qlogo.cn/mmopen/vi_32/5xl51Ja2tE9bZOVqrntjJX1He1xx6OfCEY6HIep0p4YtDzmM00NB6ia5S50AVLJv0dKHKPjOCF6jXQqhfrlKkyQ/132",
    "UpMoney": "300.00", //押金
    "Money": "200.00",  //剩余租金
    "mobile": "13292442918",  //联系方式
    "status": 0,   //协议审核状态:0=待审核,1=协议中,2=已终止,3=已欠费,4=签约失败
    "createtime": "2020-10-29" //协议生成时间
    }
    ]
    }
    })
     */
    public function MyCustomer()
    {
        $UserId = $this->IsToken($this->request->header());
        $params = $this->request->param();
        if ($params['type'] == 0 || $params['type'] == 1 || $params['type'] == 3) {
            $map['a.status'] = ['eq', $params['type']];
        } else {
            $map['a.status'] = ['IN', '2,4'];
        }
        $Arr = Db::name('agreement')
            ->alias('a')
            ->join('seller s', 's.id=a.seller_id')
            ->where('s.user_id', $UserId)
            ->join('user u', 'u.id=a.user_id')
            ->where($map)
            ->page($params['pages'], $params['rows'])
            ->order('id desc')
            ->field('a.id,a.mobile,u.nickname,a.status,u.avatar,a.UpMoney,a.Money,a.createtime')
            ->select();
        $Count = Db::name('agreement')
            ->alias('a')
            ->join('seller s', 's.id=a.seller_id')
            ->where('s.user_id', $UserId)
            ->join('user u', 'u.id=a.user_id')
            ->where('a.status', $params['type'])
            ->order('id desc')
            ->field('a.id,a.mobile,u.nickname,a.status,u.avatar,a.UpMoney,a.Money,a.createtime')
            ->select();
        if (empty($Arr)) {
            $data = [
                'Count' => 0,
                'List' => []
            ];
        } else {
            foreach ($Arr as $k => $v) {
                $List[$k]['id'] = $v['id'];
                $List[$k]['nickname'] = $v['nickname'];
                $List[$k]['avatar'] = $v['avatar'];
                $List[$k]['UpMoney'] = $v['UpMoney'];
                $List[$k]['Money'] = $v['Money'];
                $List[$k]['mobile'] = $v['mobile'];
                $List[$k]['status'] = $v['status'];
                $List[$k]['createtime'] = date('Y-m-d', $v['createtime']);
            }
            $data = [
                'Count' => count($Count),
                'List' => $List
            ];
        }
        $this->success('成功', $data);
    }


    /**
     * 首页接口
     * @ApiTitle    (押金退还列表)
     * @ApiSummary  (押金退还列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/UpMoneyList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="string", required=true, description="状态:0=待审核,1=已通过,2=已拒绝")
     * @ApiParams   (name="pages", type="string", required=true, description="pages")
     * @ApiParams   (name="rows", type="string", required=true, description="rows")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    'data':{
    })
     */
    public function UpMoneyList()
    {
        $UserId = $this->IsToken($this->request->header());
        $params = $this->request->param();
        $Arr = Db::name('agreement')
            ->alias('a')
            ->join('seller s', 's.id=a.seller_id')
            ->join('user u', 'u.id=a.user_id')
            ->where('s.user_id', $UserId)
            ->where('a.refind_status', $params['type'])
            ->page($params['pages'], $params['rows'])
            ->order('id desc')
            ->field('a.id,a.OrderSn,u.avatar,u.nickname,a.updatetime,a.status')
            ->select();
        $Count = Db::name('agreement')
            ->alias('a')
            ->join('seller s', 's.id=a.seller_id')
            ->join('user u', 'u.id=a.user_id')
            ->where('s.user_id', $UserId)
            ->where('a.refind_status', $params['type'])
            ->order('id desc')
            ->field('a.id,a.OrderSn,u.avatar,u.nickname,a.updatetime,a.status')
            ->select();
        if (empty($Arr)) {
            $data = [
                'Count' => 0,
                'List' => []
            ];
        } else {
            foreach ($Arr as $k => $v) {
                $List[$k]['id'] = $v['id'];
                $List[$k]['OrderSn'] = $v['OrderSn'];
                $List[$k]['avatar'] = $v['avatar'];
                $List[$k]['nickname'] = $v['nickname'];
                $List[$k]['updatetime'] = date('Y-m-d H:i:s', $v['updatetime']);
                $List[$k]['status'] = $v['status'];
            }
            $data = [
                'Count' => count($Count),
                'List' => $List
            ];
        }
        $this->success('成功', $data);
    }


    /**
     * 首页接口
     * @ApiTitle    (首页地图门店)
     * @ApiSummary  (首页地图门店)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/MapStor)
     * @ApiParams   (name="battery_code", type="string", required=true, description="电池编码")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    'data':{
    })
     */
    public function MapStor()
    {
        $Arr = Db::name('stor')->select();
        $Code = input('battery_code');
        if (empty($Code) || $Code == '' || $Code == "" || $Code == null) {
            $this->error('请先链接蓝牙', 0);
            die;
        }
        $map['BatteryCode'] = ['LIKE', '%' . $Code . '%'];
        //用户电池分类ID
        $Id = Db::name('battery_code')->where($map)->value('id');
        if (empty($Id)) {
            $this->error('系统没有找到该电池分类', 0);
            die;
        }
        if (empty($Arr)) {
            $List = [];
        } else {
            $i = 0;
            foreach ($Arr as $k => $v) {
                $List[$k]['id'] = $v['id'];
                $List[$k]['name'] = $v['name'];
                $List[$k]['lng'] = $v['lng'];
                $List[$k]['lat'] = $v['lat'];
                $JsonArr = json_decode($v['Battery'], true);
                foreach ($JsonArr as $k1 => $v1) {
                    if ($v1['type_id'] == $Id) {
                        $List[$k]['num'] = $v1['num'];
                        $i++;
                    }
                }
                if ($i == 0) {
                    $List[$k]['num'] = 0;
                }
            }
        }
        $this->success('成功', $List);
    }


    /**
     * 首页接口
     * @ApiTitle    (去换电)
     * @ApiSummary  (去换电)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/GoGoing)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="string", required=true, description="门店id")
     * @ApiParams   (name="battery_code", type="string", required=true, description="电池编号")
     * @ApiParams   (name="manufactor", type="string", required=true, description="厂家")
     * @ApiParams   (name="Voltage", type="string", required=true, description="电压")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',  //99998 请先签订协议 跳转签订
    'msg':'返回成功',
    'data':{
    })
     */
    public function GoGoing()
    {
        $UserId = $this->IsToken($this->request->header());
        $params = $this->request->param();
        $map['BatteryCode'] = ['LIKE', '%' . $params['battery_code'] . '%'];
        //用户电池分类ID
        $Id = Db::name('battery_code')->where($map)->value('id');
        if (empty($Id)) {
            $this->error('系统没有找到该电池分类', 0);
            die;
        }
        $IsAgreement = Db::name('stor')
            ->alias('r')
            ->where('r.id', $params['id'])
            ->join('seller s', 'r.user_id=s.user_id')
            ->join('agreement a', 'a.seller_id=s.id')
            ->where('a.user_id', $UserId)
            ->where('a.status', 1)
            ->find();
        if (empty($IsAgreement)) {
            $this->error('请先签订协议', '', '99998');
        }
        $OrderSn = $this->order_sn();
        $data = [
            'OrderSn' => $OrderSn,
            'stor_id' => $params['id'],
            'user_id' => $UserId,
            'battery_id' => $Id,
            'battery_code' => $params['battery_code'],
            'manufactor' => $params['manufactor'],
            'Voltage' => $params['Voltage'],
            'status' => 0,
            'EXP_time' => time() + 60 * 30,
            'createtime' => time(),
            'updatetime' => time()
        ];
        $res = Db::name('order')->insert($data);
        $this->res($res);
    }


    /**
     * 首页接口
     * @ApiTitle    (我的订单)
     * @ApiSummary  (我的订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/MyOrder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="string", required=true, description="订单状态:0=待换电,1=已换电,9=全部")
     * @ApiParams   (name="pages", type="string", required=true, description="pages")
     * @ApiParams   (name="rows", type="string", required=true, description="rows")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    "Count": 1, //总数
    "List": [
    {
    "id": 1,
    "OrderSn": "202010291005404651432049",
    "battery_code": "123", //设备号
    "nickname": "王叨叨",
    "avatar": "https://thirdwx.qlogo.cn/mmopen/vi_32/5xl51Ja2tE9bZOVqrntjJX1He1xx6OfCEY6HIep0p4YtDzmM00NB6ia5S50AVLJv0dKHKPjOCF6jXQqhfrlKkyQ/132",
    "status": 0,  订单状态:0=待换电,1=已换电,2=已过期,3=异常订单
    "createtime": 2020-10-27, //下单时间
    "ok_time": "", //完成时间
    "title": "电池类型一", // 电池类型
    "manufactor": "1231", //厂家
    "Voltage": "123", // 电压
    "yes_time": "" //商家确认时间
    }
    ]
    }
    })
     */
    public function MyOrder()
    {
        $UserId = $this->IsToken($this->request->header());
        $params = $this->request->param();
        $Map = [];
        if ($params['type'] != 9) {
            $Map['o.status'] = ['eq', $params['type']];
        }
        $Arr = Db::name('order')
            ->alias('o')
            ->join('stor s', 's.id=o.stor_id')
            ->where('s.user_id', $UserId)
            ->page($params['pages'], $params['rows'])
            ->where($Map)
            ->join('user u', 'u.id=o.user_id')
            ->join('battery_code b', 'b.id=o.battery_id')
            ->order('id desc')
            ->field('o.id,o.OrderSn,o.battery_code,u.nickname,u.avatar,o.status,o.createtime,o.ok_time,b.title,o.manufactor,o.Voltage,o.yes_time')
            ->select();
        $Count = Db::name('order')
            ->alias('o')
            ->join('stor s', 's.id=o.stor_id')
            ->where('s.user_id', $UserId)
            ->where($Map)
            ->join('user u', 'u.id=o.user_id')
            ->join('battery_code b', 'b.id=o.battery_id')
            ->order('id desc')
            ->field('o.id,o.OrderSn,o.battery_code,u.nickname,u.avatar,o.status,o.createtime,o.ok_time,b.title,o.manufactor,o.Voltage,o.yes_time')
            ->select();
        if (empty($Arr)) {
            $data = [
                'Count' => 0,
                'List' => []
            ];
        } else {
            foreach ($Arr as $k => $v) {
                $List[$k]['id'] = $v['id'];
                $List[$k]['OrderSn'] = $v['OrderSn'];
                $List[$k]['battery_code'] = $v['battery_code'];
                $List[$k]['nickname'] = $v['nickname'];
                $List[$k]['avatar'] = $v['avatar'];
                $List[$k]['status'] = $v['status'];
                $List[$k]['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
                if ($v['ok_time'] == null) {
                    $List[$k]['ok_time'] = '';
                } else {
                    $List[$k]['ok_time'] = date('Y-m-d H:i:s', $v['ok_time']);
                }
                $List[$k]['title'] = $v['title'];
                $List[$k]['manufactor'] = $v['manufactor'];
                $List[$k]['Voltage'] = $v['Voltage'];
                if ($v['yes_time'] == null) {
                    $List[$k]['yes_time'] = '';
                } else {
                    $List[$k]['yes_time'] = date('Y-m-d H:i:s', $v['yes_time']);
                }
            }
            $data = [
                'Count' => count($Count),
                'List' => $List
            ];
        }
        $this->success('成功', $data);
    }


    /**
     * 首页接口
     * @ApiTitle    (账户押金)
     * @ApiSummary  (账户押金)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/MyUpMoney)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="pages", type="string", required=true, description="pages")
     * @ApiParams   (name="rows", type="string", required=true, description="rows")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    "Count": 1, //总数
    "List": [
    {
    "id": 1,
    "OrderSn": "202010290928196016917223", //订单
    "UpMoney": "300.00", //押金
    "Money": "200.00", //剩余租金
    "status": 2, 协议审核状态:0=待审核,1=协议中,2=已终止,3=已欠费,4=签约失败
    "refind_status": 3, 退款审核状态:0=待审核,1=已通过,2=已拒绝,3=未发起退款
    "CompanyName": "王二蛋的垃圾站"
    }
    ]
    }
    })
     */
    public function MyUpMoney()
    {
        $UserId = $this->IsToken($this->request->header());
        $params = $this->request->param();
        $Arr = Db::name('agreement')
            ->alias('a')
            ->where('a.user_id', $UserId)
            ->join('seller s', 's.id=a.seller_id')
            ->page($params['pages'], $params['rows'])
            ->order('id desc')
            ->field('a.id,a.OrderSn,a.UpMoney,a.Money,a.status,a.refind_status,s.CompanyName')
            ->select();
        $Count = Db::name('agreement')
            ->alias('a')
            ->join('seller s', 's.id=a.seller_id')
            ->where('a.user_id', $UserId)
            ->order('id desc')
            ->field('a.id,a.OrderSn,a.UpMoney,a.Money,a.status,a.refind_status,s.CompanyName')
            ->select();
        if (empty($Arr)) {
            $data = [
                'Count' => 0,
                'List' => []
            ];
        } else {
            foreach ($Arr as $k => $v) {
                $List[$k]['id'] = $v['id'];
                $List[$k]['OrderSn'] = $v['OrderSn'];
                $List[$k]['UpMoney'] = $v['UpMoney'];
                $List[$k]['Money'] = $v['Money'];
                $List[$k]['status'] = $v['status'];
                $List[$k]['refind_status'] = $v['refind_status'];
                $List[$k]['CompanyName'] = $v['CompanyName'];
            }
            $data = [
                'Count' => count($Count),
                'List' => $List
            ];
        }
        $this->success('成功', $data);
    }


    /**
     * 首页接口
     * @ApiTitle    (门店删除)
     * @ApiSummary  (门店删除)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/DeleteStor)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="string", required=true, description="门店ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    }
    })
     */
    public function DeleteStor()
    {
        $UserId = $this->IsToken($this->request->header());
        $params = $this->request->param();
        $res = Db::name('stor')->where('id', $params['id'])->where('user_id', $UserId)->delete();
        $this->res($res);
    }


    /**
     * 首页接口
     * @ApiTitle    (电池电量存储)
     * @ApiSummary  (电池电量存储)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/InsertBattery)
     * @ApiParams   (name="code", type="string", required=true, description="电池编码")
     * @ApiParams   (name="battery", type="string", required=true, description="电量")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    }
    })
     */
    public function InsertBattery()
    {
        $Params = $this->request->param();
        $data = [
            'code' => $Params['code'],
            'battery' => $Params['battery'],
            'createtime' => time()
        ];
        $Res = Db::name('battery')->insert($data);
        $this->Res($Res);
    }
}