package com.steering.pss.plnsaleord.order.baseMethod;

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

import java.sql.SQLException;
import java.util.List;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.pss.plnsaleord.order.model.SlmWellUseListEntity;

/**
 * 调整产能井月
 * @author WenHB
 */
public class CoreAdjustWellMonth {

    /**
     * 根据排产月份调整产能井
     * @param con
     * @param ordLnDlyPk
     * @param mon
     * @throws SQLException
     */
    public static void adjustWellMonth(Connection con, String ordLnDlyPk,
            int mon) throws SQLException {

        // OrdLnDlyPk获取生产周期分析记录
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreAdjustWellMonth.GetCycle");

        sql = StringTool.ReplaceAll(sql, "#ORD_LN_DLY_PK#", ordLnDlyPk);
        List<SlmWellUseListEntity> list = EntityTool.getEntitysBySqlNoClsCon(
                sql, con, SlmWellUseListEntity.class);
        for (SlmWellUseListEntity swl : list) {
            // 释放产能井SQL语句
            String sql1 = CoreXmlSqlParsersFactory
                    .getSql("CoreAdjustWellMonth.InsertWellList");
            swl.setUseType("释放");
            swl.setUseWgt("-" + swl.getUseWgt());
            sql1 = EntityTool.fillSqlByEntity(sql1, swl);
            PreparedStatement ps = con.prepareStatement(sql1);
            ps.executeUpdate();
            ps.close();
            int wellMonth = Integer.parseInt(swl.getUseMonth());
            int wellYear = Integer.parseInt(swl.getWellYear());

            if (mon > 0) {
                wellMonth = wellMonth + mon % 12;
                if (wellMonth > 12) {
                    wellYear += wellMonth / 12;
                    wellMonth = wellMonth % 12;
                }
                wellYear += mon / 12;
            }
            else {
                wellMonth = wellMonth + mon % 12;
                if (wellMonth <= 0) {
                    wellYear = wellYear + wellMonth / 12;
                    wellMonth = wellMonth % 12 + 12;
                }
                wellYear += mon / 12;
            }
            // 占用新的产能井
            String sql2 = CoreXmlSqlParsersFactory
                    .getSql("CoreAdjustWellMonth.InsertWellList");
            swl.setWellYear(String.valueOf(wellYear));
            swl.setUseMonth(String.valueOf(wellMonth));
            swl.setUseType("消耗");
            swl.setUseWgt(String.valueOf(Math.abs(Double.parseDouble(swl
                    .getUseWgt()))));
            sql2 = EntityTool.fillSqlByEntity(sql2, swl);
            ps = con.prepareStatement(sql2);
            ps.executeUpdate();
            ps.close();
        }

    }
}
