package com.huimv.receive.timer;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.receive.entity.BillGate;
import com.huimv.receive.mapper.BillGateMapper;
import com.huimv.receive.timer.test.TestWeight;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.sql.*;
import java.util.List;


@Configuration
@EnableScheduling
public class DiBangimer {

    @Autowired
    private BillGateMapper billGateMapper;

    //多利托地磅
    @Scheduled(cron = "0 0 * * * ? ")
    private void getShenChan() throws Exception {
        String url = "jdbc:jtds:sqlserver://218.76.168.4:1433/CMTWeight"; // 使用 jTDS 连接字符串
        String user = "sa";
        String password = "jh@123";
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        try (Connection conn = DriverManager.getConnection(url, user, password)) {
            // 创建 Statement
            Statement stmt = conn.createStatement();
            // 创建 PreparedStatement（推荐）
            String sql = "select * from T_Standard where  F_BeginTime > '2025-04-18 00:00:00'";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            // 执行查询...
            ResultSet resultSet = pstmt.executeQuery();

            while (resultSet.next()) {
                BillGate billGate = billGateMapper.selectOne(new QueryWrapper<BillGate>().eq("stdno", resultSet.getString("F_StdNo")));
                if (ObjectUtil.isEmpty(billGate)){
                    List<BillGate> billGate1 = billGateMapper.selectList(new QueryWrapper<BillGate>().eq("car_num", resultSet.getString("F_CarNo"))
                            .between("check_date", resultSet.getString("F_BeginTime"), resultSet.getString("F_EndTime")));

                    if (ObjectUtil.isNotEmpty(billGate1)){
                        billGate = billGate1.get(0);
                        billGate.setGross(resultSet.getDouble("F_Gross")+"");
                        billGate.setTare(resultSet.getDouble("F_Tare")+"");
                        billGate.setNet(resultSet.getDouble("F_Net")+"");
                        billGate.setStdno(resultSet.getString("F_StdNo"));
                        billGateMapper.updateById(billGate);
                    }
                }



//                TestWeight testWeight = new TestWeight(resultSet.getString("F_StdNo")
//                        ,resultSet.getString("F_CarNo")
//                        ,resultSet.getString("F_ProName")
//                        ,resultSet.getString("F_Model")
//                        ,resultSet.getString("F_unitFrom")
//                        ,resultSet.getString("F_Load")
//                        ,resultSet.getString("F_UnitTo")
//                        ,resultSet.getString("F_UnLoad")
//                        ,resultSet.getString("F_Transport")
//                        ,resultSet.getString("F_Driver")
//                        ,resultSet.getString("F_Memo")
//                        ,resultSet.getString("F_Type")
//                        ,resultSet.getDouble("F_Number")
//                        ,resultSet.getDouble("F_Gross")
//                        ,resultSet.getDouble("F_Tare")
//                        ,resultSet.getDouble("F_Net")
//                        ,resultSet.getDouble("F_Deduct")
//                        ,resultSet.getDouble("F_Fact")
//                        ,resultSet.getDouble("F_Deduct2")
//                        ,resultSet.getDouble("F_Fact2")
//                        ,resultSet.getDouble("F_Price")
//                        ,resultSet.getDouble("F_Money")
//                        ,resultSet.getString("F_BeginTime")
//                        ,resultSet.getString("F_EndTime")
//
//                );
//                System.out.println(resultSet.getDate("F_BeginTime"));
            }

        } catch (Exception  e) {
            e.printStackTrace();
        }
    }

}
