package com.steering.jobface.lims;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.Util.StringUtils;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.jobface.lims.service.LimsDataService;
import com.steering.model.lims.MMeasureTEntity;
import com.steering.model.lims.MeterWorkCarActualBksEntity;
import com.steering.model.lims.MeterWorkCarActualFirstEntity;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.TimerTask;

public class LimsDelDataTask extends TimerTask {

    DBCommon db = null;
    LimsDelDataTask(){
        db = new DBCommon();
    }

    @Override
    public void run() {

        Connection con = null;
        LimsDataService limsDataService = new LimsDataService();
        ArrayList<MeterWorkCarActualBksEntity> entitys = null;
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDelDataTask.getCarActual");
        try {
            con = db.getDao("LimsDao").getConnection();
            con.setAutoCommit(false);
            entitys = EntityTool.getEntitysBySqlNoClsCon(sql, con, MeterWorkCarActualBksEntity.class);
            for (MeterWorkCarActualBksEntity p : entitys) {
                MMeasureTEntity mMeasureTEntity = new MMeasureTEntity();
                //ÑéÅäID
                mMeasureTEntity.setMatchid(p.getActualNo());
                if (!StringUtils.isEmpty(p.getNoticeNo()) && "001006003".equals(p.getMeterTypeNo())) { // ÖÐ¿Øµ¹ÔË
                    mMeasureTEntity.setMatchid(p.getNoticeNo());
                }
                mMeasureTEntity.setOldmatchid(mMeasureTEntity.getMatchid());

                MMeasureTEntity measure = limsDataService.getMeasure(mMeasureTEntity);
                if(measure!=null){
                    limsDataService.deleteMeasure(measure);
                }

                limsDataService.deleteCarActualBks(p);
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                if (con != null && !con.isClosed()) {
                    con.rollback();
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
