System.php 35.7 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 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 当燃
 * Date: 2015-10-09
 */

namespace app\manager\controller;

use app\manager\logic\GoodsLogic;
use think\db;
use Thread;
use think\Page;
use think\Cache;

class System extends Base
{

    /*
     * 配置入口
     */
    public function basic()
    {
        //$config=M('manager_config')->find();

        $config = getManagerConf();
        $this->assign('basiclist', $config);
        $timeconf = get_config(ROOT_PATH . '/application/timer/timeconfig.php', 'handshoptime', 'int');

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

    //保存
    public function handle()
    {
        $data = I('post.');

        //$r = M('manager_config')->where('id',1)->save($data);
        $r = getManagerConf($data);
        if ($r) {
            $this->success('操作成功', U('Manager/System/basic'));
        } else {
            $this->error("操作失败", U('Manager/System/basic'));
        }
    }

    public function otherset()
    {


        return $this->fetch();
    }

    public function otherset_hand()
    {
        $info_role = M('role')->where('role_id', 190)->find();
        if ($info_role) {

            $sql = "update wxd_role set act_list='" . $info_role['act_list'] . "'  WHERE (`role_name` IN ('超级用户','管理员') )";
            Db::execute($sql);

            $this->success('操作成功', U('Manager/System/otherset'));

        }
    }

    public function weappbasic()
    {
        $config = getManagerConf();
        $this->assign('basiclist', $config);

        return $this->fetch();
    }

    public function weappbasic_hand()
    {
        $data = I('post.');
        $r = getManagerConf($data);
        if ($r) {
            $this->success('操作成功', U('Manager/System/weappbasic'));
        } else {
            $this->error("操作失败", U('Manager/System/weappbasic'));
        }
    }

    public function settimeconfig()
    {
        $state = I("status");
        if ($state == 1) {
            update_config(ROOT_PATH . '/application/timer/timeconfig.php', 'handshoptime', 1, 'int');
            //$mytask = new mytask();
            //$mytask->start();
        } else {
            update_config(ROOT_PATH . '/application/timer/timeconfig.php', 'handshoptime', 0, 'int');
        }
        return json(['code' => 1, 'msg' => '修改成功']);
    }

    public function unbinduser()
    {
        $isajax = I('isajax');
        $getstoid = I('stoid');
        $storelist = M('store')->where('is_audit', 1)->field('store_id,ERPId,store_name')->order('ERPId')->select();
        if ($isajax) {
            $stoid = I('store_id');
            $mobile = I('mobile');
            $mobarr = explode(",", $mobile);
            foreach ($mobarr as $k => $v) {
                $mobarr[$k] = trim($v);
            }
            $saved = [
                'mobile' => NULL,
                'erpvipid' => NULL,
                'erpvipno' => NULL,
                'first_leader' => 0,
                'second_leader' => 0,
                'third_leader' => 0,
                'is_distribut' => 0,
                'underling_number' => 0,
                'is_mzvip' => 0,
                'mzvip_remark' => NULL
            ];

            $ud = M("users")->where('store_id', $stoid)->where('mobile', 'in', $mobarr)->field('user_id')->select();
            $rs = M("users")->where('store_id', $stoid)->where('mobile', 'in', $mobarr)->save($saved);
            if ($rs) {
                $idarr = [];
                foreach ($ud as $k1 => $v1) {
                    $idarr[] = $v1['user_id'];
                }
                M("users")->where('store_id', $stoid)->where('first_leader', 'in', $idarr)->save(['first_leader' => 0]);
                M("users")->where('store_id', $stoid)->where('second_leader', 'in', $idarr)->save(['second_leader' => 0]);
                M("users")->where('store_id', $stoid)->where('third_leader', 'in', $idarr)->save(['third_leader' => 0]);

                $this->success('操作成功', U('System/unbinduser', array('stoid' => $stoid)));
            } else
                $this->success('操作失败', U('System/unbinduser', array('stoid' => $stoid)));
        }
        $this->assign('storelist', $storelist);
        $this->assign('getstoid', $getstoid);
        return $this->fetch();
    }


    public function store_newsale()
    {
        $isajax = I('isajax');
        $getstoid = I('stoid');
        $storelist = M('store')->where('is_audit', 1)->field('store_id,ERPId,store_name')->order('ERPId')->select();
        if ($isajax) {
            $stoid = I('store_id');
            $is_newsales_rules = I('is_newsales_rules/d', 0);
            $store_config = M('store_config')->where(array('store_id' => $stoid))->find();
            if (empty($store_config)) {
                $this->error("操作失败", U('Manager/System/store_newsale'));
            }
            $switch_list = $store_config["switch_list"];
            $switch_list = json_decode($switch_list, true);
            foreach ($switch_list as $k => $v) {
                $newArr[$k] = trim($v);
            }
            $newArr["is_newsales_rules"] = $is_newsales_rules;

            $updatadata['switch_list'] = json_encode($newArr);
            M('store_config')->where(array('store_id' => $stoid))->save($updatadata);

            ClearALLCache();
            delFile(TEMP_PATH . "/" . getAdmStoId());
            $this->success('操作成功', U('Manager/System/store_newsale'));


        }
        $this->assign('storelist', $storelist);
        $this->assign('getstoid', $getstoid);
        return $this->fetch();
    }

    //----解绑等级会员----
    public function unbind_dengji()
    {
        $isajax = I('is_ajax');
        if ($isajax) {
            $erpid = I("erpid");
            $moblie = I("mobile");

            $store = M("store")->where('ERPId', $erpid)->field('store_id')->find();
            if (empty($store)) {
                $this->error('未找到门店', U('System/unbind_dengji'));
                exit;
            }

            $user = M("users")->where('mobile', $moblie)->where('store_id', $store['store_id'])->find();
            if (empty($user)) {
                $this->error('未找到会员', U('System/unbind_dengji'));
                exit;
            }
            if (empty($user['card_field'])) {
                $this->error('该会员不是等级会员', U('System/unbind_dengji'));
                exit;
            }

            $sconfig = M("store_config")->where('store_id', $store['store_id'])->field('dj_num')->find();
            if (empty($sconfig['dj_num'])) {
                $this->error('等级会员数量已经为0', U('System/unbind_dengji'));
                exit;
            }

            $data['VIPId'] = urlencode($user['erpvipid']);
            $rr = getApiData_java_p("/api/erp/vip/mem/referee/remove", $erpid, $data, null, null, null, "GET");
            $rr = json_decode($rr, true);
            if ($rr['code'] === 0) {
                M('users')->where('user_id', $user['user_id'])->where('store_id', $store['store_id'])
                    ->save(['card_field' => '', 'card_expiredate' => '']);
                //M("store_config")->where('store_id',$store['store_id'])->setDec('dj_num');
                //默认初始话那里也有要清理掉
                if ($sconfig['fir_yaoqing_no'] == $moblie) {
                    M("store_config")->where('store_id', $store['store_id'])->save(['fir_yaoqing_no' => '']);
                }

                //调用接口
                $this->success('操作成功', U('System/unbind_dengji'));
                exit;
            }

            $this->error('操作失败', U('System/unbind_dengji'));
            exit;

        }
        return $this->fetch();
    }

    public function show_redis()
    {
        return $this->fetch();
    }

    public function re_orderpt()
    {
        $isajax = I('is_ajax');
        if ($isajax) {
            $erpid = I("erpid");
            $ordersn = I("ordersn");

            $store = M("store")->where('ERPId', $erpid)->field('store_id')->find();
            if (empty($store)) {
                $this->error('未找到门店', U('System/re_orderpt'));
                exit;
            }

            $order = M("order")->where('order_sn', $ordersn)->where('store_id', $store['store_id'])->find();
            if (empty($order)) {
                $this->error('未找到订单', U('System/re_orderpt'));
                exit;
            }
            disoucponpt($order);
            $this->success('操作成功', U('System/re_orderpt'));
            exit;


        }
        return $this->fetch();
    }

    //显示商家的redis
    public function ajax_show_redis()
    {

        $erpid = I("erpid");
        $sear_type = I("sear_type");
        $now = time();
        $redis = get_redis_handle();

        //获取门店
        $sto = M("store")->where("ERPId", $erpid)->field("store_id")->find();

        $flash_list = M("flash_sale")->where("store_id", $sto['store_id'])
            ->where("is_end", 0)
            ->where("end_time>" . $now)
            ->select();
        $group_list = M("group_buy")->where("store_id", $sto['store_id'])
            ->where("is_end", 0)
            ->where("end_time>" . $now)
            ->select();
        $pt_list = M("teamlist")->where("store_id", $sto['store_id'])
            ->where("is_end", 0)
            ->where("end_time>" . $now)
            ->select();

        $zl_list = M("marketing_help_form")->where("store_id", $sto['store_id'])
            ->where("isdel", 0)
            ->where("enddate>" . $now)
            ->select();


        //更换换后
        if ($sear_type == 1) {
            $this->set_change_after($flash_list, $group_list, $pt_list,$zl_list,$redis);
        } else {
            $this->set_change_before($flash_list, $group_list, $pt_list, $redis);
        }
        return $this->fetch();
    }

    //映射设置之后的redis数量
    public function set_change_after($flash_list, $group_list, $pt_list,$zl_list, $redis)
    {
        if ($flash_list) {
            foreach ($flash_list as $k => $v) {
                $name = get_redis_name($v['id'], 1, $v['store_id']);
                $len = $redis->lLen($name);
                $flash_list[$k]['redis_len'] = $len;
            }
            $this->assign("flash_list", $flash_list);
        }

        if ($group_list) {
            foreach ($group_list as $kk => $vk) {
                $name = get_redis_name($vk['id'], 2, $vk['store_id']);
                $len = $redis->lLen($name);
                $group_list[$kk]['redis_len'] = $len;
            }
            $this->assign("group_list", $group_list);
        }

        if ($pt_list) {
            foreach ($pt_list as $kl => $vl) {
                $name = get_redis_name($vl['id'], 6, $vl['store_id']);
                $len = $redis->lLen($name);
                $pt_list[$kl]['redis_len'] = $len;

                if ($vl['kttype'] == 2) {
                    //正在进行中的团
                    $teamg = M("teamgroup")->where("team_id", $vl['id'])
                        ->where("state", 2)
                        ->select();
                    if ($teamg) {
                        //把团的组织一下
                        foreach ($teamg as $km => $vm) {
                            $name_t = $name . ":" . $vm['listno'];
                            $len = $redis->lLen($name_t);
                            $teamg[$km]['redis_len'] = $len;
                        }
                        $pt_list[$kl]['teamgroup'] = $teamg;
                    }
                }
            }
            $this->assign("pt_list", $pt_list);
        }

        if($zl_list){
            foreach ($zl_list as $kh => $vh) {
                 $h_list=M("marketing_help_list")->where("help_id",$vh['id'])->select();
                 foreach ($h_list as $ku=>$vu){
                      $name = get_redis_name($vu['id'], 7, $vu['store_id']);
                      $len = $redis->lLen($name);
                      $h_list[$ku]['redis_len']=$len;
                 }
                $zl_list[$kh]['list']=$h_list;
            }
            $this->assign("zl_list", $zl_list);
        }

    }

    //映射设置之钱的redis数量
    public function set_change_before($flash_list, $group_list, $pt_list, $redis)
    {
        if ($flash_list) {
            foreach ($flash_list as $k => $v) {
                $name = 'ms' . $v['id'] . '-' . $v['store_id'];
                $len = $redis->lLen($name);
                $flash_list[$k]['redis_len'] = $len;
            }
            $this->assign("flash_list", $flash_list);
        }

        if ($group_list) {
            foreach ($group_list as $kk => $vk) {
                $name = 'grb' . $vk['id'] . '-' . $vk['store_id'];
                $len = $redis->lLen($name);
                $group_list[$kk]['redis_len'] = $len;
            }
            $this->assign("group_list", $group_list);
        }

        if ($pt_list) {
            foreach ($pt_list as $kl => $vl) {

                $name = 'pind' . $vl['id'] . '-' . $vl['store_id'];
                $len = $redis->lLen($name);
                $pt_list[$kl]['redis_len'] = $len;

                if ($vl['kttype'] == 2) {
                    //正在进行中的团
                    $teamg = M("teamgroup")->where("team_id", $vl['id'])
                        ->where("state", 2)
                        ->select();
                    if ($teamg) {
                        //把团的组织一下
                        foreach ($teamg as $km => $vm) {
                            $name2 = 'pind' . $vl['id'] . '-' . $vm['listno'] . $vl['store_id'];
                            $len = $redis->lLen($name2);
                            $teamg[$km]['redis_len'] = $len;
                        }
                        $pt_list[$kl]['teamgroup'] = $teamg;
                    }
                }
            }
            $this->assign("pt_list", $pt_list);
        }
    }

    //--处理api,列表--
    public function re_deal_api()
    {

        $pagenum = I("pagenum", 10);
        $where = "1=1";

        $erpid = I("erpid");
        $keywords = I("keywords");

        if ($erpid) $where .= " and erpid='" . $erpid . "'";
        if ($keywords) $where .= " and api like %'" . $erpid . "'%'";


        $store = M("store")->where('is_audit', 1)->select();
        $this->assign('storelist', $store);

        $count = M("api_err")->where($where)->count();
        $Page = $pager = new Page($count, $pagenum);// 实例化分页类 传入总记录数和每页显示的记录数
        $rs = M("api_err")->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $show = $Page->show();// 分页显示输出
        $this->assign('list', $rs);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出
        $this->assign('pager', $pager);
        return $this->fetch();
    }

    //--处理api--
    public function deal_api()
    {
        $id = I("id");
        $data = M("api_err")->where("id", $id)->find();
        if (empty($data)) return json(['code' => -1, 'msg' => '未找到数据']);
        $p_data = json_decode($data['data']);
        $rs = getApiData_java_p($data['api'], $data['erpid'], $p_data, 1, 10, null, "POST");
        if (empty($rs)) return json(['code' => -1, 'msg' => '处理失败']);
        $rs_data = json_decode($rs, true);
        if ($rs_data['code'] != 0) return json(['code' => -1, 'msg' => $rs_data[msg]]);
        M("api_err")->where("id", $id)->save(['is_deal' => 1]);
        return json(['code' => 0, 'msg' => '处理成功']);
    }

    //--重新注册等级--
    public function re_chuce_dj()
    {

        $is_ajax = I("is_ajax");
        if ($is_ajax) {
            $data = I("data");
            $date = I("date");
            $accdb = I("accdb");

            $data = json_decode($data, true);
            if (!is_array($data)) $this->success("补卡失败,补卡数据错误", U('System/re_chuce_dj'));
            $store = M("store")->where("ERPId", $accdb)->find();
            if (!$store) $this->success("补卡失败,未找到商家", U('System/re_chuce_dj'));

            $data['RegisterDate'] = $date;
            $api = "/api/erp/vip/mem/card/register";
            $mrs = getApiData_java_p($api, $accdb, $data, 1, 10, null, "POST");
            $mrs = json_decode($mrs, true);

            if ($mrs['code'] == 0) {
                $this->success("补卡成功", U('System/re_chuce_dj'));
                exit();
            }

            $this->success("补卡失败", U('System/re_chuce_dj'));

        }
        return $this->fetch();
    }


    //--补发代金券--
    public function re_sendcoupon()
    {

        $is_ajax = I("is_ajax");
        if ($is_ajax) {


            $stoid = I('stoid/d', 0);
            $couponid = I('couponid/d', 0);

            if (empty($stoid) || empty($couponid)) {
                $this->error("补卡失败,参数有误");
            }
            $storeinfo = M('store')->where(array('store_id' => $stoid))->find();
            if (empty($storeinfo)) {
                $this->error("补卡失败,没有该商家");
            }
            $sto_erpid = $storeinfo['ERPId'];
            $coupon = M("coupon")->where(array("id" => $couponid, 'send_start_time' => array('elt', time()), 'send_end_time' => array('egt', time())))->find();
            if (empty($coupon)) {
                $this->error("补卡失败,没有代金券或代金券发放时间未开始或已结束");
            }
            $userlist = Db::query("select a.user_id,a.mobile,a.weapp_openid,b.id,b.cid,FROM_UNIXTIME(reg_time),a.erpvipid,a.erpvipno from wxd_users a left join wxd_coupon_list b on a.user_id=b.uid and b.type=2 and b.remark='注册发放' where a.store_id=" . $stoid . " and a.reg_time>" . $coupon['add_time'] . " and a.reg_time>1570694939  and a.mobile<>'' and a.erpvipid<>'' and b.id is null order by reg_time desc");
            if (empty($userlist)) {
                $this->error("补卡失败,没有记录可补发");
            }
            foreach ($userlist as $k => $value) {
                $this->reg_send_quan($coupon, $value, $sto_erpid, $stoid);
            }
            $this->success('补发成功', U('System/re_sendcoupon'));


        }
        return $this->fetch();
    }

    public function reg_send_quan($coupon, $user_info, $sto_erpid, $stoid, $t = 0)
    {
        $first_info = $user_info;
        if ($coupon["send_end_time"] > time()) {
            $CashRepNo = "1005" . date("ymdHis") . get_total_millisecond();
            if ($sto_erpid) {
                $data = array(
                    'CashRepNo' => $CashRepNo,//券号
                    'VIPId' => $first_info["erpvipid"],//会员id
                    'Sum' => $coupon["money"],//金额
                    'SendMan' => '注册发放',//发放类型
                    'Operator' => '微信商城',//来源
                    'BuySum' => $coupon["condition"],//满多少才能使用
                    'Remark' => '注册送微券【消费满' . $coupon["condition"] . '元可用】',
                );
                if ($t == 1) {
                    $data['Remark'] = '邀请送微券【消费满' . $coupon["condition"] . '元可用】';
                    $data['SendMan'] = '邀请发放';
                }
                if ($coupon['useobjecttype']) {
                    $data['UseObjectType'] = $coupon['useobjecttype'];
                    $data['UseObjectID'] = $coupon['useobjectid'];
                    $data['UseObjectNo'] = $coupon['useobjectno'];
                    $data['UseObjectName'] = $coupon['useobjectname'];
                }

                //如果有设置有效期
                if ($coupon['endtype'] == 1) {
                    $data['BeginDate'] = date('Y-m-d');//使用开始日期
                    if ($coupon['days'] > 0) {
                        $ed = strtotime("+" . $coupon['days'] . " day");
                        $data['ValidDate'] = date('Y-m-d', $ed);//截止期限
                    }
                } else {
                    if ($coupon["use_start_time"] != "") {
                        $data['BeginDate'] = date('Y-m-d', $coupon["use_start_time"]);//使用开始日期
                    }
                    if ($coupon["use_end_time"] != "") {
                        $data['ValidDate'] = date('Y-m-d', $coupon["use_end_time"]);//截止期限
                    }
                }

                $erp = tpCache("shop_info.ERPId", $stoid);
                $add_rs = getApiData_java_p("/api/erp/vip/cash/insert", $erp, $data, null, null, null, "POST");
                if ($add_rs) {
                    $add_rs = json_decode($add_rs, true);
                    if ($add_rs['code'] == 0) {
                        //插入线上优惠券领取表
                        $coupon_list['cid'] = $coupon['id'];
                        $coupon_list['type'] = 2;
                        $coupon_list['uid'] = $first_info['user_id'];
                        $coupon_list['send_time'] = time();
                        $coupon_list['code'] = $CashRepNo;
                        $coupon_list['store_id'] = getMobileStoId();
                        $coupon_list['code'] = time();

                        if ($coupon['endtype'] == 1) {
                            $coupon_list['begintime'] = time();
                            if ($coupon['days'] > 0)
                                $coupon_list['validtime'] = strtotime("+" . $coupon['days'] . " day");
                            else
                                $coupon_list['validtime'] = 0;
                        } else {
                            $coupon_list['begintime'] = $coupon["use_start_time"];
                            $coupon_list['validtime'] = $coupon["use_end_time"];
                        }

                        $coupon_list['sum'] = $coupon["money"];
                        $coupon_list['buysum'] = $coupon["condition"];
                        $coupon_list['remark'] = "注册发放";
                        if ($t == 1) $coupon_list['remark'] = "邀请发放";
                        $add_couponinfo = add_coupon($coupon_list, $first_info, $stoid);

                    }
                }
            }


        }
    }


    //清除缓存
    public function clearredis()
    {

        $is_ajax = I("is_ajax");
        if ($is_ajax) {
            $erpid = I('erpid');
            if ($erpid) {
                $where1['ERPId'] = $erpid;
            }
            $store = M('store')->where($where1)->select();

            if (empty($store)) {
                $this->error("没有该商家");
            }
            foreach ($store as $k => $v) {
                $name = 'weshop:PROD:erp:newToken:' . strtolower($store['ERPId']);
                Cache::rm($name);


                $accdb = strtolower($v['ERPId']);
                $wxdtoken_access_data = cache_new("common", "wxderp", "wxdtoken_access_data:" . $accdb);
                $wxdtoken_access_time = cache_new("common", "wxderp", "wxdtoken_access_time:" . $accdb);


                cache_new("common", "wxderp", "wxdtoken_access_data:" . $accdb, '');
                cache_new("common", "wxderp", "wxdtoken_access_time:" . $accdb, 11);
            }

            $this->success('清除成功', U('System/clearredis'));


        }
        return $this->fetch();
    }


    //公众号迁移
    public function openidmove()
    {

        $is_ajax = I("is_ajax");
        if ($is_ajax) {


            $stoid = I('stoid/d', 0);
            $from_appid = I('from_appid');
            $old_kt = I('old_kt');

            if (empty($stoid) || empty($from_appid) || empty($old_kt)) {
                $this->error("提交失败,参数有误");
            }
            $page = 1;
            $pagenum = 200;
            switch ($is_ajax) {
                case "1":
                    $count = M('users')->where(array('store_id' => $stoid, 'openid' => array('like', '' . $old_kt . '%')))->count();
                    if (empty($count)) {
                        $this->error("提交失败,找不到相关记录");
                    }
                    $Page = new Page($count, $pagenum);
                    for ($i = 0; $i < $Page->totalPages; $i++) {
                        $userinfo = M('users')->where(array('store_id' => $stoid, 'openid' => array('like', '' . $old_kt . '%')))
                            ->field('openid')
                            ->limit($Page->firstRow . ',' . $Page->listRows)->select();
                        $newopenlist = array();
                        if ($userinfo) {
                            foreach ($userinfo as $k => $v) {
                                $newopenlist[] = $v['openid'];
                            }
                        }


                        $this->weixin_config = M('wx_user')->where('store_id=' . $stoid)->find(); //获取微信配置
                        $access_token2 = m_get_access_token($this->weixin_config);
                        $url = "http://api.weixin.qq.com/cgi-bin/changeopenid?access_token=" . $access_token2;
                        $postdata['from_appid'] = $from_appid;
                        $postdata['openid_list'] = $newopenlist;
                        $list = httpRequest($url, 'POST', json_encode($postdata));
                        if ($list) {
                            $list = json_decode($list, true);
                            if ($list['result_list']) {
                                foreach ($list['result_list'] as $k => $v) {
                                    if ($v['err_msg'] == 'ok') {
                                        $updatadata['openid'] = $v['new_openid'];
                                        M('users')->where(array('store_id' => $stoid, 'openid' => $v['ori_openid']))->update($updatadata);
                                    }
                                }
                            }

                        }

                    }

                    break;
                case "2":
                    $count = M('wx_openlist')->where(array('store_id' => $stoid, 'wxopenid' => array('like', '' . $old_kt . '%')))->count();
                    if (empty($count)) {
                        $this->error("提交失败,找不到相关记录");
                    }
                    $Page = new Page($count, $pagenum);
                    for ($i = 0; $i < $Page->totalPages; $i++) {
                        $userinfo = M('wx_openlist')->where(array('store_id' => $stoid, 'wxopenid' => array('like', '' . $old_kt . '%')))
                            ->field('wxopenid')
                            ->limit($Page->firstRow . ',' . $Page->listRows)->select();
                        $newopenlist = array();
                        if ($userinfo) {
                            foreach ($userinfo as $k => $v) {
                                $newopenlist[] = $v['wxopenid'];
                            }
                        }
                        $weixin_config = M('wx_user')->where('store_id=' . $stoid)->find(); //获取微信配置
                        $access_token2 = m_get_access_token($weixin_config);
                        $url = "http://api.weixin.qq.com/cgi-bin/changeopenid?access_token=" . $access_token2;
                        $postdata['from_appid'] = $from_appid;
                        $postdata['openid_list'] = $newopenlist;
                        $list = httpRequest($url, 'POST', json_encode($postdata));

                        if ($list) {
                            $list = json_decode($list, true);
                            if ($list['result_list']) {
                                foreach ($list['result_list'] as $k => $v) {
                                    if ($v['err_msg'] == 'ok') {
                                        $updatadata['wxopenid'] = $v['new_openid'];
                                        M('wx_openlist')->where(array('store_id' => $stoid, 'wxopenid' => $v['ori_openid']))->update($updatadata);
                                    } else {
                                        M('wx_openlist')->where(array('store_id' => $stoid, 'wxopenid' => $v['ori_openid']))->delete();

                                    }
                                }
                            }

                        }


                    }

                    break;
            }


            $this->success('解绑成功' . $is_ajax, U('System/openidmove'));


        }
        return $this->fetch();
    }

    //替换生成二维码
    public function resetqrcode()
    {

        $is_ajax = I("is_ajax");
        if ($is_ajax) {
            $stoid = I('stoid/d', 0);

            if (empty($stoid)) {
                $this->error("提交失败,参数有误");
            }
            $page = 1;
            $pagenum = 100;
            //,'date'=>array('lt',strtotime("-1 hour"))
            $date=I('date');
            if ($date)
            {
                $where['date']=array('lt',$date);
            }
            $count = M('wx_ewmlist')->where(array('store_id' => $stoid))->where($where)->count();


            if (empty($count)) {
                $this->error("提交失败,找不到相关记录");
            }
            $Page = new Page($count, $pagenum);

            $weixin_config = M('wx_user')->where(array('store_id' => $stoid))->find(); //获取微信配置
            $access_token2 = m_get_access_token($weixin_config);

            for ($i = 0; $i < $Page->totalPages; $i++) {
                $userinfo = M('wx_ewmlist')->where(array('store_id' => $stoid))
                    ->where($where)
                    ->limit($Page->firstRow . ',' . $Page->listRows)->select();
                $newopenlist = array();
                if ($userinfo) {
                    foreach ($userinfo as $k => $v) {

                        if (empty($v['scene_id'])) {
                            $scene_id = '';
                        } else {
                            $scene_id = $v['scene_id'];
                        }

                        //获取数据的地址(微信提供)
                        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $access_token2;
                        //发送给微信服务器的数据
                        if ($scene_id) {
                            $jsonstr = '{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "' . $scene_id . '"}}}';
                        } else {
                            $jsonstr = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": "' . $scene_id . '"}}}';
                        }

                        $return = httpRequest($url, 'POST', $jsonstr);


                        $return = json_decode($return, 1);

                        if ($return['ticket']) {
                            $updatedata['ticket'] = $return['ticket'];
                            $updatedata['date'] = time();
                            $updatedata['loadurl'] = $return['url'];
                            $updateres = M('wx_ewmlist')->where(array('id' => $v['id']))->save($updatedata);
                        }
                    }
                }


            }
            $this->success('生成成功', U('System/resetqrcode'));
        }
        return $this->fetch();

    }

    //补发礼包
    public function buylibao()
    {
        $is_ajax = I("is_ajax");
        if ($is_ajax) {
            $userid = I('userid/d', 0);
            $storeid = I('stoid/d', 0);
            $lbid = I('lbid');
            $lbnumber = I('lbnumber');


            if (empty($userid) || empty($storeid) || empty($lbid) || empty($lbnumber)) {
                $this->error("提交失败,参数有误");
            }
            $libao_list = M('libao_formvip')->where(array('store_id' => $storeid, 'number' => $lbnumber))->find();
            if ($libao_list) {
                $this->error("提交失败,该订单已存在");
                exit();
            }
            $libao = M('libao_form')->where('store_id=' . $storeid . ' and startime<=' . time() . ' and endtime>=' . time() . ' and id=' . $lbid)->select();
            if (empty($libao)) {
                $this->error("提交失败,找不到礼包 ");
            }


            $data['user_id'] = $userid;
            $data['store_id'] = $storeid;
            $data['lbid'] = $lbid;
            $data['expdate'] = strtotime('+' . $libao[0]['expday'] . ' day', time());
            $data['number'] = $lbnumber;
            $data['paytype'] = 1;
            $data['fbillstate'] = 1;
            $data['add_time'] = time();
            $uselbid = M('libao_formvip')->add($data);
            $list = M('libao_list')->where(array('store_id' => $storeid, 'lbid' => $lbid))->select();
            if ($list) {
                for ($i = 0; $i < count($list); $i++) {
                    if ($libao[0]['lbtype'] == 2)//如果是礼券的话,一个数量生成一条记录
                    {
                        for ($k = 0; $k < $list[$i]['goods_num']; $k++) {
                            unset($data);
                            $data['user_id'] = $userid;
                            $data['store_id'] = $storeid;
                            $data['lbvipid'] = $uselbid;
                            $data['goods_num'] = 1;
                            $data['goods_name'] = $list[$i]['cpid'];
                            $data['goods_sn'] = $list[$i]['goods_sn'];
                            $data['lb_state'] = $list[$i]['state'];
                            M('libao_listvip')->add($data);
                        }
                    } else {
                        unset($data);
                        $data['user_id'] = $userid;
                        $data['store_id'] = $storeid;
                        $data['lbvipid'] = $uselbid;
                        $data['goods_num'] = $list[$i]['goods_num'];
                        $data['goods_name'] = $libao[0]['lbtype'] == 1 ? $list[$i]['goods_name'] : $list[$i]['cpid'];
                        $data['goods_sn'] = $list[$i]['goods_sn'];
                        $data['lb_state'] = $list[$i]['state'];
                        M('libao_listvip')->add($data);
                    }
                }
            }

            DB::execute("update wxd_libao_form set salenum=salenum+1 where store_id=" . $storeid . " and id=" . $lbid . "");
            $this->success('生成成功', U('System/buylibao'));

        }
        return $this->fetch();
    }

    //更新订单
    public function updateorder()
    {
        $is_ajax = I("is_ajax");
        if ($is_ajax) {
            $order_id = I('order_id/d', 0);
            $storeid = I('stoid/d', 0);


            if (empty($order_id) || empty($storeid)) {
                $this->error("提交失败,参数有误");
            }
            $libao_list = M('order')->where(array('store_id' => $storeid, 'order_id' => $order_id,'order_status'=>3,'pay_status'=>0))->find();
            if (empty($libao_list)) {
                $this->error("提交失败,该订单不存在");
                exit();
            }


            $updatedata['order_status'] = 1;
            $updatedata['pay_status'] = 1;

            $list = M('order')->where(array('store_id' => $storeid, 'order_id' => $order_id))->save($updatedata);
            if ($list) {
                M('order_action')->where(array('store_id' => $storeid, 'order_id' => $order_id,'action_note'=>'购扣积分失败取消订单'))->delete();
            }
            $this->success('生成成功', U('System/updateorder'));

        }
        return $this->fetch();
    }
}