package com.steering.pss.sale.order;

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

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.model.SlmOrderLineEntity;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

/**
 * 现货合同未提单时处理
 * @author WenHB
 */
public class CoreOrderLineSpotLineTask extends TimerTask {

    public void run() {
        DBCommon db = new DBCommon();
        Connection con = null;
        try {
            con = db.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            // 查询符合条件的现货合同行（从2016-02-23以后的合同行开始创建时间大于7天(SQL写的9天 考虑到周末)）
            // 删除合同所在的库存信息（将其变成成品）
            // 删除合同行信息，交货行信息
            List<String> list = new ArrayList<String>();
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderLineSpotLineTask.GetLineSql");

            List<SlmOrderLineEntity> lines = EntityTool.getEntitysBySqlNoClsCon(
                    sql, con, SlmOrderLineEntity.class);

            for (SlmOrderLineEntity line : lines) {
                if (list.contains(line.getOrdPk()))
                    continue;
                String sql1 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderLineSpotLineTask.IsExistsBill");
                PreparedStatement ps = con.prepareStatement(sql1);
                ps.setObject(1, line.getOrdPk());
                ResultSet rs = ps.executeQuery();
                String count = "";
                if (rs.next()) {
                    count = rs.getString(1);
                }
                rs.close();
                ps.close();
                if (count == null || count.equals("")) {
                    // 未做提单的合同行将被作废 并释放库存
                    String sql2 = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderLineSpotLineTask.TakeOffLine");
                    ps = con.prepareStatement(sql2);
                    ps.setObject(1, line.getOrdLnPk());
                    ps.executeUpdate();
                    ps.close();
                    // 删除合同行 交货行 发运行
                    String sql3 = CoreXmlSqlParsersFactory.getSql(
                            "CoreOrderLineSpotLineTask.DeleteLineDlyLineTransit");
                    ps = con.prepareStatement(sql3);
                    ps.setObject(1, line.getOrdLnPk());
                    ps.setObject(2, line.getOrdLnPk());
                    ps.setObject(3, line.getOrdLnPk());
                    ps.setObject(4, line.getOrdLnPk());
                    ps.setObject(5, line.getOrdPk());
                    ps.setObject(6, line.getOrdPk());
                    ps.executeUpdate();
                    ps.close();
                    con.commit();
                }
                else {
                    // 合同行所在的合同任一行做了提单 不再处理
                    if (!list.contains(line.getOrdPk())) {
                        list.add(line.getOrdPk());
                    }
                }

            }
        }
        catch (SQLException 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();
            }
        }
    }

}
