<?php

use App\Base\Base;
use App\Models\marketing\mysql\Marketing;
use App\Services\marketing\MarketingService;
use App\Exception\custom\MarketingException;
use App\Services\marketing\MarketingGoodsService;
use \Validate\MarketingGoodsRateValidate;
use \Validate\MarketingOnlineStatusValidate;
use \Validate\MarketingInfoValidate;
use Api\PhpUtils\Redis\RedisUtil;

class MarketingController extends Base
{

    public function get_marketing_listAction()
    {
        $params = $this->params;
        $list = MarketingService::getMarketingList($params);
        $distributionList['result'] = $list;
        $this->success($distributionList);
    }

    /**
     * 营销列表
     * 后台管理
     */
    public function marketing_listAction()
    {
        $params = $this->params;
        $distributionList = MarketingService::marketingList($params);
        $this->success($distributionList);
    }

    /**
     * 创建活动
     * 后台管理
     */
    public function add_marketingAction()
    {
        $params = $this->params;
        $addMarketing = MarketingService::addMarketing($params);
        if (!empty($addMarketing)) {
            $this->success();
        }
    }

    /**
     * op后台获取创建活动
     * 需要勾选的商品
     *
     * @param $params
     * @return array|mixed
     */
    public function marketing_goods_listAction()
    {
        $params = $this->params;
        $goodsList = MarketingService::marketingGooodsList($params);
        $this->success(['result' => $goodsList]);
    }


    /**
     * 更新数据
     * 后台管理
     *
     */
    public function update_marketingAction()
    {
        $params = $this->params;

        $where['marketing_id'] = !empty($params['marketing_id']) ? $params['marketing_id'] : '';
        $param = [];
        if (empty($where['marketing_id'])) {
            throw new MarketingException(['cus' => 6]);
        }

        if (!empty($params['marketing_name'])) {
            $param['marketing_name'] = $params['marketing_name'];
        }

        if (!empty($params['op_cur_user'])) {
            $param['update_user_email'] = $params['op_cur_user'];
        }

        if (!empty($params['online_status'])) {
            $param['online_status'] = $params['online_status'];
        }

        if (!empty($params['commission_mode'])) {
            $param['commission_mode'] = $params['commission_mode'];
        }

        if (!empty($params['second_commission_value'])) {
            $param['second_commission_value'] = $params['second_commission_value'];
        }

        if (!empty($params['first_commission_value'])) {
            $param['first_commission_value'] = $params['first_commission_value'];
        }

        if (!empty($params['start_time'])) {
            $param['start_time'] = $params['start_time'];
        }

        if (!empty($params['end_time'])) {
            $param['end_time'] = $params['end_time'];
        }

        $srt = MarketingService::updateMarketing($param, $where, $params);

        if (!empty($srt)) {
            $this->success();
        } else {
            throw new MarketingException(['cus' => 7]);
        }
    }


    /**
     * 获取详情数据
     * 后台管理
     *
     */
    public function marketing_infoAction()
    {
        (new MarketingInfoValidate())->validate();

        $params = $this->params;
        $marketingInfo = MarketingService::marketingInfo($params);
        $this->success(['result' => $marketingInfo]);
    }

    /**
     * 获取指定商品分销活动的分成比例（第一分销人的分成比例）
     * @throws Exception
     */
    public function marketing_goods_rateAction()
    {
        (new MarketingGoodsRateValidate())->validate();

        $params = $this->params;
        $goodsSkuIds = explode(",", $params["goods_sku_ids"]);
        $marketingInfo = MarketingGoodsService::getRunningMarketing($goodsSkuIds);
        $this->success(['result' => $marketingInfo]);
    }

    /**
     * 拼单活动修改活动状态
     * @throws Exception
     */
    public function update_marketing_online_statusAction()
    {
        (new MarketingOnlineStatusValidate())->validate();

        MarketingService::updateMarketingOnlineStatus($this->params);
        $this->success();
    }

    /**
     * app内团餐活动列表
     * @throws \App\Exception\custom\InterfaceException
     */
    public function tuancan_listAction()
    {
        $page = $this->params['page'] ?? 1;
        $pageSize = $this->params['page_size'] ?? 20;

        $list = MarketingService::getPindanActiveAndReadyList($page,$pageSize);
        $list['end'] = MarketingService::getPindanEndList($page,$pageSize);
        $list['life_account_id'] = MarketingService::getPublicLifeAccountId();

        $this->success(['result' => $list]);
    }

    /**
     * 正在进行中的活动简单信息列表
     * @throws \App\Exception\custom\InterfaceException
     */
    public function pindan_active_basic_listAction()
    {
        $excludeMarketingId = $this->params['exclude_marketing_id'] ?? '';
        $limit = $this->params['limit'] ?? 1;
        $list = MarketingService::pindanActiveBasicList($excludeMarketingId,$limit,true);
        $this->success(['result' => $list]);
    }

    /**
     * 正在进行和未开始的拼单活动列表
     * @throws \App\Exception\custom\InterfaceException
     */
    public function pindan_active_and_ready_listAction()
    {
        $page = $this->params['page'] ?? 1;
        $pageSize = $this->params['page_size'] ?? 50;
        $list = MarketingService::getPindanActiveAndReadyList($page,$pageSize);
        $this->success(['result' => $list]);
    }

    /**
     *
     * @throws Exception
     */
    public function pindan_end_listAction()
    {
        $limit = $this->params['limit'] ?? 5;
        $useCache = $this->params['use_cache'] ?? true;

        $list = MarketingService::getPindanEndList(1,$limit,$useCache);
        $this->success(['result' => $list]);
    }

    /**
     * 切换活动状态
     * @throws \App\Exception\custom\ParamException
     */
    public function toogle_yingxiao_online_statusAction()
    {
        (new MarketingOnlineStatusValidate())->scene('yingxiao')->validate();
        $res = MarketingService::toogleOnlineStatus($this->params['marketing_id'],$this->params['online_status']);

        $this->success(['result' => $res]);
    }

    /**
     * 营销中心-活动列表
     * @throws Exception
     */
    public function yingxiao_listAction()
    {
        $lists = MarketingService::yingxiaoList($this->params);
        $this->success(['result' => $lists]);
    }

    /**
     * 小程序审核查询设置safe_mode版本号
     * @throws Exception
     */
    public function get_small_program_configAction()
    {
        $redis = RedisUtil::getInstance('cache',['serializer'=>'none']);
        $key = "small_program_config";
        $value = $redis->get($key);
        $this->success(['result' => $value]);
    }

    /**
     * 小程序审核设置safe_mode版本号
     * @throws Exception
     */
    public function set_small_program_configAction()
    {
        $params = $this->params;
        $cvVersion = empty($params["version"]) ? "" : $params["version"];
        $redis = RedisUtil::getInstance('cache',['serializer'=>'none']);
        $key = "small_program_config";
        $res = $redis->set($key, $cvVersion);
        $this->success(['result' => $res]);
    }

    public function get_operate_configAction()
    {
        $userId = $this->params['user_id'] ?? 0;
        $res = MarketingService::getMarketingOperateConfig($userId);
        $this->success(['result' => $res]);
    }

    /**
     * 查询活动是否在线 1在线 0不在线
     * @throws \App\Exception\custom\ParamException
     */
    public function is_onlineAction()
    {
        (new MarketingInfoValidate())->validate();

        $marketingId = $this->params['marketing_id'];
        $status = MarketingService::getStatusByMarketingIds($marketingId);

        if(isset($status[$marketingId]) && $status[$marketingId]['online_status'] == Marketing::ONLINE_STATUS_QIDONG) {
            $result = 1;
        }else {
            $result = 0;
        }
        $this->success(['result' => $result]);
    }
}