<?php
/**
 * 客户统计部分埋点
 * Created by PhpStorm.
 * User: wxj
 * Date: 2019/11/14
 * Time: 11:58
 */

namespace Jobs\Cache;

use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Factory;

class CustomerCache
{
    private $cache;
    protected $key = '';
    protected $registerSmsCountKey = 'todayCreateOrderCustomer::'; //今日下单用户key
    protected $customerExpire = 10;//过期时间为一天
    protected $allCustomerNumKey = 'allCustomerNum';//全部客户数量key
    protected $lastMonthNewCustomerKey = 'lastMonthNewCustomer';//新增客户数量:最近30天新增,且没有成功付款的客户 key
    protected $interestCustomerKey = 'interestCustomer';//兴趣人群数量:近7天有加购行为,但没有成功付款的客户

    public function __construct()
    {
        $this->key = $this->registerSmsCountKey . date('Ymd', time());
        $this->cache = Factory::cache('user');
    }

    /***************************** 今日下单用户相关 start **********************************/

    /**
     * 下单时调用此方法,添加一条客户下单信息  todayCreateOrderCustomer::20190101:0001:0002:0003
     * @param $enterpriseId
     * @param $provinceCode
     * @param $cityCode
     * @param $districtCode
     * @param $customerId
     * @param $shopId
     * @return bool|ResultWrapper
     */
    public function cacheCustomerOrderInfo($enterpriseId, $provinceCode = null, $cityCode = null, $districtCode = null, $customerId, $shopId = null)
    {
        if (empty($enterpriseId) || empty($customerId)) {
            return false;
        }
        $this->key = $this->key . '::enterprise_' . $enterpriseId;
        $this->cache->zincrby($this->key, 1, $customerId);//存全国
        $this->cache->expire($this->key, $this->customerExpire);

        if (!empty($provinceCode)) {
            $key = $this->key . '::areaCode_' . $provinceCode;
            $this->cache->zincrby($key, 1, $customerId);//存省
            $this->cache->expire($key, $this->customerExpire);
        }

        if (!empty($cityCode)) {
            $key = $this->key . '::areaCode_' . $cityCode;
            $this->cache->zincrby($key, 1, $customerId);//存市
            $this->cache->expire($key, $this->customerExpire);
        }

        if (!empty($districtCode)) {
            $key = $this->key . '::areaCode_' . $districtCode;
            $this->cache->zincrby($key, 1, $customerId);//存区
            $this->cache->expire($key, $this->customerExpire);
        }

        if (!empty($shopId)) {
            $key = $this->key . '::shopId_' . $shopId;
            $this->cache->zincrby($key, 1, $customerId);
            $this->cache->expire($key, $this->customerExpire);
        }
        return true;
    }

    /**
     * 今日下单用户统计,按地区搜索
     * @param $enterpriseId 企业id
     * code不传则查的是全国的客户下单信息
     * @param $code
     * @param $shopId
     * @return array
     */
    public function getTodayCustomerOrderInfo($enterpriseId, $code = null, $shopId = null)
    {
        $key = $this->key . '::enterprise_' . $enterpriseId;
        if (!empty($code)) {
            $key .= '::areaCode_' . $code;
        }
        if (!empty($shopId)) {
            $key .= '::shopId_' . $shopId;
        }
        $result = $this->cache->zrange($key, 0, -1);
        return $result ? $result : [];
    }

    /***************************** 今日下单用户相关 end **********************************/

    /***************************** 全部客户数量相关 start **********************************/

    /**
     * 增加客户数量:每次客户注册调用此方法
     * @param $enterpriseId
     * @return bool
     */
    public function incrCustomerNum($enterpriseId)
    {
        $result = $this->cache->incr($this->allCustomerNumKey . '::' . $enterpriseId);
        return $result ? true : false;
    }

    /**
     * 获取当前客户总数
     * @param $enterpriseId
     * @return bool
     */
    public function getAllCustomerNum($enterpriseId)
    {
        return $this->cache->get($this->allCustomerNumKey . '::' . $enterpriseId);
    }

    /***************************** 全部客户数量相关 end **********************************/


    /***************************** 新增客户数量相关 start **********************************/

    /**
     * 新增客户 customerId  score存时间戳   注册用户时调用此方法
     * @param $customerId
     * @param $enterpriseId
     * @return bool
     */
    public function incrCustomer($customerId, $enterpriseId)
    {
        $result = $this->cache->zadd($this->lastMonthNewCustomerKey . '::' . $enterpriseId, time(), $customerId);
        return $result ? true : false;
    }

    /**
     * 删除所有企业注册超过30天的用户
     * @return bool   true代表有数据被删除  false相反
     */
    public function delCustomerOfOneMonthAgo()
    {
        //获取所有key  ['lastMonthNewCustomer::12','lastMonthNewCustomer::11']
        $keysList = $this->cache->keys($this->lastMonthNewCustomerKey . '*');
        $oneMonthAgoTimestamp = strtotime("-30 day");
        foreach ($keysList as $value) {
            $keyArr = explode('::', $value);
            $result = $this->cache->zremrangebyscore($this->lastMonthNewCustomerKey . '::' . $keyArr[2], 0, $oneMonthAgoTimestamp);
            if (!$result) {
                return false;//没有数据可删除也返回false
            }
        }
        return true;
    }

    /**
     * 客户下单后删除客户信息
     * @param $customerId
     * @param $enterpriseId
     * @return bool
     */
    public function delCustomerAfterPlaceOrder($customerId, $enterpriseId)
    {
        $result = $this->cache->zrem($this->lastMonthNewCustomerKey . '::' . $enterpriseId, $customerId);
        return $result ? true : false;
    }
    /***************************** 新增客户数量相关 end **********************************/

    /***************************** 兴趣人群数量 : 近7天加购行为 start **********************************/
    /**
     * 加入购物车 customerId  score存时间戳   注册用户时调用此方法
     * @param $customerId
     * @param $enterpriseId
     * @return bool
     */
    public function incrInterestCustomer($customerId, $enterpriseId)
    {
        $result = $this->cache->zadd($this->interestCustomerKey . '::' . $enterpriseId, time(), $customerId);
        return $result ? true : false;
    }

    /**
     * 删除所有企业加购超过7天的用户
     * @return bool   true代表有数据被删除  false相反
     */
    public function delInterestCustomerOfSevenDaysAgo()
    {
        //获取所有key  ['lastMonthNewCustomer::12','lastMonthNewCustomer::11']
        $keysList = $this->cache->keys($this->interestCustomerKey . '*');
        $SeveralDaysAgoTimestamp = strtotime("-7 day");
        foreach ($keysList as $value) {
            $keyArr = explode('::', $value);
            $result = $this->cache->zremrangebyscore($this->interestCustomerKey . '::' . $keyArr[2], 0, $SeveralDaysAgoTimestamp);
            if (!$result) {
                return false;//没有数据可删除也返回false
            }
        }
        return true;
    }

    /**
     * 客户成功付款后删除信息
     * @param $customerId
     * @param $enterpriseId
     * @return bool
     */
    public function delInterestCustomerAfterPay($customerId, $enterpriseId)
    {
        $result = $this->cache->zrem($this->interestCustomerKey . '::' . $enterpriseId, $customerId);
        return $result ? true : false;
    }

    /***************************** 近7天加购行为 end **********************************/


}