<?php
/**
 * Created by PhpStorm.
 * User: kang
 * Date: 2021/6/9
 * Time: 17:33
 */

namespace JinDouYun\Controller\Stock;

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

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Stock\MStorageLocation;


class StorageLocation extends BaseController
{
    private $objMStorageLocation;
    
    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMStorageLocation = new MStorageLocation($this->onlineEnterpriseId, $this->onlineUserId);
    }
    
    /**
     * 获取参数
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        
        $data = [
            "enterpriseId" => $this->onlineEnterpriseId,
            "areaId" => getArrayItem($params,'areaId'),//库区id
            "name" => getArrayItem($params,'name'),
            "code" => getArrayItem($params,'code'),
        ];
        
        foreach ($data as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $data['enableStatus'] = getArrayItem($params,'enableStatus',5);
        return $data;
    }
    
    /**
     * 仓库库位添加
     */
    public function addStorageLocation()
    {
        $data = $this->commonFieldFilter();
        $result = $this->objMStorageLocation->addStorageLocation($data);
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }
    
    /**
     * 仓库库位删除
     */
    public function deleteStorageLocation()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        
        $update = [
            'deleteStatus' => StatusCode::$delete
        ];
        $result = $this->objMStorageLocation->updateStorageLocation($update, ['id' => $id,'enterpriseId' => $this->onlineEnterpriseId]);
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }
    
    /**
     * 仓库库位修改
     */
    public function updateStorageLocation()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $update = $this->commonFieldFilter();
        $result = $this->objMStorageLocation->updateStorageLocation($update, ['id' => $id,'enterpriseId' => $this->onlineEnterpriseId]);
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }
    
    /**
     * 仓库库位启用/禁用
     */
    public function enableStorageLocation()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $params = $this->request->getRawJson();
        $data = [
            'enableStatus' => getArrayItem($params,'enableStatus',5),
        ];
        foreach ($data as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $result = $this->objMStorageLocation->updateStorageLocation($data, ['id' => $id,'enterpriseId' => $this->onlineEnterpriseId]);
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }
    
    /**
     * 仓库库位列表
     */
    public function getAllStorageLocation()
    {
        $params = $this->request->getRawJson();
        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];

        if(isset($params['areaId']) && !empty($params['areaId'])){
            $selectParams['areaId'] = $params['areaId'];
        }
        if(isset($params['enableStatus']) && !empty($params['enableStatus'])){
            $selectParams['enableStatus'] = $params['enableStatus'];
        }
        
        if(isset($params['warehouseId']) && !empty($params['warehouseId'])){
            $selectParams['warehouseId'] = $params['warehouseId'];
        }
        $result = $this->objMStorageLocation->getAllStorageLocation($selectParams);
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
        $returnData = $result->getData();
        $pageData = [
            'pageIndex' => $params['page'],
            'pageSize' => $params['pageSize'],
            'pageTotal' => $returnData['total'],
        ];
        parent::sendOutput($returnData['data'], 0, $pageData);
    }
    
    /**
     * 仓库库位列表(不分页)
     */
    public function getListStorageLocation()
    {
        $params = $this->request->getRawJson();
        $selectParams = [];
        if(isset($params['areaId']) && !empty($params['areaId'])){
            $selectParams['areaId'] = $params['areaId'];
        }
        if(isset($params['type']) && !empty($params['type'])){
            $selectParams['type'] = $params['type'];
        }
        $selectParams['enterpriseId'] =  $this->onlineEnterpriseId;
        $result = $this->objMStorageLocation->getListStorageLocation($selectParams);
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
        parent::sendOutput($result->getData());
    }
    
    /**
     * 仓库库位详情
     */
    public function getStorageLocationInfo()
    {
        $params['id'] = $this->request->param('request_id');
        if (empty($params['id'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        
        $result = $this->objMStorageLocation->getStorageLocationInfo($params);
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }
    
}