package com.steering.pss.judge.DAL;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.judge.model.QcmBaseLanguageEntity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 朱少波 on 2018/11/27.
 */
public class QcmBaseLanguageFrm extends DBCommon {

    public CoreReturnObject QueryAllLanguageSet(String[] Control,String  typeid){

          String  sql = CoreXmlSqlParsersFactory.getSql("QcmBaseLanguageFrm.QueryAllLanguageSet");

       String ID= CoreConverter.toSqlConditionString("ID", Control);
       if(ID.equals("")){
           ID = " AND 1<>1 ";
       }

       sql = sql.replace("@ID",ID);
        return this.getData(sql,new Object[]{typeid});
    }


    public CoreReturnObject Save(ArrayList<String> backJsons, String Name) throws SQLException {
        ArrayList<String> sqls= new ArrayList<String>();

        for(String backJson : backJsons) {
            QcmBaseLanguageEntity Entity = EntityTool.getEntityByGson(
                    backJson, QcmBaseLanguageEntity.class);



            if(Entity.getId().equals("")){
                QcmBaseLanguageEntity ExistsEntity = getExitsEntity(Entity.getLanguageCode(),Entity.getOriginalValue());
                if(ExistsEntity!=null){
                    Entity.setId(ExistsEntity.getId());
                }
            }


            if(Entity.getId().equals("")){
                Entity.setCreateName(Name);
              // sqls.add(EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql("QcmBaseLanguageFrm.BeforeAdd"), Entity));
                sqls.add(EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql("QcmBaseLanguageFrm.Add"), Entity));
               // sqls.add(EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql("QcmBaseLanguageFrm.AddC"), Entity));
            }
            else {

                Entity.setUpdateName(Name);
              //  sqls.add(EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql("QcmBaseLanguageFrm.BeforeUpdate"), Entity));
              //  sqls.add(EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql("QcmBaseLanguageFrm.UpdateC"), Entity));
                sqls.add(EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql("QcmBaseLanguageFrm.Update"), Entity));
               // sqls.add(EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql("QcmBaseLanguageFrm.AddC"), Entity));
            }
        }

        return this.setData(sqls,new ArrayList());
    }



    public CoreReturnObject Delete(ArrayList<String> backJsons, String Name){
        ArrayList<String> sqls= new ArrayList<String>();

        for(String backJson : backJsons) {
            QcmBaseLanguageEntity Entity = EntityTool.getEntityByGson(
                    backJson, QcmBaseLanguageEntity.class);
              Entity.setDeleteName(Name);
                sqls.add(EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql("QcmBaseLanguageFrm.Delete"), Entity));
             //   sqls.add(EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql("QcmBaseLanguageFrm.DeleteC"), Entity));
        }

        return this.setData(sqls,new ArrayList());
    }

    public  QcmBaseLanguageEntity getExitsEntity( String Lcode, String OrValue) throws SQLException {

        CoreReturnObject cro = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        QcmBaseLanguageEntity entity = null;
        try{

        String sql = CoreXmlSqlParsersFactory
                .getSql("QcmBaseLanguageFrm.Query1");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, Lcode);
            ps.setObject(2, OrValue);
        ResultSet rs = ps.executeQuery();
        List<QcmBaseLanguageEntity> Entitys = EntityTool.getEntitysByRs(rs,
                QcmBaseLanguageEntity.class);
        rs.close();
        ps.close();
        if (Entitys == null || Entitys.size() == 0) {
            return null;
        } else {

            entity= Entitys.get(0);
        }
        }catch(Exception ex){
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            cro.setV_errMsg(ex.getMessage());
        }finally{
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return entity;
    }
}
