<?php

namespace Business\Store;

use DAO\Order\DiliverymanModel;
use DAO\StoreModel;
use Error\CodeConfigModel;
use Error\ErrorModel;
use Our\ApiConst;


/**
 * 店主service
 *
 * @date 2018-5-11
 * @author csw
 */
class DeliveryServiceModel extends \Business\AbstractModel
{


    public $clientType = false;

    public function init()
    {
        $this->storeDao = StoreModel::getInstance();
    }

    public function selectDeliveryMan($data){
            $orderDao=\DAO\Order\OrderModel::getInstance(DbNameConst::masterDBConnectName);
            $qmDeliveryManLog=\DAO\Order\QmDeliverymanLogModel::getInstance(DbNameConst::masterDBConnectName);
            $orderDao->setDb($orderDao->dbName);
            $orderDao->db->doTransaction();
            $orderData['diliveryman_id']=$data['id'];
            $orderRes=$orderDao->updateByOrderId($orderData,$data['orderId']);
            if(!$orderRes){
                $orderDao->db->doRollback();
                ErrorModel::throwException(CodeConfigModel::updateOrderFail);
            }
            $qmDeliveryManLogData['diliveryman_id']=$data['id'];
            $qmDeliveryManLogData['store_id']=$data['storeId'];
            $qmDeliveryManLogData['order_id']=$data['orderId'];
            $qmDeliveryManLogData['delivery_state']=ApiConst::deliveryStateWait;
            $qmDeliveryManLogData['distribution_fee']=$data['distributionFee'];
            $qmDeliveryManLogData['order_type']=$data['orderType'];
            $qmDeliveryManLogData['get_time']=TIMESTAMP;
            $res=$qmDeliveryManLog->insert($data);
            if(!res){
                $orderDao->db->doRollback();
                ErrorModel::throwException(CodeConfigModel::selectDeliveryFail);
            }
            $orderDao->db->doCommit();
            return $res;
    }
    /**
     * 登录业务
     *
     * @var \Business\User\LoginV2Model
     */
    private
    static $_instance = null;

    /**
     * 单例模式获取类实例
     *
     * @return \Business\User\LoginV2Model
     */
    public
    static function getInstance()
    {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

}
