<?php
/**
 * Created by PhpStorm.
 * User: pengfei@yidian-inc.com
 * Date: 2021/8/20 2:06 下午
 */

use App\Base\Base;
use App\Services\marketing\ColonelService;
use \Validate\ColonelApplyValidate;
use \Validate\ColonelConfigValidate;
use \Validate\ColonelAuditValidate;
use \Validate\ColonelCenterValidate;
use \Validate\ColonelAddPointValidate;
use \App\Services\marketing\PindanActivityColonelConfigService;
use \App\Services\marketing\PindanActivityInviteOrderService;
use \App\Services\user\UserService;

class ColonelController extends Base
{

    /**
     * Notes: 申请成为团长
     * User: pengfei@yidian-inc.com
     * Date: 2021/8/20 3:21 下午
     * @throws \App\Exception\custom\MarketingException
     * @throws \App\Exception\custom\ParamException
     */
    public function applyAction()
    {
        $params = $this->params;
        (new ColonelApplyValidate())->validate();
        $applyId = ColonelService::apply($params);
        return $this->success(['result' => ['apply_id' => $applyId]]);
    }

    /**
     * Notes: 是否已申请
     * User: pengfei@yidian-inc.com
     * Date: 2021/8/25 9:56 上午
     * @throws \App\Exception\custom\ParamException
     */
    public function is_applyAction()
    {
        $params = $this->params;
        (new ColonelCenterValidate())->validate();
        $applyInfo = ColonelService::isApply($params['user_id']);
        return $this->success(['result' => $applyInfo]);
    }

    /**
     * 编辑团长分销配置
     * @throws \App\Exception\custom\MarketingException
     * @throws \App\Exception\custom\ParamException
     */
    public function add_configAction()
    {
        (new ColonelConfigValidate())->scene('add')->validate();

        $params = $this->params;
        PindanActivityColonelConfigService::editColonelConfig($params);
        return $this->success();
    }

    /**
     * 查看团长分销配置
     * @throws \App\Exception\custom\ParamException
     */
    public function colonel_configAction()
    {
        $params = $this->params;
        $data = PindanActivityColonelConfigService::colonelConfig($params);
        return $this->success(["result" => $data]);
    }

    /**
     * Notes: 管理后台 - 申请团长审核列表
     * User: pengfei@yidian-inc.com
     * Date: 2021/8/23 2:17 下午
     */
    public function manage_colonel_apply_listAction()
    {
        $params = $this->params;
        $manageList = ColonelService::getManageApplyList($params);
        return $this->success($manageList);
    }

    /**
     * Notes: 管理后台 - 团长审核列表
     * User: pengfei@yidian-inc.com
     * Date: 2021/8/24 3:09 下午
     */
    public function manage_colonel_listAction()
    {
        $params = $this->params;
        $manageList = ColonelService::getManageList($params);
        return $this->success($manageList);
    }

    /**
     * Notes: 管理后台 - 申请团长审核
     * User: pengfei@yidian-inc.com
     * Date: 2021/8/23 2:16 下午
     * @throws \App\Exception\custom\MarketingException
     */
    public function manage_auditAction()
    {
        $params = $this->params;
        (new ColonelAuditValidate())->validate();
        ColonelService::handleManageAudit($params['colonel_apply_id'], $params['audit_status']);
        return $this->success();
    }

    /**
     * Notes: 管理后台 - 获取自提点信息
     * User: pengfei@yidian-inc.com
     * Date: 2021/8/24 2:43 下午
     */
    public function manage_show_apply_pointAction()
    {
        $params = $this->params;
        $colonelApply = ColonelService::getTakePlaceInfo($params['colonel_apply_id']);
        return $this->success(['result' => $colonelApply]);
    }

    /**
     * Notes: 管理后台 - 添加自提点信息
     * User: pengfei@yidian-inc.com
     * Date: 2021/8/25 2:52 下午
     * @throws \App\Exception\custom\MarketingException
     * @throws \App\Exception\custom\ParamException
     */
    public function manage_add_apply_pointAction()
    {
        $params = $this->params;
        (new ColonelAddPointValidate())->validate();
        $takePlaceId = ColonelService::handleAddTaskPlace($params);
        return $this->success(['result' => ['take_place_id' => $takePlaceId]]);
    }

    /**
     * 团长分销下单处理统计数据逻辑
     * @throws \App\Exception\custom\MarketingException
     */
    public function place_order_process_logicAction()
    {
        $params = $this->params;
        $data = PindanActivityInviteOrderService::placeOrder($params);
        return $this->success(["result" => $data]);
    }

    /**
     * 团长分销下单处理统计数据逻辑
     * @throws \App\Exception\custom\MarketingException
     */
    public function refund_order_process_logicAction()
    {
        $params = $this->params;
        $data = PindanActivityInviteOrderService::refundOrder($params);
        return $this->success(["result" => $data]);
    }

    /**
     * 团长分销，任务中心
     * @throws Exception
     */
    public function task_controlAction()
    {
        $params = $this->params;
        $data = PindanActivityColonelConfigService::getTaskData($params);
        return $this->success(["result" => $data]);
    }

    /**
     * 团长分销，数据统计
     * @throws Exception
     */
    public function colonel_indexAction()
    {
        (new ColonelCenterValidate())->validate();
        $params = $this->params;
        $userId = $params["user_id"];
        $user = [];
        if (!empty($userId)) {
            $wechatInfo = UserService::userWechatBind(["user_id" => $userId]);
            if (!empty($wechatInfo)) {
                $user = [
                    "user_id" => $wechatInfo[0]["user_id"],
                    "user_nick" => $wechatInfo[0]["user_nick"],
                    "user_avatar" => $wechatInfo[0]["user_avatar"]
                ];
            }

            $incomeData = PindanActivityInviteOrderService::incomeStatistics(["user_ids" => [$userId]]);
            $orderNumData = PindanActivityInviteOrderService::inviteOrderNumberStatistics(["user_ids" => [$userId]]);
            return $this->success(["result" => [
                "income" => $incomeData[$userId],
                "order_num" => $orderNumData[$userId],
                "user" => $user]]);
        }

        return $this->success(["result" => []]);
    }

    /**
     * 团长分销，账单详情
     * @throws Exception
     */
    public function bill_infoAction()
    {
        (new ColonelCenterValidate())->validate();
        $params = $this->params;
        $data = PindanActivityInviteOrderService::getPayInfoList($params);
        return $this->success(["result" => $data]);
    }

    /**
     * 团长分销，下单记录
     * @throws \App\Exception\custom\ParamException
     */
    public function invite_order_recordAction()
    {
        (new ColonelCenterValidate())->validate();
        $params = $this->params;
        $data = PindanActivityInviteOrderService::getInviteOrderRecord($params);
        return $this->success(["result" => $data]);
    }
}
