package com.steering.pss.sale.order.orderchange;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.CoreOrderInfoUpdate;
import com.steering.pss.sale.order.model.CrmSlmOrder;

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

public class CrmSlmOrderService {

    //根据合同号，合同行查询
    public List<CrmSlmOrder> getOrderInfo(
            Connection con, String ordPk, String ordLnPk) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CrmSlmOrder.getOrderInfo");
        if (!ordPk.equals("")) {
            sql += " AND H.ORD_PK = '" + ordPk + "'";
        }
        if (!ordLnPk.equals("")) {
            sql += " AND L.ORD_LN_PK = '" + ordLnPk + "'";
        }
        List<CrmSlmOrder> list = new ArrayList<CrmSlmOrder>();
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        list = EntityTool
                .getEntitysByRs(rs, CrmSlmOrder.class);

        rs.close();
        ps.close();
        return list;
    }

    //删除原有交货行 新增交货行
    public void insertDeliveryToCrmOrder(
            Connection con, String ordPk, String ordLnPk) throws SQLException {
//        //删除原表中变更了的数据
//        String sql = CoreXmlSqlParsersFactory.getSql(
//                "CrmSlmOrder.deleteCrmOrder");
//        PreparedStatement ps = con.prepareStatement(sql);
//        ps.setObject(1, ordPk);
//        ps.setObject(2, ordLnPk);
//        ps.executeUpdate();
//        ps.close();
        //新增变更数据
        String sql1 = CoreXmlSqlParsersFactory
                .getSql("CrmSlmOrder.insertDeliveryToCrmOrder");
        PreparedStatement ps = con.prepareStatement(sql1);
        ps.setObject(1, ordPk);
        ps.setObject(2, ordLnPk);
        ps.executeUpdate();
        ps.close();
    }

    // 新增合同行
    public void insertOrdLnToCrmOrder(
            Connection con, String ordPk, String ordLnPk, String dataStatus) throws SQLException {
        //新增变更数据
        String sql1 = CoreXmlSqlParsersFactory
                .getSql("CrmSlmOrder.insertOrdLnToCrmOrder");
        PreparedStatement ps = con.prepareStatement(sql1);
        ps.setObject(2, ordPk);
        ps.setObject(3, ordLnPk);
        ps.setObject(1, dataStatus);
        ps.executeUpdate();
        ps.close();
    }

    //修改合同行
    public void updateOrdLnToCrmOrder(
            Connection con, String ordPk, String ordLnPk, String ordLnPkAfter) throws SQLException {
        //作废原合同行
        //deleteOrdLnToCrmOrder(con, ordPk, ordLnPk, "2");
        //新增 变更后合同行
    	insertOrdLnToCrmOrder(con, ordPk, ordLnPk, "2");
        insertOrdLnToCrmOrder(con, ordPk, ordLnPkAfter, "0");
        new CoreOrderInfoUpdate().doCrmEvent(con, "",ordLnPkAfter,"","0","2");
    }

    //作废合同行
    public void deleteOrdLnToCrmOrder(Connection con, String ordPk, String ordLnPk, String dataStatus) throws SQLException {
        //查询合同信息，crm中进行修改
//        List<CrmSlmOrder> orderInfo = this.getOrderInfo(con, ordPk, ordLnPk);
//        for (CrmSlmOrder o : orderInfo) {
//            o.setDataStatus(dataStatus);
//            String sql = CoreXmlSqlParsersFactory.getSql("CrmSlmOrder.updateOrdLnToCrmOrder");
//            sql = EntityTool.fillSqlByEntity(sql, o);
//            PreparedStatement ps = con.prepareStatement(sql);
//            ps.executeUpdate();
//            ps.close();
//        }
        insertOrdLnToCrmOrder(con, ordPk, ordLnPk, dataStatus);
    }

    //修改合同头
    public void updateOrdToCrmOrder(
            Connection con, String ordPk, String dataStatus) throws SQLException {
        //查询合同信息，crm中进行修改
        List<CrmSlmOrder> orderInfo = this.getOrderInfo(con, ordPk, "");
        for (CrmSlmOrder o : orderInfo) {
            //修改变更数据
            o.setDataStatus(dataStatus);
            String sql = CoreXmlSqlParsersFactory.getSql("CrmSlmOrder.insertOrdToCrmOrder");
            sql = EntityTool.fillSqlByEntity(sql, o);
            PreparedStatement ps = con.prepareStatement(sql);
            ps.executeUpdate();
            ps.close();
        }
    }
}
