<?php


namespace JinDouYun\Controller\Shop;


use JinDouYun\Controller\BaseController;
use JinDouYun\Dao\BaseDao;
use JinDouYun\Dao\Customer\DCustomerCardNum;
use JinDouYun\Model\Customer\MCustomer;
use JinDouYun\Model\Customer\MCustomerCard;
use JinDouYun\Model\Shop\MShopCard;
use JinDouYun\Model\Shop\MShopCardBind;
use JinDouYun\Model\Shop\MShopCardOrder;
use JinDouYun\Model\Shop\MShopProject;
use JinDouYun\Model\Shop\MShopRostering;
use JinDouYun\Model\Shop\MShopSubscribe;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\Ding;
use Mall\Framework\Factory;

class ApiShopCard extends BaseController
{

    private $obj;
    private $objCardBind;
    private $objCardOrder;

    public function __construct($isCheckAcl = true, $isMustLogin = false, $checkToken = true, $getAreaCode = false, $checkShopToken = true, $checkSupplierToken = false)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken, $getAreaCode, $checkShopToken, $checkSupplierToken);
        $authorization = $this->request->getServerParam('HTTP_AUTHORIZATION');
        if (!empty($authorization)) {
            self::getUserIdByAuthorization();
        }
        $this->obj = new MShopCard($this->onlineEnterpriseId, $this->onlineUserId);
        $this->objCardBind = new MShopCardBind($this->onlineEnterpriseId);
        $this->objCustomer = new MCustomer($this->onlineEnterpriseId, $this->onlineUserId);
        $this->objCardOrder = new MShopCardOrder($this->onlineEnterpriseId, $this->onlineUserId);
        $this->objProject = new MShopProject($this->onlineEnterpriseId, $this->onlineUserId);
        $this->objCardNum = new DCustomerCardNum('default');
    }


    /**
     * 购买会员卡
     * @return void
     */
    public function buy_card()
    {
        $id = $this->request->param('id');
        $pay_type = $this->request->param('pay_type');
        $source = $this->request->param('source');
        $card = $this->obj->details(['id' => $id]);
        if (empty($this->onlineUserId)) parent::sendOutput('未登录', ErrorCode::$dberror);
        if (!$card->isSuccess()) parent::sendOutput('该优惠卡不存在', ErrorCode::$dberror);
        $card = $card->getData();

        $base = new BaseDao();
        $base->beginTransaction();

        $customer = $this->objCustomer->get(['userCenterId' => $this->onlineUserId]);

        $orderInsert = [
            'enterprise_id' => $this->onlineEnterpriseId,
            'card_id' => $card['id'],
            'customer_id' => $customer['id'],
            'order_id' => 'car'.$customer['id'].time().$this->getRandPass(),
            'ot_price' => $card['ot_price'],
            'pay_price' => $card['price'],
            'pay_type' => $pay_type,
        ];
        $order = $this->objCardOrder->insert($orderInsert);
        if (!$order->isSuccess()){
            parent::sendOutput('订单生成失败', ErrorCode::$dberror);
        }

        if ($pay_type == 1){
            //余额支付
            $res = $this->objCardOrder->yuePay($orderInsert);
            if (!$res->isSuccess()){
                parent::sendOutput($res->getData(), ErrorCode::$dberror);
            }
            $base->commit();
            parent::sendOutput(['status' => 'complete', 'data' => '支付成功']);
        }elseif ($pay_type == 2){
            //微信支付
            $res = $this->objCardOrder->wxPay($orderInsert, $this->request->get_onlineip(), $source);
            $base->commit();
            parent::sendOutput(['status' => 'weixinPay', 'data'  => $res->getData()]);
        }elseif ($pay_type == 3){
            //支付宝支付
            parent::sendOutput('支付方式错误', ErrorCode::$dberror);
        }
        $base->rollBack();
        parent::sendOutput('支付方式错误', ErrorCode::$dberror);

    }


    /**用户优惠卡订单
     * @return void
     */
    public function card_order()
    {
        $uid = $this->onlineUserId;
        if (empty($uid))  parent::sendOutput('未登录', 1002);

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

        $customer = $this->objCustomer->get(['userCenterId' => $uid]);
        $selectParams['customer_id'] = $customer['id'];
        $selectParams['paid'] = 1;

        $list = $this->objCardOrder->list($selectParams)->getData();

        $pageData = [
            'pageIndex' => $params['page'],
            'pageSize' => $params['pageSize'],
            'pageTotal' => $list['total'],
        ];
        parent::sendOutput($list['data'], 0, $pageData);
    }


    /**
     * 用户拥有优惠券列表
     * @return void
     */
    public function userCard()
    {
        $params = $this->request->getRawJson();
        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $limit = $pageParams['limit'];
        $offset = $pageParams['offset'];
        $uid = $this->onlineUserId;
        if (empty($uid))  parent::sendOutput('未登录', 1002);

        $customer = $this->objCustomer->get(['userCenterId' => $uid]);
        $selectParams['customer_id'] = $customer['id'];
        $selectParams['enterprise_id'] = $this->onlineEnterpriseId;

        $list = $this->objCardNum->select($selectParams, '*', 'id DESC', $limit, $offset);
        $count = $this->objCardNum->count($selectParams);
        foreach ($list as &$item){
            $project = $this->objProject->details(['id' => $item['project_id']])->getData();
            $item['name'] = $project['name'];
            $item['image'] = $project['image'];
        }
        $pageData = [
            'pageIndex' => $params['page'],
            'pageSize' => $params['pageSize'],
            'pageTotal' => $count,
        ];
        parent::sendOutput($list, 0, $pageData);
    }


}
