package com.steering.pss.ydm.base;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.ydm.entity.ProcessOrderCEntity;
import com.steering.pss.ydm.entity.ProcessOrderMEntity;
import com.steering.pss.ydm.entity.SlmOrderLineDeliveryEntity;
import com.steering.pss.ydm.entity.SlmOrderLineEntity;

/**
 * 合同进程跟踪方法
 * @author Administrator
 */
public class ProcessTracking extends DBCommon {
    /**
     * 写进程表
     * @param con 连接字符串
     * @param ordLnPk 合同行主键
     * @param ordLnDlyPk 交货行主键
     * @param num 支数
     * @param wgt 重量
     * @param eventId 事件编码
     * @param eventName 事件名称
     * @param formName 命名空间加类名
     * @param callFuncCoding 方法名
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static boolean processRecord(Connection con, String ordLnPk,
            String ordLnDlyPk, double num, double wgt, String eventId,
            String eventName, String formName, String callFuncCoding)
            throws SQLException, IllegalAccessException,
            InvocationTargetException {
        if (ordLnPk.length() == 0 && ordLnDlyPk.length() == 0) {
            return false;
        }
        PreparedStatement ps = null;
        String sql = "";
        // 合同行操作
        // 判断合同行是否在进程主表中有记录
        ProcessOrderMEntity processOrderMEntity = ProcessTracking
                .queryProcessOrderM(con, ordLnPk);
        // 如果没有则新增记录
        if (processOrderMEntity == null) {
            // 查询合同行信息(如果合同行信息没有,则返回)
            SlmOrderLineEntity slmOrderLineEntity = ProcessTracking
                    .queryOrderLineByPk(con, ordLnPk);
            if (slmOrderLineEntity == null) {
                return false;
            }
            processOrderMEntity = new ProcessOrderMEntity();
            BeanUtils.copyProperties(processOrderMEntity, slmOrderLineEntity);
            sql = CoreXmlSqlParsersFactory
                    .getSql("ProcessTracking.addProcessOrderM");
            sql = EntityTool.fillSqlByEntity(sql, processOrderMEntity);
            ps = con.prepareStatement(sql);
            ps.executeUpdate();
            ps.close();
        }
        sql = CoreXmlSqlParsersFactory
                .getSql("ProcessTracking.updateProcessOrderM");
        // 脱单
        if (eventId.equals("900101")) {
            if (processOrderMEntity != null
                    && !processOrderMEntity.getElimNum().equals("")) {
                num += Double.parseDouble(processOrderMEntity.getElimNum());
            }
            if (processOrderMEntity != null
                    && !processOrderMEntity.getElimWgt().equals("")) {
                wgt += Double.parseDouble(processOrderMEntity.getElimWgt());
            }
            sql += " ELIM_NUM= ?,ELIM_WGT= ? WHERE ORD_LN_PK = ? ";
        }
        // 利库
        if (eventId.equals("900102")) {
            if (processOrderMEntity != null
                    && !processOrderMEntity.getChargeNum().equals("")) {
                num += Double.parseDouble(processOrderMEntity.getChargeNum());
            }
            if (processOrderMEntity != null
                    && !processOrderMEntity.getChargeWgt().equals("")) {
                wgt += Double.parseDouble(processOrderMEntity.getChargeWgt());
            }
            sql += " CHARGE_NUM= ?,CHARGE_WGT= ? WHERE ORD_LN_PK = ?";
        }
        ps = con.prepareStatement(sql);
        ps.setObject(1, eventId);
        ps.setObject(2, eventName);
        ps.setObject(3, formName);
        ps.setObject(4, callFuncCoding);
        ps.setObject(5, num);
        ps.setObject(6, wgt);
        ps.setObject(7, ordLnPk);
        ps.executeUpdate();
        ps.close();
        // 代表只执行合同行的操作
        if (ordLnDlyPk.length() == 0) {
            return true;
        }
        // 交货行操作
        ProcessOrderCEntity processOrderCEntity = ProcessTracking
                .queryProcessOrderC(con, ordLnDlyPk, "Z");

        if (processOrderCEntity == null) {
            SlmOrderLineDeliveryEntity slmOrderLineDeliveryEntity = ProcessTracking
                    .queryOrderLineDlyByPk(con, ordLnDlyPk);
            if (slmOrderLineDeliveryEntity == null) {
                return false;
            }
            processOrderCEntity = new ProcessOrderCEntity();
            BeanUtils.copyProperties(processOrderCEntity,
                    slmOrderLineDeliveryEntity);
            processOrderCEntity.setProcessCdoe("Z");
            processOrderCEntity.setProcessDesc("成品");
            sql = CoreXmlSqlParsersFactory
                    .getSql("ProcessTracking.addProcessOrderC");
            sql = EntityTool.fillSqlByEntity(sql, processOrderCEntity);
            ps = con.prepareStatement(sql);
            ps.executeUpdate();
            ps.close();
        }
        sql = CoreXmlSqlParsersFactory
                .getSql("ProcessTracking.updateProcessOrderC");
        // 脱单
        if (eventId.equals("900101")) {
            if (processOrderCEntity != null
                    && !processOrderCEntity.getElimNum().equals("")) {
                num += Double.parseDouble(processOrderCEntity.getElimNum());
            }
            if (processOrderCEntity != null
                    && !processOrderCEntity.getElimWgt().equals("")) {
                wgt += Double.parseDouble(processOrderCEntity.getElimWgt());
            }
            sql += "  ELIM_NUM= ?,ELIM_WGT= ? WHERE ORD_LN_DLY_PK = ? AND PROCESS_CDOE = 'Z' ";
        }
        // 利库
        if (eventId.equals("900102")) {
            if (processOrderCEntity != null
                    && !processOrderCEntity.getChargeNum().equals("")) {
                num += Double.parseDouble(processOrderCEntity.getChargeNum());
            }
            if (processOrderCEntity != null
                    && !processOrderCEntity.getChargeWgt().equals("")) {
                wgt += Double.parseDouble(processOrderCEntity.getChargeWgt());
            }
            sql += " CHARGE_NUM= ?,CHARGE_WGT= ?  WHERE ORD_LN_DLY_PK = ? AND PROCESS_CDOE = 'Z' ";
        }
        ps = con.prepareStatement(sql);
        ps.setObject(1, eventId);
        ps.setObject(2, eventName);
        ps.setObject(3, formName);
        ps.setObject(4, callFuncCoding);
        ps.setObject(5, num);
        ps.setObject(6, wgt);
        ps.setObject(7, ordLnDlyPk);
        ps.executeUpdate();
        ps.close();
        return true;
    }

    /**
     * 查询合同行信息
     * @param con
     * @param ordLnPk
     * @return
     * @throws SQLException
     */
    public static SlmOrderLineEntity queryOrderLineByPk(Connection con,
            String ordLnPk) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("ProcessTracking.queryOrderLineByPk");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnPk);
        ResultSet rs = ps.executeQuery();
        List<SlmOrderLineEntity> list = EntityTool.getEntitysByRs(rs,
                SlmOrderLineEntity.class);
        rs.close();
        ps.close();
        if (list != null && list.size() > 0) {
            for (SlmOrderLineEntity slmOrderLineEntity : list) {
                QualityDesignTool.nullConverNullString(slmOrderLineEntity);
            }
            return list.get(0);
        }
        else {
            return null;
        }
    }

    /**
     * 查询交货行信息
     * @param con
     * @param ordLnDlyPk
     * @return
     * @throws SQLException
     */
    public static SlmOrderLineDeliveryEntity queryOrderLineDlyByPk(
            Connection con, String ordLnDlyPk) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("ProcessTracking.queryOrderLineDlyByPk");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnDlyPk);
        ResultSet rs = ps.executeQuery();
        List<SlmOrderLineDeliveryEntity> list = EntityTool.getEntitysByRs(rs,
                SlmOrderLineDeliveryEntity.class);
        rs.close();
        ps.close();
        if (list != null && list.size() > 0) {
            for (SlmOrderLineDeliveryEntity slmOrderLineEntity : list) {
                QualityDesignTool.nullConverNullString(slmOrderLineEntity);
            }
            return list.get(0);
        }
        else {
            return null;
        }
    }

    /**
     * 查询主进程信息
     * @param con
     * @param ordLnDlyPk
     * @return
     * @throws SQLException
     */
    public static ProcessOrderMEntity queryProcessOrderM(Connection con,
            String ordLnDlyPk) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("ProcessTracking.queryProcessOrderM");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnDlyPk);
        ResultSet rs = ps.executeQuery();
        List<ProcessOrderMEntity> list = EntityTool.getEntitysByRs(rs,
                ProcessOrderMEntity.class);
        rs.close();
        ps.close();
        if (list != null && list.size() > 0) {
            for (ProcessOrderMEntity processOrderMEntity : list) {
                QualityDesignTool.nullConverNullString(processOrderMEntity);
            }
            return list.get(0);
        }
        else {
            return null;
        }
    }

    /**
     * 查询从进程信息
     * @param con
     * @param ordLnDlyPk
     * @return
     * @throws SQLException
     */
    public static ProcessOrderCEntity queryProcessOrderC(Connection con,
            String ordLnDlyPk, String processCode) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("ProcessTracking.queryProcessOrderC");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnDlyPk);
        ps.setObject(2, processCode);
        ResultSet rs = ps.executeQuery();
        List<ProcessOrderCEntity> list = EntityTool.getEntitysByRs(rs,
                ProcessOrderCEntity.class);
        rs.close();
        ps.close();
        if (list != null && list.size() > 0) {
            for (ProcessOrderCEntity processOrderCEntity : list) {
                QualityDesignTool.nullConverNullString(processOrderCEntity);
            }
            return list.get(0);
        }
        else {
            return null;
        }
    }
}
