<?php
/**
* Created by PhpStorm.
* User: kang
* Date: 2021/3/10
* Time: 10:55
*/

namespace JinDouYun\Controller\Customer;

use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Customer\MCustomerVisitsLog;

class CustomerVisitsLog extends BaseController
{
    private $objMCustomerVisitsLog;
    
    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMCustomerVisitsLog = new MCustomerVisitsLog($this->onlineEnterpriseId, $this->onlineUserId);
    }
    
    /**
     * 添加和编辑客户指定访问记录公共字段处理方法
     *
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }
        $visitsLogData = [
            'usercenterid'      => getArrayItem($params,'usercenterid'),
            'customerid'       => getArrayItem($params,'customerid'),
            'shopid'       => getArrayItem($params,'shopid'),
            'goodsid'       => getArrayItem($params,'goodsid'),
        ];
        $visitsLogData['updateTime'] = time();
        foreach($visitsLogData as $key => $value){
            if(empty($value) && $value !== 0){
                $this->sendOutput($key.'参数错误', ErrorCode::$paramError );
            }
        }
        return $visitsLogData;
    }
    
    /**
     * 获取指定客户浏览记录
     */
    public function getCustomerVisitsLogInfo()
    {
        $customerVisitsLogId  = $this->request->param('request_id');
        if ( !$customerVisitsLogId ) {
            $this->sendOutput('参数错误', ErrorCode::$paramError );
        }
        $result = $this->objMCustomerVisitsLog->getCustomerVisitsLogInfo($customerVisitsLogId);
        if($result->isSuccess()){
            $this->sendOutput($result->getData());
        }else{
            $this->sendOutput($result->getData(), $result->getErrorCode());
        }
    }
    
    /**
     * 获取所有的客户浏览记录
     */
    public function getAllCustomerVisitsLog()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $params['limit'] = $pageParams['limit'];
        $params['offset'] = $pageParams['offset'];
        
        $returnData = $this->objMCustomerVisitsLog->getAllCustomerVisitsLog($params);
        if ($returnData->isSuccess()) {
            $returnData = $returnData->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize'  => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($returnData->getData(), ErrorCode::$dberror);
        }
    }
}