<?php
/**
 * 订单退货Controller
 * Created by PhpStorm.
 * User: 小威
 * Date: 2019/10/31
 * Time: 15:58
 */

namespace JinDouYun\Controller\Order;

use Mall\Framework\Core\ErrorCode;

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Order\MOrderReturn;
use Mall\Framework\Core\StatusCode;

class OrderReturn extends BaseController
{
    private $objMOrderReturn;

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

    /**
     * 获取参数
     *
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'originId'     => isset($params['originId']) ? $params['originId'] : '',
            'originNo'     => isset($params['originNo']) ? $params['originNo'] : '',
            'shopId'       => isset($params['shopId']) ? $params['shopId'] : '',
            'shopName'     => isset($params['shopName']) ? $params['shopName'] : '',
            'operatorName' => isset($params['operatorName']) ? $params['operatorName'] : '',
            'operatorId'   => $this->onlineUserId,
            'userCenterId' => isset($params['userCenterId']) ? $params['userCenterId'] : '',
            'customerId'   => isset($params['customerId']) ? $params['customerId'] : '',
            'customerName'   => isset($params['customerName']) ? $params['customerName'] : '',
            'remark'       => isset($params['remark']) ? $params['remark'] : '',
        ];
        foreach ($data as $key => $value) {
            if ($value != null && empty($value) && $value !== 0) {
                parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

        $data['no'] = isset($params['no']) ? $params['no'] : '';
        $data['auditId'] = isset($params['auditName']) ? $this->onlineUserId : null;
        $data['auditName'] = isset($params['auditName']) ? $params['auditName'] : null;
        $data['auditId'] = isset($params['auditId']) ? $params['auditId'] : 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 $key => $value) {
            $data['num']++;
            $details = [
                'goodsId'       => isset($value['goodsId']) ? $value['goodsId'] : '',
                'goodsName'     => isset($value['goodsName']) ? $value['goodsName'] : '',
                'goodsCode'     => isset($value['goodsCode']) ? $value['goodsCode'] : '',
                'goodsBasicId'  => isset($value['goodsBasicId']) ? $value['goodsBasicId'] : '',
                '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'] : '',
                'returnUnitPrice' => isset($value['returnUnitPrice']) ? $value['returnUnitPrice'] : '',
                'saleUnitPrice' => isset($value['saleUnitPrice']) ? $value['saleUnitPrice'] : '',
                'returnTotalPrice' => isset($value['returnTotalPrice']) ? $value['returnTotalPrice'] : '',
                'saleTotalPrice' => isset($value['saleTotalPrice']) ? $value['saleTotalPrice'] : '',
                'updateTime'    => time(),
            ];
            (isset($value['id']) && !empty($value['id'])) && $details['id'] = $value['id'];
            $details['deleteStatus'] = isset($value['deleteStatus']) ? $value['deleteStatus'] : StatusCode::$standard;
            isset($value['saleNum']) && $details['extend'] = json_encode(['saleNum' => $value['saleNum']]);

            $data['details'][] = $details;
        }
        $data['updateTime'] = time();
        return $data;
    }

    /**
     * 订单退货添加
     * @throws \Exception
     */
    public function addOrderReturn()
    {
        $params = $this->commonFieldFilter();
        $modelResult = $this->objMOrderReturn->addOrderReturn($params);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 销售退货删除
     */
    public function deleteOrderReturn()
    {
        $params = $this->request->param('request_id');
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $modelResult = $this->objMOrderReturn->deleteOrderOut($params);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 销售退货审核
     * @throws \Exception
     */
    public function auditOrderReturn()
    {
        $params = $this->request->param('request_id');
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $updateData['id'] = $params;
        $params = $this->request->getRawJson();
        $updateData['auditId'] = $this->onlineUserId;
        $updateData['auditName'] = isset($params['auditName']) ? $params['auditName'] : '';
        foreach ($updateData as $key => $value) {
            if (empty($value)) {
                parent::sendOutput($key . '参数为空', ErrorCode::$paramError);
            }
        }
        $modelResult = $this->objMOrderReturn->auditOrderOut($updateData);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 销售退货修改
     */
    public function updateOrderReturn()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $updateData = $this->commonFieldFilter();
        $updateData['id'] = $id;
        $modelResult = $this->objMOrderReturn->updateOrderOut($updateData);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 销售退货列表
     */
    public function getAllOrderReturn()
    {
        $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;
        isset($params['shopId']) && $selectParams['shopId'] = $params['shopId'];
        if (isset($this->shopId) && !empty($this->shopId)) $selectParams['shopId'] = $this->shopId;
        $result = $this->objMOrderReturn->getAllOrderOut($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);
        }
    }

    /**
     * 入库列表搜索
     * @throws \Exception
     */
    public function searchAllOrderReturn()
    {
        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $selectParams = [
            'shopId' => isset($params['shopId']) ? $params['shopId'] : '',
            'operatorId' => isset($params['operatorId']) ? $params['operatorId'] : '',
            'auditStatus' => isset($params['auditStatus']) ? $params['auditStatus'] : '',
            'start' => isset($params['start']) ? $params['start'] : '',
            'end' => isset($params['end']) ? $params['end'] : '',
            'search' => isset($params['search']) ? $params['search'] : '',
        ];
        if (isset($this->shopId) && !empty($this->shopId)) $selectParams['shopId'] = $this->shopId;
        $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->objMOrderReturn->searchAllOrderOut($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 getOrderReturn()
    {
        $params['id'] = $this->request->param('request_id');
        if (empty($params['id'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $result = $this->objMOrderReturn->getOrderOut($params);

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

    /**
     * 驳回销售退货单
     */
    public function rejectOrderReturn()
    {
        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $rejectData = [
            'id' => isset($params['id']) ? $params['id'] : '', //退货单id
            'auditFailReason' => getArrayItem($params, 'auditFailReason', ''),// 驳回原因
        ];

        foreach ($rejectData as $key => $value) {
            if (empty($value) && $value !== 0) {
                parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

        $updateData = [
            'auditFailReason' => $rejectData['auditFailReason'],// 驳回原因
            'auditStatus'     => StatusCode::$auditStatus['auditNotPass'], //审核通过
            'updateTime'      => time(),
        ];
        $result = $this->objMOrderReturn->updateOrderReturnData($updateData, $rejectData['id']);
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }
}
