<?php

namespace App\Services\marketing;


use App\Exception\custom\MarketingException;
use App\Models\marketing\mysql\ColonelDistributorInviteOrderNum;
use App\Models\marketing\mysql\Marketing;
use App\Models\marketing\mysql\ColonelDistributorConfig;

class PindanActivityColonelConfigService
{
    /**
     * 编辑团长分销活动配置
     * @param array $params
     * @return bool
     * @throws MarketingException
     */
    public static function editColonelConfig($params = [])
    {
        $date = $params["date"];
        $type = empty($params["type"]) ? ColonelDistributorConfig::TYPE_COLONEL : $params["type"];
        $configList = $params["config"];
        $marketingId = $params["marketing_id"];

        if (empty($configList)) {
            throw new MarketingException(['cus' => MarketingException::COLONEL_CONFIG_NULL]);
        }

        if (count($configList) > 6) {
            throw new MarketingException(['cus' => MarketingException::COLONEL_LEVEL_SIX]);
        }

        $idRes = ColonelDistributorConfig::getRecord(["date" => $date, "type" => $type, "marketing_id" => $marketingId], ["colonel_config_id"]);

        if (!empty($idRes)) {
            ColonelDistributorConfig::save([
                "marketing_id" => $marketingId,
                "date" => $date,
                "type" => $type,
                "data" => $configList,
            ], ["colonel_config_id" => $idRes["colonel_config_id"]]);
        } else {
            ColonelDistributorConfig::save([
                "marketing_id" => $marketingId,
                "date" => $date,
                "type" => $type,
                "data" => $configList,
            ]);
        }

        return true;
    }

    /**
     * 团长分销活动配置
     * @param array $params
     * @return array
     */
    public static function colonelConfig($params = [])
    {
        $day = empty($params["date"]) ? date("Y-m-d") : $params["date"];
        $type = empty($params["type"]) ? ColonelDistributorConfig::TYPE_COLONEL : $params["type"];
        $config = ColonelDistributorConfig::getRecord(["date" => $day, "type" => $type]);
        if (empty($config)) {
            $config = ColonelDistributorConfig::getRecord([
                "date[<=]" => $day,
                "ORDER" => ["date" => "DESC"],
                "LIMIT" => 1
            ]);
        }

        $data = [];
        if (!empty($config)) {
            $configData = json_decode($config["data"], true);
            foreach ($configData as $key => $item) {
                $data[$key]["date"] = $config["date"];
                $data[$key]["level"] = $item["level"];
                $data[$key]["assess_order_num"] = $item["assess_order_num"];
                $data[$key]["reward_amount"] = $item["reward_amount"];
            }
            $sortKeys = array_column($data, "level");
            array_multisort($sortKeys, SORT_ASC, $data);
        }
        return $data;
    }

    /**
     * 团长分销活动任务中心列表
     * @param array $params
     * @return array
     */
    public static function getTaskData($params = [])
    {
        $hour = date("H");
        if ($hour < 10 && $hour > 0) {
            $date = date("Y-m-d", strtotime("-1 day"));
        } elseif ($hour > 16) {
            $date = date("Y-m-d");
        } else {
            $date = date("Y-m-d");
        }

        $num = 0;
        if (!empty($params["user_id"])) {
            $inviteOrderNumRes = ColonelDistributorInviteOrderNum::getRecord([
                "colonel_user_id" => $params["user_id"],
                "date" => $date,
            ]);
            $num = empty($inviteOrderNumRes["num"]) ? 0 : $inviteOrderNumRes["num"];
        }

        $configList = self::colonelConfig(["date" => $date, "type" => ColonelDistributorConfig::TYPE_COLONEL]);
        if (!empty($configList)) {
            foreach ($configList as $key => $config) {
                $configList[$key]["is_finish"] = 0;
                if ($config["assess_order_num"] <= $num) {
                    $configList[$key]["is_finish"] = 1;
                }
            }
        }
        return ["number" => $num, 'list' => $configList];
    }
}