<?php
/**
* Created by PhpStorm.
* User: kang
* Date: 2021/5/26
* Time: 10:28
*/

namespace JinDouYun\Controller\Delivery;

use JinDouYun\Model\Delivery\MDelivery;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Delivery\MDistributionCentre;

class DistributionCentre extends BaseController
{
    private $objMDistributionCentre;

    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMDistributionCentre = new MDistributionCentre($this->onlineEnterpriseId, $this->onlineUserId);
    }
    
    /**
     * 待司机配送获取参数
     *
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        
        $returnData = [
            'enterpriseId' => $this->onlineEnterpriseId,
            'documentDate' => getArrayItem($params,'documentDate'), // 单据日期
            'documentNo'   => getArrayItem($params,'documentNo'), // 单据编号
            'sourceNo'     => getArrayItem($params,'sourceNo'), // 来源编号
            'documentType' => getArrayItem($params,'documentType'), // 单据类型
            'customerId'   => getArrayItem($params,'customerId'), // 客户id
            'customerName' => getArrayItem($params,'customerName'), // 客户名称
            'warehouseId'  => getArrayItem($params,'warehouseId'), // 仓库id
            'deliveryWarehouse'=> getArrayItem($params,'deliveryWarehouse'), // 发货仓库
            'manager'           => getArrayItem($params,'manager'), // 经手人
            'producer'          => getArrayItem($params,'producer'), // 制单人
            'receiver'          => getArrayItem($params,'receiver'), // 收货人
            'mobile'            => getArrayItem($params,'mobile'), // 联系人电话
            'modeOfProduction'  => getArrayItem($params,'modeOfProduction'), // 产生方式
            'driverId'          => getArrayItem($params,'driverId'), // 司机id
            'distributionDriver'=> getArrayItem($params,'distributionDriver'), // 配送司机
        ];
        
        //必填项
        foreach ($returnData as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

        //选填项
        $returnData['deliveryState']    = getArrayItem($params,'deliveryState'); // 配送状态
        $returnData['deliveryQuantity'] = getArrayItem($params,'deliveryQuantity'); // 发货数量
        $returnData['deliveryAmount']   = getArrayItem($params,'deliveryAmount'); // 发货金额
        $returnData['returnAmount']     = getArrayItem($params,'returnAmount'); // 退货金额
        $returnData['returnGoodsNum']   = getArrayItem($params,'returnGoodsNum',0); // 退货数量
        $returnData['containerLocation']= getArrayItem($params,'containerLocation'); // 集货位
        $returnData['binningNum']       = getArrayItem($params,'binningNum'); // 装箱数量
        $returnData['receivingAddress'] = getArrayItem($params,'packingNum'); // 收货地址
        $returnData['distributionRoute']= getArrayItem($params,'distributionRoute');// 配送线路
        $returnData['expectedDelivery'] = getArrayItem($params,'expectedDelivery'); // 预计发货
        $returnData['remarks']          = getArrayItem($params,'remarks'); // 备注
        $returnData['packingNum']       = getArrayItem($params,'packingNum'); // 包装数量
        $returnData['returnWarehouse']  = getArrayItem($params,'returnWarehouse'); // 退货仓库
        $receiveData['deleteStatus']    = StatusCode::$standard;
        $returnData['createTime']       = time();//创建时间
        
        return $returnData;
    }
    /**
     * 添加待配送单
     */
    public function addDistribution()
    {
        $distributionData = $this->commonFieldFilter();
        $result = $this->objMDistributionCentre ->addDistribution($distributionData);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
    
    /**
     * 待司机配送删除
     */
    public function delDistribution()
    {
        $distributionId  = $this->request->param('request_id');
        if ( !$distributionId ) {
            $this->sendOutput('参数错误', ErrorCode::$paramError );
        }
        $result = $this->objMDistributionCentre->delDistribution($distributionId);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
    
    
    /**
     * 获取所有的待司机配送单
     */
    public function getAllDistribution()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $params['page'] = isset($params['page']) ? $params['page'] : 1;
        $params['pageSize'] = isset($params['pageSize']) ? $params['pageSize'] : 10;
        $pageParams = pageToOffset($params['page'], $params['pageSize']);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        
        $returnData = $this->objMDistributionCentre->getAllDistribution($selectParams);
        if ($returnData->isSuccess()) {
            $returnData = $returnData->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize'  => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($returnData->getData(), ErrorCode::$dberror);
        }
    }
    
    /**
     * 添加配送任务
     */
    public function addTask()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $taskData = [
            'enterpriseId' => $this->onlineEnterpriseId,
            'deliveryDate' => getArrayItem($params,'deliveryDate'), // 单据日期
            'driverId'     => getArrayItem($params,'driverId'), // 司机id
            'driverName'   => getArrayItem($params,'driverName'), // 配送司机
            'deliveryTaskData'=> getArrayItem($params,'deliveryTaskData',[]), // 配送任务主数据
        ];
        foreach ($taskData as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        
        $taskData['TaskNo']           = getArrayItem($params,'TaskNo'); // 任务编号
        $taskData['deliveryStatus']   = getArrayItem($params,'deliveryStatus',1); // 配送状态
        $taskData['deliveryEndTime']  = getArrayItem($params,'deliveryEndTime'); // 结束配送时间
        $taskData['carId']            = getArrayItem($params,'carId'); // 配送车辆id
        $taskData['deliveryCar']      = getArrayItem($params,'deliveryCar'); // 配送车辆
        $taskData['deliveryBoy']      = getArrayItem($params,'deliveryBoy'); // 送货员
        $taskData['deliveryOrderNum'] = getArrayItem($params,'deliveryOrderNum'); // 配送单量
        $taskData['returnGoodsOrderNum']  = getArrayItem($params,'returnGoodsOrderNum'); // 退货数量
        $taskData['deliverGoodsQuantity'] = getArrayItem($params,'deliverGoodsQuantity'); // 发货数量
        $taskData['deliveryAmount']       = getArrayItem($params,'deliveryAmount'); // 发货金额
        $taskData['signedQuantity']       = getArrayItem($params,'signedQuantity'); // 签收数量
        $taskData['signedAmount']         = getArrayItem($params,'signedAmount'); // 签收金额
        $taskData['rejectionQuantity']    = getArrayItem($params,'rejectionQuantity'); // 拒收数量
        $taskData['rejectionAmount']      = getArrayItem($params,'rejectionAmount'); // 拒收金额
        $taskData['returnGoodsQuantity']  = getArrayItem($params,'returnGoodsQuantity'); // 退货数量
        $taskData['returnGoodsAmount']    = getArrayItem($params,'returnGoodsAmount'); // 退货金额
        $taskData['binningQuantity']      = getArrayItem($params,'binningQuantity');  //装箱数量
        $taskData['deliveryMileage']      = getArrayItem($params,'deliveryMileage');  //配送里程
        $taskData['remark']               = getArrayItem($params,'remark'); // 备注
        $taskData['deliveryStartTime']    = time(); // 开始配送时间
        $taskData['deleteStatus']         = StatusCode::$standard;
        $taskData['createTime']           = time();//创建时间
        $result = $this->objMDistributionCentre ->addTask($taskData);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
    
    /**
     * 配送任务完成配送
     */
    public function taskComplete()
    {
        $taskCompleteId  = $this->request->param('request_id');
        if ( !$taskCompleteId ) {
            $this->sendOutput('参数错误', ErrorCode::$paramError );
        }
        $result = $this->objMDistributionCentre->taskComplete($taskCompleteId);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
}