package com.steering.pss.qcm.DAL;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.Util.StringUtils;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.comm.tools.TransctionTool;
import com.steering.pss.qcm.model.QcmProblemHandlingEntity;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class QcmProblemHandling  extends DBCommon {

    public CoreReturnObject query(String judgeStoveNo, String plines, String timeB, String timeE){
        ArrayList<Object> parms = new ArrayList<Object>();
        parms.add(judgeStoveNo);
        parms.add(plines.split(","));
        parms.add(timeB);
        parms.add(timeE);
        SqlBuilder b = new SqlBuilder("QcmProblemHandling.getProblem");
        b.Fill(parms.toArray());
        return this.getData(b.toString());
    }

    public CoreReturnObject query1(String judgeStoveNo, String plines, String timeB, String timeE){
        ArrayList<Object> parms = new ArrayList<Object>();
        parms.add(judgeStoveNo);
        parms.add(plines.split(","));
        parms.add(timeB);
        parms.add(timeE);
        SqlBuilder b = new SqlBuilder("QcmProblemHandling.query");
        b.Fill(parms.toArray());
        return this.getData(b.toString());
    }


    /**
     * 新增
     */
    public CoreReturnObject add(String json) {
        ArrayList<String> jsons = new ArrayList<String>();
        jsons.add(json);
        return add(jsons);
    }

    public CoreReturnObject add(ArrayList<String> jsons){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            Map<String, String> map = new HashMap<String, String>();
            map.put("炼钢","A");
            map.put("轧管","D");
            map.put("墩拔扩","E");
            map.put("热处理","F");
            map.put("加工","G");
            for(String json : jsons){
                QcmProblemHandlingEntity qcmProblemHandlingEntity = EntityTool.getEntityByGson(json, QcmProblemHandlingEntity.class);
                String sqlInsert = CoreXmlSqlParsersFactory.getSql("QcmProblemHandling.insert");
                sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, qcmProblemHandlingEntity);
                if(!transction.ExcuteTransction(sqlInsert)){
                    return transction.rollBack();
                }
                if (!StringUtils.isEmpty(qcmProblemHandlingEntity.getProcessCodeNext())) {
                    for (String processCode : qcmProblemHandlingEntity.getProcessCodeNext().split(",")) {
                        qcmProblemHandlingEntity.setProcessCode(map.get(processCode));
                        sqlInsert = CoreXmlSqlParsersFactory.getSql("QcmProblemHandling.insert");
                        sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, qcmProblemHandlingEntity);
                        if(!transction.ExcuteTransction(sqlInsert)){
                            return transction.rollBack();
                        }
                    }
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }

    /**
     * 修改
     */
    public CoreReturnObject update(String json) {
        ArrayList<String> jsons = new ArrayList<String>();
        jsons.add(json);
        return update(jsons);
    }
    public CoreReturnObject update(ArrayList<String> jsons){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            for(String json : jsons){
                QcmProblemHandlingEntity qcmProblemHandlingEntity = EntityTool.getEntityByGson(json, QcmProblemHandlingEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("QcmProblemHandling.Update");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, qcmProblemHandlingEntity);
                if(!transction.ExcuteTransction(sqlUpdate)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }

    public CoreReturnObject updateConfirm(String json) {
        ArrayList<String> jsons = new ArrayList<String>();
        jsons.add(json);
        return updateConfirm(jsons);
    }
    public CoreReturnObject updateConfirm(ArrayList<String> jsons){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            for(String json : jsons){
                QcmProblemHandlingEntity qcmProblemHandlingEntity = EntityTool.getEntityByGson(json, QcmProblemHandlingEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("QcmProblemHandling.UpdateConfirm");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, qcmProblemHandlingEntity);
                if(!transction.ExcuteTransction(sqlUpdate)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }

    /**
     * 删除
     */
    public CoreReturnObject delete(String json) {
        ArrayList<String> jsons = new ArrayList<String>();
        jsons.add(json);
        return delete(jsons);
    }
    public CoreReturnObject delete(ArrayList<String> jsons){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            for(String json : jsons){
                QcmProblemHandlingEntity qcmProblemHandlingEntity = EntityTool.getEntityByGson(json, QcmProblemHandlingEntity.class);
                String sqlDelete = CoreXmlSqlParsersFactory.getSql("QcmProblemHandling.Delete");
                sqlDelete = EntityTool.fillSqlByEntity(sqlDelete, qcmProblemHandlingEntity);
                if(!transction.ExcuteTransction(sqlDelete)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
}
