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

import CoreFS.SA08.CoreXmlSqlParsersFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CrmBillService {

    public boolean selectCrmBill(
            Connection con, String billNo,String dataStatus) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CrmBill.selectCrmBill");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1,billNo);
        ps.setObject(2,dataStatus);
        ps.executeUpdate();
        boolean b = false;
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            int i = rs.getInt(1);
            if(i > 0){
                b = true;
            }
        }
        ps.close();
        return b;
    }

    // crm是否已结算
    public boolean selectCrmBillIsPay(
            Connection con, String billNo) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CrmBill.selectCrmBillIsPay");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1,billNo);
        ps.executeUpdate();
        boolean b = false;
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            int i = rs.getInt(1);
            if(i > 0){
                b = true;
            }
        }
        ps.close();
        return b;
    }

    ////新增数据CrmBill
    public void insertCrmBill(
            Connection con, String billNo,String dataStatus) throws SQLException {
        Date now = new Date();
        SimpleDateFormat dfT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strNowTime = dfT.format(now);
        String sql = CoreXmlSqlParsersFactory
                .getSql("CrmBill.insertCrmBillM");
        PreparedStatement ps = con.prepareStatement(sql);
//        ps.setObject(1,strNowTime);
        ps.setObject(1,dataStatus);
        ps.setObject(2,dataStatus);
        ps.setObject(3,billNo);
        ps.executeUpdate();
        ps.close();

        String sql2 = CoreXmlSqlParsersFactory
                .getSql("CrmBill.insertCrmBillC");
         ps = con.prepareStatement(sql2);
//        ps.setObject(1,strNowTime);
        ps.setObject(1,dataStatus);
        ps.setObject(2,billNo);
        ps.executeUpdate();
        ps.close();
    }
}
