Pickup.php 26.9 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
<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: dyr
 * Date: 2016-08-19
 */

namespace app\admin\controller;

use think\Page;
use think\AjaxPage;
use think\Db;

class Pickup extends Base
{

    public function index()
    {
        $pagenum = 10;//每页显示多少条
        if ((int)I('pagenum/s') > 0) {
            $pagenum = I('pagenum/s');
        }
        $p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
        $this->assign('province', $p);
        $province_id = I('province_id');
        $city_id = I('city_id');
        $district_id = I('district_id');
        $category_id = I('category_id');
        $key_word = urldecode(urldecode(I('key_word')));
        $getp = I('p');
        $this->assign('province_id', $province_id);// 赋值分页输出
        $this->assign('city_id', $city_id);
        $this->assign('district_id', $district_id);
        $this->assign('category_id', $category_id);
        $this->assign('key_word', $key_word);
        $this->assign('cur_page', $getp);
        $order_by_field = I('order_by_field', 'pickup_id');
        $order_by_mode = I('order_by_mode', 'desc');

        $categoryList = M('storage_category')->where(array('store_id' => getAdmStoId()))->select();
        $this->assign('categoryList', $categoryList);

        $oldurl = U('Admin/Pickup/index', array(
            "province_id" => $province_id,
            "category_id" => $category_id,
            "city_id" => $city_id,
            "district_id" => $district_id,
            "order_by_field" => $order_by_field,
            "order_by_mode" => $order_by_mode,
            "key_word" => $key_word,
            "pagenum" => $pagenum,
            "p" => $getp
        ));
        $this->assign('oldurl', urlencode($oldurl));
        $this->assign('pagenum', $pagenum);
        return $this->fetch('', getAdmStoId());
    }

    public function ajaxPickupList()
    {
        $province_id = I('province_id');                //省
        $city_id = I('city_id');                        //市
        $district_id = I('district_id');                //区
        $category_id = I('category_id');                     //门店列表
        $key_word = urldecode(urldecode(I('key_word')));                      //关键字
        $order_by_field = I('order_by_field', 'pickup_id');
        $order_by_mode = I('order_by_mode', 'desc');
        $isstop = I('isstop/d',0);
        $cur_page = 1;//当前页数
        if ((int)I('p/s') > 0) {
            $cur_page = I('p/s');
        }
        $pagenum = 20;//每页显示多少条
        if ((int)I('pagenum/s') > 0) {
            $pagenum = I('pagenum/s');
        }
        $categoryList = M('storage_category')->where(array('store_id' => getAdmStoId()))->select();
        $this->assign('categoryList', $categoryList);
        $this->assign('pagenum', $pagenum);
        $this->assign('cur_page', $cur_page);
        $this->assign('province_id', $province_id);
        $this->assign('city_id', $city_id);
        $this->assign('district_id', $district_id);
        $this->assign('category_id', $category_id);
        $this->assign('key_word', $key_word);


        $oldurl = U('Admin/Pickup/index', array(
            "province_id" => $province_id,
            "category_id" => $category_id,
            "city_id" => $city_id,
            "district_id" => $district_id,
            "order_by_field" => $order_by_field,
            "order_by_mode" => $order_by_mode,
            "key_word" => $key_word,
            "pagenum" => $pagenum,
            "p" => $cur_page
        ));


        $where = ' p.store_id=' . getAdmStoId();
        if (!empty($province_id)) {
            $where .= ' and p.province_id=' . $province_id;
        }
        if (!empty($city_id)) {
            $where .= ' and p.city_id=' . $city_id;
        }
        if (!empty($district_id)) {
            $where .= ' and p.district_id=' . $district_id;
        }
        if (!empty($key_word)) {
            $where .= " and (p.pickup_name like '%" . $key_word . "%' or p.pickup_no like '%" . $key_word . "%')";
        }
        if (!empty($category_id)) {
            $where .= ' and p.category_id=' . $category_id;
        }
        if ($isstop)
        {
            switch ($isstop)
            {
                case "1":
                    $where .= ' and p.isstop=0';
                    break;
                case "2":
                    $where .= ' and p.isstop=1';
                    break;
            }
        }

        $count = M('pick_up')->alias('p')->where($where)->count();
        $Page = new AjaxPage($count, $pagenum);
        $show = $Page->show();

        $pickupList = M('pick_up')
            ->alias('p')
            ->field('p.*,r1.name as province_name,r2.name as city_name,
				     r3.name as district_name,s.suppliers_name,s2.cat_name')
            ->join('__REGION__ r1', 'r1.id = p.province_id', 'LEFT')
            ->join('__REGION__ r2', 'r2.id = p.city_id', 'LEFT')
            ->join('__REGION__ r3', 'r3.id = p.district_id', 'LEFT')
            ->join('__SUPPLIERS__ s', 's.suppliers_id = p.suppliersid', 'LEFT')
            ->join('wxd_storage_category s2', 's2.cat_id = p.category_id', 'LEFT')
            ->where($where)
            ->order($order_by_field . ' ' . $order_by_mode)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();

        $this->assign('pickupList', $pickupList);
        $this->assign('page', $show);// 赋值分页输出
        $this->assign('pager', $Page);
        $this->assign('pagenum', $pagenum);

        $this->assign('oldurl', urlencode($oldurl));
//        upload_ylp_log('门店列表');
        return $this->fetch('', getAdmStoId());
    }

    //线下门店
    public function search_storage()
    {

        return $this->fetch('', getAdmStoId());
    }

    public function import_storage()
    {
        $stoid = getAdmStoId();
        $getid = urlencode(I('idlist'));
        $getid1 = explode(",", $getid);
        $getsumcount = count($getid1);
        $getnewidlist = $getid;


        $wdata['Ids'] = $getnewidlist;

        $accdb = tpCache('shop_info.ERPId', $stoid);
        $rs = getApiData_java("api/erp/msg/storages/import/page", $accdb, $wdata, 1, 1000);
        if ($rs) {
            $rs = json_decode($rs, true);
            if ($rs['code'] == 0 && $rs['data']['pageData']) {
                foreach ($rs['data']['pageData'] as $k => $v) {

                    $check = M('pick_up')->where(['store_id' => $stoid, 'keyid' => $v['Id']])->find();

                    if (empty($check)) {
                        $getaddressname = $v['Address'];
                        $getaddressname1 = explode("\\", $getaddressname);
                        $getprovince = "";
                        $getcity = "";
                        $getdistrict = "";
                        $getprovince = $getaddressname1[0];
                        $getcity = $getaddressname1[1];
                        $getdistrict = $getaddressname1[2];

                        $res = M('region')->where(array('name' => $getprovince, 'level' => 1))->find();
                        if ($getcity) {
                            $res1 = M('region')->where(array('name' => $getcity, 'level' => 2))->find();
                        }
                        if ($getdistrict) {
                            $res2 = M('region')->where(array('name' => $getdistrict, 'level' => 3))->find();
                        }
                        $geteditaddress = $v['editaddress'];

                        if ($res) {
                            $import["province_id"] = $res["id"];
                        }
                        if ($res1) {
                            $import["city_id"] = $res1["id"];
                        }
                        if ($res2) {
                            $import["district_id"] = $res2["id"];
                        }
                        $getfulladdress = $getprovince . $getcity . $getdistrict . $geteditaddress;

                        //查询经纬度
                        $getlon = 0;
                        $getlat = 0;
                        $qqinfo = httpRequest("https://apis.map.qq.com/ws/geocoder/v1/?address=" . $getfulladdress . "&key=H5ABZ-KIZK5-3WAIY-QY3TE-XVVHV-GPB4J");
                        if ($qqinfo) {
                            $qqinfo = json_decode($qqinfo, true);

                            if ($qqinfo['status'] == 0) {
                                $getlon = $qqinfo['result']['location']['lng'];
                                $getlat = $qqinfo['result']['location']['lat'];
                            }
                        }


                        $import['pickup_no'] = $v['StorageNo'];
                        $import['pickup_name'] = $v['StorageName'];
                        $import['pickup_address'] = $geteditaddress;
                        $import['fulladdress'] = $getfulladdress;

                        $import['pickup_phone'] = $v['Tel'] ? $v['Tel'] : "";
                        $import['pickup_contact'] = $v['Manager'] ? $v['Manager'] : "";
                        $import['store_id'] = $stoid;
                        $import['keyid'] = $v['Id'];
                        $import['lon'] = $getlon;
                        $import['lat'] = $getlat;
                        $import['isstop'] = $v['IsStop'] ? 1 : 0;
                        $import['isoff'] = $v['IsOff'] ? 1 : 0;

//                        if($v['ClassId']) {
//                            $rs = M('storage_category')->where('move_classid', $v['ClassId'])->find();
//                            if ($rs) {
//                                $import['category_id'] = $rs['cat_id'];
//                            } else {
//                            }
//                        }
                        M('pick_up')->save($import); // 写入数据到数据库
                    } else {
                        if ($getsumcount = 1) {
                            return json(['code' => 0, 'msg' => '该门店已经存在门店列表,编号为' . $check['pickup_no'] . ',请点击同步信息']);
                        }
                    }
                }
                return json(['code' => 1, 'msg' => '导入成功']);
            } else {
                return json(['code' => 0, 'msg' => '未找到数据']);
            }
        } else {
            return json(['code' => 0, 'msg' => '未找到数据']);
        }
    }

    public function add()
    {
        $oldurl = I('oldurl/s');

        $category = M("storage_category")->where("store_id", getAdmStoId())->select();
        $this->assign('category', $category);
        $url = urldecode(I('oldurl'));
        if (IS_POST) {

            ClearALLCache();
            delFile(TEMP_PATH . "/" . getAdmStoId());

            $data = I('post.');


            $pickup_id = I('post.pickup_id');
            $model = M('pick_up');


            mlog(json_encode($data) . ':' . $pickup_id, '/' . getAdmStoId());
            $accdb=getERPId();
            if (empty($pickup_id)) {
                //添加
                unset($data['pickup_id']);
                $data["store_id"] = getAdmStoId();
                $searchexit = M('pick_up')->where(array('store_id' => getAdmStoId(), 'pickup_no' => I('post.pickup_no')))->find();
                $searchkeyid = M('pick_up')->where(array('store_id' => getAdmStoId(), 'keyid' => I('post.keyid')))->find();
                if ($searchexit) {
                    $this->error('添加的门店编号已存在', U('Admin/Pickup/add'));
                    exit;
                }
                if ($searchkeyid) {
                    $this->error('该门店已经存在门店列表,编号为' . $searchkeyid['pickup_no'] . ',请点击同步信息', U('Admin/Pickup/add'));
                    exit;
                }
                if ($data['lat'] && $data['lon'] && $data['keyid'])
                {
                    $updatadata = array();
                    $updatadata[0]['GPSX'] = $data['lat'];
                    $updatadata[0]['GPSY'] = $data['lon'];
                    $updatadata[0]['Id'] = $data['keyid'];
                    $updatainfo = getApiData_java_p("/api/erp/msg/storages/gps/update", $accdb, json_encode($updatadata), 1, 1, null, 'PUT', 1);

                }
                $add_res = $model->add($data);
                if ($add_res === false) {
                    $this->error('添加失败', U('Admin/Pickup/add'));
                } else {
                    $this->success('添加成功', U('Admin/Pickup/index'));
                }
            } else {
                unset($data['pickup_id']);
                $info = M('pick_up')->where(array('store_id' => getAdmStoId(), 'pickup_id' => $pickup_id))->find();
                if ($data['lat']!=$info['lat'] || $data['lon']!=$info['lon'])
                {
                    $updatadata = array();
                    $updatadata[0]['GPSX'] = $data['lat'];
                    $updatadata[0]['GPSY'] = $data['lon'];
                    $updatadata[0]['Id'] = $data['keyid'];
                    $updatainfo = getApiData_java_p("/api/erp/msg/storages/gps/update", $accdb, json_encode($updatadata), 1, 1, null, 'PUT', 1);

                }
                //修改
                $update_res = $model->where('pickup_id', $pickup_id)->save($data);

                if ($update_res === 0) {
                    $this->error('更新失败', U('Admin/Pickup/edit_address', array('pickup_id' => $pickup_id)));
                } else {
                    upload_ylp_log('A09编辑门店/确认提交');
                    $this->success('更新成功', $oldurl);
                }
            }
        }
        $p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
        $this->assign('province', $p);
        $this->assign('url', $url);
        return $this->fetch('', getAdmStoId());
    }

    /**
     * 地址编辑
     */
    public function edit_address()
    {
        $oldurl = I('oldurl/s');
        if ($oldurl) {
            $oldurl = urldecode(urldecode($oldurl));
        }

        $category = M("storage_category")->where("store_id", getAdmStoId())->select();
        $this->assign('category', $category);
        $this->assign('oldurl', $oldurl);
        $url = urldecode(I('oldurl'));
        $id = I('get.pickup_id');
        $pickup = M('pick_up')->alias('a')
            ->join('store b', 'a.store_id=b.store_id', 'left')
            ->field('a.*,b.api_token')
            ->where(array('pickup_id' => $id))->find();
        if ($pickup) {
            if (empty($pickup['keyid'])) {

                $map['StorageNo'] = array('=', $pickup['pickup_no']);
                $rs = getApiData('wxd.base.storages.list.get', $pickup['api_token'],
                    $data2 = null, $map, $page = 1, $len = 10, $order = null);
                if ($rs) {
                    $rs = json_decode($rs, true);
                    if ($rs['code'] == 1) {
                        $pickup["keyid"] = $rs['data'][0]['Id'];
                    }

                }
            }
        }
        //获取省份
        $p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
        $c = M('region')->where(array('parent_id' => $pickup['province_id'], 'level' => 2))->select();
        $d = M('region')->where(array('parent_id' => $pickup['city_id']))->select();


        $this->assign('province', $p);
        $this->assign('city', $c);
        $this->assign('district', $d);
        $this->assign('pickup', $pickup);
        $this->assign('url', $oldurl);
        return $this->fetch('add', getAdmStoId());
    }

    public function del()
    {
        ClearALLCache();
        delFile(TEMP_PATH . "/" . getAdmStoId());
        $id = I('get.pickup_id');

        $od = M("order")->where('store_id', getAdmStoId())->where('pickup_id', $id)->limit(1)->select();
        if ($od) {
            $return_arr = array('status' => -1, 'msg' => '该门店有订单,不能删除');
            $this->ajaxReturn($return_arr);
        }

        M('pick_up')->where(array('pickup_id' => $id))->delete();
//		upload_ylp_log('门店删除');
        $return_arr = array('status' => 1, 'msg' => '操作成功', 'data' => '',);
        $this->ajaxReturn($return_arr);
    }

    public function deltype()
    {
        ClearALLCache();
        delFile(TEMP_PATH . "/" . getAdmStoId());

        $id = I('get.id');
        $list = M('pick_up')->where(array('category_id' => $id))->count();
        if ($list > 0) {
            $return_arr = array('status' => -1, 'msg' => '操作失败,分类底下有门店', 'data' => '',);
        } else {
            M('storage_category')->where(array('cat_id' => $id))->delete();
            $return_arr = array('status' => 1, 'msg' => '操作成功', 'data' => '',);
        }
        $this->ajaxReturn($return_arr);
    }


    /*---选择门店--*/
    public function search()
    {
        return $this->fetch('', getAdmStoId());
    }

    /*--访问接口选择门店--*/
    public function SelectShop()
    {

        $p = I('p/d', 0);
        $isonline = I('isonline');
        /*--测试用常量--*/
        $key = urldecode(I('key/s'));
        $accdb = tpCache("shop_info.ERPId", getAdmStoId());
        if (!empty($isonline) && $isonline == 1) {
            $rs = DB::query("select * from wxd_pick_up where store_id='" . getAdmStoId() . "' and (pickup_name like '%" . $key . "%' or pickup_no like  '%" . $key . "%')");
            if (!empty($rs)) {
                foreach ($rs as $k => $vo) {
                    $rs[$k] = array(
                        'Id' => $rs[$k]['pickup_id'],
                        'StorageNo' => $rs[$k]['pickup_no'],
                        'StorageName' => $rs[$k]['pickup_name'],
                        'Address' => $rs[$k]['pickup_address']
                    );
                }
                $rs = array(
                    'code' => 1,
                    'data' => $rs,
                    'count' => count($rs)
                );
                return $rs;
            }
        } else {
            $wdata['KeyWords'] = urlencode($key);
            $rs = getApiData_java_p("api/erp/msg/storages/import/page", $accdb, $wdata, 1, 1000);
        }

        if ($rs) {
            $data = json_decode($rs, true);
            $str = json($data);
            return $str;
        } else {
            return json(["code" => "-1", "msg" => "获取接口数据错误"]);
        }
    }

    /*--keyid 是否存在--*/
    public function searchshop()
    {

        /*--测试用常量--*/
        $keyid = urldecode(I('keyid/s'));
        if (empty($keyid)) {
            return json(["code" => "-1", "msg" => "参数有误"]);
        }
        $rs = M('pick_up')->where(array('store_id' => getAdmStoId(), 'keyid' => $keyid))->field('pickup_no,pickup_name')->find();
        if ($rs) {
            $rs = array(
                'code' => 1,
                'data' => $rs,
            );
            return $rs;


        } else {
            return json(["code" => "-1", "msg" => "所添加的门店信息不存在"]);
        }
    }

    //门店分类列表
    public function storages_category_list()
    {

        $model = M("storage_category");

        $pagenum = 20;//每页显示多少条
        if ((int)I('pagenum/s') > 0) {
            $pagenum = I('pagenum/s');
        }
        $keyword = I('keyword');
        $getAdmStoId = getAdmStoId();
        $where['store_id'] = $getAdmStoId;
        if (!empty($keyword)) {
            $where['cat_name'] = array(['like', '%' . $keyword . '%']);
        }

        $count = $model->where($where)->count();
        $Page = $pager = new Page($count, $pagenum);
        $nationList = $model->where($where)->order("`sort` asc")->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $show = $Page->show();
        $this->assign('pager', $pager);
        $this->assign('show', $show);
        $this->assign('cateList', $nationList);

        $this->assign('pagenum', $pagenum);
        $this->assign('oldurl', urlencode(curPageURL()));
//        upload_ylp_log('门店分类');
        return $this->fetch('category_list', getAdmStoId());

    }

    //门店分类编辑
    public function addEditCate()
    {

        $id = I('id');
        $oldurl = I('oldurl/s');
        if ($oldurl) {
            $oldurl = urldecode(urldecode($oldurl));
        }
        if (IS_POST) {
            ClearALLCache();
            delFile(TEMP_PATH . "/" . getAdmStoId());

            $data = input('post.');
            $getAdmStoId = getAdmStoId();
            $data['store_id'] = $getAdmStoId;
            upload_ylp_log('A08分类详情/确认提交');
            if ($id)
                M("storage_category")->update($data);
            else
                M("storage_category")->insert($data);
            if ($oldurl) {
                $this->success('操作成功', $oldurl);
            } else {
                $this->success('操作成功', U('Pickup/storages_category_list'));
            }
            exit;
        }
        $brand = M("storage_category")->find($id);
        if (empty($brand)) {
            $m = Db::query("select max(sort)odr from wxd_storage_category where store_id=" . getAdmStoId());
            $md = (int)$m[0]["odr"];
            $md++;
            $brand["sort"] = $md;
        }
        $this->assign('category', $brand);
        return $this->fetch('category', getAdmStoId());
    }

    //更新线下门店名称和编号
    public function updateerpinfo()
    {
        $getkeyid = I('keyid');
        $getapi_token = tpCache('shop_info.api_token', getAdmStoId());
        $where['Id'] = $getkeyid;
        $res = getApiData('wxd.base.storages.list.get', $getapi_token, null, $where, 1, 10, null);


        if ($res) {
            $res = json_decode($res, true);
            if ($res['code'] == 1 && $res['data']) {

                $postdata['pickup_no'] = $res['data'][0]['StorageNo'];
                $postdata['pickup_name'] = $res['data'][0]['StorageName'];
                $row = M('pick_up')->where(array('store_id' => getAdmStoId(), 'keyid' => $getkeyid))->save($postdata);


                $return_arr = array('status' => 1, 'msg' => '操作成功', 'data' => '',);
            } else {
                $return_arr = array('status' => -1, 'msg' => '获取资料失败', 'data' => '',);
            }

        } else {
            $return_arr = array('status' => -1, 'msg' => '获取资料失败', 'data' => '',);

        }

        $this->ajaxReturn($return_arr);

    }

    //根据名称查询地区ID
    public function getRegionId()
    {
        $getaddressname = urldecode(urldecode(I('addressname')));

        if (empty($getaddressname)) {
            $return_arr = array('code' => -1, 'msg' => '传递参数有误', 'data' => '');
            $this->ajaxReturn($return_arr);
        }
        $getaddressname1 = explode("\\", $getaddressname);
        $getprovince = "";
        $getcity = "";
        $getdistrict = "";
        $getprovince = $getaddressname1[0];
        $getcity = $getaddressname1[1];
        $getdistrict = $getaddressname1[2];


        $res = M('region')->where(array('name' => $getprovince, 'level' => 1))->find();
        if ($getcity) {
            $res1 = M('region')->where(array('name' => $getcity, 'level' => 2))->find();
        }
        if ($getdistrict) {
            $res2 = M('region')->where(array('name' => $getdistrict, 'level' => 3))->find();
        }
        if (empty($res)) {
            $return_arr = array('code' => -1, 'msg' => '找不到相关记录', 'data' => '');
            $this->ajaxReturn($return_arr);
        }
        $idlist = array();
        $idlist["province_id"] = $res["id"];
        $idlist["city_id"] = $res1["id"];
        $idlist["district_id"] = $res2["id"];
        $return_arr = array('code' => 0, 'msg' => 'ok', 'data' => $idlist);
        $this->ajaxReturn($return_arr);
    }

    //单个同步线下GPS
    public function updateGPS()
    {
        $pickup_id = I('pickup_id/d', 0);
        if (empty($pickup_id)) {
            $return_arr = array('code' => -1, 'msg' => '请选择要同步的记录', 'data' => '');
            $this->ajaxReturn($return_arr);
        }
        $getstoid = getAdmStoId();
        $accdb = tpCache("shop_info.ERPId", $getstoid);
        $info = M('pick_up')->where(array('store_id' => getAdmStoId(), 'pickup_id' => $pickup_id))->find();
        if (empty($info)) {
            $return_arr = array('code' => -1, 'msg' => '选择的记录不存在', 'data' => '');
            $this->ajaxReturn($return_arr);
        }
        $updatadata = array();
        $updatadata[0]['GPSX'] = $info['lat'];
        $updatadata[0]['GPSY'] = $info['lon'];
        $updatadata[0]['Id'] = $info['keyid'];

        $updatainfo = getApiData_java_p("/api/erp/msg/storages/gps/update", $accdb, json_encode($updatadata), 1, 1, null, 'PUT', 1);
        if (empty($updatainfo)) {
            $return_arr = array('code' => -1, 'msg' => '同步失败!', 'data' => '');
            $this->ajaxReturn($return_arr);
        }
        $updatainfo = json_decode($updatainfo, true);
        if ($updatainfo['code'] != 0) {
            $return_arr = array('code' => -1, 'msg' => '同步失败[' . $updatainfo['msg'] . ']!', 'data' => '');
            $this->ajaxReturn($return_arr);
        }
        $return_arr = array('code' => 0, 'msg' => '同步成功!', 'data' => '');
        $this->ajaxReturn($return_arr);

    }
    //同步线下GPS
    public function updateALLGPS()
    {

        $getstoid=getAdmStoId();
        $accdb = tpCache("shop_info.ERPId", $getstoid);
        $list = M('pick_up')->where(array('store_id' => getAdmStoId()))->select();
        if (empty($list))
        {
            $return_arr = array('code' => -1, 'msg' => '无记录可同步', 'data' => '');
            $this->ajaxReturn($return_arr);
        }
        $updatadata=array();
        foreach ($list as  $item=>$value) {
            $updatadata[$item]['GPSX'] = $value['lat'];
            $updatadata[$item]['GPSY'] = $value['lon'];
            $updatadata[$item]['Id'] = $value['keyid'];
        }

        $updatainfo = getApiData_java_p("/api/erp/msg/storages/gps/update", $accdb, json_encode($updatadata), 1, 1,null,'PUT',1);
        if (empty($updatainfo))
        {
            $return_arr = array('code' => -1, 'msg' => '同步失败!', 'data' => '');
            $this->ajaxReturn($return_arr);
        }
        $updatainfo=json_decode($updatainfo,true);
        if ($updatainfo['code']!=0)
        {
            $return_arr = array('code' => -1, 'msg' => '同步失败['.$updatainfo['msg'].']!', 'data' => '');
            $this->ajaxReturn($return_arr);
        }
        $return_arr = array('code' => 0, 'msg' => '同步成功!', 'data' => '');
        $this->ajaxReturn($return_arr);

    }
}