遍历渲染table多个表单输入框,name[][],PHP端

<?php


namespace theme\modules\operate\models;


use common\dao\CurlServiceDao;
use common\extensions\Common;
use common\models\CommonModel;
use theme\constants\ThemeConstants;
use theme\dao\CdoOpenThemeDao;
use theme\dao\CdoOperationThemeDao;
use theme\dao\CdoPortalTemplateDao;
use theme\exception\ThemeErrMap;
use theme\exception\ThemeException;
use theme\extensions\Helper;
use theme\modules\operate\controllers\VipBannerConfig;
use theme\modules\page\models\Content;
use yii\base\Exception;
use yii\base\Model;
use Yii;
use theme\modules\resource\models\Resource;

class ResourceDetail extends Model
{
    public $cardCode;
    public $type;
    public $title;
    public $subTitle;
    public $desc;
    public $buttonTxt;
    public $actionType;
    public $bgPic;
    public $productPic;
    public $picUrl;
    public $symbol_title;
    public $symbol_desc;
    public $fold_text;
    public $post_fold_text;
    public $button_text;
    public $post_button_text;
    public $voucher_id;

    const CARD_TYPE_AD = 1; // 详情页运营位广告卡片
    const CARD_TYPE_BANNER = 2; // 全屏详情页运营横条卡片
    const CARD_TYPE_GENERAL = 3; // 全屏详情页通用活动卡片

    const TYPE_APP = 1; // App
    const TYPE_PIC = 2; // 图片
    const TYPE_VIDEO = 3; // 视频
    const TYPE_EC = 4; // 电商
    const TYPE_GAME = 5; // 预约游戏
    const TYPE_NEW_GAME = 6; // 新游预约福利
    const TYPE_PHOTO_PRINT = 7; // 照片打印
    const TYPE_VIP_BANNER = 9; // 会员开通横条
    const TYPE_TICKET_BANNER = 10; // 优惠券横条
    const TYPE_AD_BANNER = 11; // 任务广告横条
    const TYPE_AD_OPERATE_ACTIVITY = 12; // 运营活动

    const SCENE_TYPE = 2; // 资源详情卡片定投
    const QUERY_APP_URL = '/cardadmin/app/subscribe/query';

    const APP_HOME_PAGE_TABLE = 't_app_home_page';
    const GAME_ZONE_PART_TYPE = 'informationStream_game-zone';

    public static $cardType = [
        self::CARD_TYPE_AD => '详情页运营位广告卡片',
        self::CARD_TYPE_BANNER => '全屏详情页运营横条卡片',
        self::CARD_TYPE_GENERAL => '全屏详情页通用活动卡片',
    ];
    public static $operationType = [
        self::TYPE_APP => 'App',
        self::TYPE_PIC => '图片',
        self::TYPE_VIDEO => '视频',
        self::TYPE_EC => '电商',
        self::TYPE_GAME => '预约游戏',
        self::TYPE_NEW_GAME => '新游预约福利',
        self::TYPE_PHOTO_PRINT => '照片打印',
        self::TYPE_VIP_BANNER => '会员开通横条',
        self::TYPE_TICKET_BANNER => '优惠券横条',
        self::TYPE_AD_BANNER => '任务广告横条',
        self::TYPE_AD_OPERATE_ACTIVITY => '运营活动',
    ];
    // 卡片类型对应的运营位类型
    public static $cardTypeOperationTypeMap = [
        self::CARD_TYPE_AD => [
            self::TYPE_APP => 'App',
            self::TYPE_PIC => '图片',
            self::TYPE_VIDEO => '视频',
            self::TYPE_EC => '电商',
            self::TYPE_GAME => '预约游戏',
            self::TYPE_NEW_GAME => '新游预约福利',
            self::TYPE_PHOTO_PRINT => '照片打印',
        ],
        self::CARD_TYPE_BANNER => [
            self::TYPE_VIP_BANNER => '会员开通横条',
            self::TYPE_TICKET_BANNER => '优惠券横条',
            self::TYPE_AD_BANNER => '任务广告横条',
        ],
        self::CARD_TYPE_GENERAL => [
            self::TYPE_PHOTO_PRINT => '照片打印',
            self::TYPE_AD_OPERATE_ACTIVITY => '运营活动',
        ],
    ];

    // 支持dp的类型
    public static $supportDpType = [
        self::TYPE_PIC, // 图片
        self::TYPE_VIDEO,  // 视频
        self::TYPE_PHOTO_PRINT, // 照片打印
        self::TYPE_VIP_BANNER, // 会员开通横条
        self::TYPE_AD_OPERATE_ACTIVITY, // 运营活动
    ];

    public function rules()
    {
        return [
            [ ['cardCode', 'type'], 'required' ],
            [['actionType'], 'required', 'when' => function ( $model ) {
                return ! in_array( $model->type, [self::TYPE_TICKET_BANNER, self::TYPE_AD_BANNER] );
            }],
            [['title', 'subTitle', 'buttonTxt', 'bgPic', 'productPic'], 'required', 'when' => function ( $model ) {
                return $model->type == self::TYPE_EC;
            }],
            [['picUrl'], 'required', 'when' => function ( $model ) {
                return in_array($model->type, [self::TYPE_PIC, self::TYPE_VIDEO, self::TYPE_VIP_BANNER, self::TYPE_AD_OPERATE_ACTIVITY]);
            }],
            [['symbol_title', 'symbol_desc', 'fold_text', 'post_fold_text', 'button_text', 'post_button_text', 'voucher_id'],
                'required', 'when' => function ( $model ) {
                return $model->type == self::TYPE_TICKET_BANNER; // 优惠券横条
            }],
            [['picUrl', 'symbol_title', 'symbol_desc', 'fold_text', 'button_text'], 'required', 'when' => function ( $model ) {
                return $model->type == self::TYPE_AD_BANNER; // 任务广告横条
            }],
            [['desc'], 'required', 'when' => function ( $model ) {
                // 通用活动卡片且照片打印、运营活动
                return $model->cardCode == self::CARD_TYPE_GENERAL && $model->type == self::TYPE_PHOTO_PRINT;
            }],
        ];
    }

    public function select() {
        $params = Common::params();

        $subWhere = [
            'and',
            [ 'valid_state' => 1 ],
        ];

        $ids = [];
        if ( ! empty( $params['title'] ) ) {
            $subInfo = CdoOperationThemeDao::getInstance()->getWhereList( 't_delivery_content_property',
                array_merge( $subWhere, [['name' => 'title']], [['like', 'value', $params['title']]] ), 'content_id' );
            if ( empty( $subInfo ) ) {
                return [ 'rows' => [], 'total' => 0 ];
            }

            $ids['ids'] = isset( $ids['ids'] ) ? array_intersect( $ids['ids'] ,
                array_column( $subInfo, 'content_id' ) ) : array_column( $subInfo, 'content_id' );
            $ids['ids'] = array_unique( $ids['ids'] );
        }
        if ( ! empty( $params['cardCode'] ) ) {
            $subInfo = CdoOperationThemeDao::getInstance()->getWhereList( 't_delivery_content_property',
                array_merge( $subWhere, [['name' => 'cardCode']], [['value' => $params['cardCode']]] ), 'content_id' );
            if ( empty( $subInfo ) ) {
                return [ 'rows' => [], 'total' => 0 ];
            }

            $ids['ids'] = isset( $ids['ids'] ) ? array_intersect( $ids['ids'] ,
                array_column( $subInfo, 'content_id' ) ) : array_column( $subInfo, 'content_id' );
            $ids['ids'] = array_unique( $ids['ids'] );
        }
        if ( ! empty( $params['type'] ) ) {
            $subInfo = CdoOperationThemeDao::getInstance()->getWhereList( 't_delivery_content_property',
                array_merge( $subWhere, [['name' => 'contentType']], [['value' => $params['type']]] ), 'content_id' );
            if ( empty( $subInfo ) ) {
                return [ 'rows' => [], 'total' => 0 ];
            }

            $ids['ids'] = isset( $ids['ids'] ) ? array_intersect( $ids['ids'] ,
                array_column( $subInfo, 'content_id' ) ) : array_column( $subInfo, 'content_id' );
            $ids['ids'] = array_unique( $ids['ids'] );
        }
        if ( ! empty( $params['actionType'] ) ) {
            $subInfo = CdoOperationThemeDao::getInstance()->getWhereList( 't_delivery_content_property',
                array_merge( $subWhere, [['name' => 'actionType']], [['value' => $this->getRealActionType( $params['actionType'] )]] ), 'content_id' );
            if ( empty( $subInfo ) ) {
                return [ 'rows' => [], 'total' => 0 ];
            }

            $ids['ids'] = isset( $ids['ids'] ) ? array_intersect( $ids['ids'] ,
                array_column( $subInfo, 'content_id' ) ) : array_column( $subInfo, 'content_id' );

            if ( ThemeConstants::ACTION_TYPE_OAPS == $params['actionType'] ) { // 本地跳转
                $contentInfo = CdoOperationThemeDao::getInstance()->getWhereList( 't_delivery_content_property',
                    ['content_id' => $ids['ids'], 'name' => 'actionContent'] );
                foreach ( $contentInfo as $item ) {
                    $tmpActionType = Helper::getActionType($params['actionType'], $item['value']);
                    if ( $tmpActionType != $params['actionType'] ) {
                        $ids['ids'] = array_diff( $ids['ids'], [ $item['content_id'] ] );
                    }
                }
            } else {
                $formatContent = Helper::getOapContent( $params['actionType'], '' );
                if ( $formatContent ) {
                    $subInfo = CdoOperationThemeDao::getInstance()->getWhereList( 't_delivery_content_property',
                        array_merge( $subWhere, [['name' => 'actionContent']], [['like', 'value', $formatContent]] ), 'content_id' );
                    if ( empty( $subInfo ) ) {
                        return [ 'rows' => [], 'total' => 0 ];
                    }
                    $ids['ids'] = isset( $ids['ids'] ) ? array_intersect( $ids['ids'] ,
                        array_column( $subInfo, 'content_id' ) ) : array_column( $subInfo, 'content_id' );
                }
            }

            $ids['ids'] = array_unique( $ids['ids'] );
        }

        if ( ! empty( $params['id'] ) ) {
            $ids['ids'] = isset( $ids['ids'] ) ? array_intersect( $ids['ids'] , [ $params['id'] ] ) : [ $params['id'] ];
            $ids['ids'] = array_unique( $ids['ids'] );
        }

        if ( isset( $ids['ids'] ) && empty( $ids['ids'] ) ) {
            return [ 'rows' => [], 'total' => 0 ];
        }

        $where = [
            'and',
            ['source' => self::SCENE_TYPE],
            ['status' => 1],
        ];
        if ( isset( $ids['ids'] ) ) {
            $where[] = [ 'id' => $ids['ids'] ];
        }
        if ( ! empty( $params['name'] ) ) {
            $where[] = ['like', 'name', $params['name']];
        }

        $delivery = CdoOperationThemeDao::getInstance()->getBootstrapList( 't_delivery_content', $where );
        if ( empty( $delivery['rows'] ) ) {
            return [ 'rows' => [], 'total' => 0 ];
        }
        $properties = CdoOperationThemeDao::getInstance()->getWhereList( 't_delivery_content_property',
            [ 'content_id' => array_column( $delivery['rows'], 'id' ) ] );
        foreach ( $delivery['rows'] as $k => $v ) {
            foreach ( $properties as $sv ) {
                if ( $v['id'] == $sv['content_id'] ) {
                    $delivery['rows'][ $k ][ $sv['name'] ] = $sv['value'];
                }
            }
        }

        foreach ( $delivery['rows'] as $k => $v ) {
            $delivery['rows'][ $k ]['cardCodeText'] = self::$cardType[ $v['cardCode'] ] ?: '';
        	$ext = ! empty( $v['ext'] ) && json_decode( $v['ext'], true ) ? json_decode( $v['ext'], true ) : [];
            $delivery['rows'][ $k ]['type'] = $v['contentType'];
            $delivery['rows'][ $k ]['typeText'] = self::$operationType[ $v['contentType'] ];
            $v['actionType'] = Helper::getActionType($v['actionType'], $v['actionContent']);
            $delivery['rows'][ $k ]['actionType'] = $v['actionType'];
            $delivery['rows'][ $k ]['actionTypeText'] = ThemeConstants::ACTION_TYPE_TEXT_MAP[ $v['actionType'] ];
            $delivery['rows'][ $k ]['actionContentText'] = $ext['content']['text'] ?: $v['actionContent'];
            $delivery['rows'][ $k ]['app_name'] = $ext['content']['text'] ?: ''; // 预约游戏App名称
            $delivery['rows'][ $k ]['pkg_name'] = isset($ext['content']['pkg_name']) ? $ext['content']['pkg_name'] : '';; // 预约游戏包名

            if ( ! empty( $v['actionType1'] ) ) {
				$v['actionType1'] = Helper::getActionType($v['actionType1'], $v['actionContent1']);
				$delivery['rows'][ $k ]['actionType1'] = $v['actionType1'];
				$delivery['rows'][ $k ]['actionType1Text'] = ThemeConstants::ACTION_TYPE_TEXT_MAP[ $v['actionType1'] ];
			}
            $delivery['rows'][ $k ]['voucherText'] = ! empty( $ext['voucherInfo'] ) ? $ext['voucherInfo']['text'] : '';
        }

        return $delivery;
    }

    public function add() {
        $this->handle('add');
    }

    public function edit() {
        $this->handle('edit');
    }

    public function handle( $oper ) {
        $params = Common::params();
        if ( $oper == 'edit' && ! is_numeric( $params['id'] ) ) {
            throw new ThemeException( ThemeErrMap::ERROR_PARAMS, 'id不能为空' );
        }
        Yii::$app->params['operation'] = $params['operation_reason'];

		// 图片、视频支持dp跳转
		// 100软件商店首页,101游戏中心首页,103软件商店活动页,104游戏中心活动页
        if ( in_array( $params['type'], self::$supportDpType ) && ThemeConstants::ACTION_TYPE_DP == $params['actionType'] &&
			! in_array( $params['action_type1'], [ThemeConstants::ACTION_TYPE_MK_HOME_PAGE, ThemeConstants::ACTION_TYPE_GC_HOME_PAGE,
				ThemeConstants::ACTION_TYPE_MK_ACTIVITY_DETAIL, ThemeConstants::ACTION_TYPE_GC_ACTIVITY_DETAIL] ) && empty( $params['action_content1'] )) {
			throw new ThemeException( ThemeErrMap::ERROR_PARAMS, '打底内容不能为空' );
		}
		if ( in_array( $params['type'], self::$supportDpType ) && ThemeConstants::ACTION_TYPE_DP == $params['actionType'] &&
			in_array( $params['action_type1'], [ThemeConstants::ACTION_TYPE_MK_ACTIVITY_DETAIL, ThemeConstants::ACTION_TYPE_GC_ACTIVITY_DETAIL] ) &&
			empty( $params['content1'] )) {
			throw new ThemeException( ThemeErrMap::ERROR_PARAMS, '打底内容不能为空' );
		}

        $realActionType = 0;
        $realActionContent = '';
        if ( in_array( $params['actionType'], [ThemeConstants::ACTION_TYPE_MK_ACTIVITY_DETAIL,
            ThemeConstants::ACTION_TYPE_GC_ACTIVITY_DETAIL, ThemeConstants::ACTION_TYPE_SDK_SHOP_DETAIL,
            ThemeConstants::ACTION_TYPE_SDK_SHOP_H5] ) ) {
            $params['action_content'] = $params['content'];
        }
        // 图片、视频、照片打印、会员开通横条支持dp跳转
		if ( ThemeConstants::ACTION_TYPE_DP == $params['actionType'] &&  in_array( $params['action_type1'],
				[ThemeConstants::ACTION_TYPE_MK_ACTIVITY_DETAIL, ThemeConstants::ACTION_TYPE_GC_ACTIVITY_DETAIL,
					ThemeConstants::ACTION_TYPE_SDK_SHOP_DETAIL,ThemeConstants::ACTION_TYPE_SDK_SHOP_H5] ) ) {
			$params['action_content1'] = $params['content1'];
		}
        if ( self::TYPE_APP == $params['type'] ) { // App
            $realActionType = $this->getRealActionType( $params['actionType'] );
            $realActionContent = Helper::getOapContent($params['actionType'], $params['action_content']);
        } elseif ( self::TYPE_PIC == $params['type'] || self::TYPE_VIDEO == $params['type'] ) { // 图片,视频
            $realActionType = $this->getRealActionType( $params['actionType'] );
            if ( in_array( $params['actionType'], [ThemeConstants::ACTION_TYPE_MK_HOME_PAGE, ThemeConstants::ACTION_TYPE_GC_HOME_PAGE,
                ThemeConstants::ACTION_TYPE_MK_APP_DETAIL, ThemeConstants::ACTION_TYPE_MK_ACTIVITY_DETAIL,
                ThemeConstants::ACTION_TYPE_GC_ACTIVITY_DETAIL] ) ) {
                $realActionContent = Helper::getOapContent($params['actionType'], $params['action_content']);
            } else {
                $realActionContent = Content::getJumpContent( $params['actionType'], $params['action_content'] );
            }
        } elseif ( self::TYPE_EC == $params['type'] ) {
            $realActionType = $this->getRealActionType( $params['actionType'] );
            if ( in_array( $params['actionType'], [ThemeConstants::ACTION_TYPE_MK_HOME_PAGE, ThemeConstants::ACTION_TYPE_GC_HOME_PAGE,
                ThemeConstants::ACTION_TYPE_MK_APP_DETAIL,ThemeConstants::ACTION_TYPE_MK_ACTIVITY_DETAIL,
                ThemeConstants::ACTION_TYPE_GC_ACTIVITY_DETAIL, ThemeConstants::ACTION_TYPE_SDK_SHOP_HOME_PAGE,
                ThemeConstants::ACTION_TYPE_SDK_SHOP_CATEGORY_PAGE] ) ) {
                $realActionContent = Helper::getOapContent($params['actionType'], $params['action_content']);
            } else {
                $realActionContent = Content::getJumpContent( $params['actionType'], $params['action_content'] );
            }
        } elseif (self::TYPE_GAME == $params['type'] || self::TYPE_NEW_GAME == $params['type']) {
            // 预约游戏||新游预约福利
            $realActionType = $this->getRealActionType($params['actionType']);
            $realActionContent = Helper::getOapContent($params['actionType'], $params['pkg_name']);
            if (!$params['action_content']) {
                throw new ThemeException(ThemeErrMap::ERROR_PARAMS, '预约游戏App不能为空,请选择预约游戏App');
            }
            if (self::TYPE_NEW_GAME == $params['type']) {
                // 新游预约福利   游戏中心指定详情页链接:oaps://gc/dt?id=XXX&tab=1&win=1(XXX:APPID)
                $realActionContent = 'oaps://gc/dt?id=' . $params['action_content'] . '&tab=1&win=1';
            }
        } elseif (self::TYPE_PHOTO_PRINT == $params['type'] || self::TYPE_VIP_BANNER == $params['type'] ||
            self::TYPE_AD_OPERATE_ACTIVITY == $params['type']) { // 照片打印、会员开通横条、运营活动
            $realActionType = $this->getRealActionType($params['actionType']);
            $realActionContent = Content::getJumpContent($params['actionType'], $params['action_content']);
        }

        $ext = [
			'content' => [ 'id' => $params['action_content'], 'text' => $params['action_content_text'],
				'video_name' => self::TYPE_VIDEO == $params['type'] ? $params['video_name'] : '' ],
		];
        if (self::TYPE_GAME == $params['type'] || self::TYPE_NEW_GAME == $params['type']) {
            // 预约游戏||新游预约福利
            $ext['content']['pkg_name'] = $params['pkg_name'];
        }
		// 图片、视频支持dp跳转
		if ( in_array( $params['type'], self::$supportDpType ) && ThemeConstants::ACTION_TYPE_DP == $params['actionType'] ) {
			$ext['content1'] = ['id' => $params['action_content1'], 'text' => $params['action_content1_text']];
		}
        // 全屏详情页运营横条卡片 下 优惠券横条 的 可币券配置
        if ( self::CARD_TYPE_BANNER == $params['cardCode'] && self::TYPE_TICKET_BANNER == $params['type'] ) {
            $ext['voucherInfo'] = ['id' => $params['voucher_id'], 'text' => $params['voucher_text']];
        }

		$ext = json_encode( $ext, 256 );

        $fields = [];
        if ( self::TYPE_APP == $params['type'] ) {
            $fields = [
                'cardCode' => $params['cardCode'],
                'contentType' => $params['type'],
                'desc' => $params['desc'],
                'actionType' => $realActionType,
                'actionContent' => $realActionContent,
                'ext' => $ext,
            ];

        } elseif ( self::TYPE_PIC == $params['type'] ) {
            $fields = [
                'cardCode' => $params['cardCode'],
                'contentType' => $params['type'],
                'desc' => $params['desc'],
                'picUrl' => $params['picUrl'],
                'actionType' => $realActionType,
                'actionContent' => $realActionContent,
                'ext' => $ext,
            ];

        } elseif ( self::TYPE_VIDEO == $params['type'] ) {
            $fields = [
                'cardCode' => $params['cardCode'],
                'contentType' => $params['type'],
                'desc' => $params['desc'],
                'picUrl' => $params['picUrl'],
                'videoId' => $params['video_id'],
                'videoName' => $params['video_name'],
                'videoUrl' => $params['video_url'],
                'videoThumbnail' => $params['video_thumbnail'],
                'actionType' => $realActionType,
                'actionContent' => $realActionContent,
                'ext' => $ext,
            ];
        } elseif ( self::TYPE_EC == $params['type'] ) {
            $fields = [
                'cardCode' => $params['cardCode'],
                'contentType' => $params['type'],
                'title' => $params['title'],
                'subTitle' => $params['subTitle'],
                'desc' => $params['desc'],
                'productPic' => $params['productPic'],
                'bgPic' => $params['bgPic'],
                'buttonTxt' => $params['buttonTxt'],
                'actionType' => $realActionType,
                'actionContent' => $realActionContent,
                'ext' => $ext,
            ];
            if ( $params['actionType'] == ThemeConstants::ACTION_TYPE_SDK_SHOP_DETAIL ) { // SDK商城商品详情页
                $fields['skuId'] = $params['content'];
                self::checkSkuId( $fields['skuId'] );
            } else if ( $params['actionType'] == ThemeConstants::ACTION_TYPE_SDK_SHOP_H5 ) { // SDK商城指定页面
                $fields['actionContent'] = trim( $fields['actionContent'] );
                if ( 0 !== strpos( $fields['actionContent'], 'http' ) ) { // 必须http开头
                    throw new ThemeException( ThemeErrMap::ERROR_OPERATION, '输入的跳转链接' . $fields['actionContent'] . '有误' );
                }
            }
        } elseif (self::TYPE_GAME == $params['type'] || self::TYPE_NEW_GAME == $params['type']) {
            // 预约游戏||新游预约福利
            $fields = [
                'cardCode' => $params['cardCode'],
                'contentType' => $params['type'],
                'desc' => $params['desc'],
                'actionType' => $realActionType,
                'appId' => $params['action_content'], // 应用ID
                'actionContent' => $realActionContent,
                'ext' => $ext,
            ];
        } elseif (self::TYPE_PHOTO_PRINT == $params['type']) {
            // 照片打印
            if ( self::CARD_TYPE_AD == $params['cardCode'] ) { // 详情页运营位广告卡片
                $fields = [
                    'cardCode' => $params['cardCode'],
                    'contentType' => $params['type'],
                    'title' => $params['title'],
                    'subTitle' => $params['subTitle'],
                    'buttonTxt' => $params['buttonTxt'],
                    'actionType' => $realActionType,
                    'actionContent' => $realActionContent,
                    'ext' => $ext,
                ];
            } elseif ( self::CARD_TYPE_GENERAL == $params['cardCode'] ) { // 全屏详情页通用活动卡片
                $fields = [
                    'cardCode' => $params['cardCode'],
                    'contentType' => $params['type'],
//                    'desc' => $params['desc'],
                    'title' => $params['desc'], // 服务端要求 照片打印 模板字段一致
                    'actionType' => $realActionType,
                    'actionContent' => $realActionContent,
                    'ext' => $ext,
                ];
            }
        } elseif ( self::TYPE_VIP_BANNER == $params['type'] ) { // 会员开通横条
            $fields = [
                'cardCode' => $params['cardCode'],
                'contentType' => $params['type'],
                'picUrl' => $params['picUrl'],
                'actionType' => $realActionType,
                'actionContent' => $realActionContent,
                'ext' => $ext,
            ];
            $textConfig = VipBannerConfig::getDetailConfig( $params );
            if ( empty( $textConfig ) ) {
                throw new ThemeException( ThemeErrMap::ERROR_PARAMS, '会员横条配置不赠送单条不能为空' );
            }
            $defaultTextConfig = VipBannerConfig::getDetailConfig( $params, 1 );
            if ( empty( $defaultTextConfig ) ) {
                throw new ThemeException( ThemeErrMap::ERROR_PARAMS, '会员横条兜底配置不赠送单条不能为空' );
            }
            $fields['textConfig'] = json_encode( $textConfig, 256 );
            $fields['defaultTextConfig'] = json_encode( $defaultTextConfig, 256 );
            $textConfig1 = VipBannerConfig::getDetailConfig1( $params );
            if ( empty( $textConfig1 ) ) {
                throw new ThemeException( ThemeErrMap::ERROR_PARAMS, '会员横条配置有赠送单条不能为空' );
            }
            $defaultTextConfig1 = VipBannerConfig::getDetailConfig1( $params, 1 );
            if ( empty( $defaultTextConfig1 ) ) {
                throw new ThemeException( ThemeErrMap::ERROR_PARAMS, '会员横条兜底配置有赠送单条不能为空' );
            }
            $fields['textConfig1'] = json_encode( $textConfig1, 256 );
            $fields['defaultTextConfig1'] = json_encode( $defaultTextConfig1, 256 );
        } elseif ( self::TYPE_TICKET_BANNER == $params['type'] ) { // 优惠券横条
            if (! preg_match('/^\d+$/', $params['voucher_id'])) {
                throw new ThemeException( ThemeErrMap::ERROR_PARAMS, '可币券ID应为数字' );
            }
            $fields = [
                'cardCode' => $params['cardCode'],
                'contentType' => $params['type'],
                'picUrl' => $params['picUrl'],
                'title' => $params['symbol_title'],
                'desc' => $params['symbol_desc'],
                'foldText' => $params['fold_text'],
                'postFoldText' => $params['post_fold_text'],
                'buttonText' => $params['button_text'],
                'postButtonText' => $params['post_button_text'],
                'voucherId' => $params['voucher_id'],
                'ext' => $ext,
            ];
        } elseif ( self::TYPE_AD_BANNER == $params['type'] ) { // 任务广告横条
            $fields = [
                'cardCode' => $params['cardCode'],
                'contentType' => $params['type'],
                'picUrl' => $params['picUrl'],
                'title' => $params['symbol_title'],
                'desc' => $params['symbol_desc'],
                'foldText' => $params['fold_text'],
                'buttonText' => $params['button_text'],
                'ext' => $ext,
            ];
        } elseif ( self::TYPE_AD_OPERATE_ACTIVITY == $params['type'] ) { // 运营活动
            $fields = [
                'cardCode' => $params['cardCode'],
                'contentType' => $params['type'],
//                'desc' => $params['desc'], 描述已隐藏
                'picUrl' => $params['picUrl'],
                'actionType' => $realActionType,
                'actionContent' => $realActionContent,
                'ext' => $ext,
            ];
        }

		// 图片、视频支持dp跳转
		if ( in_array( $params['type'], self::$supportDpType ) && ThemeConstants::ACTION_TYPE_DP == $params['actionType'] ) {
			$realActionType1 = $this->getRealActionType( $params['action_type1'] );
			if ( in_array( $params['action_type1'], [ThemeConstants::ACTION_TYPE_MK_HOME_PAGE, ThemeConstants::ACTION_TYPE_GC_HOME_PAGE,
                ThemeConstants::ACTION_TYPE_MK_APP_DETAIL,ThemeConstants::ACTION_TYPE_MK_ACTIVITY_DETAIL,
                ThemeConstants::ACTION_TYPE_GC_ACTIVITY_DETAIL] ) ) {
				$realActionContent1 = Helper::getOapContent($params['action_type1'], $params['action_content1']);
			} else {
				$realActionContent1 = Content::getJumpContent( $params['action_type1'], $params['action_content1'] );
			}
			$fields['actionType1'] = $realActionType1;
			$fields['actionContent1'] = $realActionContent1;
		}

        $transaction = CdoOperationThemeDao::getDb()->beginTransaction();
        try {
            $properties = [];
            if ( 'add' == $oper ) {
                $data = [
                    'name' => $params['name'],
                    'desc' => $params['desc'],
                    'source' => self::SCENE_TYPE,
                    'status' => 1,
                    'create_time' => date('Y-m-d H:i:s'),
                    'update_time' => date('Y-m-d H:i:s'),
                ];
                $contentId = CdoOperationThemeDao::getInstance()->addOne( $data, 't_delivery_content' );

                foreach ( $fields as $k => $v ) {
                    $properties[] = [
                        'content_id' => $contentId,
                        'name' => $k,
                        'value' => $v,
                        'valid_state' => 1,
                        'create_time' => date('Y-m-d H:i:s'),
                        'update_time' => date('Y-m-d H:i:s'),
                        'create_operator' => Yii::$app->session['userInfo']['real_name'],
                        'update_operator' => Yii::$app->session['userInfo']['real_name'],
                    ];
                }
                $column = array_keys($properties[0]);
                CdoOperationThemeDao::find()->createCommand()->batchInsert('t_delivery_content_property', $column, $properties)->execute();
            } elseif ( 'edit' == $oper ) {
                $data = [
                    'name' => $params['name'],
                    'desc' => $params['desc'],
                    'update_time' => date('Y-m-d H:i:s'),
                ];
                CdoOperationThemeDao::getInstance()->saveOne( $params['id'], $data, 't_delivery_content' );
                CdoOperationThemeDao::getInstance()->saveOneWhere( ['valid_state' => 0, 'update_time' => date('Y-m-d H:i:s')],
                    ['content_id' => $params['id']], 't_delivery_content_property' );

                foreach ( $fields as $k => $v ) {
                    $properties[] = [
                        'content_id' => $params['id'],
                        'name' => $k,
                        'value' => $v,
                        'valid_state' => 1,
                        'create_time' => date('Y-m-d H:i:s'),
                        'update_time' => date('Y-m-d H:i:s'),
                        'create_operator' => Yii::$app->session['userInfo']['real_name'],
                        'update_operator' => Yii::$app->session['userInfo']['real_name'],
                    ];
                }

                $column = array_keys($properties[0]);
                CdoOperationThemeDao::find()->createCommand()->batchInsert('t_delivery_content_property', $column, $properties)->execute();
            }

            $transaction->commit();
        } catch ( \Exception $e ) {
            $transaction->rollBack();
            throw new ThemeException( $e->getCode(), $e->getMessage() );
        }
    }

    public function delete() {
        $params = Common::params();
        if ( ! is_numeric( $params['id'] ) ) {
            throw new ThemeException( ThemeErrMap::ERROR_PARAMS, 'id不能为空' );
        }
        Yii::$app->params['operation'] = $params['operation_reason'];

        $trransaction = CdoOperationThemeDao::getDb()->beginTransaction();
        try {
            CdoOperationThemeDao::getInstance()->saveOne( $params['id'],
                ['status' => 0, 'update_time' => date('Y-m-d H:i:s') ], 't_delivery_content' );
            CdoOperationThemeDao::getInstance()->saveOneWhere( ['valid_state' => 0, 'update_time' => date('Y-m-d H:i:s') ],
            ['content_id' => $params['id']], 't_delivery_content_property');

            $trransaction->commit();
        } catch ( \Exception $e ) {
            $trransaction->rollBack();
            throw new ThemeException( $e->getCode(), $e->getMessage() );
        }
    }

    public static function jumpType($product_id)
    {
        $jumpList = CdoPortalTemplateDao::getInstance()->getWhereList('jump_support_version', [
            'valid_state' => 1, 'product_id' => $product_id,'jump'=>[1,2,3,4,5,6,7,8,11,12,24,41,42,9,10,15,18,19,23,27,32,43,64]
        ], ['jump', 'jump_name']);
        $jumpList = array_column($jumpList,'jump_name','jump');
        ksort($jumpList);

        return $jumpList;
    }

    private function getRealActionType($action_type) {
        if (in_array($action_type, [
            ThemeConstants::ACTION_TYPE_MK_HOME_PAGE,
            ThemeConstants::ACTION_TYPE_GC_HOME_PAGE,
            ThemeConstants::ACTION_TYPE_MK_APP_DETAIL,
            ThemeConstants::ACTION_TYPE_MK_ACTIVITY_DETAIL,
            ThemeConstants::ACTION_TYPE_GC_ACTIVITY_DETAIL,
            ThemeConstants::ACTION_TYPE_SDK_SHOP_HOME_PAGE,
            ThemeConstants::ACTION_TYPE_SDK_SHOP_CATEGORY_PAGE,
        ])) {
            return ThemeConstants::ACTION_TYPE_OAPS;
        }
        return $action_type;
    }

    public static function getSkuById() {
        $params = Common::params();
        try {
            $ret = CurlServiceDao::get( '/cardadmin/goods/info', [ 'skuId' => $params['skuId'] ], 'saveMessageApi', false);
            $ret = json_decode( $ret, true );
            if ( 200 != $ret['code'] || empty( $ret['data'] ) ) {
                throw new ThemeException( ThemeErrMap::ERROR_OPERATION, '商品信息有误,code:' . $ret['code'] . ',data:' . var_export( $ret['data'], true ) );
            }
            exit( json_encode( ['errno' => 0, 'data' => $ret['data']] ) );
        } catch ( \Exception $e ) {
            exit( json_encode( ['errno' => 1, 'data' => [], 'msg' => $e->getMessage()] ) );
        }
    }

    public static function checkSkuId( $skuId ) {
        try {
            $ret = CurlServiceDao::get( '/cardadmin/goods/info', [ 'skuId' => $skuId ], 'saveMessageApi', false);
            $ret = json_decode( $ret, true );
            if ( 200 != $ret['code'] || empty( $ret['data'] ) ) {
                throw new ThemeException( ThemeErrMap::ERROR_OPERATION, '商品信息有误,code:' . $ret['code'] . ',data:' . var_export( $ret['data'], true ) );
            }
            if ( 1 != $ret['data']['visible'] ) { // 是否上架
                throw new ThemeException( ThemeErrMap::ERROR_OPERATION, '商品未上架' );
            }
        } catch ( \Exception $e ) {
            throw new ThemeException( ThemeErrMap::ERROR_OPERATION, $e->getMessage() );
        }
    }

    /**
     * 预约游戏App
     */
    public function getGameApp()
    {
        $params = Common::params();

        $searchData = [];
        try {
            // 调用接口
            $result = CurlServiceDao::get(self::QUERY_APP_URL, ['appId' => $params['id']], 'themeTemplateApi', false);
            if ($result) {
                $response_data = json_decode($result, true);
//                print_r($response_data);
                if ($response_data && is_array($response_data)) {
                    if ($response_data['appId'] && $response_data['appName'] && $response_data['pkgName']) {
                        $item = [
                            'id' => $response_data['appId'],
                            'name' => $response_data['appName'],
                            'description' => $response_data['desc'],
                            'pkg_name' => $response_data['pkgName'],
                        ];
                        $searchData[] = $item;
                    }
                }
            }
        } catch (\Exception $e) {
//            var_dump($e);
        }

        $start = (int)Yii::$app->request->post('offset', (int)Yii::$app->request->get('offset', 0));
        $limit = (int)Yii::$app->request->post('limit', (int)Yii::$app->request->get('limit', 10));

        $info = ['rows' => array_slice($searchData, $start, $limit), 'total' => count($searchData)];


        return $info;
    }

    /**
     * 游戏卡片列表
     */
    public function selectGameCard()
    {
        $params = Common::params();

        $where = [
            'and',
        ];
        if ($params['name']) {
            $where[] = ['like', 'name', $params['name']];
        }
        if ($params['desc']) {
            $where[] = ['like', 'desc', $params['desc']];
        }
        if ($params['app_name']) {
            $where[] = ['like', 'app_name', $params['app_name']];
        }
        if ($params['page_name']) {
            $app_ids = [];
            // 根据页面名称筛选t_app_home_page信息
            $info_where = ['and'];
            $info_where[] = ['like', 'name', $params['page_name']];
            $info_list = CdoOperationThemeDao::getInstance()->getWhereList('t_app_home_page', $info_where, 'app_id');
            if ($info_list) {
                $app_ids = array_column($info_list, 'app_id');
            }
            $where[] = ['app_id' => $app_ids];
        }

        $info = CdoOperationThemeDao::getInstance()->getBootstrapList('t_app_resource_card', $where);
        if ($info['rows']) {
            foreach ($info['rows'] as &$row) {
                $t_app_home_page = CdoOperationThemeDao::getInstance()->getOneWhere(['app_id' => $row['app_id']], '*', 't_app_home_page');
                if ($t_app_home_page) {
                    $row['page_name'] = $t_app_home_page['name'];
                    $row['head_img'] = $t_app_home_page['head_img'];
                    $row['focus_color'] = $t_app_home_page['focus_color'];
                    $row['button_color'] = $t_app_home_page['button_color'];
                    $row['res_url'] = $t_app_home_page['res_url'];
                } else {
                    $row['page_name'] = '';
                    $row['head_img'] = '';
                    $row['focus_color'] = '';
                    $row['button_color'] = '';
                    $row['res_url'] = '';
                }
                $explicit_resource = ''; // 外显资源
                $res_types = explode(',', $row['res_types']);
                $res_ids = explode(',', $row['res_ids']);
                if ($res_types && $res_ids) {
                    $resourceIds = [];
                    foreach ($res_types as $k => $type) {
                        $resourceIds[$type][] = $res_ids[$k];
                    }
                    $resourceInfoMap = [];
                    foreach ($resourceIds as $type => $ids) {
                        // 视频铃声,自营铃声,动态壁纸
                        if (ThemeConstants::SOURCE_TYPE_VIDEO_RING == $type ||
                            ThemeConstants::SOURCE_TYPE_RING == $type ||
                            ThemeConstants::SOURCE_TYPE_LIVE_WP == $type || ThemeConstants::SOURCE_TYPE_SCREEN_CLOCK == $type) {
                            $resourceInfoMap += Helper::getResourceDao($type)->getNewCurVersionInfoByIds($type, $ids);
                        } else {
                            $resourceInfoMap += Helper::getResourceDao($type)->getVersionInfoByIds($ids);
                        }
                    }
                    $explicit_resource_list = [];
                    foreach ($res_types as $k => $type) {
                        $type_name = ThemeConstants::SOURCE_TYPE_TEXT_MAP[$type] ?: '';
                        $resource_id = $res_ids[$k];
                        $resource_name = $resourceInfoMap[$resource_id]['name'] ?: '';
                        $explicit_resource_list[] = $type_name . ':' . $resource_name . '(' . $resource_id . ')';
                    }
                    $explicit_resource = implode('<br>', $explicit_resource_list);
                }
                $row['explicit_resource'] = $explicit_resource; // 外显资源
            }
        }

        return $info;
    }

    /**
     * 新增卡片配置
     */
    public function addGameCard()
    {
        $params = Common::params();
        if (!$params['name'] || !$params['desc'] || !$params['excel_url'] || !$params['app_id'] || !$params['app_name'] || !$params['operation_reason']) {
            throw new ThemeException(ThemeErrMap::ERROR_PARAMS, '参数错误');
        }
        Yii::$app->params['operation'] = $params['operation_reason'];
        $this->checkLimit($params);
        $file_path = CommonModel::downloadFile('appDomain', $params['excel_url']);
        if (!file_exists($file_path)) {
            throw new ThemeException(ThemeErrMap::ERROR_OPERATION, '请选择文件并上传');
        }
        $templateData = self::getTemplateData($file_path);
        // 检查资源
        $resource_result = $this->checkResource($templateData);

        $item = [
            'name' => $params['name'],
            'desc' => $params['desc'],
            'res_types' => $resource_result['res_types'],
            'res_ids' => $resource_result['res_ids'],
            'app_id' => $params['app_id'],
            'app_name' => $params['app_name'],
            'create_time' => date('Y-m-d H:i:s'),
            'update_time' => date('Y-m-d H:i:s'),
            'create_operator' => Yii::$app->session['userInfo']['real_name'],
            'update_operator' => Yii::$app->session['userInfo']['real_name'],
        ];

        CdoOperationThemeDao::getInstance()->addOne($item, 't_app_resource_card');
    }

    /**
     * 编辑卡片配置
     */
    public function editGameCard()
    {
        $params = Common::params();
        if (!is_numeric($params['id'])) {
            throw new ThemeException(ThemeErrMap::ERROR_PARAMS, 'id不能为空');
        }
        $info = CdoOperationThemeDao::getInstance()->getOne($params['id'], '*', 't_app_resource_card');
        if (!$info) {
            throw new ThemeException(ThemeErrMap::ERROR_PARAMS, '该记录不存在');
        }
        if (!$params['name'] || !$params['desc'] || !$params['excel_url'] || !$params['app_id'] || !$params['app_name'] || !$params['operation_reason']) {
            throw new ThemeException(ThemeErrMap::ERROR_PARAMS, '参数错误');
        }
        Yii::$app->params['operation'] = $params['operation_reason'];
        $this->checkLimit($params);
        $file_path = CommonModel::downloadFile('appDomain', $params['excel_url']);
        if (!file_exists($file_path)) {
            throw new ThemeException(ThemeErrMap::ERROR_OPERATION, '请选择文件并上传');
        }
        $templateData = self::getTemplateData($file_path);
        // 检查资源
        $resource_result = $this->checkResource($templateData);

        $item = [
            'name' => $params['name'],
            'desc' => $params['desc'],
            'res_types' => $resource_result['res_types'],
            'res_ids' => $resource_result['res_ids'],
            'app_id' => $params['app_id'],
            'app_name' => $params['app_name'],
            'update_time' => date('Y-m-d H:i:s'),
            'update_operator' => Yii::$app->session['userInfo']['real_name'],
        ];
        CdoOperationThemeDao::getInstance()->saveOne($params['id'], $item, 't_app_resource_card');
    }

    /**
     * 删除卡片配置
     */
    public function deleteGameCard()
    {
        $params = Common::params();
        if (!is_numeric($params['id'])) {
            throw new ThemeException(ThemeErrMap::ERROR_PARAMS, 'id不能为空');
        }
        $info = CdoOperationThemeDao::getInstance()->getOne($params['id'], '*', 't_app_resource_card');
        if (!$info) {
            throw new ThemeException(ThemeErrMap::ERROR_PARAMS, '该记录不存在');
        }
        Yii::$app->params['operation'] = $params['operation_reason'];
        $transaction = CdoOperationThemeDao::getDb()->beginTransaction();
        try {
            CdoOperationThemeDao::getInstance()->delOne($params['id'], 't_app_resource_card');
            CdoOperationThemeDao::getInstance()->delWhere(['app_id' => $info['app_id']], 't_app_home_page');
            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollBack();
            throw new ThemeException($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 修改绑定游戏主页
     */
    public function editBindGame()
    {
        $params = Common::params();
        if (!is_numeric($params['id'])) {
            throw new ThemeException(ThemeErrMap::ERROR_PARAMS, 'id不能为空');
        }
        $t_app_resource_card = CdoOperationThemeDao::getInstance()->getOne($params['id'], '*', 't_app_resource_card');
        if (!$t_app_resource_card) {
            throw new ThemeException(ThemeErrMap::ERROR_PARAMS, '该记录不存在');
        }
        if (!$params['page_name'] || !$params['res_url'] || !$params['head_img'] || !$params['focus_color'] || !$params['button_color'] || !$params['operation_reason']) {
            throw new ThemeException(ThemeErrMap::ERROR_PARAMS, '参数错误');
        }
        Yii::$app->params['operation'] = $params['operation_reason'];

        $t_app_home_page = CdoOperationThemeDao::getInstance()->getOneWhere(['app_id' => $t_app_resource_card['app_id']], '*', 't_app_home_page');
        if ($t_app_home_page) {
            $info = [
                'name' => $params['page_name'],
                'res_url' => $params['res_url'],
                'head_img' => $params['head_img'],
                'focus_color' => $params['focus_color'],
                'button_color' => $params['button_color'],
                'update_time' => date('Y-m-d H:i:s'),
                'update_operator' => Yii::$app->session['userInfo']['real_name'],
            ];
            CdoOperationThemeDao::getInstance()->saveOne($t_app_home_page['id'], $info, 't_app_home_page');
        } else {
            $info = [
                'app_id' => $t_app_resource_card['app_id'],
                'name' => $params['page_name'],
                'res_url' => $params['res_url'],
                'head_img' => $params['head_img'],
                'focus_color' => $params['focus_color'],
                'button_color' => $params['button_color'],
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s'),
                'create_operator' => Yii::$app->session['userInfo']['real_name'],
                'update_operator' => Yii::$app->session['userInfo']['real_name'],
            ];
            CdoOperationThemeDao::getInstance()->addOne($info, 't_app_home_page');
        }
    }

    /**
     * 判断游戏APP是否重复
     */
    private function checkLimit($params)
    {
        $where = [
            'and',
            ['=', 'app_id', $params['app_id']],
        ];
        if ($params['id']) {
            $where[] = ['!=', 'id', $params['id']];
        }

        $count = CdoOperationThemeDao::getInstance()->find()->from('t_app_resource_card')->where($where)->count();
        if ($count > 0) {
            throw new ThemeException(ThemeErrMap::ERROR_PARAMS, '该游戏APP已绑定');
        }
    }

    private static function getTemplateData($excel_url)
    {
        $limit = 500;
        $data = [];
        if ($excel_url) {
            set_time_limit(30 * 60);
            ini_set('memory_limit', '500M');
            $phpexcel = \PHPExcel_IOFactory::load($excel_url);
            $phpexcel->getActiveSheet()->getStyle('A')->getNumberFormat()
                ->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER);
            $worksheet = $phpexcel->getSheet(0);
            $total_line = $worksheet->getHighestRow();
            $total_col = $worksheet->getHighestColumn();

            for ($line = 1; $line <= $total_line; $line++) {
                if (1 == $line) {
                    continue; // 跳过表头
                }
                $cellA = $worksheet->getCell('A' . $line)->getValue();
                $cellB = $worksheet->getCell('B' . $line)->getValue();
                if (empty($cellA) && empty($cellB)) {
                    continue;
                } elseif (empty($cellA) && !empty($cellB) || !empty($cellA) && empty($cellB)) {
                    throw new ThemeException(ThemeErrMap::ERROR_OPERATION, $line . '行有数据为空');
                }
                /*if ( ! is_string( $cellB ) ) {
                    $cellB = number_format($cellB, 0, '.', ''); // 科学计数法转化为字符串
                }*/
                $data[$cellA] = $cellB;
            }

            if (empty($data)) {
                throw new ThemeException(ThemeErrMap::ERROR_OPERATION, '导入数据为空,total_line:' . $total_line . ',url:' . $excel_url);
            }
            if (count($data) > intval($limit)) {
                throw new ThemeException(ThemeErrMap::ERROR_OPERATION, '导入数据不能超过' . $limit . '条');
            }
        }
        return $data;
    }

    private function checkResource($arrResources)
    {
        /**
         * 外显资源:
         * 在详情游戏主页模块外显(竖图样式),最少上传3个资源
         * 支持不同资源批量上传(主题/字体/壁纸/动态壁纸/视频铃声)
         * 外显资源需与游戏主页内资源保持一致(运营配置保证)
         */
        $count = count($arrResources);
        if ($count < 3) {
            throw new ThemeException(ThemeErrMap::ERROR_OPERATION, '最少上传3个资源');
        }
        $batchData = [];
        foreach ($arrResources as $key => $value) {
            if (!$value) {
                continue;
            }
            $data['resource_id'] = $key;
            $data['resource_type_name'] = $value;
            $data['resource_type'] = array_search($value, ThemeConstants::SOURCE_TYPE_TEXT_MAP);
            $limit_resource_type_list = [
                ThemeConstants::SOURCE_TYPE_THEME,
                ThemeConstants::SOURCE_TYPE_FONT,
                ThemeConstants::SOURCE_TYPE_WP,
                ThemeConstants::SOURCE_TYPE_VIDEO_RING,
                ThemeConstants::SOURCE_TYPE_LIVE_WP,
            ];
            if (!$data['resource_type'] || !in_array($data['resource_type'], $limit_resource_type_list)) {
                throw new ThemeException(ThemeErrMap::ERROR_CREATE, '导入资源类型错误:' . $data['resource_id']);
            }
            $resourceInfo = self::getResourceInfo($data['resource_id'], $data['resource_type']);
            if (!$resourceInfo) {
                throw new ThemeException(ThemeErrMap::ERROR_CREATE, '资源不存在:' . $data['resource_id']);
            }
            $batchData[] = $data;
        }
        if (count($batchData) < 3) {
            throw new ThemeException(ThemeErrMap::ERROR_OPERATION, '最少上传3个资源');
        }

        return [
            'res_types' => implode(',', array_column($batchData, 'resource_type')),
            'res_ids' => implode(',', array_column($batchData, 'resource_id')),
        ];
    }

    protected static function getResourceInfo($resource_id, $resource_type)
    {
        switch (intval($resource_type)) {
            case ThemeConstants::SOURCE_TYPE_THEME:
                $info = CdoOpenThemeDao::getInstance()->getOneWhere(['theme_id' => $resource_id], '*', 'theme_version');
                if (!$info) {
                    $info = CdoOpenThemeDao::getInstance()->getOneWhere(['theme_id' => $resource_id], '*', 'theme');
                }
                break;
            case ThemeConstants::SOURCE_TYPE_FONT:
                $info = CdoOpenThemeDao::getInstance()->getOneWhere(['font_id' => $resource_id], '*', 'font');
                if (!$info) {
                    $info = CdoOpenThemeDao::getInstance()->getOneWhere(['font_id' => $resource_id], '*', 'font_version');
                }
                break;
            case ThemeConstants::SOURCE_TYPE_WP:
                $info = CdoOpenThemeDao::getInstance()->getOneWhere(['wp_id' => $resource_id], '*', 'wallpaper');
                if (!$info) {
                    $info = CdoOpenThemeDao::getInstance()->getOneWhere(['wp_id' => $resource_id], '*', 'wallpaper_version');
                }
                break;
            case ThemeConstants::SOURCE_TYPE_VIDEO_RING:
                $info = CdoOpenThemeDao::getInstance()->getOneWhere(['app_id' => $resource_id], '*', 'video_ring');
                if (!$info) {
                    $info = CdoOpenThemeDao::getInstance()->getOneWhere(['app_id' => $resource_id], '*', 'video_ring_version');
                }
                break;
            case ThemeConstants::SOURCE_TYPE_RING:
                $info = CdoOpenThemeDao::getInstance()->getOneWhere(['app_id' => $resource_id], '*', 'ring_version');
                if (!$info) {
                    $info = CdoOpenThemeDao::getInstance()->getOneWhere(['app_id' => $resource_id], '*', 'ring');
                }
                break;
            case ThemeConstants::SOURCE_TYPE_LIVE_WP:
                $info = CdoOpenThemeDao::getInstance()->getOneWhere(['app_id' => $resource_id], '*', 'live_wp_version');
                if (!$info) {
                    $info = CdoOpenThemeDao::getInstance()->getOneWhere(['app_id' => $resource_id], '*', 'live_wp');
                }
                break;
            case ThemeConstants::SOURCE_TYPE_SCREEN_CLOCK:
                $info = CdoOpenThemeDao::getInstance()->getOneWhere(['app_id' => $resource_id], '*', 'screen_clock');
                if (!$info) {
                    $info = CdoOpenThemeDao::getInstance()->getOneWhere(['app_id' => $resource_id], '*', 'screen_clock_version');
                }
                break;
            default:
                throw new ThemeException(ThemeErrMap::ERROR_PARAMS, '资源类型异常');
                break;
        }
        return $info;
    }

    /**
     * 拉取游戏素材
     */
    public function getGameMaterial()
    {
        $params = Common::params();
        if (!is_numeric($params['id'])) {
            throw new ThemeException(ThemeErrMap::ERROR_PARAMS, 'id不能为空');
        }
        $t_app_resource_card = CdoOperationThemeDao::getInstance()->getOne($params['id'], '*', 't_app_resource_card');
        if (!$t_app_resource_card) {
            throw new ThemeException(ThemeErrMap::ERROR_PARAMS, '该记录不存在');
        }
        $app_id = $t_app_resource_card['app_id'];
        // @todo调用接口拉取游戏素材

        $info = [
            'head_img' => '', // 游戏头图
            'focus_color' => '', // 游戏焦点色
            'button_color' => '', // 游戏按钮色
        ];

        return $info;
    }
}
<?php


namespace theme\modules\operate\controllers;


use common\extensions\Common;
use common\models\CommonModel;
use theme\exception\ThemeErrMap;
use theme\exception\ThemeException;
use yii\base\Model;
use Yii;

class VipBannerConfig extends Model
{
    // 会员开通横条 通配符
    public static $vipSymbol = [
        '{vip_discount}' => '会员折扣',
        '{vip_discount_amount}' => '优惠金额',
        '{vip_gift}' => '赠品',
        '{vip_left_days}' => '会员将到期天数',
        '{vip_overdue_days}' => '会员过期天数',
    ];
    // 优惠券横条 通配符
    public static $ticketSymbol = [
        '{voucher_discount}' => '优惠折扣',
        '{voucher_discount_amount}' => '优惠金额',
        '{voucher_limit}' => '使用门槛',
        '{voucher_type}' => '券类型',
    ];
    // 任务广告横条 通配符
//    public static $adSymbol = [
//        '{time_limit_price}' => '限时价格',
//    ];

    // 会员资源类型
    const RES_PAY_TYPE_NORMAL_FREE = 1; // 普通免费
    const RES_PAY_TYPE_VIP_PAY= 2; // 普通付费
    const RES_PAY_TYPE_VIP_FREE = 3; // 会员免费
    const RES_PAY_TYPE_VIP_DISCOUNT = 4; // 会员折扣
    const RES_PAY_TYPE_VIP_ONLY = 5; // 会员专享
    const RES_PAY_TYPE_VIP_FIRST = 6; // 会员先享

    // 会员身份状态
    const VIP_STATUS_NOT_VIP_RECORD = 0; // 未开通过VIP的用户
    const VIP_STATUS_EXPIRED_OVER_10 = 1; // 过期>=10天
    const VIP_STATUS_BETWEEN_1_AND_5 = 2; // 1<会员剩余时间<=5天
    const VIP_STATUS_TODAY_EXPIRED_VIP = 3; // VIP今日到期
    const VIP_STATUS_LESS_10_DAYS_EXPIRED_VIP = 4; // VIP过期<10天

    // 会员资源类型
    public static $resPayTypeList = [
        self::RES_PAY_TYPE_NORMAL_FREE => '普通免费',
        self::RES_PAY_TYPE_VIP_PAY => '普通付费',
        self::RES_PAY_TYPE_VIP_FREE => '会员免费',
        self::RES_PAY_TYPE_VIP_DISCOUNT => '会员折扣',
        self::RES_PAY_TYPE_VIP_ONLY => '会员专享',
        self::RES_PAY_TYPE_VIP_FIRST => '会员先享',
    ];

    // 会员身份状态
    public static $vipStatusList = [
        self::VIP_STATUS_NOT_VIP_RECORD => '未开通过VIP的用户(含未登录)',
        self::VIP_STATUS_BETWEEN_1_AND_5 => '1<=会员剩余时间<=5天',
        self::VIP_STATUS_TODAY_EXPIRED_VIP => 'VIP今日到期',
        self::VIP_STATUS_LESS_10_DAYS_EXPIRED_VIP => 'VIP过期<10天',
        self::VIP_STATUS_EXPIRED_OVER_10 => '过期>=10天',
    ];

    // 主文案、副文案、按钮、收起文案 字节长度限制
    public static $lengthLimit = [
        'mainText' => 16,
        'subText' => 24,
        'buttonText' => 6,
        'putAwayText' => 34,
    ];

    // 模板的行号对应的会员资源类型和会员身份状态
    public static $tempLineStatus = [
        '2' => self::RES_PAY_TYPE_NORMAL_FREE . '_' . self::VIP_STATUS_NOT_VIP_RECORD,
        '3' => self::RES_PAY_TYPE_NORMAL_FREE . '_' . self::VIP_STATUS_BETWEEN_1_AND_5,
        '4' => self::RES_PAY_TYPE_NORMAL_FREE . '_' . self::VIP_STATUS_TODAY_EXPIRED_VIP,
        '5' => self::RES_PAY_TYPE_NORMAL_FREE . '_' . self::VIP_STATUS_LESS_10_DAYS_EXPIRED_VIP,
        '6' => self::RES_PAY_TYPE_NORMAL_FREE . '_' . self::VIP_STATUS_EXPIRED_OVER_10,
        '7' => self::RES_PAY_TYPE_VIP_PAY . '_' . self::VIP_STATUS_NOT_VIP_RECORD,
        '8' => self::RES_PAY_TYPE_VIP_PAY . '_' . self::VIP_STATUS_BETWEEN_1_AND_5,
        '9' => self::RES_PAY_TYPE_VIP_PAY . '_' . self::VIP_STATUS_TODAY_EXPIRED_VIP,
        '10' => self::RES_PAY_TYPE_VIP_PAY . '_' . self::VIP_STATUS_LESS_10_DAYS_EXPIRED_VIP,
        '11' => self::RES_PAY_TYPE_VIP_PAY . '_' . self::VIP_STATUS_EXPIRED_OVER_10,
        '12' => self::RES_PAY_TYPE_VIP_FREE . '_' . self::VIP_STATUS_NOT_VIP_RECORD,
        '13' => self::RES_PAY_TYPE_VIP_FREE . '_' . self::VIP_STATUS_BETWEEN_1_AND_5,
        '14' => self::RES_PAY_TYPE_VIP_FREE . '_' . self::VIP_STATUS_TODAY_EXPIRED_VIP,
        '15' => self::RES_PAY_TYPE_VIP_FREE . '_' . self::VIP_STATUS_LESS_10_DAYS_EXPIRED_VIP,
        '16' => self::RES_PAY_TYPE_VIP_FREE . '_' . self::VIP_STATUS_EXPIRED_OVER_10,
        '17' => self::RES_PAY_TYPE_VIP_DISCOUNT . '_' . self::VIP_STATUS_NOT_VIP_RECORD,
        '18' => self::RES_PAY_TYPE_VIP_DISCOUNT . '_' . self::VIP_STATUS_BETWEEN_1_AND_5,
        '19' => self::RES_PAY_TYPE_VIP_DISCOUNT . '_' . self::VIP_STATUS_TODAY_EXPIRED_VIP,
        '20' => self::RES_PAY_TYPE_VIP_DISCOUNT . '_' . self::VIP_STATUS_LESS_10_DAYS_EXPIRED_VIP,
        '21' => self::RES_PAY_TYPE_VIP_DISCOUNT . '_' . self::VIP_STATUS_EXPIRED_OVER_10,
        '22' => self::RES_PAY_TYPE_VIP_ONLY . '_' . self::VIP_STATUS_NOT_VIP_RECORD,
        '23' => self::RES_PAY_TYPE_VIP_ONLY . '_' . self::VIP_STATUS_BETWEEN_1_AND_5,
        '24' => self::RES_PAY_TYPE_VIP_ONLY . '_' . self::VIP_STATUS_TODAY_EXPIRED_VIP,
        '25' => self::RES_PAY_TYPE_VIP_ONLY . '_' . self::VIP_STATUS_LESS_10_DAYS_EXPIRED_VIP,
        '26' => self::RES_PAY_TYPE_VIP_ONLY . '_' . self::VIP_STATUS_EXPIRED_OVER_10,
        '27' => self::RES_PAY_TYPE_VIP_FIRST . '_' . self::VIP_STATUS_NOT_VIP_RECORD,
        '28' => self::RES_PAY_TYPE_VIP_FIRST . '_' . self::VIP_STATUS_BETWEEN_1_AND_5,
        '29' => self::RES_PAY_TYPE_VIP_FIRST . '_' . self::VIP_STATUS_TODAY_EXPIRED_VIP,
        '30' => self::RES_PAY_TYPE_VIP_FIRST . '_' . self::VIP_STATUS_LESS_10_DAYS_EXPIRED_VIP,
        '31' => self::RES_PAY_TYPE_VIP_FIRST . '_' . self::VIP_STATUS_EXPIRED_OVER_10,
    ];

    // 列文案信息
    public static $tempColumnText = [
        'C' => 'mainText', // C列为主文案
        'D' => 'subText', // D列为副文案
        'E' => 'buttonText', // E列为按钮
        'F' => 'putAwayText', // F列为收起文案
    ];

    public static $templateList = [];

    public function init()
    {
        foreach ( self::$resPayTypeList as $resPayType => $resPayTypeName ) {
            $tmp = [
                'res_pay_type' => $resPayType,
                'res_pay_type_name' => $resPayTypeName,
                'vipInfo' => [],
            ];
            foreach ( self::$vipStatusList as $vipStatus => $vipStatusName ) {
                $tmp['vipInfo'][] = [
                    'vip_status' => $vipStatus,
                    'vip_status_name' => $vipStatusName,
                ];
            }
            self::$templateList[] = $tmp;
        }
    }

    /**
     * 模板导入
     */
    public function import() {
        set_time_limit(30 * 60);
        ini_set('memory_limit', '500M');
        $params = Common::params();
        Yii::$app->params['operation'] = $params['operation_reason'] ?: '会员横条文件导入';

        if ( empty( $params['save_path'] ) ) {
            throw new ThemeException( ThemeErrMap::ERROR_PARAMS, '文件链接不能为空' );
        }

        $path = CommonModel::downloadFile('', $params['save_path']);
        $phpexcel = \PHPExcel_IOFactory::load($path);
        $worksheet = $phpexcel->getSheet(0);
//        $total_line = $worksheet->getHighestRow(); //总行数
//        $total_column = $worksheet->getHighestColumn(); //英文总列数

        $data = [];
        foreach ( self::$tempColumnText as $column => $fieldName ) {
            foreach ( self::$tempLineStatus as $line => $statusInfo ) {
                $arrStatus = explode('_', $statusInfo);
                $value = $worksheet->getCell($column . $line)->getValue();
                if ( empty( $value ) ) {
                    throw new ThemeException( ThemeErrMap::ERROR_OPERATION, '不赠送单条工作表中' . $column . $line . '不能为空' );
                }
                $data[ $fieldName ][ $arrStatus[0] ][ $arrStatus[1] ] = trim( $value );
            }
        }
        $detail_config = json_encode(self::getDetailConfig($data), 256);

        try {
            $worksheet1 = $phpexcel->getSheet(1);
        } catch (\Exception $e) {
            throw new ThemeException(ThemeErrMap::ERROR_OPERATION, '有赠送单条工作表打开失败#' . $e->getMessage());
        }
        $data1 = [];
        foreach (self::$tempColumnText as $column => $fieldName) {
            foreach (self::$tempLineStatus as $line => $statusInfo) {
                $arrStatus = explode('_', $statusInfo);
                $value = $worksheet1->getCell($column . $line)->getValue();
                if (empty($value)) {
                    throw new ThemeException(ThemeErrMap::ERROR_OPERATION, '有赠送单条工作表中' . $column . $line . '不能为空');
                }
                $data1[$fieldName][$arrStatus[0]][$arrStatus[1]] = trim($value);
            }
        }
        $detail_config1 = json_encode(self::getDetailConfig($data1), 256);

        return ['detail_config' => $detail_config, 'detail_config1' => $detail_config1];
    }

    /**
     * 解析详情配置
     * @param $params
     * @param int $isDefault 是否兜底配置
     * @return array
     */
    public static function getDetailConfig( $params, $isDefault = 0 ) {
        $detailConfig = [];
        foreach ( self::$lengthLimit as $name => $_ ) {
            $paramName = $isDefault ? ($name . '_default') : $name;
            foreach ( $params[ $paramName ] as $resPayType => $vipInfo ) {
                foreach ( $vipInfo as $vipStatus => $text ) {
                    $detailConfig[ $resPayType ][ $vipStatus ]['resPayType'] = $resPayType;
                    $detailConfig[ $resPayType ][ $vipStatus ]['vipStatus'] = $vipStatus;
                    $detailConfig[ $resPayType ][ $vipStatus ][ $name ] = $text;
                }
            }
        }

        $data = [];
        foreach ( $detailConfig as $item ) {
            foreach ( $item as $config ) {
                array_push( $data, $config );
            }
        }

        return $data;
    }

    /**
     * 有赠送单条解析详情配置
     * @param $params
     * @param int $isDefault 是否兜底配置
     * @return array
     */
    public static function getDetailConfig1( $params, $isDefault = 0 ) {
        $detailConfig = [];
        foreach ( self::$lengthLimit as $name => $_ ) {
            $paramName = $isDefault ? ($name . '_default1') : ($name . '1');
            foreach ( $params[ $paramName ] as $resPayType => $vipInfo ) {
                foreach ( $vipInfo as $vipStatus => $text ) {
                    $detailConfig[ $resPayType ][ $vipStatus ]['resPayType'] = $resPayType;
                    $detailConfig[ $resPayType ][ $vipStatus ]['vipStatus'] = $vipStatus;
                    $detailConfig[ $resPayType ][ $vipStatus ][ $name ] = $text;
                }
            }
        }

        $data = [];
        foreach ( $detailConfig as $item ) {
            foreach ( $item as $config ) {
                array_push( $data, $config );
            }
        }

        return $data;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值