采购订单的模型分析
1.1. 从页面怎样判断数据库的关系
如果是下拉列表:一般是多对一,一对一
如果是复选框:一般是多对多,一对多
1.1. 组合关系
页面处理方式
在一个页面同时操作2张表采购订单和采购订单明细
组合关系映射配置要求
整体和部分,整体和部分不能分割,本质还是双向一对多
一方(主表):
@OneToMany(cascade
= CascadeType.ALL, mappedBy = "bill", fetch = FetchType.LAZY,
orphanRemoval = true)
private
List<purchasebillitem> items = new ArrayList<purchasebillitem>();
cascade = CascadeType.ALL级联操作最全
mappedBy = "bill"一方放弃管理多方,多方的外键字段bill_id,一方不管
orphanRemoval = true如果在一方解除了和多方的关系,一方是可以删除掉多方
1.
多方(从表)billitem:bill_id配置为非空
@ManyToOne(fetch
= FetchType.LAZY, optional = false)
@JoinColumn(name
= "bill_id")
private
Purchasebill bill;// 组合关系,非空
1.1.1.
关系数据库
CONSTRAINT `FK9BD788C83FF7A83F`
FOREIGN KEY (`auditor_id`) REFERENCES `employee` (`id`),
CONSTRAINT `FK9BD788C89FE0CD6A`
FOREIGN KEY (`buyer_id`) REFERENCES `employee` (`id`),
CONSTRAINT `FK9BD788C8A902BD48`
FOREIGN KEY (`inputUser_id`) REFERENCES `employee` (`id`)
1.1. 采购订单其他属性:
`vdate` datetime 交易时间,建议可以录入-15天,并且把-15保存到数据库config表里面
`totalAmount` decimal(19,2) 总金额
`totalNum` decimal(19,2) 总数量,一般数量都是设计为int,但是现在项目属于产品,设计产品考虑通用性
`inputTime` datetime 录入时间
`auditorTime` datetime 审核时间
`status` int(11) 状态,非常重要字段,根据状态做一些业务操作,0待审,1已审,-1作废
1.1. 采购订单明细其他属性:
`price` decimal(19,2) 采购价格
`num` decimal(19,2) 采购数量
`amount` decimal(19,2) 采购小计
`descs` varchar(255) 备注
1.1. 那些属性不能为null
1.1.1.
采购订单
录入员,采购员,供应商外键值
`supplier_id` bigint(20) NOT NULL,
`inputUser_id` bigint(20) NOT
NULL,
`buyer_id` bigint(20) NOT NULL,
1.1.2.
采购订单明细
产品,订单外键值
`product_id` bigint(20) NOT NULL,
`bill_id` bigint(20) NOT NULL,
2. 组合关系-采购订单模型完整代码
2.1. Purchasebill组合关系的一方
/**
*
* 采购订单:组合关系的一方
*
*/
@Entity
@Table(name
= "purchasebill")
public
class Purchasebill extends BaseDomain
{
private Date vdate;// 交易时间
-> 需要录入(时间set的时候加上@DateTimeFormat[Office1] (pattern = "yyyy-MM-dd"))
private BigDecimal totalAmount; //总金额 -> 明细计算
private BigDecimal totalNum; //总数量 -> 明细计算
private Date inputTime = new Date(); //录入时间 ->当前系统时间
private
Date auditorTime; //审核时间 -> 可以为空,审核时自己生成
/**
*
0待审,1已审,-1作废
*/
private Integer status = 0; //单据状态 -> 默认待审
@ManyToOne(fetch = FetchType.LAZY, optional =
false)
@JoinColumn(name = "supplier_id")
private Supplier supplier;// 多对一,非空 供应商(需要选择)
@ManyToOne(fetch
= FetchType.LAZY)
@JoinColumn(name = "auditor_id")
private Employee auditor;// 多对一,可以为空
@ManyToOne(fetch = FetchType.LAZY, optional =
false)
@JoinColumn(name = "inputUser_id")
private Employee inputUser;// 多对一,非空 录入人 -> 登录用户就是录入人
@ManyToOne(fetch = FetchType.LAZY, optional =
false)
@JoinColumn(name = "buyer_id")
private Employee buyer;// 多对一,非空 采购员
-> 需要
// 一般组合关系使用List
@OneToMany(cascade = CascadeType.ALL, mappedBy = "bill", fetch = FetchType.LAZY, orphanRemoval = true)
private List<Purchasebillitem> items =
new ArrayList<Purchasebillitem>();
…
@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;
}[Office2]
SpringMVC接收时间需要加上相应注解,不要忘记了
注意日期的处理
1.1. purchasebillitem组合关系的多方
/**
*
* 采购订单明细:组合关系的多方
*
*/
@Entity
@Table(name
= "purchasebillitem")
public
class purchasebillitem extends BaseDomain {
private BigDecimal price; //价格
private BigDecimal num; //数量
private BigDecimal amount; //小计 = 价格*数量
private String descs; //描述
@ManyToOne(fetch = FetchType.LAZY, optional =
false)
@JoinColumn(name = "product_id")
private Product product;// 多对一,非空 产品
@ManyToOne(fetch = FetchType.LAZY, optional =
false)
@JoinColumn(name = "bill_id")
@JsonIgnore //生成json的时候忽略这个属性[Office1]
private Purchasebill bill;// 组合关系,非空
双向关系,生成json的时候是相互调用,需要有一方忽略对方
1.1.1.
purchasebill.jsp展示
<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>
1.1.1.
purchasebill.js
function formatObj(data) {
if(data){
return data.name;
}
}
function formatEmp(data) {
if(data){
return data.username;
}
}
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.1.1.
purchasebill.jsp
<!--
这部门是查询的功能 -->
<div id="cc"
class="easyui-calendar"></div>
<form id="searchForm" action="/purchasebill/download"
method="post">
日期 :
<input name="beginDate" class="easyui-datebox" style="height:32px" sharedCalendar="#cc">
- <input
name="endDate" class="easyui-datebox" style="height:32px" sharedCalendar="#cc">
状态 :<select class="easyui-combobox" name="status"
data-options="panelHeight:'auto'"
>
<option
value="">--请选择--</option>
<option value="0">待审</option>
<option value="-1">作废</option>
<option value="1">已审</option>
</select>
<a href="#"
data-method="search" class="easyui-linkbutton"
iconCls="icon-search">查找</a>
</form>
1.1.1.
purchasebillQuery.java
package cn.itsource.pss.query;
import cn.itsource.pss.domain.Purchasebill;
import com.github.wenhao.jpa.Specifications;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
public class PurchasebillQuery extends BaseQuery {
//SpringMVC接收日期的格式设置(建议写在setter上)
@DateTimeFormat(pattern = "yyyy-MM-dd")
[Office1] private Date beginDate;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date endDate;
private Integer status;
@Override
public Specification createSpecification(){
//根据条件把数据返回即可
Specification<Purchasebill>
spec = Specifications.<Purchasebill>and()
.eq(status!=null,"status",status )//等于
.ge(beginDate!=null, "vdate",beginDate) //大于等于
.le(endDate!=null, "vdate",endDate) //小于等于
.build();
return spec;
}
//getter,setter略…
}
SpringMVC接收日期需要加入相应的注解支持
1.1. PurchasebillQuery .java结束时间查询处理
1.1.1.
最好的设计
应该把vdate设计为date类型(yyyy-MM-dd),而不是datetime时间戳
1.1.2.
为什么现在要设计datetime
面试要知道怎样对日期进行加减
1.1.3.
不能查询出数据的原因
前端的日期时分秒按照0:00:00来处理,如果不对日期+1处理,结束时间是不能成功获取查询的值.
如下单时间是09-30 15:00:00,而endDate输入09-30,后台获取到09-30 0:00:00
加了一天之后进行查询,不能写<=,只能写<
1.1.4.
方案1:直接计算
if(endDate != null) {
Date date = new Date(endDate.getTime()
+ 24
* 60 * 60 * 1000);
System.out.println("date:" + date.toLocaleString());
}