<?php
/**
 * 订单相关处理方法
 */
namespace JinDouYun\Model\Order;

use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\StatusCode;
use Mall\Framework\Core\ErrorCode;

use JinDouYun\Model\Customer\MCustomer;
use JinDouYun\Model\Finance\MCustomerBalance;
use JinDouYun\Model\Stock\MReservoirArea;
use JinDouYun\Model\System\MDeliverySetting;
use JinDouYun\Model\Customer\MShippingAddress;

class MOrderProcess
{
    private $onlineUserId;

    private $onlineEnterpriseId;

    public function __construct($onlineUserId, $onlineEnterpriseId)
    {
        $this->onlineUserId = $onlineUserId;
        $this->onlineEnterpriseId = $onlineEnterpriseId;
    }

    /**
     * 格式化订单关联数据
     * @param $common
     * @return ResultWrapper
     * @throws \Exception
     */
    public function formatCommonParams($common)
    {
        $objMCustomer = new MCustomer($this->onlineEnterpriseId, $this->onlineUserId);
        $objMCustomerBalance = new MCustomerBalance($this->onlineEnterpriseId, $this->onlineUserId);

        // 获取客户信息
        $customerData = $objMCustomer->getCustomerInfoByUserCenterId($common['userCenterId']);
        if (!$customerData->isSuccess()) {
            return ResultWrapper::fail($customerData->getData(), $customerData->getErrorCode());
        }
        $customerData = $customerData->getData();

        // 获取客户当前欠款
        $customerOwe = $objMCustomerBalance->getCustomerBalance($customerData['id']);
        $customerData['customerOwe'] = $customerOwe;
        unset($customerData['remark']);
        $common = array_merge($common, $customerData);

        if (isset($common['version']) && $common['version'] == 1){
            //多配送方式
            foreach ($common['proporties'] as $shopId => &$item){
                $deliveryResult = self::getObjectByDeliveryType($item);
                if (!$deliveryResult->isSuccess()){
                    return ResultWrapper::fail($deliveryResult->getData(),$deliveryResult->getErrorCode());
                }
                $deliveryData = $deliveryResult->getData();
                $item['selfRuleData'] = getArrayItem($deliveryData,'selfRuleData',[]);
                $item['addressData'] = getArrayItem($deliveryData,'addressData',[]);
            }
            unset($item);
        }

        // 获取物流信息
        $deliveryResult = self::getObjectByDeliveryType($common);
        if (!$deliveryResult->isSuccess()){
            return ResultWrapper::fail($deliveryResult->getData(),$deliveryResult->getErrorCode());
        }
        $deliveryData = $deliveryResult->getData();
        $common['selfRuleData'] = getArrayItem($deliveryData,'selfRuleData',[]);
        $common['addressData'] = getArrayItem($deliveryData,'addressData',[]);

        // 根据客户的库位id获取库位信息
        $reservoir = [];
        $reservoirId = getArrayItem($customerData, 'reservoirId', 0);
        if (!empty($reservoirId)) {
            $objMReservoirArea = new MReservoirArea($this->onlineEnterpriseId, $this->onlineUserId);
            $reservoirResult = $objMReservoirArea->getReservoirInfo($reservoirId);
            if (!$reservoirResult->isSuccess()) {
                return ResultWrapper::fail($reservoirResult->getData(), $reservoirResult->getErrorCode());
            }
            $reservoir = $reservoirResult->getData();
            $reservoir = [
                'name'        => getArrayItem($reservoir, 'name', ''),
                'warehouseId' => getArrayItem($reservoir, 'warehouseId', 0),
                'code'        => getArrayItem($reservoir, 'code', ''),
                'type'        => getArrayItem($reservoir, 'type', 0)
            ];
        }
        $common['reservoir'] = $reservoir;
        return ResultWrapper::success($common);
    }


    public function getObjectByDeliveryType(array $common): ResultWrapper
    {
        $objMDeliverySetting = new MDeliverySetting($this->onlineUserId, $this->onlineEnterpriseId);
        $selfRuleData = [];
        $addressData = [];
        switch ($common['deliveryType']) {
            case StatusCode::$deliveryType['selfMention']:
                if (isset($common['selfRuleId']) && !empty($common['selfRuleId'])) {
                    $selfRuleDataResult = $objMDeliverySetting->getSelfRuleInfo($common['selfRuleId']);
                    if (!$selfRuleDataResult->isSuccess()) {
                        return ResultWrapper::fail($selfRuleDataResult->getData(), $selfRuleDataResult->getErrorCode());
                    }
                    $selfRuleData = $selfRuleDataResult->getData();
                    $selfRuleData = isset($selfRuleData['setData']) ? $selfRuleData['setData'] : [];
                }
                break;
            case in_array( $common['deliveryType'],[StatusCode::$deliveryType['goodsDelivery'],StatusCode::$deliveryType['logistics']] ):
                if (isset($common['addressId']) && !empty($common['addressId'])){
                    $common['receiveAddressId'] = $common['addressId'];
                }
                if (isset($common['receiveAddressId']) && !empty($common['receiveAddressId'])) {
                    $objMShippingAddress = new MShippingAddress($this->onlineEnterpriseId);
                    $result = $objMShippingAddress->getShippingAddressInfo($common['receiveAddressId']);
                    if (!$result->isSuccess()) {
                        return ResultWrapper::fail($result->getData(), ErrorCode::$dberror);
                    }
                    $addressData = $result->getData();
                    if (empty($addressData)) {
                        return ResultWrapper::fail('收货地址信息不存在', ErrorCode::$dberror);
                    }
                }
                break;
        }
        return ResultWrapper::success([
            'selfRuleData' => $selfRuleData,
            'addressData'  => $addressData
        ]);
    }
}