<?php

namespace JinDouYun\Controller\Common;


use JinDouYun\Controller\BaseController;
use JinDouYun\Model\System\MReceipt;
use Mall\Framework\Core\ErrorCode;

/**
 * 小票打印
 * Class Receipt
 * @package JinDouYun\Controller\Common
 */
class Receipt extends BaseController
{
    /**
     * @var MReceipt
     */
    private $objMReceipt;

    /**
     * Receipt constructor.
     * @param bool $isCheckAcl
     * @param bool $isMustLogin
     * @param bool $checkToken
     * @param bool $getAreaCode
     */
    public function __construct($isCheckAcl = false, $isMustLogin = true, $checkToken = true, $getAreaCode = false)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken, $getAreaCode);
        $this->objMReceipt = new MReceipt($this->onlineEnterpriseId,$this->onlineUserId);
    }

    /**
     * 公共参数
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if(empty($params)){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }
        $data = [
            'objectId' => isset($params['objectId']) ? $params['objectId'] : null,
            'objectType' => isset($params['objectType']) ? $params['objectType'] : null
        ];
        foreach($data as $key => $value){
            if(empty($value)){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError );
            }
        }
        return $data;
    }

    /**
     * 打印小票
     * @throws \Exception
     */
    public function toPrint()
    {
        $data = self::commonFieldFilter();
        $result = $this->objMReceipt->toPrint($data['objectId'],$data['objectType']);
        if($result->isSuccess()){
            parent::sendOutput('操作成功');
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }
}