<?php

Class BaseCode extends BaseDB {
  function __construct($db_option=""){
    parent::__construct($db_option);
    $this->DB->tableExists(TB_basecode);
  }

  public function getAllBaseCodeList($Bran_Uid="", $Group_Uid="", $Code_Uid="", $where=""){
    if( !$where ){
      $where = new DB_where("and");
    }
    if( $Bran_Uid !== "" ) $where->add("CD.Bran_Uid = ?", $Bran_Uid);
    if( $Group_Uid !== "" ) $where->add("CD.Group_Uid = ?", $Group_Uid);
    if( $Code_Uid !== "" ) $where->add("CD.Code_Uid = ?", $Code_Uid);

    $wqry = $where->get_where();
    $whereqry = $wqry['qry'];
    $whererow = $wqry['val'];

    $sql = "select
      CD.Bran_Uid
      ,BR.Code_Val as Bran_Val
      ,CD.Group_Uid
      ,GR.Code_Val as Group_Val
      ,CD.Code_Uid
      ,CD.Code_Val
      ,CD.Code_Exp
      ,BR.Code_Idx as Bran_Idx
      ,case when CD.Bran_Uid = CD.Code_Uid then -1 else GR.Code_Idx end as Group_Idx
      ,CD.Code_Idx
      ,case when CD.Bran_Uid = CD.Code_Uid then -1 when CD.Group_Uid = CD.Code_Uid then 0 else CD.Code_Idx end as Code_Idx2
      ,BR.Active as Bran_Active
      ,GR.Active as Group_Active
      ,CD.Active as Code_Active
    from ". TB_basecode ." as CD
    inner join ". TB_basecode ." as BR on CD.Bran_Uid = BR.Code_Uid
    inner Join ". TB_basecode ." as GR on CD.Bran_Uid = GR.Bran_Uid and CD.Group_Uid = GR.Code_Uid
    where {$whereqry}
    order by Bran_Idx, Bran_Uid, Group_Idx, Group_Uid, Code_Idx2, Code_Uid";

    $this->DB->query_record = false; //query log 기록 금지
    $rows = $this->DB->query($sql, $whererow);
    $this->DB->query_record = true; //query log 기록 재개

    return $rows;
  }

  public function getBaseCodeList($Bran_Uid="", $Group_Uid="", $Code_Uid="", $where=""){
    if( !$where ){
      $where = new DB_where("and");
    }
    $where->add("BR.Active = ?", 'Y');
    $where->add("GR.Active = ?", 'Y');
    $where->add("CD.Active = ?", 'Y');

    return $this->getAllBaseCodeList($Bran_Uid, $Group_Uid, $Code_Uid, $where);
  }

  public function getBaseCode($Bran_Uid="", $Group_Uid="", $Code_Uid="", $where=""){
    if( !$where ){
      $where = new DB_where("and");
    }
    if( $Bran_Uid !== "" ) $where->add("Bran_Uid = ?", $Bran_Uid);
    if( $Group_Uid !== "" ) $where->add("Group_Uid = ?", $Group_Uid);
    if( $Code_Uid !== "" ) $where->add("Code_Uid = ?", $Code_Uid);

    $wqry = $where->get_where();
    $whereqry = $wqry['qry'];
    $whererow = $wqry['val'];

    $sql = "select * from ". TB_basecode ." where {$whereqry}";
    $row = $this->DB->queryFirstRow($sql, $whererow);

    return $row;
  }

  public function putBaseCode($sqldata){
    return $this->DB->insert(TB_basecode, $sqldata);
  }

  public function setBaseCode($sqldata, $Bran_Uid="", $Group_Uid="", $Code_Uid="", $where=""){
    if( !$where ){
      $where = new DB_where("and");
    }
    if( $Bran_Uid !== "" ) $where->add("Bran_Uid = ?", $Bran_Uid);
    if( $Group_Uid !== "" ) $where->add("Group_Uid = ?", $Group_Uid);
    if( $Code_Uid !== "" ) $where->add("Code_Uid = ?", $Code_Uid);

    $wqry = $where->get_where();
    $whereqry = $wqry['qry'];
    $whererow = $wqry['val'];

    return $this->DB->update(TB_basecode, $sqldata, $whereqry, $whererow);
  }

  public function delBaseCode($Bran_Uid="", $Group_Uid="", $Code_Uid="", $where=""){
    if( !$where ){
      $where = new DB_where("and");
    }
    if( $Bran_Uid !== "" ) $where->add("Bran_Uid = ?", $Bran_Uid);
    if( $Group_Uid !== "" ) $where->add("Group_Uid = ?", $Group_Uid);
    if( $Code_Uid !== "" ) $where->add("Code_Uid = ?", $Code_Uid);

    $wqry = $where->get_where();
    $whereqry = $wqry['qry'];
    $whererow = $wqry['val'];

    return $this->DB->delete(TB_basecode, $whereqry, $whererow);
  }

  public function sortBaseCode($Bran_Uid, $Group_Uid, $Code_Uid, $Code_Idx, $adv=1){ //$incl => 코드 추가시 현 Code_Uid의 Code_idx 값보다 +1 씩 해서 sort 값을 줘야 하나, 삭제시에는 Code_Uid 자리부터 시작하므로 +1 하지 않기 위함.
    $Code_Idx = $Code_Idx?:1;

    $where = new DB_where("and");
    $where->add("Code_Uid != ?",$Code_Uid);
    $where->add("Code_Idx >= ?",$Code_Idx);
    if( $Bran_Uid == $Code_Uid ){
      $where->add("Bran_Uid = ?",$this->DB->sqleval("Code_Uid"));
    }else if( $Group_Uid == $Code_Uid ){
      $where->add("Bran_Uid = ?",$Bran_Uid);
      $where->add("Group_Uid = ?",$this->DB->sqleval("Code_Uid"));
      $where->add("Bran_Uid != ?",$this->DB->sqleval("Code_Uid"));
    }else{
      $where->add("Bran_Uid = ?",$Bran_Uid);
      $where->add("Group_Uid = ?",$Group_Uid);
      $where->add("Bran_Uid != ?",$this->DB->sqleval("Code_Uid"));
      $where->add("Group_Uid != ?",$this->DB->sqleval("Code_Uid"));
    }

    $wqry = $where->get_where();
    $whereqry = $wqry['qry'];
    $whererow = $wqry['val'];

    $sql = "select * from ". TB_basecode ." where {$whereqry} order by Code_Idx";
    $rst = $this->DB->qry($sql, $whererow);

    $sidx = $Code_Idx + $adv;

    $urst = true;
    while( $row = $this->DB->fetch_array($rst) ){
      $sqldata = Array("Code_Idx" => $sidx++);
      $uwhere = new DB_where("and");

      $uwhere->add("Bran_Uid = ?", $row['Bran_Uid']);
      $uwhere->add("Group_Uid = ?", $row['Group_Uid']);
      $uwhere->add("Code_Uid = ?", $row['Code_Uid']);

      $uwqry = $uwhere->get_where();
      $uwhereqry = $uwqry['qry'];
      $uwhererow = $uwqry['val'];

      $urst = $urst && $this->DB->update(TB_basecode, $sqldata, $uwhereqry, $uwhererow);
    }
    return $urst;
  }

  public function getMaxBaseCodeIdx($Bran_Uid="", $Group_Uid="", $where=""){
    if( !$where ){
      $where = new DB_where("and");
    }
    if( $Bran_Uid !== "" ){
      $where->add("Bran_Uid = ?", $Bran_Uid);
      $where->add("Bran_Uid != ?", $this->DB->sqleval("Code_Uid"));
    }else{
      $where->add("Bran_Uid = ?", $this->DB->sqleval("Group_Uid"));
    }
    if( $Group_Uid !== "" ){
      $where->add("Group_Uid = ?", $Group_Uid);
      $where->add("Group_Uid != ?", $this->DB->sqleval("Code_Uid"));
    }else{
      $where->add("Group_Uid = ?", $this->DB->sqleval("Code_Uid"));
    }

    $wqry = $where->get_where();
    $whereqry = $wqry['qry'];
    $whererow = $wqry['val'];

    $sql = "select max(Code_Idx) from ". TB_basecode ." where {$whereqry}";
    $maxIdx = $this->DB->queryFirstField($sql, $whererow);

    return $maxIdx;
  }

  public function getBaseCodeVars($Bran_Uid="", $Group_Uid=""){
    $Bran_Uid = $Bran_Uid?:$Group_Uid;

    $where = new DB_where("and");
    $where->add("CD.Group_Uid != ?", $this->DB->sqleval("CD.Code_Uid"));

    $row = $this->getBaseCodeList($Bran_Uid, $Group_Uid, "", $where);

    $ret = array();
    foreach( (array)$row as $val ){
      $ret[$val['Code_Uid']] = $val['Code_Val'];
    }
    return $ret;
  }

  public function getBaseGroupVars($Bran_Uid){
    $where = new DB_where("and");
    $where->add("CD.Bran_Uid != ?", $this->DB->sqleval("CD.Code_Uid"));

    $row = $this->getBaseCodeList($Bran_Uid, "", "", $where);
    $ret = array();
    foreach( (array)$row as $val ){
      if( $val['Group_Uid'] == $val['Code_Uid'] ){
        $ret[$val['Group_Uid']] = $val;
      }else{
        $ret[$val['Group_Uid']]['children'][$val['Code_Uid']] = $val['Code_Val'];
      }
    }
    return $ret;
  }

  public function getBaseGCodeVars($Bran_Uid="", $Group_Uid=""){
    $Bran_Uid = $Bran_Uid?:$Group_Uid;

    $where = new DB_where("and");
    $where->add("CD.Group_Uid != ?", $this->DB->sqleval("CD.Code_Uid"));

    $row = $this->getBaseCodeList($Bran_Uid, $Group_Uid, "", $where);

    $ret = array();
    foreach( (array)$row as $val ){
      if( !isset($ret[$val['Group_Uid']]) ){
        $ret[$val['Group_Uid']] = array();
      }
      $ret[$val['Group_Uid']][$val['Code_Uid']] = $val['Code_Val'];
    }
    return $ret;
  }

  public function getBaseCodeVarsExp($Bran_Uid="", $Group_Uid=""){
    $Bran_Uid = $Bran_Uid?:$Group_Uid;

    $where = new DB_where("and");
    $where->add("CD.Group_Uid != ?", $this->DB->sqleval("CD.Code_Uid"));

    $row = $this->getBaseCodeList($Bran_Uid, $Group_Uid, "", $where);

    $ret = array();
    foreach( (array)$row as $val ){
      $ret[$val['Code_Uid']] = Array($val['Code_Val'],$val['Code_Exp']);
    }
    return $ret;
  }

  public function getBaseCodeVarsAll($Bran_Uid="", $Group_Uid=""){
    $Bran_Uid = $Bran_Uid?:$Group_Uid;

    $where = new DB_where("and");
    $where->add("CD.Group_Uid != ?", $this->DB->sqleval("CD.Code_Uid"));

    $row = $this->getBaseCodeList($Bran_Uid, $Group_Uid, "", $where);

    $ret = array(
      "CODE"   =>Array(),
      "CODEEXP"=>Array(),
    );
    foreach( (array)$row as $val ){
      if( !isset($ret['CODE'][$val['Bran_Uid']]) ){
        $ret['CODE'][$val['Bran_Uid']] = Array();
        $ret['CODEEXP'][$val['Bran_Uid']] = Array();
      }

      $ret['CODE'][$val['Bran_Uid']][$val['Code_Uid']] = $val['Code_Val'];;
      $ret['CODEEXP'][$val['Bran_Uid']][$val['Code_Uid']] = Array($val['Code_Val'],$val['Code_Exp']);
    }
    return $ret;
  }

//  public static function val2code( $rows, $val ){
//    foreach( $rows as $key => $vrow ){
//      if( is_array($vrow) && $val == $vrow['val'] || $vrow == $val ){
//        return $key;
//      }
//    }
//    return false;
//  }
//
//  public static function val2txt( $rows, $val ){
//    foreach( $rows as $key => $vrow ){
//      if( is_array($vrow) && $val == $vrow['val'] ){
//        return $vrow['txt'];
//      }else if( $vrow == $val ){
//        return $vrow;
//      }
//    }
//    return false;
//  }
}
