<?php

use App\Base\Base;
use App\Services\marketing\MarketingService;
use App\Exception\custom\MarketingException;
use App\Services\marketing\MarketingGoodsService;
use \Validate\MarketingGoodsRateValidate;
use \Validate\MarketingOnlineStatusValidate;
use \Validate\MarketingInfoValidate;

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();
    }

    /**
     * 切换活动状态
     * @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]);
    }
}