package com.steering.Mcms;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.Mcms.entity.CmmRecordTemplateEntity;
import com.steering.comm.db.DBCommon;
import com.steering.common.EntityTool;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * Created by 朱少波 on 2020/11/20.
 */
public class TemplateServer extends DBCommon {


    public CoreReturnObject doQueryTempate(String location) {

        String sql = CoreXmlSqlParsersFactory
                .getSql("TemplateServer.doQueryTempate");

        if (!location.equals("")) {
            sql += " AND TEMPLATE_LOCATION = '" + location + "'";
        }



        sql += " Order by TEMPLATE_NAME ";

        return this.getData(sql);
    }



    public CoreReturnObject DoSaveTemplateNo(String templateNoJson) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();

        try {
            connection.setAutoCommit(false); //


            CmmRecordTemplateEntity cmmRecordTemplateEntity =
                    EntityTool.getEntityByJSONStr(templateNoJson, CmmRecordTemplateEntity.class);

            PreparedStatement pst = null;

            String sqldeleteTemplate = CoreXmlSqlParsersFactory
                    .getSql("TemplateServer.DoDeleteTemplate2");
            pst = connection.prepareStatement(sqldeleteTemplate);
            pst.setObject(1, cmmRecordTemplateEntity.getCreateUser());
            pst.setObject(2, cmmRecordTemplateEntity.getTemplateName());
            pst.executeUpdate();
            pst.close();


            String sqlAddTemplate = CoreXmlSqlParsersFactory
                    .getSql("TemplateServer.doSaveTemplate");
            sqlAddTemplate = com.steering.common.EntityTool.fillSqlByEntity(sqlAddTemplate, cmmRecordTemplateEntity);
            pst = connection.prepareStatement(sqlAddTemplate);
            pst.executeUpdate();
            pst.close();


            core.setV_errMsg("保存成功！");
            core.setResult(1);
            connection.commit();

        } catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                } catch (Exception e2) {
                }
            }
        } finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (Exception e2) {
            }
        }
        return core;
    }

    public CoreReturnObject DoDeleteTemplate(String key, String userName) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();

        try {
            connection.setAutoCommit(false); //

            PreparedStatement pst = null;


            String sqlAddTemplate = CoreXmlSqlParsersFactory
                    .getSql("TemplateServer.DoDeleteTemplate");
            pst = connection.prepareStatement(sqlAddTemplate);
            pst.setObject(1, userName);
            pst.setObject(2, key);
            pst.executeUpdate();
            pst.close();


            core.setV_errMsg("删除成功！");
            core.setResult(1);
            connection.commit();

        } catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                } catch (Exception e2) {
                }
            }
        } finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (Exception e2) {
            }
        }
        return core;
    }
}
