package com.steering.mes.mcp.Mch;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.MchBundlingTypeEntity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

/**
 * Created by 朱少波 on 2019/2/26.
 */
public class FrmBundingType  extends DBCommon {
    public CoreReturnObject query() {
        return this.getDataBySqlID("FrmBundingType.query",
                new Object[] {});
    }

    public CoreReturnObject DoSave(ArrayList<String> Resuls, String UserName){
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String systime = dateFormat.format(now);

        try {
            connection.setAutoCommit(false); //



            String sqlAdd = CoreXmlSqlParsersFactory
                    .getSql("FrmBundingType.Add");// 新增
            PreparedStatement patAdd = connection
                    .prepareStatement(sqlAdd);

            String addUpdate = CoreXmlSqlParsersFactory
                    .getSql("FrmBundingType.Update");// 修改
            PreparedStatement patsUpdate = connection
                    .prepareStatement(addUpdate);


            for (int i = 0; i < Resuls.size(); i++) {

                MchBundlingTypeEntity Entity = EntityTool.getEntityByJSONStr(Resuls.get(i),MchBundlingTypeEntity.class);
                if(Entity.getBaseCode().equals("")){
                    patAdd.setObject(1,Entity.getBaseName());
                    patAdd.setObject(2,Entity.getPricingType());
                    patAdd.setObject(3,Entity.getUnitPrice());
                    patAdd.setObject(4,Entity.getMemo());
                    patAdd.setObject(5,UserName);
                    patAdd.addBatch();
                }else {
                    patsUpdate.setObject(1,Entity.getBaseName());
                    patsUpdate.setObject(2,Entity.getPricingType());
                    patsUpdate.setObject(3,Entity.getUnitPrice());
                    patsUpdate.setObject(4,Entity.getMemo());
                    patsUpdate.setObject(5,UserName);
                    patsUpdate.setObject(6,Entity.getBaseCode());
                    patsUpdate.addBatch();


                }

            }
            patAdd.executeBatch();
            patsUpdate.executeBatch();


            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 DoDelete(ArrayList<String> Resuls, String UserName){
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String systime = dateFormat.format(now);

        try {
            connection.setAutoCommit(false); //



            String sqlDelete = CoreXmlSqlParsersFactory
                    .getSql("FrmBundingType.Delete");// 新增
            PreparedStatement patDelete = connection
                    .prepareStatement(sqlDelete);



            for (int i = 0; i < Resuls.size(); i++) {

                MchBundlingTypeEntity Entity = EntityTool.getEntityByJSONStr(Resuls.get(i),MchBundlingTypeEntity.class);
                if(!Entity.getBaseCode().equals("")){
                    patDelete.setObject(1,UserName);
                    patDelete.setObject(2,Entity.getBaseCode());
                    patDelete.addBatch();
                }

            }
            patDelete.executeBatch();


            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;
    }
}
