<?php

namespace App\Domains\Virtual\Commands\Sync;

use App\Models\Virtual\Package;
use Illuminate\Support\Facades\DB;
use App\Domains\Virtual\Repositories\PackageRepository;

class PackageSync extends Command
{
    protected $name = 'virtual:sync-package';

    protected $description = '同步VD套餐数据';

    protected static $carrier_operator = [10 => 0, 11 => 1, 12 => 2];

    protected static $flowedPackages = ['DYD-011','DYD-002','DYD-012','DYD-008','YY-003','Y08','SYY-003','SYY-010','DYF-011','DYF-002','DYF-012','DYF-008','SYY-013','YY-012-01','Y01','Y02','Y03'];

    public function handle()
    {
        DB::table('virtual_packages')->truncate();

        $datetime = $this->getDateTime();

        $basePackages = $this->getBasePackages()->toArray();
        $renewalPackages = $this->getRenewalPackages()->toArray();
        $flowPackages = $this->getFlowPackages()->toArray();

        $packages = array_merge($basePackages, $renewalPackages, $flowPackages);

        Package::upsert($packages, ['sn', 'deleted_at']);

        app(PackageRepository::class)->forgetCached();
    }

    // 基础包
    protected function getBasePackages()
    {
        $packages = DB::connection('vd_old')->table('ckb_package')->get();

        return $packages->map(function ($item) {
            $flowed = strpos($item->name, 'S') === 0 || in_array($item->name, self::$flowedPackages);

            $data = [
                'sn' => $item->package_sn,
                'name' => trim($item->name),
                'type' => 0,
                'carrier_operator' => self::$carrier_operator[$item->carrieroperator],
                'cost_price' => intval(floatval($item->price) * 100),
                'guide_price' => intval(floatval($item->package_rp) * 100),
                'flows' => ($item->tag == 10) ? intval($item->package_value) : 0,
                'voices' => ($item->tag == 11) ? intval($item->package_value) : 0,
                'messages' => 0,
                'has_messages' => 0,
                'has_lbs' => 0,
                'reset_months' => ($item->unit === 'year') ? $item->value * 12 : $item->value,
                'service_months' => ($item->cycle_unit === 'year') ? $item->cycle_value * 12 : $item->cycle_value,
                'effect_months' => 0,
                'delay_months' => 0,
                'description' => $item->description ?: '',
                'flowed' => intval($flowed),
                'created_at' => date('Y-m-d H:i:s', $item->create_time),
                'updated_at' => date('Y-m-d H:i:s', $item->update_time),
                'deleted_at' => $item->del ? date('Y-m-d H:i:s', $item->update_time) : null,
            ];

            return $data;
        });
    }

    // 续费包
    protected function getRenewalPackages()
    {
        $packages = DB::connection('vd_old')->table('ckb_add_value_package')->where('type', 1)->get();

        return $packages->map(function ($item) {
            return [
                'sn' => $item->package_sn,
                'name' => trim($item->package_name),
                'type' => 2,
                'carrier_operator' => self::$carrier_operator[$item->operator],
                'cost_price' => intval($item->price * 100),
                'guide_price' => intval($item->guide_price * 100),
                'flows' => empty($item->flows) ? 0 : $item->flows,
                'voices' => empty($item->voice_num) ? 0 : $item->voice_num,
                'messages' => empty($item->sms_num) ? 0 : $item->sms_num,
                'has_messages' => empty($item->sms_num) ? 0 : 1,
                'has_lbs' => ($item->LBS === 10) ? 1 : 0,
                'reset_months' => $item->flows_cycle,
                'service_months' => $item->service_cycle,
                'effect_months' => ($item->buy_enabled == 10) ? 0 : 1,
                'delay_months' => ($item->service_extend == 10) ? 1 : 0,
                'description' => $item->package_text ?: '',
                'flowed' => strpos($item->package_name, 'S') === 0,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
                'deleted_at' => $item->del ? date('Y-m-d H:i:s') : null,
            ];
        });
    }

    // 加油包
    protected function getFlowPackages()
    {
        $packages = DB::connection('vd_old')->table('ckb_add_value_package')->where('type', 2)->get();

        return $packages->map(function ($item) {
            return [
                'sn' => $item->package_sn,
                'name' => trim($item->package_name),
                'type' => 3,
                'carrier_operator' => self::$carrier_operator[$item->operator],
                'cost_price' => intval($item->price * 100),
                'guide_price' => intval($item->guide_price * 100),
                'flows' => empty($item->flows) ? 0 : $item->flows,
                'voices' => empty($item->voice_num) ? 0 : $item->voice_num,
                'messages' => empty($item->sms_num) ? 0 : $item->sms_num,
                'has_messages' => empty($item->sms_num) ? 0 : 1,
                'has_lbs' => ($item->LBS === 10) ? 1 : 0,
                'reset_months' => $item->flows_cycle,
                'service_months' => $item->service_cycle,
                'effect_months' => ($item->buy_enabled == 10) ? 0 : 1,
                'delay_months' => ($item->service_extend == 10) ? 1 : 0,
                'description' => $item->package_text ?: '',
                'flowed' => strpos($item->package_name, 'S') === 0,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
                'deleted_at' => $item->del ? date('Y-m-d H:i:s') : null,
            ];
        });
    }
}
