Reservation.php 11.6 KB
<?php
/**
 * Created by PhpStorm.
 * User: wxd1061
 * Date: 2017-05-02
 * Time: 11:31
 */
namespace app\mobile\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\response\Json;
use think\Session;
use think\Cookie;

class Reservation extends MobileBase
{
    public $user_id = 0;
    public $user = array();

    /**
     * 析构流函数
     */
    public function __construct()
    {

        $address = urlencode(curHostURL() . $_SERVER['REQUEST_URI']);
        parent::__construct();
        if (session('?user')) {
            $user = session('user');
            $reuser = M('users')->where("user_id", $user['user_id'])->find();
            session('user', $reuser);  //覆盖session 中的 user
            $this->user = $reuser;
            $this->user_id = $reuser['user_id'];
            $this->assign('user', $reuser); //存储用户信息


        } else {
            $uid = Cookie::get('user_id');
            if ($uid) {
                $user = M('users')->where("user_id", $uid)->where("store_id", getMobileStoId())->find();
                session('user', $user);  //覆盖session 中的 user
                $this->user = $user;
                $this->user_id = $user['user_id'];
                $this->assign('user', $user); //存储用户信息
            }
        }
        if (empty($this->user_id)) {
            $this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId(), "oldurl" => $address)));
            exit;
        }

        if (($this->pm_erpid && (!$user || !$user['erpvipid'])) || (empty($this->pm_erpid) && !$user || !$user['mobile'])) {
            $this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId(), "oldurl" => $address)));
            exit;
        }
    }
    //        wxd.pos.vipreservation.list.get 查询预约
    //        wxd.pos.vipreservation.add 添加预约
    //        wxd.pos.vipreservation.edit 編輯预约
    //        wxd.pos.vipreproject.list.get 预约項目
    //        wxd.pos.vvipreservation.list 预约详情视图
    /** 预约页面**/
    public function index()
    {

        $getstoid=getMobileStoId();
        $getsotinfo=tpCache('shop_info',$getstoid);
        $user_info['api_token'] =$getsotinfo['api_token'];
        $user_info['store_logo'] = $getsotinfo['store_logo'];
        $user_info['store_name'] = $getsotinfo['store_name'];
        $user_info['ERPId'] = $getsotinfo['ERPId'];
        $user_info['erpvipid']=  $this->user['erpvipid'];
        if ($user_info) {
            $mapuser['VIPID'] = $user_info['erpvipid'];
            $where = array(
                'VIPID' => $mapuser['VIPID'],
                'State' => '0',
            );

            $vip_ord = getApiData('wxd.pos.vvipreservation.list', $user_info['api_token'], null, $where);//查询是否有预约项目



            $vip_ord = json_decode($vip_ord, 'true');

            if ($vip_ord && $vip_ord['code'] == 1 && $vip_ord['data']) {
                //已有预约单,调取这条订单,显示详情
                $state = 'see';
                $see_data = $vip_ord['data'][0];
            } else {
                //没有预约单,跳转添加
                $state = 'add';
                $where = array(
                    'IsStop' => 0,//项目:0启用,1未启用
                );
                //获取当前时间以及往后一个月的时间
                $thistime = array(
                    'open' => date("Y-m-d"),
                    'end' => date('Y-m-d', strtotime("+1 month"))
                );

                $accdb = $user_info['ERPId'] ;
                $project=getApiData_java("api/erp/vip/reservation/project/list",$accdb,$where,1,1000);  //预约项目

             //   $project = getApiData('wxd.pos.vipreproject.list.get', $user_info['api_token'], null, $where);
                $project = json_decode($project,true);
                if ($project && $project['code'] == 0 && $project['data']['pageData']) {
                    $projectdata = $project['data']['pageData'];

                }
            }
            $this->assign('state', $state);//传递状态add||see
            $this->assign('see_data', $see_data);//查看信息
            $this->assign('project', $projectdata);//预约项目
           // $this->assign('stores', $storesdata);//门店
            $this->assign('thistime', $thistime);//渲染时间
        }
        return $this->fetch();
    }

    //获取门店
    public  function storelist()
    {
        $data = I('post.');
        if (empty($data['WareId']))
        {
            return json(["code" => -1, "msg" => "请选择项目"]);
        }
        $where = array(
            'WareId' => $data['WareId'],
        );
        $accdb =  tpCache('shop_info.ERPId', getMobileStoId());
        $selectnum=getApiData_java("api/erp/vip/reservation/storage/list",$accdb,$where,1,1000);  //查询门店

        $selectnum = json_decode($selectnum, true);
        if ($selectnum && $selectnum['code'] == 0 &&  $selectnum['data']) {
            return json(["code" => 1, "msg" => "ok", "data" => $selectnum['data']]);
        } else {
            return json(["code" => -1, "msg" => "接口出错"]);
        }
    }
    /** 预约提交**/
    public function makeinfo()
    {
        $getstoid=getMobileStoId();
        $getsotinfo=tpCache('shop_info',$getstoid);
        $user_info['api_token'] =$getsotinfo['api_token'];
        $user_info['store_logo'] = $getsotinfo['store_logo'];
        $user_info['store_name'] = $getsotinfo['store_name'];
        $user_info['ERPId'] = $getsotinfo['ERPId'];
        $user_info['erpvipid']=  $this->user['erpvipid'];
        $user_info['vipname']=  $this->user['vipname'];
        $user_info['store_id']=  $this->user['store_id'];
        if ($user_info) {
            $yyh = 'yyfw' . date("YmdHis", time()) . get_total_millisecond();
            $mapuser['VIPNo'] = $user_info['erpvipno'];//token验证
            $data = I('post.');
            $data1 = I('post.');
            $shoppers = $data['shoppers'];//导购ID
//            $shoppers = $user_info['erpvipid'].",".$user_info['erpvipid'];
            $data['VIPID'] = $user_info['erpvipid'];
            $data['State'] = 0;
            $data['number'] = $yyh;
            unset($data['StorageName']);
            unset($data['StorageAddress']);
            unset($data['WareName']);
            unset($data['WareName']);
            unset($data['shoppers']);
            //获取预约人数
            $selmap = array(
                'WareId' => $data['WareId'],
                'StorageId' => $data['StorageId'],
                'RsDate' => $data['RSDate'],
            );
            $selectnum = getApiData('wxd.vip.reservation.peopleqty', $user_info['api_token'], null, $selmap);//查询剩余预约数
            $selectnum = json_decode($selectnum, 'true');
            if ($selectnum){
                if ($selectnum['data']['0']['PeopleQty'] < $data['PeopleQty']) {
                    return json(["code" => -2, "msg" => "预约人数超过可预约人数", "data" => $selectnum['data']['0']['PeopleQty']]);
                } else {
                    //预约
                    $add_data = getApiData('wxd.pos.vipreservation.add', $user_info['api_token'], array($data));//预约项目
                    mlog("返回1:" . $add_data, "makeinfo/" . $user_info['store_id']);
                    $add_data = json_decode($add_data, true);
                    if ($add_data && $add_data['code'] == 1) {
                        //推送消息
                        $url = curHostURL() . "/home/api/poswxcode?typeid=1011&Acc=" . $user_info['ERPId'] . "&ErpVipid=" . $user_info['erpvipid'] . "&title=恭喜您成功预约,您的预约号是" . $yyh . "&key1=" . $user_info['vipname'] . "&key2=" . $data1['StorageName'] . "&key3=" . $data1['StorageAddress'] . "&key4=" . $data1['RSDate'] . "&key5=" . $data1['WareName'] . "&remark=" . $data1['Remrak'] . "&backurl=" . curHostURL() . "/mobile/reservation/index/stoid/" . $user_info['store_id'];
                        $resp = httpRequest($url, "GET");
                        if ($shoppers) {
                            $shoppers = explode(",", $shoppers);
                            foreach ($shoppers as $key => $vo) {
                                $url1 = curHostURL() . "/home/api/poswxcode?typeid=1011&Acc=" . $user_info['ERPId'] . "&ErpVipid=" . $shoppers[$key] . "&title=恭喜您成功预约,您的预约号是" . $yyh . "&key1=" . $user_info['vipname'] . "&key2=" . $data1['StorageName'] . "&key3=" . $data1['StorageAddress'] . "&key4=" . $data1['RSDate'] . "&key5=" . $data1['WareName'] . "&remark=" . $data1['Remrak'] . "&backurl=" . curHostURL() . "/mobile/reservation/index/stoid/" . $user_info['store_id'];
                                $resp1 = httpRequest($url1, "GET");
                            }
                        }
                        return json(["code" => 1, "msg" => "预约成功"]);
                    } else {
                        return json(["code" => -1, "msg" => "服务器繁忙,请稍后再试"]);
                    }
                }
            }else{
                return json(["code" => -1, "msg" => "服务器繁忙,请稍后再试"]);
            }

            return $this->fetch();
        }
    }

    /**取消預約**/
    public
    function cancel()
    {
        $ID = I('ID');
        $getstoid=getMobileStoId();
        $getsotinfo=tpCache('shop_info',$getstoid);
        $user_info['api_token'] =$getsotinfo['api_token'];
        $user_info['store_logo'] = $getsotinfo['store_logo'];
        $user_info['store_name'] = $getsotinfo['store_name'];
        $user_info['ERPId'] = $getsotinfo['ERPId'];
        $user_info['erpvipid']=  $this->user['erpvipid'];
        if ($user_info) {
//            $mapuser['VIPNo'] = $user_info['erpvipno'];//token验证
            $edit = array(
                'Id' => $ID,
                'State' => '-1'
            );
            mlog("取消预约数据1:" . json_encode($edit), "reservation/" . getMobileStoId());
            $edit_state = getApiData('wxd.pos.vipreservation.edit', $user_info['api_token'], array($edit));//预约项目
            $edit_state = json_decode($edit_state, 'true');
            mlog("取消预约接口返回2:" . json_encode($edit_state), "reservation/" . getMobileStoId());
            if ($edit_state && $edit_state['code'] == 1) {
                return json(["code" => 1, "msg" => "取消预约"]);
            } else {
                return json(["code" => -1, "msg" => "服务器繁忙,请稍后再试"]);
            }
        }
        return $this->fetch();
    }

    /**判断预约人数**/
    public function resernum()
    {
        $getstoid=getMobileStoId();
        $getsotinfo=tpCache('shop_info',$getstoid);
        $user_info['api_token'] =$getsotinfo['api_token'];
        $user_info['store_logo'] = $getsotinfo['store_logo'];
        $user_info['store_name'] = $getsotinfo['store_name'];
        $user_info['ERPId'] = $getsotinfo['ERPId'];
        $user_info['erpvipid']=  $this->user['erpvipid'];
        if ($user_info) {
            $data = I('post.');
            $where = array(
                'WareId' => $data['WareId'],
                'StorageId' => $data['StorageId'],
                'RsDate' => $data['RSDate'],
            );
            $selectnum = getApiData('wxd.vip.reservation.peopleqty', $user_info['api_token'], null, $where);//查询剩余预约数
            $selectnum = json_decode($selectnum, true);
            if ($selectnum && $selectnum['code'] == 1) {
                return json(["code" => 1, "msg" => "返回预约名额", "peopledata" => $selectnum['data']['0']]);
            } else {
                return json(["code" => -1, "msg" => "接口出错"]);
            }
        }
        return $this->fetch();
    }
}