<?php
/**
 * 客户沟通记录Controller
 * Created by PhpStorm.
 * User: haoren
 * Date: 2021/02/23
 * Time: 13:00
 */

namespace JinDouYun\Controller\Customer;

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

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Customer\MCustomerCommunication;

class CustomerCommunication extends BaseController
{
    private $objMCustomerCommunication;

    public function __construct($isCheckAcl = false, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMCustomerCommunication = new MCustomerCommunication($this->onlineEnterpriseId,$this->onlineUserId);
    }

    /**
     * 获取参数
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $data = [
            'customerId' => isset($params['customerId']) ? $params['customerId'] : '',
            'staffId' => isset($params['staffId']) ? $params['staffId'] : '',
            'content' => isset($params['content']) ? $params['content'] : '',
        ];

        //必填项
        foreach ($data as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

        //选填项
        $data['time']     = isset($params['time']) && !empty($params['time']) ? $params['time'] : time();
        $data['location'] = isset($params['location']) && !empty($params['location']) ? $params['location'] : '';
        $data['picture']  = isset($params['picture']) && !empty($params['picture']) ? $params['picture'] : '';
        return $data;
    }

    /**
     * 客户沟通记录添加
     */
    public function addCustomerCommunication()
    {
        $data = $this->commonFieldFilter();
        $result = $this->objMCustomerCommunication->addCustomerCommunication($data);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 客户沟通记录删除
     * @throws Exception
     */
    public function delCustomerCommunication()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $result = $this->objMCustomerCommunication->delCustomerCommunication($id);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 客户沟通记录修改
     */
    public function updateCustomerCommunication()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('id参数为空', ErrorCode::$paramError);
        }
        $data = $this->commonFieldFilter();

        $result = $this->objMCustomerCommunication->updateCustomerCommunication($data, $id);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 客户沟通记录列表
     */
    public function getAllCustomerCommunication()
    {
        $params = $this->request->getRawJson();
        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];

        if(isset($params['customerId']) && !empty($params['customerId'])){
            $selectParams['customerId'] = $params['customerId'];
        }

        if(isset($params['staffId']) && !empty($params['staffId'])){
            $selectParams['staffId'] = $params['staffId'];
        }

        if(isset($params['start']) && !empty($params['start'])){
            $selectParams['start'] = $params['start'];
        }

        if(isset($params['end']) && !empty($params['end'])){
            $selectParams['end'] = $params['end'];
        }

        $result = $this->objMCustomerCommunication->getAllCustomerCommunication($selectParams);

        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize'  => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }

    /**
     * 客户沟通记录详情
     */
    public function getCustomerCommunication()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $result = $this->objMCustomerCommunication->getCustomerCommunication(['id' => $id]);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
    
    /**
     * 客户拜访报表
     */
    public function getCustomerCallOnReportForm()
    {
        $params = $this->request->getRawJson();
        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $selectParams['departmentId'] = isset($params['departmentId']) ? $params['departmentId'] :'';
        $selectParams['start'] = isset($params['start']) ? $params['start'] :'';
        $selectParams['end'] = isset($params['end']) ? $params['end'] :'';
        $result = $this->objMCustomerCommunication->getCustomerCallOnReportForm($selectParams);
        
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize'  => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }
    
    /**
     * 客户拜访记录详情
     */
    public function getCustomerVisitInfo()
    {
        $params = $this->request->getRawJson();
        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $selectParams['staffId'] = getArrayItem($params,'staffId');
        
        $result = $this->objMCustomerCommunication->getCustomerVisitInfo($selectParams);
        
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize'  => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }
    
    /**
     * 未拜访客户统计列表
     */
    public function getCustomerNoVisit()
    {
        $params = $this->request->getRawJson();
        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $selectParams['days'] = getArrayItem($params,'intervalDay',0);
        $selectParams['customerName'] = getArrayItem($params,'customerName');//客户id
        $selectParams['staffId'] = getArrayItem($params,'staffId');//员工id
        $selectParams['province'] = getArrayItem($params,'province');
        $selectParams['city'] = getArrayItem($params,'city');
        $selectParams['district'] = getArrayItem($params,'district');
        $result = $this->objMCustomerCommunication->getCustomerNoVisit($selectParams);
        
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize'  => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }
}