<?php
/**
 * 单据打印统计模型
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2020/4/17
 * Time: 16:55
 */

namespace JinDouYun\Model\System;

use JinDouYun\Cache\SystemCache;
use JinDouYun\Controller\Common\Logger;
use JinDouYun\Dao\System\DPrintNum;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;

class MPrintNum
{
    private $onlineUserId;

    private $onlineEnterpriseId;

    private $objDPrintNum;

    /**
     * MPrintNum constructor.
     * @param $enterpriseId
     */
    public function __construct($enterpriseId)
    {
        $this->onlineEnterpriseId = $enterpriseId;
        $this->objDPrintNum = new DPrintNum('default');
    }

    /**
     * @param int $objectNo 单据编号
     * @param int $objectType 单据类型 @var StatusCode::$orderType
     * @return ResultWrapper
     */
    public function printIncr($objectNo, $objectType = 0)
    {
        $dbResult = $this->objDPrintNum->get(['objectNo' => $objectNo, 'enterpriseId' => $this->onlineEnterpriseId]);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDPrintNum->error(), ErrorCode::$dberror);
        }
        if (isset($dbResult['id'])) {
            //增加打印次数
            $dbResult = $this->objDPrintNum->set_inc('printNum', ['id' => $dbResult['id']]);
        } else {
            //从未打印过
            $dbResult = $this->objDPrintNum->insert(
                [
                    'objectNo'     => $objectNo,
                    'printNum'     => 1,
                    'enterpriseId' => $this->onlineEnterpriseId
                ]
            );
        }
        $objSystemCache = new SystemCache();
        $objSystemCache->delObjectPrintNum($this->onlineEnterpriseId,$objectNo);
        return ResultWrapper::success($dbResult);
    }

    /**
     * Doc: (des="获取打印次数")
     * User: XMing
     * Date: 2020/11/14
     * Time: 12:12 下午
     * @param $objectNo
     * @param $objectType
     * @return int
     * @throws \Exception
     */
    public function getObjectPrintNum($objectNo,$objectType = 0): int
    {
        $objSystemCache = new SystemCache();
        $printNum = $objSystemCache->getObjectPrintNum($this->onlineEnterpriseId,$objectNo);
        if ($printNum != false && $printNum != 0){
            return $printNum;
        }
        $printNum = $this->objDPrintNum->get_field('printNum', ['objectNo' => $objectNo, 'enterpriseId' => $this->onlineEnterpriseId]);
        if ($printNum === false || empty($printNum)) {
            return 0;
        }
        $objSystemCache->setObjectPrintNum($this->onlineEnterpriseId,$objectNo,(int)$printNum);
        return (int)$printNum;
    }

    /**
     * Doc: (des="批量获取打印次数")
     * User: XMing
     * Date: 2020/11/14
     * Time: 12:27 下午
     * @param $objectNos
     * @return array
     * @throws \Exception
     */
    public function getObjectPrintNumByNos($objectNos)
    {
        $objSystemCache = new SystemCache();
        $map = [];
        $notNos = [];
        foreach ($objectNos as $no){
            $printNum = $objSystemCache->getObjectPrintNum($this->onlineEnterpriseId,$no);
            if ($printNum != false && $printNum != 0){
                $map[] = [
                    'objectNo' => $no,
                    'printNum' => (int)$printNum,
                ];
                continue;
            }
            $notNos[] = $no;
        }
        if (!empty($notNos)){
            $printNumLists = $this->objDPrintNum->select(['objectNo' => $objectNos, 'enterpriseId' => $this->onlineEnterpriseId],'objectNo,printNum');
            if ($printNumLists === false){
                Logger::logs(E_USER_ERROR,'sql错误',__CLASS__,__LINE__,$this->objDPrintNum->error());
            }else{
                foreach ($printNumLists as $list){
                    $map[] = [
                        'objectNo' => $list['objectNo'],
                        'printNum' => $list['printNum'],
                    ];
                    if ($list['printNum'] != 0){
                        $objSystemCache->setObjectPrintNum($this->onlineEnterpriseId,$list['objectNo'],(int)$list['printNum']);
                    }
                }
            }
        }
        return $map;
    }
}