<?php
/**
 * Created by PhpStorm.
 * User: kang
 * Date: 2021/3/8
 * Time: 16:29
 */
namespace JinDouYun\Controller\Customer;

use JinDouYun\Controller\Market\VipCard;
use JinDouYun\Model\Customer\MCustomer;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Customer\MCustomerDemand;

class CustomerDemand extends BaseController
{
    private $objMCustomerDemand;

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

    /**
     * 编辑客户需求提报管理公共字段处理方法
     *
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
    
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $demandData = [
                'enterpriseId' => $this->onlineEnterpriseId,
                'customerId'      => getArrayItem($params,'customerId'),
                'demand'       => getArrayItem($params,'demand'),
            ];
        $demandData['updateTime'] = time();
        foreach ($demandData as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        return $demandData;
    }
    
    /**
     * 获取所有的客户需求提报
     */
    public function getAllCustomerdemand()
    {
        $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->objMCustomerDemand->getAllCustomerdemand($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);
        }
    }
    
    /**
     * 获取指定的客户需求提报
     */
    public function getCustomerdemandInfo()
    {
        $customerdemandId = $this->request->param('request_id');
        if (!$customerdemandId) {
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        
        $result = $this->objMCustomerDemand->getCustomerdemandInfo($customerdemandId);
        
        if ($result->isSuccess()) {
            $this->sendOutput($result->getData());
        } else {
            $this->sendOutput($result->getData(), $result->getErrorCode());
        }
    }
    
    
}