package com.csc.library.dwr;

import java.util.Iterator;

import com.csc.library.database.MyHashMap;
import com.csc.library.databean.Tot0;
import com.csc.library.session.DbRecord;
import com.csc.library.session.DbTable;
import com.csc.library.session.InitialRecord;
import com.csc.library.session.InitialTable;
import com.csc.library.utilities.CscCalendar;
import com.csc.library.utilities.CscTime;
import com.csc.library.utilities.MyLog;
import com.csc.library.utilities.MyTreeMap;
import com.csc.library.utilities.UProfile;

public class Tot0_dwr extends DWR_base{

    public Tot0_dwr() {
    }
    
    private DbTable dtTot0 = null;
    private DbTable dtTot1 = null;
    
    public void chkDtTot0(){
    	try{
    		if (this.dtTot0 == null){
    			this.dtTot0 = new InitialTable(this.up).getDbTable("Tot0");
    			this.dtTot0.setColumn("*");
    		}
    	}catch(Exception e){
    		MyLog.error(this, e);
    	}
    }
    
    public void chkDtTot1(){
    	try{
    		if (this.dtTot1 == null){
    			this.dtTot1 = new InitialTable(this.up).getDbTable("TOT1");
    			this.dtTot1.setColumn("*");
    		}
    	}catch(Exception e){
    		MyLog.error(this, e);
    	}
    }
    
    public String getDWROT(String[] str){
        this.setUProfile();
        String data="";
        String startdate = this.chkEmptyData(str, 0);
        String enddate = this.chkEmptyData(str, 1);
        String starttime = this.chkEmptyData(str, 2);
        String endtime = this.chkEmptyData(str, 3);

        MyTreeMap tr = new MyTreeMap();
        String[] empList = this.chkEmptyData(str, 4).split("#");

        for (int i=0; i<empList.length; i++) {
            MyHashMap hm = new MyHashMap();
            hm.put("employeeid", this.chkEmptyData(empList,i));
            tr.put(Integer.valueOf((String.valueOf(i))), hm);
        }
        
        int loop =1;
        MyHashMap hmValue = new MyHashMap();
        DbTable dbOT = new InitialTable(this.up).getDbTable("TOT1");
        Iterator it = tr.keySet().iterator();
        while (it.hasNext()) {
            hmValue.clear();
            Integer key = (Integer) it.next();
            hmValue = (MyHashMap) tr.get(key);
            
            try{
                DbRecord recOT = dbOT.getNewRecord();
                recOT.set("OTID","0");
                recOT.set("COMPANYID", this.up.get("companyid"));
                recOT.set("LINE_NO", loop);
                recOT.set("EMPLOYEEID", hmValue.getString("employeeid"));
                recOT.set("START_DATE", startdate);
                recOT.set("END_DATE", enddate);
                recOT.set("START_TIME", starttime);
                recOT.set("END_TIME", endtime);
                dbOT.putNewRecord(recOT);
            } catch (Exception e) {
                MyLog.error(this, e);
            }
            loop++;
        }
        
        Tot0 rec = (Tot0) new InitialRecord(up).getDbRecord("TOT0");
        rec.set("OTID","0");
        rec.set("COMPANYID", this.up.get("companyid"));
        rec.putChild(dbOT);
        MyHashMap hm = rec.getDWRData();
        MyLog.debug(this, "===="+hm);
        
        Iterator it_ot  = hm.keySet().iterator();
        float ot_date;
        CscTime cs_ot = new CscTime();
        while(it_ot.hasNext()){
            String key = (String) it_ot.next();
            MyHashMap hmOT = (MyHashMap) hm.get(key);
            ot_date = hmOT.getFloat("LV");
            cs_ot.add(ot_date);
        }
        
        if(cs_ot.getFloat() > 4){
            data = "true";
        }else{
            data = "false";
        }
        
        MyLog.debug("===== TOTAL OT : "+cs_ot.getFloat());
        
        return data;        
    }
    
    /**
     * �礢����š�â� OT �������ö�����������
     * @author PipoXP
     * @param str
     * @return ��ѡ�ҹ���� OT ��
     */
    public String[] getDWROT(String[][] str){
    	this.setUProfile();
    	String startdate = this.chkEmptyData(str[0], 0);
    	String enddate = this.chkEmptyData(str[0], 1);
    	String starttime = this.chkEmptyData(str[0], 2);
    	String endtime = this.chkEmptyData(str[0], 3);

	    this.chkDtTot1();
	    try{
	    	for(int i=0; i<str[1].length; i++){
	    		DbRecord rec = this.dtTot1.getNewRecord();
	    		rec.set("EMPLOYEEID", str[1][i]);
	    		rec.set("COMPANYID", this.up.get("companyid"));
	    		rec.set("START_DATE", startdate);
	    		rec.set("END_DATE", enddate);
	    		rec.set("START_TIME", starttime);
	    		rec.set("END_TIME", endtime);
	    		this.dtTot1.putNewRecord(rec);
	    	}
	    }catch(Exception e){
	    	MyLog.error(this, e);
	    }
	    
    	Tot0 rec =  (Tot0) new InitialRecord(this.up).getDbRecord("Tot0");
    	rec.set("OTID", "0");
    	rec.set("COMPANYID", this.up.get("companyid"));
    	rec.putChild(this.dtTot1);
    	MyHashMap hm = rec.getDWRData();

    	String[] empReturn = new String[hm.size()];
    	Iterator it = hm.keySet().iterator();
    	while (it.hasNext()) {
    		String key = it.next().toString();
    		MyHashMap hmOt = (MyHashMap) hm.get(key);
    		empReturn[Integer.parseInt(key)-1] = hmOt.getString("EMPLOYEEID");
    	}

    	return empReturn;
    }
    
    public String[] getDWROTData(String[][] str){
    	this.setUProfile();    	
	    this.chkDtTot1();
	    try{
	    	for(int i=0; i<str.length; i++){
	    		DbRecord rec = this.dtTot1.getNewRecord();
	    		String startdate = this.chkEmptyData(str[i], 1);
	        	String enddate = this.chkEmptyData(str[i], 2);
	        	String starttime = this.chkEmptyData(str[i], 3);
	        	String endtime = this.chkEmptyData(str[i], 4);
	        	
	    		rec.set("EMPLOYEEID", str[i][0]);
	    		rec.set("COMPANYID", this.up.get("companyid"));
	    		rec.set("START_DATE", startdate);
	    		rec.set("END_DATE", enddate);
	    		rec.set("START_TIME", starttime);
	    		rec.set("END_TIME", endtime);
	    		this.dtTot1.putNewRecord(rec);
	    	}
	    }catch(Exception e){
	    	MyLog.error(this, e);
	    }
	    
    	Tot0 rec =  (Tot0) new InitialRecord(this.up).getDbRecord("Tot0");
    	rec.set("OTID", "0");
    	rec.set("COMPANYID", this.up.get("companyid"));
    	rec.putChild(this.dtTot1);
    	MyHashMap hm = rec.getDWRData();

    	String[] empReturn = new String[hm.size()];
    	Iterator it = hm.keySet().iterator();
    	while (it.hasNext()) {
    		String key = it.next().toString();
    		MyHashMap hmOt = (MyHashMap) hm.get(key);
    		empReturn[Integer.parseInt(key)-1] = hmOt.getString("EMPLOYEEID");
    	}

    	return empReturn;
    }
    
    /*public void setUProfile(){
    	this.up = new UProfile();
        up.setDbName("mykkbdb-csc");
        up.setSchemaName("dbo");
        up.setUserName("L01");
        up.set("appname","ta");
        up.set("companyid", "100");
        up.set("lang", "tha");
        up.set("actorid", "APP03");
    }*/

}