1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.bestray.transtracking.dao.impl;
import com.bestray.transtracking.dao.CostToVehicleDao;
import com.bestray.transtracking.dao.mapper.CostToVehicleResultSetExtractor;
import com.bestray.transtracking.dao.mapper.VehicleResultSetExtractor;
import com.bestray.transtracking.dto.CostToVehicleSearchDTO;
import com.bestray.trastrack.domain.CostToVehicle;
import com.bestray.trastrack.domain.Vehicle;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
/**
*
* @author User1
*/
public class CostToVehicleDaoImpl implements CostToVehicleDao{
private JdbcTemplate jdbcTemplate;
private static final String VEHICLE_QUERY = "SELECT `v`.`id`,`v`.`vehicle_number`,`v`.`vehicle_name`,`v`.`catagory_id`,`v`.`vehicle_owner`,`v`.`vehicle_driver`,`v`.`hsd_opening`,`v`.`lock`,`v`.`oil_entry_id`,`v`.`last_oil_vol`,`v`.`last_meter_reading`,`v`.`current_meter_reading`,`v`.`last_oil_change_date` FROM `code` c INNER JOIN `Vehicle_master` v ON c.`id` = v.`catagory_id` WHERE v.lock = 'NO' AND c.`name`='Owned Vehicle' ";
private static final String VEHICLE_SEARCH_QUERY = "SELECT `v`.`id`,`v`.`vehicle_number`,`v`.`vehicle_name`,`v`.`catagory_id`,`v`.`vehicle_owner`,`v`.`vehicle_driver`,`v`.`hsd_opening`,`v`.`lock`,`v`.`oil_entry_id`,`v`.`last_oil_vol`,`v`.`last_meter_reading`,`v`.`current_meter_reading`,`v`.`last_oil_change_date` FROM `code` c INNER JOIN `Vehicle_master` v ON c.`id` = v.`catagory_id` WHERE c.`name`='Owned Vehicle' ";
private static final String COST_TO_VEHICLE_QUERY = "SELECT * from t_cost_to_vehicle ";
private static final String ORDER_BY = " order by id desc ";
public CostToVehicleDaoImpl(DataSource dataSourceObject) {
jdbcTemplate = new JdbcTemplate(dataSourceObject);
}
public List<Vehicle> findOwnedVehicleList() {
StringBuffer queryString = new StringBuffer(VEHICLE_QUERY).append("order by v.`vehicle_number` asc");
List<Vehicle> employeeList = (List<Vehicle>) jdbcTemplate.query(queryString.toString(), new VehicleResultSetExtractor());
return employeeList;
}
@Override
public void addCostToVehicle(CostToVehicle costTovehicle) throws Exception {
//JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
String sql1 = "INSERT INTO t_cost_to_vehicle (`cost_to_vehicle_date`,`vehicle_id`,`paid_mode_id`,`cost_amount`,`paid_by_id`,`note`) values(?,?,?,?,?,?)";
jdbcTemplate.update(sql1, costTovehicle.getCost_to_vehicle_date(), costTovehicle.getVehicle_id(),costTovehicle.getPaid_mode_id(),costTovehicle.getCost_amount(),costTovehicle.getPaid_by_id(), costTovehicle.getNote());
}
@Override
public List<CostToVehicle> getSearchResults(final CostToVehicleSearchDTO search) throws Exception {
StringBuffer queryString = new StringBuffer(COST_TO_VEHICLE_QUERY);
queryString.append(" WHERE `vehicle_id`=? ");
if(search.getSearchFrom() != null){
queryString.append(" and cost_to_vehicle_date>=?");
}
if(search.getSearchTo() != null){
queryString.append(" and cost_to_vehicle_date<=?");
}
queryString.append(ORDER_BY);
// JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
//System.out.println(queryString.toString());
return (List<CostToVehicle>) jdbcTemplate.query(queryString.toString(), new PreparedStatementSetter() {
public void setValues(PreparedStatement ps) throws SQLException {
int index =0;
ps.setLong(++index, search.getVehicleId());
if(search.getSearchFrom() != null){
//System.out.println(search.getSearchFrom());
ps.setDate(++index, new java.sql.Date(search.getSearchFrom().getTime()));
}
if(search.getSearchTo() != null){
//System.out.println(search.getSearchTo());
ps.setDate(++index, new java.sql.Date(search.getSearchTo().getTime()));
}
}
} , new CostToVehicleResultSetExtractor());
}
@Override
public CostToVehicle findCostToVehicleEntryRegistraion(Long costovehicleID) throws Exception {
StringBuffer queryString = new StringBuffer(COST_TO_VEHICLE_QUERY)
.append("where id = ? ")
.append(ORDER_BY);
List<CostToVehicle> CostToVehicleList = (List<CostToVehicle>) jdbcTemplate.query(queryString.toString(), new Object[]{costovehicleID}, new CostToVehicleResultSetExtractor());
return CostToVehicleList.size()>0?CostToVehicleList.get(0):null;
}
@Override
public void update(CostToVehicle costTovehicle) throws Exception {
//final java.util.Date date= new java.util.Date();
String sql = "update t_cost_to_vehicle set cost_to_vehicle_date=?, vehicle_id=?, paid_mode_id=?, cost_amount=?, paid_by_id=?, note=? where id = ?";
jdbcTemplate.update(sql, new Object[]{costTovehicle.getCost_to_vehicle_date(),costTovehicle.getVehicle_id(),costTovehicle.getPaid_mode_id(),costTovehicle.getCost_amount(),costTovehicle.getPaid_by_id(),costTovehicle.getNote(),costTovehicle.getId()});
}
@Override
public void delete(Long id) throws Exception {
jdbcTemplate.update("DELETE FROM t_cost_to_vehicle WHERE id = ?", new Object[]{id});
}
@Override
public Double getTotalOfficeExpense() throws Exception {
String sql = "SELECT COALESCE(sum(office_expense.`office_expense_amount`),0) AS totalamount FROM `office_expense` office_expense";
return jdbcTemplate.queryForObject(sql, Double.class);
}
@Override
public Double getTotalCostToVehicles() throws Exception {
String sql = "SELECT COALESCE(sum(t_cost_to_vehicle.`cost_amount`),0) AS total_cost_amount FROM `t_cost_to_vehicle` t_cost_to_vehicle";
return jdbcTemplate.queryForObject(sql, Double.class);
}
@Override
public List<Vehicle> findOwnedVehicleSearchList() throws Exception {
StringBuffer queryString = new StringBuffer(VEHICLE_SEARCH_QUERY).append("order by v.`vehicle_number` asc");
List<Vehicle> employeeList = (List<Vehicle>) jdbcTemplate.query(queryString.toString(), new VehicleResultSetExtractor());
return employeeList;
}
}
|