<?php
/**
 * Created by PhpStorm.
 * User: phperstar
 * Date: 2020/6/17
 * Time: 4:07 PM
 */

namespace JinDouYun\Controller\Common;

use Jindouyun\Cache\EnterpriseCache;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Factory;

use JinDouYun\Controller\BaseController;

use Util\CloudPrint\Gainshca;
use Util\CloudPrint\Feie;


class CloudPrint extends BaseController
{
    private $objEnterpriseCache;

    public function __construct($isCheckAcl = false, $isMustLogin = false, $checkToken = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken);
    }

    /**
     * 佳博打印机添加设备
     */
    public function adddev()
    {
        //$gainshcaConfigData = Factory::config()->getAppoint('cloudPrint','gainshca');
        $gainshcaConfigData = self::getConfig();
        if(empty($gainshcaConfigData)){
            $this->sendOutput('小程序配置项错误',ErrorCode::$configEroor);
        }
        $objGainshca = new Gainshca($gainshcaConfigData['memberCode'], $gainshcaConfigData['deviceID'], $gainshcaConfigData['apiKey']);
        $result = $objGainshca->adddev();
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 由于此方法在model层调用，所以传递一个企业id
     * @param $msgDetail
     * @param $onlineEnterpriseId
     */
    public function sendMsg($msgDetail,$onlineEnterpriseId)
    {
        $this->onlineEnterpriseId = $onlineEnterpriseId;
        //$gainshcaConfigData = Factory::config()->getAppoint('cloudPrint','gainshca');
        $gainshcaConfigData = self::getConfig();
        if(empty($gainshcaConfigData)){
            $this->sendOutput('小程序配置项错误',ErrorCode::$configEroor);
        }
        $objGainshca = new Gainshca($gainshcaConfigData['memberCode'], $gainshcaConfigData['deviceID'], $gainshcaConfigData['apiKey']);
        $result = $objGainshca->sendMsg($msgDetail);
        if ($result->isSuccess()) {
            return ResultWrapper::success('success');
            //parent::sendOutput($result->getData());
        } else {
            return ResultWrapper::fail($result->getData(),$result->getErrorCode());
            //parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 查询打印机设备
     * return deviceID设备id  online在线状态(0离线1在线) status状态(1正常 2缺纸 3其他异常 4过热 5开盖 8暂停 9打印中) outtime最后一次离线时间 printnum累计打印次数
     * @param bool $deviceID //打印机id
     * @param bool $selectAll //是否查询所有打印机
     */
    public function getStatus($deviceID = false, $selectAll = false)
    {
        //$gainshcaConfigData = Factory::config()->getAppoint('cloudPrint','gainshca');
        $gainshcaConfigData = self::getConfig();
        if(empty($gainshcaConfigData)){
            $this->sendOutput('小程序配置项错误',ErrorCode::$configEroor);
        }
        $objGainshca = new Gainshca($gainshcaConfigData['memberCode'], $gainshcaConfigData['deviceID'], $gainshcaConfigData['apiKey']);

        $result = $objGainshca->getStatus($deviceID, $selectAll);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 设置打印机音量
     * @param $num
     */
    public function setVolume($num)
    {
        //$gainshcaConfigData = Factory::config()->getAppoint('cloudPrint','gainshca');
        $gainshcaConfigData = self::getConfig();
        if(empty($gainshcaConfigData)){
            $this->sendOutput('小程序配置项错误',ErrorCode::$configEroor);
        }
        $objGainshca = new Gainshca($gainshcaConfigData['memberCode'], $gainshcaConfigData['deviceID'], $gainshcaConfigData['apiKey']);

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

    /**
     * 获取打印机配置项
     * @return array
     */
    public function getConfig()
    {
        $objEnterpriseCache = new EnterpriseCache();
        $cache = $objEnterpriseCache->getEnterpriseSetting($this->onlineEnterpriseId);
        if (empty($cache)){
            return [];
        }
        return [
            'memberCode' => isset($cache['memberCode']) ? $cache['memberCode'] : '',
            'deviceID'   => isset($cache['deviceID']) ? $cache['deviceID'] : '',
            'apiKey'     => isset($cache['apiKey']) ? $cache['apiKey'] : '',
        ];

    }




    /**************************************飞鹅打印机 *******************************************/

    /**
     * 添加打印机
     */
    public function addFeieDev()
    {
        $objFeie = new Feie('930534909', '8gh4jctw');
        $result = $objFeie->adddev();
        if(!$result->isSuccess()){
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 发送打印机信息
     * @param $params
     */
    public function feieSendMsg($params)
    {
        $objFeie = new Feie('930534909', '8gh4jctw');
        $result = $objFeie->sendMsg($params);
        if(!$result->isSuccess()){
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

    }

}