package com.mjm.solarInsertDB;

import com.csvreader.CsvWriter;

import javax.baja.naming.BOrd;
import javax.baja.nre.annotations.*;
import javax.baja.sys.*;
import javax.baja.util.BNotification;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by user on 2017/4/7.
 */
@NiagaraType
@NiagaraSlots(
        properties =
                {
                        @NiagaraProperty(name = "DatabaseInformation", type = "BDatabaseInformation", defaultValue = "new BDatabaseInformation()", flags = Flags.SUMMARY),
                        @NiagaraProperty(name = "disable", type = "boolean", defaultValue = "true", flags = Flags.SUMMARY),
                        @NiagaraProperty(name = "showMessage", type = "boolean", defaultValue = "true", flags = Flags.SUMMARY)
                },
        actions =
                {
                        @NiagaraAction(name = "insert"),
                        @NiagaraAction(name = "setField", parameterType = "BInteger", defaultValue = "BInteger.make(1)")
                }
)


public class BInsertData extends BComponent {
/*+ ------------ BEGIN BAJA AUTO GENERATED CODE ------------ +*/
/*@ $com.mjm.solarInsertDB.BInsertData(1868318216)1.0$ @*/
/* Generated Mon Mar 24 23:40:57 CST 2025 by Slot-o-Matic (c) Tridium, Inc. 2012 */

////////////////////////////////////////////////////////////////
// Property "DatabaseInformation"
////////////////////////////////////////////////////////////////
  
  /**
   * Slot for the {@code DatabaseInformation} property.
   * @see #getDatabaseInformation
   * @see #setDatabaseInformation
   */
  public static final Property DatabaseInformation = newProperty(Flags.SUMMARY, new BDatabaseInformation(), null);
  
  /**
   * Get the {@code DatabaseInformation} property.
   * @see #DatabaseInformation
   */
  public BDatabaseInformation getDatabaseInformation() { return (BDatabaseInformation)get(DatabaseInformation); }
  
  /**
   * Set the {@code DatabaseInformation} property.
   * @see #DatabaseInformation
   */
  public void setDatabaseInformation(BDatabaseInformation v) { set(DatabaseInformation, v, null); }

////////////////////////////////////////////////////////////////
// Property "disable"
////////////////////////////////////////////////////////////////
  
  /**
   * Slot for the {@code disable} property.
   * @see #getDisable
   * @see #setDisable
   */
  public static final Property disable = newProperty(Flags.SUMMARY, true, null);
  
  /**
   * Get the {@code disable} property.
   * @see #disable
   */
  public boolean getDisable() { return getBoolean(disable); }
  
  /**
   * Set the {@code disable} property.
   * @see #disable
   */
  public void setDisable(boolean v) { setBoolean(disable, v, null); }

////////////////////////////////////////////////////////////////
// Property "showMessage"
////////////////////////////////////////////////////////////////
  
  /**
   * Slot for the {@code showMessage} property.
   * @see #getShowMessage
   * @see #setShowMessage
   */
  public static final Property showMessage = newProperty(Flags.SUMMARY, true, null);
  
  /**
   * Get the {@code showMessage} property.
   * @see #showMessage
   */
  public boolean getShowMessage() { return getBoolean(showMessage); }
  
  /**
   * Set the {@code showMessage} property.
   * @see #showMessage
   */
  public void setShowMessage(boolean v) { setBoolean(showMessage, v, null); }

////////////////////////////////////////////////////////////////
// Action "insert"
////////////////////////////////////////////////////////////////
  
  /**
   * Slot for the {@code insert} action.
   * @see #insert()
   */
  public static final Action insert = newAction(0, null);
  
  /**
   * Invoke the {@code insert} action.
   * @see #insert
   */
  public void insert() { invoke(insert, null, null); }

////////////////////////////////////////////////////////////////
// Action "setField"
////////////////////////////////////////////////////////////////
  
  /**
   * Slot for the {@code setField} action.
   * @see #setField(BInteger parameter)
   */
  public static final Action setField = newAction(0, BInteger.make(1), null);
  
  /**
   * Invoke the {@code setField} action.
   * @see #setField
   */
  public void setField(BInteger parameter) { invoke(setField, parameter, null); }

////////////////////////////////////////////////////////////////
// Type
////////////////////////////////////////////////////////////////
  
  @Override
  public Type getType() { return TYPE; }
  public static final Type TYPE = Sys.loadType(BInsertData.class);

/*+ ------------ END BAJA AUTO GENERATED CODE -------------- +*/



    BDatabaseInformation databaseInformation = null;

    public void started() {
        databaseInformation = (BDatabaseInformation) this.getDatabaseInformation().asComponent();
        BOrd absolute = BOrd.make("station:|slot:/Services/PlatformServices/SystemService/hostId");
        String hostID = absolute.get().toString();
        System.out.println(this.getType().getTypeName() + " --onStarted-- " + hostID);
    }


    public void changed(Property property, Context context) {
        if (this.isRunning()) {
            if (property == databaseInformation) {
                System.out.println("in insert  data");
                databaseInformation = (BDatabaseInformation) this.databaseInformation.asComponent();
            }
        }
    }

    public void doInsert() {

        System.out.println("0324 doInsert: ");
        if (1 == 1) { //if (checkLicense.getLegal())
            List<String> fieldName = new ArrayList<String>();
            List<String> fieldValue = new ArrayList<String>();

            if (this.getDisable() ) {
                if (this.getShowMessage()) {
                    System.out.println("disable ");
                }
            } else {
                try {
                    Property property[] = this.getDynamicPropertiesArray();

                    for (int i = 0; i < property.length; i++) {
                        if (property[i].getName().contains("Field")) {
                            BField field = (BField) this.get(property[i]).asComponent();
                            fieldName.add(field.getFieldName());
                            fieldValue.add(field.getFieldValue());
                        }
                    }

                    if (fieldName.size() == fieldValue.size()) {
                            String insertCommand = makeInsertCommand(fieldName, fieldValue);

                            // 建立连接
                            ConnectInformation connectInformation = new ConnectInformation();
                            Connection connection = connectInformation.getSQLServerConnection(databaseInformation.getDbIp(), databaseInformation.getDbName(),
                                    databaseInformation.getDbUser(), databaseInformation.getDbPassword(), databaseInformation.getDbTableName());
                            System.out.println("------connection info: " + connection);
                            // 创建Statement
                            Statement statement = connection.createStatement();
                            System.out.println("------insertCommand info: " + insertCommand);
                            // 执行查询
                            statement.execute(insertCommand);
                            statement.close();
                            connection.close();
                        createCSV(fieldName, fieldValue);
                    }
                } catch (Exception e) {
                    System.out.println(e);
                    e.printStackTrace();
                } finally {
                }
            }
        } else {
            System.out.println("no license!");
            BNotification note = new BNotification();
            note.add("title", BString.make("No License"));
            note.add("message", BString.make("Please contact FIC"));
            note.raise(true);
        }
    }


    public void doSetField(BInteger interger) {

        int number = interger.getInt();
        Property property[] = this.getDynamicPropertiesArray();
        for (int i = 0; i < property.length; i++) {
            if (property[i].getName().contains("Field")) {
                this.remove(property[i].getName());
            }
        }
        for (int i = 0; i < number; i++) {
            this.add("Field" + (i + 1), new BField());
        }
    }

    private void createCSV(List<String> fieldName, List<String> fieldValue) {

        SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String strDate = sdFormat.format(date);
        String outputFile = "../../shared/" ;

        // before we open the file check to see if it already exists
        boolean alreadyExists = new File(outputFile).exists();
        try {
            // use FileWriter constructor that specifies open for appending
            if (!alreadyExists) {
                deleteFile();
            }

            CsvWriter csvOutput = new CsvWriter(new FileWriter(outputFile, true), ',');
            // if the file didn't already exist then we need to write out the header line
            if (!alreadyExists) {
                for (int i = 0; i < fieldName.size(); i++) {
                    csvOutput.write(fieldName.get(i));
                }
                csvOutput.endRecord();
            }
            // else assume that the file already has the correct header line
            // write out a few records

            for (int i = 0; i < fieldValue.size(); i++) {
                csvOutput.write(fieldValue.get(i));
            }
            csvOutput.endRecord();
            csvOutput.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void deleteFile() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -90);//90天前
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String before90 = format.format(cal.getTime()) + " 00:00:00";
        System.out.println("delete file before " + before90);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));

        try {
            Date sDate = sdf.parse(before90);
            long beforeTimestamp = sDate.getTime() / 1000;
//            System.out.println(beforeTimestamp);
            String fullpath = "../../shared/";
            File a = new File(fullpath);
            if (a.isDirectory()) {
                String[] filenames = a.list();
                for (int i = 0; i < filenames.length; i++) {
                    File tempFile = new File(fullpath + "\\" + filenames[i]);
                    if (tempFile.isDirectory()) {
                        System.out.println("目錄:" + filenames[i]);
                    } else {
//                        System.out.println("檔案:" + filenames[i]);
                        String dateName = filenames[i].split(" ")[0] + " 00:00:00";
                        sDate = sdf.parse(dateName);
                        long fileTimestamp = sDate.getTime() / 1000;
                        if (fileTimestamp < beforeTimestamp) {
                            System.out.println("delete file " + fullpath + filenames[i]);
                            File deleteFile = new File(fullpath + filenames[i]);
                            deleteFile.delete();
                        }
                    }
                }
            } else {
                System.out.println("[" + a + "]不是目錄");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private String makeInsertCommand(List<String> fieldName, List<String> fieldValue) {
        String command = "INSERT INTO " + databaseInformation.getDbName() + ".dbo." + databaseInformation.getDbTableName() + " (";
        for (int i = 0; i < fieldName.size(); i++) {
            command = command + fieldName.get(i) + ", ";
        }
        command = command.substring(0, command.length() - 2) + ") VALUES (";
        for (int i = 0; i < fieldValue.size(); i++) {
            command = command + "'" + fieldValue.get(i) + "', ";
        }
        command = command.substring(0, command.length() - 2) + ");";
        System.out.println("insert command: " + command);
        return command;
    }


    public void setDatabaseInformation() {
        databaseInformation = (BDatabaseInformation) this.getDatabaseInformation().asComponent();
    }
}
