<?php

namespace lib\Services;

use lib\Entities\ServiceEntity as Entity;
use lib\Models\ServiceModel as Model;

class ServiceService extends CommonService
{
  public function __construct()
  {
    parent::__construct();
  }
  final public function getClassName(): string
  {
    return "Service";
  }
  final public function getClassPath(): string
  {
    return $this->getClassName();
  }
  public function getModelClass(): string
  {
    return Model::class;
  }
  public function getEntityClass(): string
  {
    return Entity::class;
  }
  public function getEntityByCode(string $key): Entity|null|false
  {
    $this->getModel()->where('service_code', $key);
    return $this->getEntity();
  }
  //최근 $day일간 신규서비스 카운트
  public function getLatestCount(int $day, array $excepts): int|string
  {
    $this->getModel()->where("service_open_date > DATE_ADD(now(), INTERVAL -{$day} DAY)");
    $this->getModel()->where("service_status", 'o');
    $this->getModel()->whereNotIn("service_line", $excepts);
    $count =  $this->getModel()->count();
    // echo __FUNCTION__ . ":" . $this->getModel()->getLastQuery();
    return $count;
  }
  //미지급서비스 카운트
  public function getUnPaymentCount(array $excepts): int|string
  {
    $this->getModel()->where("service_payment_date > now()");
    $this->getModel()->where("service_status", 'o');
    $this->getModel()->whereNotIn("service_line", $excepts);
    $count =  $this->getModel()->count();
    // echo __FUNCTION__ . ":" . $this->getModel()->getLastQuery();
    return $count;
  }
  //최근신규서비스정보 리스트 limit갯수만큼큼
  public function getLatest(int $limit): array
  {
    $this->getModel()->orderBy($this->getModel()->getPKField(),  'DESC');
    $this->getModel()->limit($limit);
    return $this->getEntities();
  }
  //지역(치바,도쿄등)에 따른 DASHBOARD용 서비스 카운트
  private function getDistrictCountForDashboard(string $where, string $type, array $switchcodes): int
  {
    $switchcode_begin = $switchcodes['begin'];
    $switchcode_end  = $switchcodes['end'];
    $this->getModel()->where($where);
    $this->getModel()->where(["service_line" => $type, "service_status" => 'o']);
    $this->getModel()->where("service_sw BETWEEN '{$switchcode_begin}' AND '$switchcode_end'");
    $count =  $this->getModel()->count();
    // echo "<BR>" . $this->getModel()->getLastQuery();
    return $count;
  }
  final public function getTotalCountForDashboard(array $siteinfo): array
  {
    $temps = array();
    foreach ($siteinfo['totalcount_customers'] as $customer => $where) {
      $temps[$customer] = [];
      foreach ($siteinfo['totalcount_types'] as $type) {
        foreach (DBMS_SERVICE_SWITCHCODE as $district => $switchcodes) {
          $temps[$customer][$type][$district] = $this->getDistrictCountForDashboard($where, $type, $switchcodes);
        }
      } //foreach
      // echo var_dump($temps);
    } //foreach
    return $temps;
  }
  //지역(치바,도쿄등)에 따른 사용자별용 서비스 카운트
  public function getDistrictCountByClient(string $client_code, string $switchcode_begin, string $switchcode_end): int
  {
    $this->getModel()->where("client_code", "{$client_code}");
    $this->getModel()->where("service_sw BETWEEN '{$switchcode_begin}' AND '{$switchcode_end}'");
    $this->getModel()->where("service_status", "o");
    $this->getModel()->whereNotIn("service_line", ['solo', 'test', 'event', 'substitution']);
    return $this->getCount();
    // $sql = "SELECT (SELECT COUNT(*) FROM servicedb WHERE service_line = 'normal' AND service_status = 'o' AND service_sw BETWEEN '{$switchcode_begin}' AND '{$switchcode_end}' AND Client_Code = '{$client_code}') AS normal,
    //               (SELECT COUNT(*) FROM servicedb WHERE service_line = 'defence' AND service_status = 'o' AND service_sw BETWEEN '{$switchcode_begin}' AND '{$switchcode_end}' AND Client_Code = '{$client_code}') AS defence,
    //               (SELECT COUNT(*) FROM servicedb WHERE service_line = 'solo' AND service_status = 'o' AND Client_Code = '{$client_code}') AS solo,
    //               (SELECT COUNT(*) FROM servicedb WHERE service_line = 'test' AND service_status = 'o' AND service_sw BETWEEN '{$switchcode_begin}' AND '{$switchcode_end}' AND Client_Code = '{$client_code}') AS test,
    //               (SELECT COUNT(*) FROM servicedb WHERE service_line = 'event' AND service_status = 'o' AND service_sw BETWEEN '{$switchcode_begin}' AND '{$switchcode_end}' AND Client_Code = '{$client_code}') AS event,
    //               (SELECT COUNT(*) FROM servicedb WHERE service_line = 'substitution' AND service_status = 'o' AND service_sw BETWEEN '{$switchcode_begin}' AND '{$switchcode_end}' AND Client_Code = '{$client_code}') AS substitution";
    // $stmt = $this->getModel()->execute($sql);
    // // echo "<BR>" . $this->getModel()->getLastQuery();
    // return $stmt->fetch(PDO::FETCH_ASSOC);
  }
  //사용자별용 ServieLine별 카운트
  public function getServiceLineCountByClient(string $client_code, string $service_line): int
  {
    $this->getModel()->where("client_code", "{$client_code}");
    $this->getModel()->where("service_line", "{$service_line}");
    $this->getModel()->where("service_status", 'o');
    return $this->getCount();
  }
  //사용자별용 서비스 쿠폰
  public function getCouponCountByClient(string $client_code): int
  {
    $this->getModel()->where("client_code", "{$client_code}");
    $this->getModel()->whereNotIn("service_line", ['vpn', 'test', 'solo', 'substitution', 'event']);
    return $this->getCount("SUM(coupon) as cnt");
  }
}
