1.课程介绍
1. 采购订单模型设计(了解)
2. 建立采购订单模型(掌握)
3. 对采购订单进行CRUD(掌握)
明细数据的操作(难点)
一,采购单模型设计
判断数据库联系
如果是下拉列表:一般是多对一,一对一
如果是复选框:一般是多对多,一对多
采购单组合关系
主表
@Entity
@Table(name="purchasebill")
public class Purchasebill extends BaseDomain {
private Date vdate; // 交易时间 前台传过来
private BigDecimal totalAmount; //订单总金额 --后台计算出来
private BigDecimal totalNum; //订单数量 --后台计算出来
private Date inputtime = new Date();//采购单录入时间 -- 后台生成
private Date auditortime; //审核时间 --- 后台生成
private Integer status = 0; //单据状态 0 表示待审核 1 表示审核 -1表作废
//private Long supplierId;
@ManyToOne(fetch = FetchType.LAZY,optional = false)
@JoinColumn(name="supplier_id")
private Supplier supplier;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="auditor_id")
private Employee auditor;//审核人 录入可以为null
@ManyToOne(fetch = FetchType.LAZY,optional = false)
@JoinColumn(name="inputUser_id")
private Employee inputUser;//录入人 不能为null 当前登陆用户
@ManyToOne(fetch = FetchType.LAZY,optional = false)
@JoinColumn(name="buyer_id")
private Employee buyer;//采购员
//明細 强级联 orphanRemoval 一方解除关系 去删除
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,mappedBy = "bill",
orphanRemoval = true)
private List<Purchasebillitem> items = new ArrayList<>();
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
public Date getVdate() {
return vdate;
}
@DateTimeFormat(pattern = "yyyy-MM-dd")
public void setVdate(Date vdate) {
this.vdate = vdate;
}
public BigDecimal getTotalAmount() {
return totalAmount;
}
public void setTotalAmount(BigDecimal totalAmount) {
this.totalAmount = totalAmount;
}
public BigDecimal getTotalNum() {
return totalNum;
}
public void setTotalNum(BigDecimal totalNum) {
this.totalNum = totalNum;
}
public Date getInputtime() {
return inputtime;
}
public void setInputtime(Date inputtime) {
this.inputtime = inputtime;
}
public Date getAuditortime() {
return auditortime;
}
public void setAuditortime(Date auditortime) {
this.auditortime = auditortime;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Supplier getSupplier() {
return supplier;
}
public void setSupplier(Supplier supplier) {
this.supplier = supplier;
}
public Employee getAuditor() {
return auditor;
}
public void setAuditor(Employee auditor) {
this.auditor = auditor;
}
public Employee getInputUser() {
return inputUser;
}
public void setInputUser(Employee inputUser) {
this.inputUser = inputUser;
}
public Employee getBuyer() {
return buyer;
}
public void setBuyer(Employee buyer) {
this.buyer = buyer;
}
public List<Purchasebillitem> getItems() {
return items;
}
public void setItems(List<Purchasebillitem> items) {
this.items = items;
}
}
采购明细表
@Entity
@Table(name="purchasebillitem")
public class Purchasebillitem extends BaseDomain {
private BigDecimal price;//产品价格
private BigDecimal num;//产品数量
private BigDecimal amount;//产品小计
private String descs; //产品描述
// private Long productId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="product_id")
private Product product;
//订单 bill_id
@ManyToOne(fetch = FetchType.LAZY,optional = false)
@JoinColumn(name="bill_id")
@JsonIgnore //返回页面 不展示出来
private Purchasebill bill;
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public BigDecimal getNum() {
return num;
}
public void setNum(BigDecimal num) {
this.num = num;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public String getDescs() {
return descs;
}
public void setDescs(String descs) {
this.descs = descs;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Purchasebill getBill() {
return bill;
}
public void setBill(Purchasebill bill) {
this.bill = bill;
}
}
订单明细表展示页面
<table id="purchasebillGrid" class="easyui-datagrid" data-options="fit:true,fixed:true,fitColumns:true,toolbar:'#tb',singleSelect:true";
url="/purchasebill/page"
iconCls="icon-save"
rownumbers="true" pagination="true">
<thead>
<tr>
<th width="20" field="vdate">交易时间</th>
<th width="20" field="supplier" data-options="formatter:formatObj">供应商</th>
<th width="20" field="buyer" data-options="formatter:formatEmp">采购员</th>
<th width="20" field="totalNum">总数量</th>
<th width="20" field="totalAmount" >总金额</th>
<th width="20" field="status" data-options="formatter:formatStatus" >状态</th>
</tr>
</thead>
</table>
处理明细单审核状态
function formatStatus(action) {
var data = {
0:"<div style='color:red;'>待审</div>",
1:"<div style='color: green'>已审</div>",
"-1":"<div><s>作废</s></div>"
};
return data[action];
}
高级查询,
因为只查询到集体的天数,所以只能查到一条数据,
因此我们把时间往后加一天处理,注意1.结束时间是不能成功获取查询的值.
如果是月底,时间会自动添加到下一个月
@Override
public Specification createSpecification() {
Date tempDate = null;
if(this.endDate!=null){
tempDate = DateUtils.addDays(this.endDate,1 );
System.out.println(tempDate);
}
Specification<Purchasebill> spe = Specifications.<Purchasebill>and().
ge(this.beginDate != null, "vdate",this.beginDate ).
lt(this.endDate!=null,"vdate",tempDate).
eq(this.status!=null && !"".equals(this.status),"status",this.status)
.build();
return spe;
}
弹出采购对话框
加载采购员 和 供应商
<tr>
<td>供应商:</td>
<td><input class="easyui-combobox" type="text" name="supplier.id"
data-options="panelHeight:'auto',valueField:'id',textField:'name',url:'/util/findAllSupplier'"
></input></td>
</tr>
<tr>
<td>采购员:</td>
<td><input class="easyui-combobox" type="text" name="buyer.id" data-options="valueField:'id',textField:'username',url:'/util/findAllBuyer'"></input></td>
</tr>
二,采购单明细表格处理(难点)
表单样式可以从网上下载,也可以应课件准备好的
<div id="purchasebillDialog" class="easyui-dialog" data-options="closed:true,modal:true" title="功能操作" style="width:800px">
<div style="padding:10px 60px 20px 40px">
<form id="purchasebillForm" class="easyui-form" method="post" data-options="">
<input type="hidden" id="purchasebillId" name="id" >
<table cellpadding="5">
<tr>
<td>交易时间:</td>
<td><input class="easyui-datebox"name="vdate" data-options="required:true"></input></td>
</tr>
<tr>
<td>供应商:</td>
<td>
<input class="easyui-combobox" name="supplier.id"
data-options="valueField:'id',textField:'name',panelHeight:'auto',url:'/util/findAllSupplier'">
</td>
</tr>
<tr>
<td>采购员:</td>
<td>
<input class="easyui-combobox" name="buyer.id"
data-options="valueField:'id',textField:'username',url:'/util/getBuyer'">
</td>
</tr>
</table>
</form>
<div style="text-align:center;padding:5px">
<a href="javascript:void(0)" class="easyui-linkbutton" data-method="save">提交</a>
<a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#purchasebillDialog').dialog('close')">取消</a>
</div>
</div>
</div>
明细单保存
准备额外参数
//保存方法 --提交表单的数据到后台
purchasebillForm.form('submit', {
url:url,
onSubmit: function(param){
//提交 封装 items
//得到明细表格所有的数据 price num descs
var rows = $("#gridItem").datagrid('getRows');
for(var i=0;i<rows.length;i++){
var rowData = rows[i];
param["items["+i+"].descs"] = rowData.descs;
param["items["+i+"].price"] = rowData.price;
param["items["+i+"].num"] = rowData.num;
param["items["+i+"].product.id"] = rowData.productId.id;
}
// 提交之前的验证
return purchasebillForm.form('validate');
解决修改回显问题
准备一个临时副本存放数据,
用的时候才拿出来,这样就不会出现明明没有删除操作,数据却显示不出来的问题
edit:function(){
//选择一条数据进行修改
var row = purchasebillGrid.datagrid('getSelected');
if(row){
//弹出对话框
purchasebillDialog.dialog('center').dialog('open');
//供货商
if(row.supplier){
row["supplier.id"] = row.supplier.id;
}
//供货商
if(row.buyer){
row["buyer.id"] = row.buyer.id;
}
for(var i=0;i<row.items.length;i++){
var item = row.items[i];
item["productId"] = item.product;
}
var items = $.extend([], row.items);
//明细表格回显
$("#gridItem").datagrid('loadData',items);
//修改 -- 回显示数据
purchasebillForm.form('load',row);
}else{
//提示用户
$.messager.alert('温馨提示:','请选中一条数据进行修改','info');
return;
}
最后解决n to n问题
设置关联对象为null,在每次用之前清理一次
就不会出现n-to-n问题了
purchasebill.setBuyer(null);
purchasebill.setSupplier(null);
purchasebill.getItems().clear();