如何将输入值item_code1,item_code2等与另一个表列item_code比较,然后还基于item_code将库存与Quantity_reqd1,Quantity_reqd2()等进行比较。如果compare为true,则生成该项目的report1,否则生成report2。
@Controller
@RequestMapping(“需求”) 公共类RequirementController {
private static final Logger logger = LoggerFactory.getLogger(RequirementEntity.class);
@Autowired
private RequirementService reqService;
@Autowired
private Master_service master_service;
@Autowired
private SessionFactory sessionFactory;
@Autowired
private Stock_issuance_service stock_service1;
@Autowired
private Purchase_Order_Service purchase_service1;
@RequestMapping(value = "/sheet", method = RequestMethod.GET)
public ModelAndView getRequirementJsp() {
System.out.println("Controller is starting..");
ModelAndView mav = new ModelAndView("requirement_sheet");
mav.addObject("item", new Item_entity());
mav.addObject("itemList", this.master_service.getItemList());
mav.addObject("department", new department_entity());
mav.addObject("departmentList", this.master_service.getDepartmentList());
stock_register_dto dto = new stock_register_dto();
mav.addObject("stock_register", dto);
return mav;
}
@RequestMapping(value = "/sheet", method = RequestMethod.POST)
public ModelAndView saveRequirement(@ModelAttribute("requirement") RequirementEntity req, RedirectAttributes rd,
stock_register_dto dto, Purchase_Order_Entity order, ModelAndView modelAndView, ModelAndView modelAndView1,
Stock_issuance_toDepartment stockIssuance, Session session, Integer item_code, Integer stock) {
System.out.println("MAV is starting..");
ModelAndView mav = new ModelAndView("requirement_sheet");
mav.addObject("item", new Item_entity());
mav.addObject("itemList", this.master_service.getItemList());
mav.addObject("department", new department_entity());
mav.addObject("departmentList", this.master_service.getDepartmentList());
System.out.println("service is not starting..");
this.reqService.saveRequirement(req);
rd.addFlashAttribute("msg", "Requirement details added Successfully.");
List<stock_register_dto> dataList = master_service.getStockRegisterList(dto);
Map<String, Object> parameterMap = new HashMap<String, Object>();
String hql = "FROM Stock_issuance_toDepartment where serial_no = (SELECT max(serial_no) FROM Stock_issuance_toDepartment) ORDER BY serial_no DESC";
Session session1 = sessionFactory.openSession();
Query query = session1.createQuery(hql);
List list1 = query.list();
try {
String query1 = "SELECT item_code, stock FROM stock_requirement_register WHERE item_code=?, and stock=? ";
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/bjspring", "postgres",
"postgres");
PreparedStatement ps = con.prepareStatement(query1);
ps.setInt(1, item_code);
ps.setInt(2, stock);
ResultSet out = ps.executeQuery();
while (out.next()) {
Integer code = out.getInt("item_code");
Integer check = out.getInt("stock");
if (req.getItem_code1().equals(code) && req.getQuantity_reqd1().equals(check)) {
JRDataSource JRdataSource1 = new JRBeanCollectionDataSource(list1);
parameterMap.put("datasource", JRdataSource1);
modelAndView = new ModelAndView("report3", parameterMap);
return modelAndView;
} else {
return new ModelAndView("redirect:requirement/genReport");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mav;
}
@RequestMapping(method = RequestMethod.GET, value = "/genReport")
public ModelAndView generateReport(stock_register_dto dto, RequirementEntity req, Purchase_Order_Entity order,
Stock_issuance_toDepartment stock, ModelAndView modelAndView) {
Map<String, Object> parameterMap = new HashMap<String, Object>();
/*
* List<Stock_issuance_toDepartment> list1 = stock_service1.list(stock);
* List<Purchase_Order_Entity> list2 = purchase_service1.list(order);
*/
/*String hql = "FROM Stock_issuance_toDepartment where serial_no = (SELECT max(serial_no) FROM Stock_issuance_toDepartment) ORDER BY serial_no DESC";
Session session = sessionFactory.openSession();
Query query = session.createQuery(hql);
List list1 = query.list();*/
String hql1 = "FROM Purchase_Order_Entity where serial_no = (SELECT max(serial_no) FROM Purchase_Order_Entity) ORDER BY serial_no DESC";
Session session1 = sessionFactory.openSession();
Query query1 = session1.createQuery(hql1);
List list2 = query1.list();
if (list2 != null) {
JRDataSource JRdataSource2 = new JRBeanCollectionDataSource(list2);
parameterMap.put("datasource", JRdataSource2);
modelAndView = new ModelAndView("report2", parameterMap);
return modelAndView;
}
return modelAndView;
}
}
实体:
public class RequirementEntity {
@Id
@Column(name = "serial_no")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer serial_no;
@Column(name = "stock_requirement_slip_no")
private String stock_requirement_slip_no;
@Column(name = "stock_requirement_slip_date")
private Date stock_requirement_slip_date;
@Column(name = "department_code")
private String department_code;
@Column(name = "item_code1")
private Integer item_code1;
@Column(name = "quantity_reqd1")
private Integer quantity_reqd1;
@Column(name = "item_code2")
private Integer item_code2;
@Column(name = "quantity_reqd2")
private Integer quantity_reqd2;
@Column(name = "item_code3")
private Integer item_code3;
@Column(name = "quantity_reqd3")
private Integer quantity_reqd3;
@Column(name = "item_code4")
private Integer item_code4;
@Column(name = "quantity_reqd4")
private Integer quantity_reqd4;
@Column(name = "item_code5")
private Integer item_code5;
@Column(name = "quantity_reqd5")
private Integer quantity_reqd5;
@Column(name = "work_order_no")
private String work_order_no;
@Column(name = "buyer")
private String buyer;
@Column(name = "item_name1")
private String item_name1;
@Column(name = "item_name2")
private String item_name2;
@Column(name = "item_name3")
private String item_name3;
@Column(name = "item_name4")
private String item_name4;
@Column(name = "item_name5")
private String item_name5;
public Integer getSerial_no() {
return serial_no;
}
public void setSerial_no(Integer serial_no) {
this.serial_no = serial_no;
}
public String getStock_requirement_slip_no() {
return stock_requirement_slip_no;
}
public void setStock_requirement_slip_no(String stock_requirement_slip_no) {
this.stock_requirement_slip_no = stock_requirement_slip_no;
}
public Date getStock_requirement_slip_date() {
return stock_requirement_slip_date;
}
public void setStock_requirement_slip_date(Date stock_requirement_slip_date) {
this.stock_requirement_slip_date = stock_requirement_slip_date;
}
public Integer getItem_code1() {
return item_code1;
}
public void setItem_code1(Integer item_code1) {
this.item_code1 = item_code1;
}
public Integer getQuantity_reqd1() {
return quantity_reqd1;
}
public void setQuantity_reqd1(Integer quantity_reqd1) {
this.quantity_reqd1 = quantity_reqd1;
}
public Integer getItem_code2() {
return item_code2;
}
public void setItem_code2(Integer item_code2) {
this.item_code2 = item_code2;
}
public Integer getQuantity_reqd2() {
return quantity_reqd2;
}
public void setQuantity_reqd2(Integer quantity_reqd2) {
this.quantity_reqd2 = quantity_reqd2;
}
public Integer getItem_code3() {
return item_code3;
}
public void setItem_code3(Integer item_code3) {
this.item_code3 = item_code3;
}
public Integer getQuantity_reqd3() {
return quantity_reqd3;
}
public void setQuantity_reqd3(Integer quantity_reqd3) {
this.quantity_reqd3 = quantity_reqd3;
}
public Integer getItem_code4() {
return item_code4;
}
public void setItem_code4(Integer item_code4) {
this.item_code4 = item_code4;
}
public Integer getQuantity_reqd4() {
return quantity_reqd4;
}
public void setQuantity_reqd4(Integer quantity_reqd4) {
this.quantity_reqd4 = quantity_reqd4;
}
public Integer getItem_code5() {
return item_code5;
}
public void setItem_code5(Integer item_code5) {
this.item_code5 = item_code5;
}
public Integer getQuantity_reqd5() {
return quantity_reqd5;
}
public void setQuantity_reqd5(Integer quantity_reqd5) {
this.quantity_reqd5 = quantity_reqd5;
}
public String getWork_order_no() {
return work_order_no;
}
public void setWork_order_no(String work_order_no) {
this.work_order_no = work_order_no;
}
public String getBuyer() {
return buyer;
}
public void setBuyer(String buyer) {
this.buyer = buyer;
}
public String getItem_name1() {
return item_name1;
}
public void setItem_name1(String item_name1) {
this.item_name1 = item_name1;
}
public String getItem_name2() {
return item_name2;
}
public void setItem_name2(String item_name2) {
this.item_name2 = item_name2;
}
public String getItem_name3() {
return item_name3;
}
public void setItem_name3(String item_name3) {
this.item_name3 = item_name3;
}
public String getItem_name4() {
return item_name4;
}
public void setItem_name4(String item_name4) {
this.item_name4 = item_name4;
}
public String getItem_name5() {
return item_name5;
}
public void setItem_name5(String item_name5) {
this.item_name5 = item_name5;
}
public String getDepartment_code() {
return department_code;
}
public void setDepartment_code(String department_code) {
this.department_code = department_code;
}
@Override
public String toString() {
return "RequirementEntity [serial_no=" + serial_no + ", stock_requirement_slip_no=" + stock_requirement_slip_no
+ ", stock_requirement_slip_date=" + stock_requirement_slip_date + ", department_code="
+ department_code + ", item_code1=" + item_code1 + ", quantity_reqd1=" + quantity_reqd1
+ ", item_code2=" + item_code2 + ", quantity_reqd2=" + quantity_reqd2 + ", item_code3=" + item_code3
+ ", quantity_reqd3=" + quantity_reqd3 + ", item_code4=" + item_code4 + ", quantity_reqd4="
+ quantity_reqd4 + ", item_code5=" + item_code5 + ", quantity_reqd5=" + quantity_reqd5
+ ", work_order_no=" + work_order_no + ", buyer=" + buyer + ", item_name1=" + item_name1
+ ", item_name2=" + item_name2 + ", item_name3=" + item_name3 + ", item_name4=" + item_name4
+ ", item_name5=" + item_name5 + "]";
}
}
和第二个实体:
public class stock_register_dto {
@Id
@Column(name = "serial_no")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer serial_no;
@Column(name = "date")
private Date date;
@Column(name = "sisr_no")
private String sisr_no;
@Column(name = "invoice_no")
private String invoice_no;
@Column(name = "vendor_code")
private String vendor_code;
@Column(name = "department_code")
private String department_code;
@Column(name = "item_code")
private Integer item_code;
@Column(name = "hsn_code")
private Integer hsn_code;
@Column(name = "purchase_price")
private Integer purchase_price;
@Column(name = "quantity_recd")
private Integer quantity_recd;
@Column(name = "quantity_issued")
private Integer quantity_issued;
@Column(name = "stock")
private Integer stock;
public Integer getSerial_no() {
return serial_no;
}
public void setSerial_no(Integer serial_no) {
this.serial_no = serial_no;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getSisr_no() {
return sisr_no;
}
public void setSisr_no(String sisr_no) {
this.sisr_no = sisr_no;
}
public String getInvoice_no() {
return invoice_no;
}
public void setInvoice_no(String invoice_no) {
this.invoice_no = invoice_no;
}
public String getVendor_code() {
return vendor_code;
}
public void setVendor_code(String vendor_code) {
this.vendor_code = vendor_code;
}
public String getDepartment_code() {
return department_code;
}
public void setDepartment_code(String department_code) {
this.department_code = department_code;
}
public Integer getItem_code() {
return item_code;
}
public void setItem_code(Integer item_code) {
this.item_code = item_code;
}
public Integer getHsn_code() {
return hsn_code;
}
public void setHsn_code(Integer hsn_code) {
this.hsn_code = hsn_code;
}
public Integer getPurchase_price() {
return purchase_price;
}
public void setPurchase_price(Integer purchase_price) {
this.purchase_price = purchase_price;
}
public Integer getQuantity_recd() {
return quantity_recd;
}
public void setQuantity_recd(Integer quantity_recd) {
this.quantity_recd = quantity_recd;
}
public Integer getQuantity_issued() {
return quantity_issued;
}
public void setQuantity_issued(Integer quantity_issued) {
this.quantity_issued = quantity_issued;
}
public Integer getStock() {
return stock;
}
public void setStock(Integer stock) {
this.stock = stock;
}
@Override
public String toString() {
return "stock_register_dto [serial_no=" + serial_no + ", date=" + date + ", sisr_no=" + sisr_no
+ ", invoice_no=" + invoice_no + ", vendor_code=" + vendor_code + ", department_code=" + department_code
+ ", item_code=" + item_code + ", hsn_code=" + hsn_code + ", purchase_price=" + purchase_price
+ ", quantity_recd=" + quantity_recd + ", quantity_issued=" + quantity_issued + ", stock=" + stock
+ "]";
}
请帮助我。
答案 0 :(得分:1)
如果我理解您的问题并正确给出了代码,那么下面是解决方法:
在您使用saveRequirement()控制器方法编写的以下查询中,我只需要对此解释:
SELECT item_code, stock FROM stock_requirement_register WHERE item_code=?, and
stock=?
如果上述查询只是检查是否存在带有此类item_code和stock的条目,则可以,但是您可以应用count查询,而不是获取结果集并对其进行迭代。与处理局部变量本身相比,这可能会花费很大。
解决方案:
假设您正在从saveRequirement()方法中的表单中正确存储实体类中的数据
您可以在RequirementEntity类中编写一个方法,该方法返回Map集合(如下所示)
public Map<Integer, Integer> getItemCodeList(){
Map<Integer> response = new LinkedHashMap<Integer>();
response.put(this.item_code1,this.quantity_reqd1);
response.put(this.item_code2,this.quantity_reqd2);
response.put(this.item_code3,this.quantity_reqd3);
response.put(this.item_code4,this.quantity_reqd4);
response.put(this.item_code5,this.quantity_reqd5);
return response;
}
因此,当您将表单数据保存在RequirmentEntity类中时,可以调用此方法以获取Map集合对象,并使用此表单的键集进行迭代,以比较从表单接收的每个item_code与数据库中的数据。
//The place where you have called this below method
@RequestMapping(value = "/sheet", method = RequestMethod.POST)
public ModelAndView saveRequirement
//You can get data in a Integer List as below
boolean report = true;
Map<Integer,Integer> itemCodeList = req.getItemCodeList();
Set<Integer> keys = itemCodeList.keySet()
for(Integer key : keys ){
if(!(report = compare(itemCodeList, item)))
break;
}
if(report)
//code for generate report here... or redirection code as per your logic
上面的方法compare()可以在服务类中的任何位置,您可以在此处相应地调用它。您可以编写代码以比较其中的项目。您可以同时编写与所需的物料代码和数量相匹配的查询,然后从那里本身调用generate report方法。
但是,从您的代码看来(如果我错了,请纠正我),您想为每个项目生成报告,并且您正在为不可能符合条件的每个项目返回MV。您可以将报告1要考虑的项目列表和报告2要考虑的项目列表分开。然后进行相应的操作。