<?php


namespace JinDouYun\Controller\System;


use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Customer\MCustomer;
use JinDouYun\Model\Customer\MShippingAddress;
use JinDouYun\Model\System\MDeliverySetting;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\StatusCode;

/**
 * @copyright   Copyright (c) https://www.qianniaovip.com All rights reserved
 * Description: 配送方式
 * Class ApiDelivery
 * @package JinDouYun\Controller\System
 */
class ApiDelivery extends BaseController
{
    private $objMDeliverySetting;

    /**
     * ApiDelivery constructor.
     * @param bool $isCheckAcl
     * @param bool $isMustLogin
     * @param bool $checkToken
     * @param bool $getAreaCode
     * @param bool $checkShopToken
     * @param bool $checkSupplierToken
     * @throws \Exception
     */
    public function __construct($isCheckAcl = true, $isMustLogin = true, $checkToken = true, $getAreaCode = true, $checkShopToken = true, $checkSupplierToken = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken, $getAreaCode, $checkShopToken, $checkSupplierToken);
        $this->objMDeliverySetting = new MDeliverySetting($this->onlineUserId,$this->onlineEnterpriseId);
    }

    /**
     * Doc: (des="")
     * User: XMing
     * Date: 2021/2/3
     * Time: 4:47 下午
     * @throws \Exception
     */
    public function getDeliveryType()
    {
        $shopIds = $this->request->param('shopId');
        $map = [];
        $objMDeliverySetting = new MDeliverySetting($this->onlineUserId, $this->onlineEnterpriseId);
        $objMCustomer = new MCustomer($this->onlineEnterpriseId,$this->onlineUserId);
        $objMShippingAddress = new MShippingAddress($this->onlineEnterpriseId);
        $customerId = $objMCustomer->getCustomerIdByUserCenterId($this->onlineUserId);
        $addressResult = $objMShippingAddress->getAllShippingAddress(['limit' => 20,'offset' => 0,'customerId' => $customerId]);
        if (!$addressResult->isSuccess()){
            parent::sendOutput($addressResult->getData(), $addressResult->getErrorCode());
        }
        $addressLists = $addressResult->getData();
        foreach ($shopIds as $key => $shopId){
            $modelResult = $this->objMDeliverySetting->allDelivery($shopId);
            if (!$modelResult->isSuccess()) {
                parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
            }
            $results = $modelResult->getData()['data'];
            if (empty($results)){
                //此店铺没有配置
                $shopId = 0;
                $modelResult = $this->objMDeliverySetting->allDelivery($shopId);
                if (!$modelResult->isSuccess()) {
                    parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
                }
                $results = $modelResult->getData()['data'];
            }
            foreach ($results as &$val){
                switch ($val['deliveryType']){
                    case StatusCode::$deliveryType['goodsDelivery']:
                        //商品配送
                        $val['lists'] = $addressLists['data'];
                        break;
                    case StatusCode::$deliveryType['selfMention']:
                        //上门自提
                        $selfExpressRuleResult = $objMDeliverySetting->getAllSelfExpressRule($shopId);
                        if (!$selfExpressRuleResult->isSuccess()) {
                            return ResultWrapper::fail($selfExpressRuleResult->getData(), $selfExpressRuleResult->getErrorCode());
                        }
                        $val['lists'] = $selfExpressRuleResult->getData();
                        break;
                }
            }
            $map[$shopIds[$key]] = $results;
        }
        parent::sendOutput($map);
;    }

    /**
     * Doc: (des="")
     * User: XMing
     * Date: 2021/2/4
     * Time: 2:29 下午
     * @return ResultWrapper
     * @throws \Exception
     */
    public function getAllSelfExpressRule()
    {
        //获取自提点
        $objMDeliverySetting = new MDeliverySetting($this->onlineUserId, $this->onlineEnterpriseId);
        $shopIds = $this->request->param('shopId');
        $map = [];
        foreach ($shopIds as $shopId){
            $selfExpressRuleResult = $objMDeliverySetting->getAllSelfExpressRule($shopId);
            if (!$selfExpressRuleResult->isSuccess()) {
                return ResultWrapper::fail($selfExpressRuleResult->getData(), $selfExpressRuleResult->getErrorCode());
            }
            $selfExpressRule = $selfExpressRuleResult->getData();
            $map[$shopId] = $selfExpressRule;
        }
        parent::sendOutput($map);
    }
}