<?php
/**
 * 仓库调拨Controller
 * Created by PhpStorm.
 * User: 小威
 * Date: 2019/11/29
 * Time: 15:00
 */

namespace JinDouYun\Controller\Stock;

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

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Stock\MAllocate;

class Allocate extends BaseController
{
    private $objMAllocate;

    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMAllocate = new MAllocate($this->onlineEnterpriseId, $this->onlineUserId);
    }

    /**
     * 获取参数
     *
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'warehouseId' => isset($params['warehouseId']) ? $params['warehouseId'] : '',
            'inWarehouseId' => isset($params['inWarehouseId']) ? $params['inWarehouseId'] : '',
            'operatorName' => isset($params['operatorName']) ? $params['operatorName'] : '',
        ];
        isset($params['operatorName']) && $data['operatorId'] = $this->onlineUserId;
        isset($params['operatorName']) && $data['operatorName'] = $params['operatorName'];
        foreach($data as $key => $value){
            if(empty($value)){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
            }
        }
        isset($params['remark']) && $data['remark'] = $params['remark'];
        isset($params['no']) && $data['no'] = $params['no'];
        $data['auditId'] = isset($params['auditName']) ? $this->onlineUserId : null;
        $data['auditName'] = isset($params['auditName']) ? $params['auditName'] : null;
        $data['warehouseName'] = isset($params['warehouseName']) ? $params['warehouseName'] : null;
        $data['inWarehouseName'] = isset($params['inWarehouseName']) ? $params['inWarehouseName'] : null;

        $data['deleteArray'] = isset($params['deleteArray']) ? $params['deleteArray'] : [];
        if(empty($params['details']) || !isset($params['details'])) parent::sendOutput('商品数据为空', ErrorCode::$paramError);
        $data['num'] = 0;
        $data['details'] = [];
        foreach ($params['details'] as $value) {
            $data['num']++;
            $data['details'][] = [
                'id' => isset($value['id']) ? $value['id'] : '',
                'materielId' => isset($value['materielId']) ? $value['materielId'] : '',
                'materielName' => isset($value['materielName']) ? $value['materielName'] : '',
                'materielCode' => isset($value['materielCode']) ? $value['materielCode'] : '',
                'skuId' => isset($value['skuId']) ? $value['skuId'] : '',
                'unitName' => isset($value['unitName']) ? $value['unitName'] : '',
                'skuName' => isset($value['skuName']) ? $value['skuName'] : '',
                'num' => isset($value['num']) ? $value['num'] : '',
                'otherNum' => isset($value['otherNum']) ? $value['otherNum'] : '',

                'inAreaId' => isset($value['inAreaId']) ? $value['inAreaId'] : 0,
                'inAreaName' => isset($value['inAreaName']) ? $value['inAreaName'] : '',
                'inAreaCode' => isset($value['inAreaCode']) ? $value['inAreaCode'] : '',
                'inStorageLocationId' => isset($value['inStorageLocationId']) ? $value['inStorageLocationId'] : 0,
                'inStorageLocationName' => isset($value['inStorageLocationName']) ? $value['inStorageLocationName'] : '',
                'inStorageLocationCode' => isset($value['inStorageLocationCode']) ? $value['inStorageLocationCode'] : '',
                'outAreaId' => isset($value['outAreaId']) ? $value['outAreaId'] : 0,
                'outAreaName' => isset($value['outAreaName']) ? $value['outAreaName'] : '',
                'outAreaCode' => isset($value['outAreaCode']) ? $value['outAreaCode'] : '',
                'outStorageLocationId' => isset($value['outStorageLocationId']) ? $value['outStorageLocationId'] : 0,
                'outStorageLocationName' => isset($value['outStorageLocationName']) ? $value['outStorageLocationName'] : '',
                'outStorageLocationCode' => isset($value['outStorageLocationCode']) ? $value['outStorageLocationCode'] : '',

                'costUnitPrice' => isset($value['costUnitPrice']) ? $value['costUnitPrice'] : '',
                'isEq' => isset($value['isEq']) ? $value['isEq'] : 4,
            ];
        }
        return $data;
    }

    /**
     * 仓库调拨添加
     * @throws Exception
     */
    public function addAllocate()
    {
        $data = $this->commonFieldFilter();
        $result = $this->objMAllocate->addAllocate($data);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 仓库调拨记录删除
     */
    public function deleteAllocate()
    {
        $params['id'] = $this->request->param('request_id');
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $result = $this->objMAllocate->deleteAllocate($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 仓库调拨审核
     * @throws Exception
     */
    public function auditAllocate()
    {
        $params = $this->request->getRawJson();
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $updateData = [
            'id' => $id,
            'auditId' => $this->onlineUserId,
            'auditName' => isset($params['auditName']) ? $params['auditName'] : '',
        ];
        foreach($updateData as $key => $value){
            if(empty($value) && $value !== 0){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
            }
        }

        $result = $this->objMAllocate->auditAllocate($updateData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 仓库仓库调拨修改
     */
    public function updateAllocate()
    {
        $updateData = self::commonFieldFilter();
        $param = $this->request->param('request_id');
        if(empty($param)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $updateData['id'] = $param;
        $result = $this->objMAllocate->updateAllocate($updateData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }


    /**
     * 仓库调拨记录列表
     */
    public function getAllAllocate()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }

        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        
        $export = isset($params['export']) ? $params['export'] : 0;
        $result = $this->objMAllocate->getAllAllocate($selectParams,$export);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize' => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }

    /**
     * 调拨搜索
     */
    public function searchAllocate()
    {
        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $selectParams = [
            'warehouseId' => isset($params['warehouseId']) ? $params['warehouseId'] : '',
            'inWarehouseId' => isset($params['inWarehouseId']) ? $params['inWarehouseId'] : '',
            'auditStatus' => isset($params['auditStatus']) ? $params['auditStatus'] : '',
            'start' => isset($params['start']) ? $params['start'] : '',
            'end' => isset($params['end']) ? $params['end'] : '',
            'search' => isset($params['search']) ? $params['search'] : '',

        ];
        $pageParams = pageToOffset($params['page'] ? : 1, $params['pageSize'] ? : 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];

        $result = $this->objMAllocate->getAllAllocate($selectParams);
        if($result->isSuccess()){
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize' => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }





    /**
     * 仓库调拨详情
     */
    public function getAllocateInfo()
    {
        $params['id'] = $this->request->param('request_id');
        if (empty($params['id'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $result = $this->objMAllocate->getAllocateInfo($params);

        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

}