<?php
/**
 * 客户渠道(类型)模块
 * Created by PhpStorm.
 * User: tpl
 * Date: 2019/10/30
 * Time: 13:54
 */
namespace JinDouYun\Controller\System;

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

use JinDouYun\Controller\BaseController;
use JinDouYun\Model\System\MCustomerSource;

class CustomerSource extends BaseController
{
    private $objMCustomerSource;

    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMCustomerSource = new MCustomerSource($this->onlineEnterpriseId);
    }

    /**
     * 添加和编辑客户渠道(类型)管理公共字段处理方法
     *
     * @return array
     */
    public function commonFieldFilter(){

        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $customerSourceData = [
            'name' => isset($params['name']) ? $params['name'] : '',
            'defaultStatus' => isset($params['defaultStatus']) ? $params['defaultStatus'] : '',
            'enableStatus' => isset($params['enableStatus']) ? $params['enableStatus'] : '',
        ];

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

        $customerSourceData['modelType'] = getArrayItem($params,'modelType',0);
        $customerSourceData['enterpriseId'] = $this->onlineEnterpriseId;
        $customerSourceData['deleteStatus']= StatusCode::$standard;
        $customerSourceData['createTime'] = time();
        $customerSourceData['updateTime'] = time();
        return $customerSourceData;
    }

    /**
     * 添加客户渠道(类型)
     * @throws \Exception
     */
    public function addCustomerSource()
    {
        $customerSourceData = $this->commonFieldFilter();

        $result = $this->objMCustomerSource->addCustomerSource($customerSourceData);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

    }

    /**
     * 获取指定客户渠道(类型)信息
     */
    public function getCustomerSourceInfo()
    {
        $customerSourceId  = $this->request->param('request_id');
        if ( !$customerSourceId ) {
            $this->sendOutput('参数错误', ErrorCode::$paramError );
        }

        $result = $this->objMCustomerSource->getCustomerSourceInfo($customerSourceId);
        if($result->isSuccess()){
            $this->sendOutput($result->getData());
        }else{
            $this->sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 编辑客户渠道(类型)
     */
    public function editCustomerSource()
    {
        $customerSourceId  = $this->request->param('request_id');
        if(empty($customerSourceId)){
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        $customerSourceData = $this->commonFieldFilter();
        $customerSourceData['id'] = $customerSourceId;
        unset($customerSourceData['createTime']);

        $result = $this->objMCustomerSource->editCustomerSource($customerSourceData);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 删除客户渠道(类型)
     */
    public function delCustomerSource()
    {
        $customerSourceId  = $this->request->param('request_id');
        if(!$customerSourceId){
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }

        $result = $this->objMCustomerSource->delCustomerSource($customerSourceId);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 客户渠道(类型)启用和禁用
     */
    public function updateCustomerSourceStatus()
    {
        $params = $this->request->getRawJson();
        if( empty($params['id']) &&  empty($params['enableStatus'])){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $result = $this->objMCustomerSource->updateCustomerSourceStatus($params);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 客户渠道(类型)是否默认
     */
    public function updateDefaultStatus()
    {
        $params = $this->request->getRawJson();
        if( empty($params['id']) &&  empty($params['defaultStatus'])){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $result = $this->objMCustomerSource->updateDefaultStatus($params);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }


    /**
     * 后台所有客户渠道(类型)列表  分页
     */
    public function getAllCustomerSource()
    {
        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $pageParams = pageToOffset($params['page']?:1, $params['pageSize']?:10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];


        $result = $this->objMCustomerSource->getAllCustomerSource($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(), $result->getErrorCode());
        }
    }

    /**
     * 后台所有客户渠道(类型)列表  不分页
     */
    public function getCustomerSourceList()
    {
        $result = $this->objMCustomerSource->getCustomerSourceList();
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
}