<?php
/**
 * 优惠券记录
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2019/12/13
 * Time: 16:27
 */

namespace JinDouYun\Controller\Market;

use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Market\MUserCoupon;
use Mall\Framework\Core\ErrorCode;

class UserCoupon extends BaseController
{
    private $objMUserCoupon;

    /**
     * Coupon constructor.
     * @param bool $isCheckAcl
     * @param bool $isMustLogin
     * @throws \Exception
     */
    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMUserCoupon = new MUserCoupon($this->onlineUserId, $this->onlineEnterpriseId);
    }

    /**
     * 列表
     */
    public function getAll()
    {
        $params['page'] = $this->request->param('page') ?: 1;
        $params['pageSize'] = $this->request->param('pageSize') ?: 10;
        $params['startTime'] = $this->request->param('startTime');
        $params['endTime'] = $this->request->param('endTime');
        $params['isUse'] = $this->request->param('isUse');
        $params['keyword'] = $this->request->param('keyword');
        $params['customerId'] = $this->request->param('customerId');

        $orderData = $this->objMUserCoupon->getAll($params);
        if ($orderData->isSuccess()) {
            $returnData = $orderData->getData();
            $pageData = [
                'pageIndex'      => $params['page'],
                'pageSize'       => $params['pageSize'],
                'pageTotal'      => $returnData['total'],
                'useTotal'       => $returnData['useTotal'],
                'reducePrice'    => $returnData['reducePrice'],
                'useReducePrice' => $returnData['useReducePrice'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }
        parent::sendOutput($orderData->getData(), ErrorCode::$dberror);
    }

    /**
     * 搜索
     */
    public function search()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $selectParams = [
            'name'       => isset($params['name']) ? $params['name'] : '',//优惠券名称
            'isUse'      => isset($params['isUse']) ? $params['isUse'] : '',//是否使用
            'start'      => isset($params['start']) ? $params['start'] : '',
            'end'        => isset($params['end']) ? $params['end'] : '',
            'customerId' => isset($params['customerId']) ? $params['customerId'] : '',//客户id
        ];

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

        $result = $this->objMUserCoupon->search($selectParams);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize'  => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }
//测试用
//    public function test()
//    {
//        $this->objMUserCoupon->availableCoupon();
//    }
}