/**
 *
 */
package com.cnd3b.common;

import com.cnd3b.common.data.db.SQLiteJDBC;
import com.cnd3b.common.parameter.parameter;
import com.cnd3b.utility.Sms;
import p2.p2server.P2Server;
import p2.pao.PaoRemote;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

/**
 * @author Administrator
 *
 */
public class BaseClass {
    public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void printErrOut(String keyname, String msg) {
        msg = msg.replace("'", "''");
        new SQLiteJDBC().InsertLogMsg(keyname, msg, "err");
        System.err.println(keyname + ":" + msg);
    }

    public static void printInfoOut(String keyname, String msg, boolean notdebug) {
        if (notdebug || parameter.isdebug()) {
            msg = msg.replace("'", "''");
            new SQLiteJDBC().InsertLogMsg(keyname, msg, "info");
            System.err.println(keyname + ":" + msg);
        }
    }

    public static void printInfoOut(String keyname, String msg) {
        if (parameter.isdebug()) {
            msg = msg.replace("'", "''");
            new SQLiteJDBC().InsertLogMsg(keyname, msg, "info");
            System.err.println(keyname + ":" + msg);
        }
    }

    public String getWeekFirstDay() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        calendar.set(Calendar.DAY_OF_WEEK, 1);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 1);
        return format.format(calendar.getTime());
    }

    public String getWeekLastDay() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        calendar.set(Calendar.DAY_OF_WEEK, 7);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 1);
        return format.format(calendar.getTime());
    }

    public String getMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(calendar.getTime());
    }

    public String getMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return format.format(calendar.getTime());
    }

    /**
     * 获取当前系统日期，格式为yyyy-MM-dd
     *
     * @return
     */
    public String getDate_Str() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(calendar.getTime());
    }

    public String getDate_Str(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date.getTime());
    }


    public Date getDateTime() {
        return Calendar.getInstance().getTime();
    }


    /**
     * 获取当前系统时间，格式为yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public String getDateTime_Str() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(calendar.getTime());
    }

    public String getDateTime_Str(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date.getTime());
    }

    public String nullToStr(String str) {
        if (null == str || "null".equals(str)) {
            return "";
        } else {
            return str;
        }
    }

    ArrayList<PaoSetRemote> PaoSetRemoteList = new ArrayList<>();

    public PaoSetRemote getP2ServerPaoSet(String paosetname, String hrid, String where) throws P2Exception {
        PaoSetRemote paoSetRemote = getP2ServerPaoSet(paosetname, hrid);
        paoSetRemote.setWhere(where);
        paoSetRemote.reset();
        return paoSetRemote;
    }

    public PaoSetRemote getP2ServerPaoSet(String paosetname, String hrid, String where, String orderby) throws P2Exception {
        PaoSetRemote paoSetRemote = getP2ServerPaoSet(paosetname, hrid);
        paoSetRemote.setWhere(where);
        paoSetRemote.setOrderBy(orderby);
        paoSetRemote.reset();
        return paoSetRemote;
    }

    public PaoSetRemote getP2ServerPaoSet(String paosetname, String hrid) throws P2Exception {
        PaoSetRemote paoSetRemote = P2Server.getP2Server().getPaoSet(paosetname, P2Server.getP2Server().getUserInfo(hrid));
        PaoSetRemoteList.add(paoSetRemote);
        return paoSetRemote;
    }

    public PaoSetRemote getP2ServerSystemPaoSet(String paosetname, String where) throws P2Exception {
        PaoSetRemote paoSetRemote = getP2ServerSystemPaoSet(paosetname);
        paoSetRemote.setWhere(where);
        paoSetRemote.reset();
        return paoSetRemote;
    }

    public PaoSetRemote getP2ServerSystemPaoSet(String paosetname, String where, String orderby) throws P2Exception {
        PaoSetRemote paoSetRemote = getP2ServerSystemPaoSet(paosetname);
        paoSetRemote.setWhere(where);
        paoSetRemote.setOrderBy(orderby);
        paoSetRemote.reset();
        return paoSetRemote;
    }

    public PaoSetRemote getP2ServerSystemPaoSet(String paosetname) throws P2Exception {
        PaoSetRemote paoSetRemote = P2Server.getP2Server().getPaoSet(paosetname, P2Server.getP2Server().getSystemUserInfo());
        PaoSetRemoteList.add(paoSetRemote);
        return paoSetRemote;
    }

    public ArrayList<Object> getP2FieldValues(PaoSetRemote paoSetRemote, String fieldname) throws P2Exception {
        ArrayList<Object> list = new ArrayList<>();
        int i = 0;
        PaoRemote pao = null;
        while ((pao = paoSetRemote.getPao(i)) != null) {
            list.add(pao.getDatabaseValue(fieldname));
            i++;
        }
        return list;
    }


    public HashMap<String, PaoRemote> getPaoMap(PaoSetRemote paoSetRemote, String keyfieldname) throws P2Exception {
        HashMap<String, PaoRemote> map = new HashMap<>();
        int i = 0;
        while (paoSetRemote.getPao(i) != null) {
            map.put(paoSetRemote.getPao(i).getString(keyfieldname), paoSetRemote.getPao(i));
            i++;
        }
        return map;
    }

    private static HashMap<String, ArrayList<String>> monthsmap = new HashMap<>();

    public ArrayList<String> getSectionMonth(String fbegmonth, String fendmonth) {
        String key = fbegmonth + "+" + fendmonth;
        if (monthsmap.containsKey(key)) {
            return monthsmap.get(key);
        }
        ArrayList<String> list = new ArrayList<>();
        int startmonth = Integer.parseInt(fbegmonth.substring(0, 4)) * 12 + Integer.parseInt(fbegmonth.substring(5));
        int endmonth = Integer.parseInt(fendmonth.substring(0, 4)) * 12 + Integer.parseInt(fendmonth.substring(5));
        while (startmonth <= endmonth) {
            int year = startmonth / 12;
            int month = startmonth % 12;
            if (month == 0) {
                year = year - 1;
                month = 12;
            }
            list.add(year + "-" + (month < 10 ? ("0" + month) : ("" + month)));
            startmonth++;
        }
        monthsmap.put(key, list);
        return list;
    }


    /**
     * 短信发送
     * @param fphonenumber
     * @param msg
     */
    public void sendShortMsg(String fphonenumber, String msg) {
        /**
         * 发送短信提醒
         */
        if (!fphonenumber.equals("")) {
            Sms sms = new Sms();
            sms.sendOutMsg(fphonenumber, msg);
        }
    }
}
