<?php
namespace lib\Model;

use lib\Core\Model as Model;

class Service extends Model
{
    public function __construct()
    {
        parent::__construct();
    } //

    final public function getTable()
    {
        return 'servicedb';
    }
    //쿠폰 추가
    final public function addCoupon($coupon, $where = "")
    {
        $sql = sprintf("UPDATE %s SET coupon = coupon+%d %s", $this->getTable(), $coupon, $where ? "WHERE " . $where : $where);
        return $this->execute($sql);
    } //
    //쿠폰 설정
    final public function setCoupon($coupon, $where = "")
    {
        $sql = sprintf("UPDATE %s SET coupon = %d %s", $this->getTable(), $coupon, $where ? "WHERE " . $where : $where);
        return $this->execute($sql);
    } //
    //일회성서비스정보 가져오기
    final public function getOneTimes($where = "")
    {
        $sql = sprintf("SELECT * FROM %s WHERE server_code LIKE '%일회성%' %s", $this->getTable(), $where ? "WHERE " . $where : $where);
        return $this->getRows($sql);
    } //

    final public function getCount($where = "")
    {
        $sql = sprintf("SELECT COUNT(*) FROM %s WHERE %s ", $this->getTable(), $where);
        //echo $sql . "\n";
        $stmt = $this->execute($sql);
        return $stmt->fetchColumn(0);
    } //

    final public function getExtras($code)
    {
        $sql = sprintf("SELECT C.Client_Name,S.server_code,S.service_ip,S.service_os,S.service_sw
        FROM servicedb AS S
        JOIN clientdb AS C
        ON S.client_code = C.Client_Code
        WHERE S.service_code IN (SELECT service_code FROM adddb WHERE addDB_code ='%s')", $code);
        return $this->getRows($sql);
    }

    //Dashboard
    private function getCompanies($site_id)
    {
        switch ($site_id) {
            case 'PRIMEIDC':
                return array(
                    "idcjp" => "AND Client_Code NOT IN ('C116','C012','C636')",
                    "winidc" => "AND Client_Code='C116'",
                    "gamewing" => "AND Client_Code='C012'",
                    "GDIDC" => "AND Client_Code='C636'",
                );
                break;
            case "ITSOLUTION":
                return array(
                    "winidc" => "AND Client_Code NOT IN ('C237')",
                    "bosch" => "AND Client_Code='C237'",
                );
                break;
            case 'GDIDC':
                return array(
                    "helper" => "AND Client_Code NOT IN ('C916')",
                    "gdidc" => "",
                );
                break;
            default:
                throw new \Exception(sprintf(__METHOD__ . "에서 오류 SiteID[%s]가 정의되지 않았습니다.", $site_id));
                break;
        }
    }
    final public function getTypes_Dashboard()
    {
        return array("normal", "defence", "solo", "substitution", "test");
    }
    private function getCountByCenter_Dashboard($where)
    {
        $centers_conditions = array(
            "Chiba" => "service_sw BETWEEN 'C00%' AND 'C49%'",
            "Tokyo" => "service_sw BETWEEN 'C80%' AND 'C99%'",
        );
        $temps = array();
        foreach ($centers_conditions as $name => $condition) {
            $temps[$name] = $this->getCount(sprintf("%s AND %s", $where, $condition));
        }
        //return array("Chiba" => $temps['Center1'] + $temps['Center2'], "Tokyo" => $temps['Tokyo']);
        return $temps;
    }
    final public function getSummary_Dashboard($site_id)
    {
        $temps = array();
        foreach ($this->getCompanies($site_id) as $name => $condition) {
            $temps[$name] = array();
            foreach ($this->getTypes_Dashboard() as $type) {
                $where = sprintf("service_line='%s' AND service_status='o' %s", $type, $condition);
                $temps[$name][$type] = $this->getCountByCenter_Dashboard($where);
            } //foreach
            //echo var_dump($temps);
        } //foreach
        return $temps;
    }
} //Class
