<?php

namespace Admin\Controller;

use User\Api\UserApi as UserApi;
use Base\Service\PaymentMerchantService;
use Base\Service\PaymentRuleService;
use Base\Service\PromoteCompanyService;
use Base\Service\GameService;
use Think\Model;

/**
 * 支付/付款商户
 */
class PaymentMerchantController extends ThinkController
{
    public function list()
    {
        $page = I('p', 1);
        $row = I('row', 10);
        $name = I('name', '');
        $mainName = I('main_name', '');
        $adminId = I('admin_id', 0);
        $channel = I('channel', 0);
        $account = I('account', '');
        $way = I('way', 0);
        $status = I('status', -1);
        $startedAt = I('started_at', '1970-01-01');
        $endedAt = I('ended_at', '9999-01-01');

        $conditions = [];
        if ($name != '') {
            $conditions['name'] = ['like', '%' . $name . '%'];
        }
        if ($mainName != '') {
            $conditions['main_name'] = ['like', '%' . $mainName . '%'];
        }
        if ($account != '') {
            $conditions['account'] = ['like', '%' . $account . '%'];
        }
        if ($adminId != 0) {
            $conditions['admin_id'] = $adminId;
        }
        if ($channel != 0) {
            $conditions['channel'] = $channel;
        }
        if ($way != 0) {
            $conditions['_string'] = 'ways & ' . $way . '=' . $way;
        }
        if ($status != -1) {
            $conditions['status'] = $status;
        }
        
        $conditions['update_time'] = ['between', [strtotime($startedAt . ' 00:00:00'), strtotime($endedAt . ' 23:59:59')]];

        $paymentMerchantService = new PaymentMerchantService();

        $query = M('payment_merchant', 'tab_')->where($conditions);
        $countQuery = clone $query;

        $items = $query->order('id desc')->page($page, $row)->select();
        $count = $countQuery->count();

        $admins = M('ucenter_member', 'sys_')->field(['id', 'username'])->select();
        $admins = index_by_column('id', $admins);

        $records = [];
        foreach ($items as $item) {
            $records[] = [
                'id' => $item['id'],
                'name' => $item['name'],
                'main_name' => $item['main_name'],
                'account' => $item['account'],
                'status' => $item['status'],
                'channel' => $item['channel'],
                'wayNames' => implode('、', $paymentMerchantService->getWaysName($item['ways'])),
                'status_text' => $paymentMerchantService->getStatusText($item['status']),
                'channel_text' => $paymentMerchantService->getChannelText($item['channel']),
                'admin_username' => $admins[$item['admin_id']]['username'],
                'update_time' => date('Y-m-d H:i:s', $item['update_time']),
            ];
        }

        
        $page = set_pagination($count, $row);
        if($page) {
            $this->assign('_page', $page);
        }

        $aliDefaultMerchant = $paymentMerchantService->getDefault(PaymentMerchantService::WAY_ALIPAY);
        $weixinDefaultMerchant = $paymentMerchantService->getDefault(PaymentMerchantService::WAY_WEIXIN);
        $expressDefaultMerchant = $paymentMerchantService->getDefault(PaymentMerchantService::WAY_EXPRESS);
        $this->assign('aliDefaultMerchant', $aliDefaultMerchant);
        $this->assign('weixinDefaultMerchant', $weixinDefaultMerchant);
        $this->assign('expressDefaultMerchant', $expressDefaultMerchant);
        $this->assign('records', $records);
        $this->assign('admins', $admins);
        $this->assign('ways', PaymentMerchantService::$ways);
        $this->assign('statusList', PaymentMerchantService::$statusList);
        $this->assign('channels', PaymentMerchantService::$channels);
        $this->display();
    }

    public function add()
    {
        $this->meta_title = '添加收款商户';
        $this->assign('channels', PaymentMerchantService::$channels);
        $this->display('form');
    }

    public function changeStatus()
    {
        $id = I('id', 0);
        $status = I('status', 0);
        $merchant = M('payment_merchant', 'tab_')->where(['id' => $id])->find();
        if (is_null($merchant)) {
            $this->ajaxReturn([
                'status' => 0,
                'message' => '记录不存在',
            ]);
        }
        if (!in_array($status, [0, 1])) {
            $this->ajaxReturn([
                'status' => 0,
                'message' => '状态值错误',
            ]);
        }
        $statusText = $status == 0 ? '禁用' : '启用';
        M('payment_merchant', 'tab_')->where(['id' => $id])->save(['status' => $status]);

        addOperationLog([
            'op_type' => 1,
            'key' => $id,
            'op_name' => '修改商户状态',
            'url' => U('PaymentMerchant/changeStatus'),
            'menu' => '系统-扩展工具-支付收款商户-修改商户状态',
            'content' => json_encode(['status' => $status]),
        ]);

        $this->ajaxReturn([
            'status' => 1,
            'message' => $statusText . '成功',
        ]);
    }

    public function edit()
    {
        $this->meta_title = '编辑收款商户';
        $id = I('id', 0);
        $merchant = M('payment_merchant', 'tab_')->where(['id' => $id])->find();
        if ($merchant == null) {
            $this->error('收款商户不存在');
        }
        $paymentMerchantService = new PaymentMerchantService();
        $config = $merchant['config'] ? json_decode($merchant['config'], true) : null;
        $ways = $paymentMerchantService->getWaysRow($merchant['ways']);
        $this->assign('channels', PaymentMerchantService::$channels);
        $this->assign('record', $merchant);
        $this->assign('config', $config);
        $this->assign('ways', $ways);
        $this->display('form');
    }

    public function save()
    {
        $id = I('id', 0);
        $name = I('name', '');
        $identifier = I('identifier', '');
        $channel = I('channel', 0);
        $status = I('status', 0);
        $account = I('account', '');
        $config = I('config', []);
        $ways = I('ways', []);
        $mainName = I('main_name', '');

        if ($name == '') {
            $this->ajaxReturn([
                'status' => 0,
                'message' => '请输入支付配置名称',
            ]);
        }
        if ($mainName == '') {
            $this->ajaxReturn([
                'status' => 0,
                'message' => '请输入商户商户主体',
            ]);
        }
        if ($account == '') {
            $this->ajaxReturn([
                'status' => 0,
                'message' => '请输入商户账号',
            ]);
        }

        $merchant = null;
        if ($id > 0) {
            $merchant = M('payment_merchant', 'tab_')->where(['id' => $id])->find();
            if ($merchant == null) {
                $this->ajaxReturn([
                    'status' => 0,
                    'message' => '支付商户不存在',
                ]);
            }
        }
        $paymentMerchantService = new PaymentMerchantService();
        $waysValue = $paymentMerchantService->getWaysValue($ways);

        $userAuth = session('user_auth');

        $data = [];
        $data['name'] = $name;
        $data['account'] = $account;
        $data['type'] = 1;
        $data['status'] = $status;
        $data['ways'] = $waysValue;
        $data['admin_id'] = $userAuth['uid'];
        $data['config'] = json_encode($config);
        $data['update_time'] = time();
        $data['main_name'] = $mainName;
        if ($id == 0) {
            $data['create_time'] = time();
            $data['channel'] = $channel;
            $data['identifier'] = $paymentMerchantService->getIdentifierByConfig($config, $channel);
            M('payment_merchant', 'tab_')->add($data);

            addOperationLog([
                'op_type' => 0,
                'key' => $id,
                'op_name' => '添加商户',
                'url' => U('PaymentMerchant/save'),
                'menu' => '系统-扩展工具-支付收款商户-添加商户',
                'content' => json_encode($data),
            ]);

        } else {
            $data['identifier'] = $paymentMerchantService->getIdentifierByConfig($config, $merchant['channel']);
            M('payment_merchant', 'tab_')->where(['id' => $id])->save($data);

            addOperationLog([
                'op_type' => 1,
                'key' => $id,
                'op_name' => '修改商户',
                'url' => U('PaymentMerchant/save'),
                'menu' => '系统-扩展工具-支付收款商户-修改商户',
                'content' => json_encode($data),
            ]);
        }

        $this->ajaxReturn([
            'status' => 1,
            'message' => '保存成功'
        ]);
    }



    public function delete()
    {
        $id = I('id', 0);

        $merchant = M('payment_merchant', 'tab_')->where(['id' => $id])->find();
        if ($merchant === null) {
            $this->ajaxReturn([
                'status' => 0,
                'message' => '记录不存在'
            ]);
        }

        M('payment_merchant', 'tab_')->where(['id' => $id])->delete();
        addOperationLog([
            'op_type' => 2,
            'key' => $id,
            'op_name' => '删除商户',
            'url' => U('PaymentMerchant/delete'),
            'menu' => '系统-扩展工具-支付收款商户-删除商户',
            'content' => json_encode($merchant),
        ]);

        $this->ajaxReturn([
            'status' => 1,
            'message' => '删除成功'
        ]);
    }

    public function editDefault()
    {
        $paymentMerchantService = new PaymentMerchantService();
        $aliMerchants = $paymentMerchantService->getMerchantsByWay(PaymentMerchantService::WAY_ALIPAY);
        $weixinMerchants = $paymentMerchantService->getMerchantsByWay(PaymentMerchantService::WAY_WEIXIN);
        $expressMerchants = $paymentMerchantService->getMerchantsByWay(PaymentMerchantService::WAY_EXPRESS);
        $aliDefaultMerchant = $paymentMerchantService->getDefault(PaymentMerchantService::WAY_ALIPAY);
        $weixinDefaultMerchant = $paymentMerchantService->getDefault(PaymentMerchantService::WAY_WEIXIN);
        $expressDefaultMerchant = $paymentMerchantService->getDefault(PaymentMerchantService::WAY_EXPRESS);

        $this->assign('aliDefaultMerchant', $aliDefaultMerchant);
        $this->assign('weixinDefaultMerchant', $weixinDefaultMerchant);
        $this->assign('expressDefaultMerchant', $expressDefaultMerchant);
        $this->assign('aliMerchants', $aliMerchants);
        $this->assign('weixinMerchants', $weixinMerchants);
        $this->assign('expressMerchants', $expressMerchants);
        $this->display('defaultForm');
    }

    public function saveDefault()
    {
        $setting = I('setting', []);
        
        $paymentMerchantService = new PaymentMerchantService();

        $model = new Model();
        $model->startTrans();
        
        try {
            foreach ($setting as $key => $value) {
                if ($key == 'alipay') {
                    $paymentMerchantService->setDefault(PaymentMerchantService::WAY_ALIPAY, $value);
                } elseif ($key == 'weixin') {
                    $paymentMerchantService->setDefault(PaymentMerchantService::WAY_WEIXIN, $value);
                } elseif ($key == 'express') {
                    $paymentMerchantService->setDefault(PaymentMerchantService::WAY_EXPRESS, $value);
                }
            }
            $model->commit();

            addOperationLog([
                'op_type' => 1,
                'key' => implode(',', array_values($setting)),
                'op_name' => '设置默认商户',
                'url' => U('PaymentMerchant/saveDefault'),
                'menu' => '系统-扩展工具-支付收款商户-设置默认商户',
                'content' => json_encode($setting),
            ]);

            $this->ajaxReturn([
                'status' => 1,
                'message' => '设置成功'
            ]);
        } catch (\Exception $e) {
            $model->rollback();

            $this->ajaxReturn([
                'status' => 0,
                'message' => '设置失败,请联系管理员'
            ]);
        }
    }

    private function getEffectiveTimeRange($startedAt, $endedAt)
    {
        $start = 0;
        $end = PaymentRuleService::FOREVER_TIME;
        if ($startedAt != '') {
            $start = strtotime($startedAt . ' 00:00:00');
        }
        if ($endedAt != '') {
            $end = strtotime($startedAt . ' 23:59:59');
        }
        return [$start, $end];
    }

    public function rules()
    {
        $page = I('p', 1);
        $row = I('row', 10);
        $companyBelong = I('company_belong', -1);
        $companyId = I('company_id', 0);
        $gameTypeId = I('game_type_id', 0);
        $gameId = I('game_id', 0);
        $startedAt = I('started_at', '');
        $endedAt = I('ended_at', '');
        $alipayId = I('alipay_merchant_id', 0);
        $weixinId = I('weixin_merchant_id', 0);
        $expressId = I('express_merchant_id', 0);

        $conditions = [];
        if ($companyBelong != -1) {
            $conditions['company_belong'] = $companyBelong;
        }
        if ($companyId != 0) {
            $conditions['company_id'] = $companyId;
        }
        if ($gameTypeId != 0) {
            $conditions['game_type_id'] = $gameTypeId;
        }
        if ($gameId != 0) {
            $conditions['game_id'] = $gameId;
        }
        if ($alipayId != 0) {
            $conditions['alipay_merchant_id'] = $alipayId;
        }
        if ($weixinId != 0) {
            $conditions['weixin_merchant_id'] = $weixinId;
        }
        if ($expressId != 0) {
            $conditions['express_merchant_id'] = $expressId;
        }

        [$start, $end] = $this->getEffectiveTimeRange($startedAt, $endedAt);
        // var_dump($start, $end);die();
        $timeCondition = ' ((start_time >= ' . $start . ' AND start_time <= ' . $end . ') OR (start_time <= ' . $start . ' AND end_time >= ' . $end 
            . ') OR (end_time >= ' . $start . ' AND end_time <= ' . $end . '))';
        if (isset($conditions['_string'])) {
            $conditions['_string'] .= $timeCondition;
        } else {
            $conditions['_string'] = $timeCondition;
        }
        

        $paymentRuleService = new PaymentRuleService();
        $gameService = new GameService();
        $paymentMerchantService = new PaymentMerchantService();
        $companyService = new PromoteCompanyService();

        $query = M('payment_rule', 'tab_')->where($conditions);
        $countQuery = clone $query;

        $items = $query->order('id desc')->page($page, $row)->select();
        $count = $countQuery->count();
        
        $gameTypes = $gameService->getGameTypes(null, 'id,type_name');
        $games = $gameService->getBaseGames(null, 'id,name');
        $companies = $companyService->getCompanies(null, 'id,company_name');
        $companyTypes = PromoteCompanyService::$belongs;
        $merchantIds = array_merge(
            array_column($items, 'alipay_merchant_id'),
            array_column($items, 'weixin_merchant_id'),
            array_column($items, 'express_merchant_id')
        );
        $merchants = $paymentMerchantService->getMerchantsByIds($merchantIds, 'id,channel,name,account');

        $records = [];
        foreach ($items as $item) {
            $records[] = [
                'id' => $item['id'],
                'company_type_name' => $companyTypes[$item['company_belong']] ?? '--',
                'company_name' => isset($companies[$item['company_id']]) ? $companies[$item['company_id']]['company_name'] : '--',
                'game_name' => isset($games[$item['game_id']]) ? $games[$item['game_id']]['name'] : '--',
                'game_type_name' => isset($gameTypes[$item['game_type_id']]) ? $gameTypes[$item['game_type_id']]['type_name'] : '--',
                'alipay_merchant_name' => isset($merchants[$item['alipay_merchant_id']]) ? $merchants[$item['alipay_merchant_id']]['name'] : '系统默认商户',
                'alipay_merchant_account' => isset($merchants[$item['alipay_merchant_id']]) ? $merchants[$item['alipay_merchant_id']]['account'] : '系统默认商户',
                'weixin_merchant_name' => isset($merchants[$item['weixin_merchant_id']]) ? $merchants[$item['weixin_merchant_id']]['name'] : '系统默认商户',
                'weixin_merchant_account' => isset($merchants[$item['weixin_merchant_id']]) ? $merchants[$item['weixin_merchant_id']]['account'] : '系统默认商户',
                'express_merchant_name' => isset($merchants[$item['express_merchant_id']]) ? $merchants[$item['express_merchant_id']]['name'] : '系统默认商户',
                'express_merchant_account' => isset($merchants[$item['express_merchant_id']]) ? $merchants[$item['express_merchant_id']]['account'] : '系统默认商户',
                'effective_time_display' => $paymentRuleService->getEffectiveTimeDisplay($item)
            ];
        }

        $page = set_pagination($count, $row);
        if($page) {
            $this->assign('_page', $page);
        }

        $aliMerchants = $paymentMerchantService->getMerchantsByWay(PaymentMerchantService::WAY_ALIPAY);
        $weixinMerchants = $paymentMerchantService->getMerchantsByWay(PaymentMerchantService::WAY_WEIXIN);
        $expressMerchants = $paymentMerchantService->getMerchantsByWay(PaymentMerchantService::WAY_EXPRESS);

        $this->assign('aliMerchants', $aliMerchants);
        $this->assign('weixinMerchants', $weixinMerchants);
        $this->assign('expressMerchants', $expressMerchants);

        $this->assign('records', $records);
        $this->assign('games', $games);
        $this->assign('gameTypes', $gameTypes);
        $this->assign('companyTypes', $companyTypes);
        $this->assign('companies', $companies);
        $this->display();
    }
    
    public function addRule()
    {
        $this->meta_title = '新增支付商户配置';
        $gameService = new GameService();
        $gameTypes = $gameService->getGameTypes(null, 'id,type_name');
        $games = $gameService->getBaseGames(null, 'id,name');
        $companyBelongs = PromoteCompanyService::$belongs;
        $companyService = new PromoteCompanyService();
        $paymentMerchantService = new PaymentMerchantService();
        $aliMerchants = $paymentMerchantService->getMerchantsByWay(PaymentMerchantService::WAY_ALIPAY);
        $weixinMerchants = $paymentMerchantService->getMerchantsByWay(PaymentMerchantService::WAY_WEIXIN);
        $expressMerchants = $paymentMerchantService->getMerchantsByWay(PaymentMerchantService::WAY_EXPRESS);
        $companies = $companyService->getCompanies(null, 'id,company_name');
        $this->assign('aliMerchants', $aliMerchants);
        $this->assign('weixinMerchants', $weixinMerchants);
        $this->assign('expressMerchants', $expressMerchants);
        $this->assign('games', $games);
        $this->assign('gameTypes', $gameTypes);
        $this->assign('companyBelongs', $companyBelongs);
        $this->assign('companies', $companies);
        $this->display('ruleAddForm');
    }

    public function saveRule()
    {
        $records = I('records', []);
        if (count($records) == 0) {
            $this->ajaxReturn([
                'status' => 0,
                'message' => '未提交换绑数据'
            ]);
        }

        $status = true;
        $message = '';

        if (!$status) {
            $this->ajaxReturn([
                'status' => 0,
                'message' => $message
            ]);
        }

        $ids = [];
        $paymentRuleService = new PaymentRuleService();
        foreach ($records as $record) {
            $startTime = $record['start_time'] == '' ? 0 : strtotime($record['start_time'] . ' 00:00:00');
            $endTime = $record['end_time'] == '' ? PaymentRuleService::FOREVER_TIME : strtotime($record['end_time'] . ' 23:59:59');

            $repeatRules = $paymentRuleService->getTimeRepeatRules($record);
            if (count($repeatRules)) {
                $paymentRuleService->resetTimeRules($repeatRules, $startTime, $endTime);
            }

            $item = [
                'company_belong' => $record['company_id'] > 0 ? -1 : $record['company_belong'],
                'company_id' => $record['company_id'],
                'game_type_id' => $record['game_id'] > 0 ? 0 :$record['game_type_id'],
                'game_id' => $record['game_id'],
                'alipay_merchant_id' => $record['alipay_merchant_id'],
                'weixin_merchant_id' => $record['weixin_merchant_id'],
                'express_merchant_id' => $record['express_merchant_id'],
                'start_time' => $startTime,
                'end_time' => $endTime,
                'create_time' => time(),
                'update_time' => time()
            ];
            $id = M('payment_rule', 'tab_')->add($item);
            $ids[] = $id;
        }

        addOperationLog([
            'op_type' => 0,
            'key' => implode(',', $ids),
            'op_name' => '新增支付商户配置',
            'url' => U('PaymentMerchant/saveRule'),
            'menu' => '推广员-推广员管理-支付商户配置-新增支付商户配置',
            'content' => json_encode($records)
        ]);

        $this->ajaxReturn([
            'status' => 1,
            'message' => '添加成功'
        ]);
    }

    public function editRule()
    {
        $this->meta_title = '修改支付商户配置';

        $id = I('id', 0);
        $rule = M('payment_rule', 'tab_')->where(['id' => $id])->find();
        if ($rule === null) {
            $this->error('记录不存在');
        }

        $gameService = new GameService();
        $gameTypes = $gameService->getGameTypes(null, 'id,type_name');
        $games = $gameService->getBaseGames(null, 'id,name');
        $companyBelongs = PromoteCompanyService::$belongs;
        $companyService = new PromoteCompanyService();
        $paymentMerchantService = new PaymentMerchantService();
        $aliMerchants = $paymentMerchantService->getMerchantsByWay(PaymentMerchantService::WAY_ALIPAY);
        $weixinMerchants = $paymentMerchantService->getMerchantsByWay(PaymentMerchantService::WAY_WEIXIN);
        $expressMerchants = $paymentMerchantService->getMerchantsByWay(PaymentMerchantService::WAY_EXPRESS);
        $companies = $companyService->getCompanies(null, 'id,company_name');

        $rule ['start_time'] = $rule ['start_time'] == 0 ? '' : date('Y-m-d', $rule ['start_time']);
        $rule ['end_time'] = $rule ['end_time'] == PaymentRuleService::FOREVER_TIME ? '' : date('Y-m-d', $rule ['end_time']);

        $this->assign('aliMerchants', $aliMerchants);
        $this->assign('weixinMerchants', $weixinMerchants);
        $this->assign('expressMerchants', $expressMerchants);
        $this->assign('record', $rule);
        $this->assign('games', $games);
        $this->assign('gameTypes', $gameTypes);
        $this->assign('companyBelongs', $companyBelongs);
        $this->assign('companies', $companies);
        $this->display('ruleEditForm');
    }

    public function modifyRule()
    {
        $params = I('post.');
        $id = $params['id'] ?? 0;
        if ($id == 0) {
            $this->ajaxReturn([
                'status' => 0,
                'message' => '提交数据异常'
            ]);
        }

        $rule = M('payment_rule', 'tab_')->where(['id' => $id])->find();
        if ($rule === null) {
            $this->ajaxReturn([
                'status' => 0,
                'message' => '记录不存在'
            ]);
        }

        $startTime = $params['start_time'] == '' ? 0 : strtotime($params['start_time'] . ' 00:00:00');
        $endTime = $params['end_time'] == '' ? PaymentRuleService::FOREVER_TIME : strtotime($params['end_time'] . ' 23:59:59');

        if (date('Ymd', $rule['start_time']) <= date('Ymd') && $startTime != $rule['start_time']) {
            $this->ajaxReturn([
                'status' => 0,
                'message' => '原开始生效时间在今日之前,不可修改'
            ]);
        }

        if (date('Ymd', $startTime) <= date('Ymd') && $startTime != $rule['start_time']) {
            $this->ajaxReturn([
                'status' => 0,
                'message' => '开始生效时间在今日之前,不可修改'
            ]);
        }
        if (date('Ymd', $rule['end_time']) <= date('Ymd') && $endTime != $rule['end_time']) {
            $this->ajaxReturn([
                'status' => 0,
                'message' => '原最后生效时间在今日之前,不可修改'
            ]);
        }

        if (date('Ymd', $endTime) <= date('Ymd') && $endTime != $rule['end_time']) {
            $this->ajaxReturn([
                'status' => 0,
                'message' => '最后生效时间在今日之前,不可修改'
            ]);
        }

        $item = [
            'alipay_merchant_id' => $params['alipay_merchant_id'],
            'weixin_merchant_id' => $params['weixin_merchant_id'],
            'express_merchant_id' => $params['express_merchant_id'],
            'start_time' => $startTime,
            'end_time' => $endTime,
            'update_time' => time()
        ];
        
        M('payment_rule', 'tab_')->where(['id' => $id])->save($item);

        addOperationLog([
            'op_type' => 1,
            'key' => $id,
            'op_name' => '修改支付商户配置',
            'url' => U('PaymentMerchant/modifyRule', ['id' => $id]),
            'menu' => '推广员-推广员管理-支付商户配置-修改支付商户配置',
            'content' => json_encode(['rule' => $rule, 'modify' => $item]),
        ]);

        $this->ajaxReturn([
            'status' => 1,
            'message' => '修改成功'
        ]);
    }

    public function deleteRule()
    {
        $id = I('id', 0);
        $rule = M('payment_rule', 'tab_')->where(['id' => $id])->find();

        if (!$rule) {
            $this->ajaxReturn([
                'status' => 0,
                'message' => '记录不存在'
            ]);
        }

        M('payment_rule', 'tab_')->where(['id' => $id])->delete();
        addOperationLog([
            'op_type' => 2,
            'key' => $id,
            'op_name' => '删除支付商户配置',
            'url' => U('PaymentMerchant/deleteRule', ['id' => $id]),
            'menu' => '推广员-推广员管理-支付商户配置-删除支付商户配置',
            'content' => json_encode($rule),
        ]);
        $this->ajaxReturn([
            'status' => 1,
            'message' => '删除成功'
        ]);
    }

    public function getCompaniesByBelong()
    {
        $belong = I('company_belong', '');
        if ($belong === '') {
            $belong = null;
        }
        $promoteCompanyService = new PromoteCompanyService();
        $companies = $promoteCompanyService->getCompaniesByBelong($belong, 'id,company_name');

        $this->ajaxReturn([
            'status' => 1,
            'message' => '获取成功',
            'data' => [
                'companies' => $companies
            ]
        ]);
    }

    public function getGamesByType()
    {
        $gameTypeId = I('game_type_id', 0);
        $gameService = new GameService();
        $games = $gameService->getBaseGamesByType($gameTypeId, 'id,name');

        $this->ajaxReturn([
            'status' => 1,
            'message' => '获取成功',
            'data' => [
                'games' => $games
            ]
        ]);
    }

    public function checkRules()
    {
        $records = I('records', []);
        if (count($records) == 0) {
            $this->ajaxReturn([
                'status' => 0,
                'message' => '未提交换绑数据'
            ]);
        }

        $isRepat = false;
        $paymentRuleService = new PaymentRuleService();
        foreach ($records as $record) {
            $rules = $paymentRuleService->getTimeRepeatRules($record);
            if (count($rules)) {
                $isRepat = true;
                break;
            }
        }
        $this->ajaxReturn([
            'status' => 1,
            'message' => '成功',
            'data' => [
                'is_repeat' => $isRepat
            ]
        ]);
    }
}