|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- <?php
- /**
- * Created by PhpStorm.
- * User: tannerwang
- * Date: 2019/4/19
- * Time: 14:16
- */
-
- namespace backend\modules\api\models;
-
- use backend\modules\api\util\Util;
- use common\util\CurlInterface;
- use common\util\ZybBusUtil;
- use yii\base\Model;
-
- class ZybBus extends Model
- {
- public static $SUPPLIER_ID = 1835;
- public $header;
- public $identityInfo;
- public $order;
-
-
- public function rules()
- {
- return [
- [['order'], 'required'],
- [['order'], 'cancelOrderValidate', 'on' => ['cancel-order']],
- [['order'], 'queryOrderValidate', 'on' => ['query-order']],
- ];
- }
-
- public function scenarios()
- {
- return [
- 'cancel-order' => ['order'],
- 'query-order' => ['order'],
- ];
- }
-
- // 退票处理
- public function refundTicket()
- {
- $order = $this->order;
- $orderRetreat = $order['orderRetreat'];
-
- $orderMain = new OrderMain();
-
- $checkOutsideOrderNo = $orderMain->checkOutsideOrderNo($orderRetreat['zybOrderCode'], self::$SUPPLIER_ID);
- if (empty($checkOutsideOrderNo['0']['order_id'])) {
- return $this->formartResponse('fail', '订单不存在', null);
- // return ['describe' => '订单不存在', 'status' => 'fail'];
- }
-
- // 订单已退订
- if ($checkOutsideOrderNo[0]['order_status'] == '148') {
- // $orderDetail = $this->orderDetail(['corpOrderId' => $orderRetreat['zybOrderCode']]);
- return $this->formartResponse('success', '订单已取消', null);
- }
- $data = [
- 'order_id' => $checkOutsideOrderNo['0']['order_id'],
- "user_id" => 813
- ];
- $curl = new CurlInterface($data, 4);
- $result = $curl->execute('http://' . CS1_DOMAIN . '/api/submit-order/cancel-bus-order', 'POST');
- if (!isset($result['code']) || $result['code'] != '0') {
- $msg = empty($result['info']) ? '取消订单失败!' : $result['info'];
- return $this->formartResponse('fail', $msg, null);
- }
- // $orderDetail = $this->orderDetail(['corpOrderId' => $params['corpOrderId']]);
- return $this->formartResponse('success', '订单取消成功', null);
- }
-
- // 订单退订参数校验
- public function cancelOrderValidate($attribute)
- {
- if (!empty($this->$attribute)) {
- $order = $this->$attribute;
- $orderRetreat = $order['orderRetreat'];
- if (empty($orderRetreat['retreatNum'])) {
- $this->addError($attribute, "退票数量不能为空");
- }
- if (empty($orderRetreat['zybRetreatCode'])) {
- $this->addError($attribute, "智游宝退单号不能为空");
- }
- if (empty($orderRetreat['zybOrderCode'])) {
- $this->addError($attribute, "智游宝订单号不能为空");
- }
- } else {
- $this->addError($attribute, "信息不能为空");
- }
-
- }
-
- // 订单查询参数校验
- public function queryOrderValidate($attribute)
- {
- if (!empty($this->$attribute)) {
- $order = $this->$attribute;
- $orderRetreat = $order['orderInfo'];
- if (empty($orderRetreat['zybOrderCode'])) {
- $this->addError($attribute, "订单号不能为空");
- }
- } else {
- $this->addError($attribute, "查询缺少必要参数");
- }
-
- }
-
- // 智游宝查询订单信息
- public function queryOrderDetail()
- {
- $order = $this->order;
- $orderCode = $order['orderInfo']['zybOrderCode'];
- $orderMain = new OrderMain();
- $checkOutsideOrderNo = $orderMain->checkOutsideOrderNo($orderCode, self::$SUPPLIER_ID);
-
- $order_id = $checkOutsideOrderNo['0']['order_id'];
- if (empty($order_id)) {
- return $this->formartResponse('fail', '订单不存在', null);
- }
-
- $orderInfo['orderInfo']['extOrderCode'] = $order_id;
- return $this->formartResponse('success', '成功', $orderInfo);
- }
-
- // 格式化返回值
- private function formartResponse($status, $describe, $data)
- {
- return ['status' => $status, 'describe' => $describe, 'order' => $data];
- }
-
- /**
- * Des:下单
- * Name: order
- * @param $params
- * @return array
- * @author 倪宗锋
- */
- public function makeOrder($params)
- {
- $checkParams = $this->checkOrderParams($params);//参数 校验并返回产品详情
- if ($checkParams['flag'] == false) {
- return $checkParams;
- }
-
- $prodInfo = $checkParams['data'];
- //错误返回值
- $errorTicketArray = [];
- if($params['orderDetail']['credentials'] != null) {
- foreach ($params['orderDetail']['credentials'] as $val) {
- $errorTicket = [
- "certificateNo" => $val['idCard'],
- "ticketStatus" => 3,
- "noTicketReason" => "无票"
- ];
- $errorTicketArray[] = $errorTicket;
- }
- }
- $errorReturn = [
- "order_id" => "",
- "corpOrderId" => $params['orderInfo']['zybOrderCode'],
- 'smsTemplate' => '',
- "tickets" => $errorTicketArray
- ];
- #校验订单是否已经存在
- $orderDetail = $this->orderDetail(['corpOrderId' => $params['orderInfo']['zybOrderCode'], 'send_message' => ""]);//获取定下详情,如果存在则直接返回
- if ($orderDetail['flag'] == true) {
- return Util::returnArrEr('error', $errorReturn);
- }
- #不存在则去下单
- #票ID
- $ticket_id = $params['orderDetail']['goodsCode'];
- #获取lineId
- $operaTickets = new OperaTickets();
- $line_id = $operaTickets->getLineIdByTickedId($ticket_id);
- #获取班次ID
- $runMain = new RunMain();
- $startDate = date('Y-m-d', strtotime($params['orderDetail']['startDate']));
- if ($ticket_id == "464905") {
- $startTime = "07:20";
- } else {
- $startTime = "17:00";
- }
- $run_id = $runMain->getRunIdByLine($line_id, $startDate, $startTime);
- if (empty($run_id)) {
- return Util::returnArrEr('get run error', $errorReturn);
- }
-
- $outSideSubmitOrder = new OutsideSubmitOrder();
- $date = [
- 'from_org_id' => ZybBusUtil::util()->getSupplierId(),
- 'outside_order_no' => $params['orderInfo']['zybOrderCode'],
- 'ticket_id' => $ticket_id,
- 'ticket_num' => $prodInfo['prod_cnt'],
- 'price' => $prodInfo['price'],
- 'all_price' => $prodInfo['total_money'],
- 'customer_name' => $params['orderInfo']['linkName'],
- 'customer_mobile' => $params['orderInfo']['tel'],
- 'customer_id_no' => $params['orderInfo']['idCard'],
- 'passenger' => $prodInfo['passenger_str']
- ];
- $submitOrderId = $outSideSubmitOrder->addRow($date);
- #设置 返回下单接口参数
- $prod_str = [1, $run_id, $ticket_id, $prodInfo['price'], $prodInfo['prod_cnt']];
- $order_info_str = [813, 0, ZybBusUtil::util()->getSupplierId(), $params['orderInfo']['zybOrderCode'], 0, 275, 1, '李秉臻'];
- $customer_info_str = [$params['orderInfo']['linkName'], $params['orderInfo']['tel'], 1, '', ''];
- $passenger_info = $prodInfo['passenger_info'];
- $data = [
- 'prod_str' => '{' . implode(',', $prod_str) . '}',
- 'order_info_str' => '{' . implode(',', $order_info_str) . '}',
- 'customer_info_str' => '{' . implode(',', $customer_info_str) . '}',
- 'passenger_info' => json_encode($passenger_info),
- 'is_send_msg' => 0
- ];
- $curl = new CurlInterface($data, 4);
- $result = $curl->execute('http://' . CS1_DOMAIN . '/api/submit-order/submit-bus-order', 'POST');
- if (!isset($result['code']) || $result['code'] != '0') {
- $msg = empty($result['info']) ? '预定失败!' : $result['info'];
- $outSideSubmitOrder->editInfo(['status' => 2, 'ID' => $submitOrderId]);
- return Util::returnArrEr($msg, $errorReturn);
- }
- $outSideSubmitOrder->editInfo(['status' => 1, 'ID' => $submitOrderId]);
- //获取短信内容,同时模拟插入发送短信履历
- $order_id_array = explode(",", $result['data']);
- $send_message = $this->getSendMessageAndSimulateSend($order_id_array[0]);
- return $this->orderDetail(['corpOrderId' => $params['orderInfo']['zybOrderCode'], 'send_message' => $send_message]);
- }
-
- /**
- * 获取短信信息并发送
- * @param $order_id
- * @return string
- */
- public function getSendMessageAndSimulateSend($order_id)
- {
- $msg = new OperaMessageTemplate();
- $res = $msg->otaGetMsgInfo($order_id);
- if (!isset($res["code"]) || $res["code"] != 0) {
- return "获取短信错误,请联系客服!!!";
- } else {
- $current_time = date("Y-m-d H:i:s");
- $send_message = $res["content"];
- $send_tel = $res["mobile"];
- $send_msg_values = [
- 'ORDER_ID' => $order_id,
- 'SEND_MOBILE' => $send_tel,
- 'SEND_MESSAGE' => $send_message,
- 'SEND_TIME' => $current_time,
- 'SEND_ERROR' => 1,
- 'CREATE_USER_ID' => 2,
- 'CREATE_TIME' => $current_time,
- 'UPDATE_USER_ID' => 2,
- 'UPDATE_TIME' => $current_time
- ];
- $msgRecord = new OrderSendMessage();
- $msgRecord->attributes = $send_msg_values;
- $res = $msgRecord->insert();
- return $send_message;
- }
- }
-
- /**
- * Des:下单参数校验并返回整理后的数据
- * Name: checkOrderParams
- * @param $params
- * @return array
- * @author 倪宗锋
- */
- public function checkOrderParams($params)
- {
- if (empty($params['orderInfo']['zybOrderCode'])) {
- return Util::returnArrEr('params error', '', '301');
- }
- if (empty($params['orderDetail']['zybPrice'])) {
- return Util::returnArrEr('params error', '', '301');
- }
- if (empty($params['orderDetail']['zybTotalPrice'])) {
- return Util::returnArrEr('params error', '', '301');
- }
- if (empty($params['orderDetail']['num'])) {
- return Util::returnArrEr('params error', '', '301');
- }
- //购买的产品数量
- $prod_info = [
- 'total_money' => 0,
- 'price' => 0,
- 'prod_cnt' => 0,
- 'passenger_info' => []
- ];
- #产品信息
- $prod_info['price'] = $params['orderDetail']['zybPrice'];
- $prod_info['total_money'] = $params['orderDetail']['zybTotalPrice'];
- $prod_info['prod_cnt'] = $params['orderDetail']['num'];
- if($params['orderDetail']['credentials'] != null) {
- foreach ($params['orderDetail']['credentials'] as $val) {
- #出行人信息
- $pass = [
- 'passenger_name' => $val['name'],
- 'passenger_cardid' => $val['idCard'],
- ];
- $pass_str = $val['name'] . '*' . $val['idCard'];
- $prod_info['passenger_info'][] = $pass;
- if (empty($prod_info['passenger_str'])) {
- $prod_info['passenger_str'] = $pass_str;
- } else {
- $prod_info['passenger_str'] .= '/' . $pass_str;
- }
- }
- }
- return Util::returnArrSu('', $prod_info);
- }
-
- /**
- * Des:出票信息
- * Name: orderDetail
- * @param $params
- * @return array
- * @author 倪宗锋
- */
- public function orderDetail($params)
- {
- if (empty($params['corpOrderId'])) {
- return Util::returnArrEr('params error', '', '301');
- }
- $orderMain = new OrderMain();
- $checkOutsideOrderNo = $orderMain->checkOutsideOrderNo($params['corpOrderId'], ZybBusUtil::util()->getSupplierId());
- if (empty($checkOutsideOrderNo['0']['order_id'])) {
- return Util::returnArrEr('order not exist', '', '301');
- }
- /*******设置订单状态**********/
- $message = '';
- if (in_array($checkOutsideOrderNo['0']['order_status'], [146, 117, 314])) {
- $order_status = 2;
- } else if ($checkOutsideOrderNo['0']['order_status'] == 148) {
- $order_status = 5;
- } else {
- $order_status = 4;
- $message = '未支付';
- }
- #获取出行人 信息
- $order_id = $checkOutsideOrderNo['0']['order_id'];
- $orderCustomer = new OrderCustomerInfo();
- $getCustomer = $orderCustomer->getCustomer($order_id);
- $tickets = [];
- foreach ($getCustomer as $val) {
- $array = [
- 'certificateNo' => $val['cus_id_no'],//乘客证件号码
- 'ticketStatus' => $order_status,//订单出票状态,1出票中,2已出票,3 无票,4退票中,5已退票,6退票失败
- ];
- if (empty($message) == false) {
- $array['noTicketReason'] = $message;//无票原因。ticketStatus=3时必填
- }
- if ($order_status == 2) {
- $array['ticketNo'] = $order_id;//出票号
- $array['ticketPwd'] = $order_id;//票密码/验证码
- }
- $tickets[] = $array;
- }
- //获取出票短信
-
-
- //返回值
- $return = [
- 'orderId' => $order_id,
- 'corpOrderId' => $params['corpOrderId'],
- 'smsTemplate' => $params['send_message'],
- 'tickets' => $tickets
- ];
- return Util::returnArrSu('', $return);
- }
- }
|