<?php
/**
 * 盘点库存管理Controller
 * Created by PhpStorm.
 * User: 小威
 * Date: 2019/12/04
 * Time: 12: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\MStocktaking;

class Stocktaking extends BaseController
{
    private $objMStocktaking;

    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMStocktaking = new MStocktaking($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'] : '',
            'operatorName' => isset($params['operatorName']) ? $params['operatorName'] : '',
            'operatorId' => $this->onlineUserId,
            'remark' => isset($params['remark']) ? $params['remark'] : '',
            'stocktakingTime' => isset($params['stocktakingTime']) ? $params['stocktakingTime'] : '',
        ];
        foreach ($data as $key => $value) {
            if ($value != null && empty($value) && $value !== 0) {
                parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        isset($params['no']) && $data['no'] = $params['no'];
        $data['warehouseName'] = isset($params['warehouseName']) ? $params['warehouseName'] : '';

        $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 $key => $value) {
            $data['num']++;
            $details = [
                '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'] : '',
                'documentInventoryNum' => isset($value['documentInventoryNum']) ? $value['documentInventoryNum'] : '',
                'currentInventoryNum' => isset($value['currentInventoryNum']) ? abs($value['currentInventoryNum']) : '',
                'otherNum' => isset($value['otherNum']) ? abs($value['otherNum']) : '',
                'remark' => isset($value['remark']) ? $value['remark'] : '',
                'updateTime' => time(),
                'areaId' => getArrayItem($value, 'areaId', 0),
                'areaName' => getArrayItem($value, 'areaName', ''),
                'areaCode' => getArrayItem($value, 'areaCode', 0),
                'storageLocationId' => getArrayItem($value, 'storageLocationId', 0),
                'storageLocationName' => getArrayItem($value, 'storageLocationName', ''),
                'storageLocationCode' => getArrayItem($value, 'storageLocationCode', 0),
            ];
            $details['id'] = isset($value['id']) ? $value['id'] : null;
            $details['deleteStatus'] = isset($value['deleteStatus']) ? $value['deleteStatus'] : StatusCode::$standard;
            $data['details'][] = $details;
        }
        $data['updateTime'] = time();
        return $data;
    }

    /**
     * 盘点库存添加
     */
    public function addStocktaking()
    {
        $params = $this->commonFieldFilter();
        $result = $this->objMStocktaking->addStocktaking($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 盘点库存审核
     */
    public function auditStocktaking()
    {
        $paramsData = $this->request->getRawJson();
        if (empty($paramsData)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);

        }
        $params = [
            'auditId' => $this->onlineUserId,//操作人id
            'auditName' => isset($paramsData['auditName']) ? $paramsData['auditName'] : '',//操作人姓名
        ];
        $params['id'] = $this->request->param('request_id');
        foreach ($params as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

        $result = $this->objMStocktaking->auditStocktaking($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 盘点库存修改
     */
    public function updateStocktaking()
    {
        $params = $this->commonFieldFilter();
        $params['id'] = $this->request->param('request_id');
        if(empty($params['id'])){
            parent::sendOutput('参数错误',ErrorCode::$paramError);
        }
        $modelResult = $this->objMStocktaking->updateStocktaking($params);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(),$modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 盘点库存列表
     */
    public function getAllStocktaking()
    {
        $params = $this->request->getRawJson();

        $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->objMStocktaking->getAllStocktaking($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 searchAllStocktaking()
    {
        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $selectParams = [
//            'warehouseId' => isset($params['warehouseId']) ? $params['warehouseId'] : '',
            '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'];

        $export = isset($params['export']) ? $params['export'] : 0;

        $result = $this->objMStocktaking->searchAllStocktaking($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(), $result->getErrorCode());
        }
    }

    /**
     * 盘点详情
     */
    public function getStocktakingInfo()
    {
        $params['id'] = $this->request->param('request_id');
        if (empty($params['id'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $result = $this->objMStocktaking->getStocktakingInfo($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

}