<?php
/**
 * 首付款暂存
 * Created by PhpStorm.
 * User: wxj
 * Date: 2019/11/5
 * Time: 10:14
 */

namespace JinDouYun\Cache;

use http\Exception;
use Mall\Framework\Factory;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\ErrorCode;

class FinanceCache
{
    private $cache;
    protected $receivedReceiptTemporarySaveKey = 'receivedReceiptTemporarySave';//收款单暂存
    protected $paidReceiptTemporarySaveKey = 'paidReceiptTemporarySave';//付款单暂存
    protected $accountTransferTemporarySaveKey = 'accountTransferTemporarySave';//资金转账单暂存
    protected $salesOutReceiveKey = 'salesOutReceive';//销售出库生成应收,缓存应收单的id
    private $existPayEnterpriseKey = 'existPayEnterprise'; // 存在应付单的企业标识

    public function __construct()
    {
        $this->cache = Factory::cache('finance');
    }

    /**
     * 收款单暂存
     * @param $enterpriseId
     * @param $userCenterId
     * @param $data
     * @return ResultWrapper
     */
    public function saveReceivedReceipt($enterpriseId, $userCenterId, $data)
    {
        if( empty($data) ){
            return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
        }
        return $this->cache->hset($this->receivedReceiptTemporarySaveKey.'::'.$enterpriseId, $userCenterId, json_encode($data));
    }

    /**
     * 获取客户的收款单暂存数据
     * @param $enterpriseId
     * @param $userCenterId
     * @return boolean
     */
    public function getReceivedReceipt($enterpriseId, $userCenterId)
    {
        $receivedData = $this->cache->hget($this->receivedReceiptTemporarySaveKey.'::'.$enterpriseId, $userCenterId);
        return $receivedData ? json_decode($receivedData,true) : [];
    }

    /**
     * 删除客户的收款单暂存数据
     * @param $enterpriseId
     * @param $userCenterId
     */
    public function delReceivedReceipt($enterpriseId,$userCenterId)
    {
        $result =  $this->cache->hdel($this->receivedReceiptTemporarySaveKey.'::'.$enterpriseId, $userCenterId);
    }


    /**
     * 付款单暂存
     * @param $enterpriseId
     * @param $userCenterId
     * @param $data
     * @return ResultWrapper
     */
    public function savePaidReceipt($enterpriseId, $userCenterId, $data)
    {
        if( empty($data) ){
            return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
        }
        return $this->cache->hset($this->paidReceiptTemporarySaveKey.'::'.$enterpriseId, $userCenterId, json_encode($data));
    }

    /**
     * 获取客户的付款单暂存数据
     * @param $enterpriseId
     * @param $userCenterId
     * @return boolean
     */
    public function getPaidReceipt($enterpriseId, $userCenterId)
    {
        $paidData = $this->cache->hget($this->paidReceiptTemporarySaveKey.'::'.$enterpriseId, $userCenterId);
        return $paidData ? json_decode($paidData,true) : [];
    }

    /**
     * 删除客户的付款单暂存数据
     * @param $enterpriseId
     * @param $userCenterId
     */
    public function delPaidReceipt($enterpriseId,$userCenterId)
    {
        $result =  $this->cache->hdel($this->paidReceiptTemporarySaveKey.'::'.$enterpriseId, $userCenterId);
    }

    /**
     * 资金转账单暂存
     * @param $enterpriseId
     * @param $userCenterId
     * @param $data
     * @return ResultWrapper
     */
    public function saveAccountTransfer($enterpriseId, $userCenterId, $data)
    {
        if( empty($data) ){
            return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
        }
        return $this->cache->hset($this->accountTransferTemporarySaveKey.'::'.$enterpriseId, $userCenterId, json_encode($data));
    }

    /**
     * 获取资金转账单暂存数据
     * @param $enterpriseId
     * @param $userCenterId
     * @return boolean
     */
    public function getAccountTransfer($enterpriseId, $userCenterId)
    {
        $data = $this->cache->hget($this->accountTransferTemporarySaveKey.'::'.$enterpriseId, $userCenterId);
        return $data ? json_decode($data,true) : [];
    }

    /**
     * 删除资金转账单 暂存数据
     * @param $enterpriseId
     * @param $userCenterId
     */
    public function delAccountTransfer($enterpriseId,$userCenterId)
    {
        $result =  $this->cache->hdel($this->accountTransferTemporarySaveKey.'::'.$enterpriseId, $userCenterId);
    }

    /**
     * 销售出库生成应收单,应收单自动审核  缓存应收单id和时间
     * @param $enterpriseId
     * @param $id
     * @param $createTime
     * @return array|mixed
     */
    public function saveSalesOutReceive($enterpriseId, $id, $createTime)
    {
        $this->cache->zadd($this->salesOutReceiveKey.'::'.$enterpriseId, $createTime, $id);
    }

    public function delSalesOutReceive($enterpriseId, $id)
    {
        $this->cache->zrem($this->salesOutReceiveKey.'::'.$enterpriseId, $id);
    }

    public function getAllSalesOutReceive() {
        $data = [];
        $keysList = $this->cache->keys($this->salesOutReceiveKey.'*');
        foreach ($keysList as $value) {
            $keyArr = explode('::', $value);
            $result = $this->cache->zrangebyscore($this->salesOutReceiveKey.'::'.$keyArr[2], 0,  time(),array('withscores' => TRUE));
            if($result) {
                foreach ($result as $k => $v) {
                    $data[$keyArr[2]][] = [
                        'id'=> $k,
                        'createTime'=>(int)$v
                    ];
                }
            }
        }
        return $data;
    }

    /**
     * 缓存一下那个企业有应付单生成，便于计划任务进行自动切换企业，做自动审核
     */
    public function cacheExistPayEnterprise($enterpriseId)
    {
        return $this->cache->sadd($this->existPayEnterpriseKey, $enterpriseId);
    }

    /**
     * 获取一下有应付单的企业数据
     * @return array|boolean
     */
    public function getExistPayEnterprise()
    {
        return $this->cache->smembers($this->existPayEnterpriseKey);
    }

    /**
     * 把企业从缓存中删除
     */
    public function delExistPayEnterprise($enterpriseId)
    {
        return $this->cache->srem($this->existPayEnterpriseKey, $enterpriseId);
    }
}