首先,明白In模块有什么功能,功能是添加进货和查询,并且每条进货对应本次进货的进货明细 (一次进货可能进多本书,所以会有多条明细)
具体图片如下
先写值对象 两个表 所以两个值对象,与前面的差不多 所以只贴实现类了
package cn.hncu.bookStore.in.dao.impl;
import java.util.ArrayList;
import java.util.List;
import cn.hncu.bookStore.in.dao.dao.InMainDAO;
import cn.hncu.bookStore.in.vo.InMainModel;
import cn.hncu.bookStore.in.vo.InMainQueryModel;
import cn.hncu.bookStore.utils.FileIoUtil;
public class InMainDaoImpl implements InMainDAO{
private final String FILE_NAME="InMain.txt";
@Override
public boolean create(InMainModel inMain) {
//先读取所有,遍历,若存在则返回false, 否则把inMain存储并返回true
List list = getAll();
for(InMainModel model:list){
if(model.equals(inMain)){//Model内部其实就是根据uuid是否一致来判断相等的
return false;
}
}
//不存在
list.add(inMain);
FileIoUtil.write2File(list, FILE_NAME);
return true;
}
@Override
public boolean delete(String uuid) {
//先读取所有,遍历,若存在则删除并返回true, 否则返回false
List list = getAll();
for(InMainModel model:list){
if(model.getUuid().equals(uuid)){
list.remove(model);
FileIoUtil.write2File(list, FILE_NAME);
return true;
}
}
//不存在
return false;
}
@Override
public boolean update(InMainModel inMain) {
//先读取所有,遍历,若存在则替换并返回true, 否则返回false
List list = getAll();
for(int i=0;iif(list.get(i).equals(inMain)){//InMainModel内部其实就是根据uuid是否一致来判断相等的
list.set(i, inMain);
FileIoUtil.write2File(list, FILE_NAME);
return true;
}
}
//不存在
return false;
}
@Override
public InMainModel getSingle(String uuid) {
List list = getAll();
for(InMainModel model:list){
if(model.getUuid().equals(uuid)){
return model;
}
}
//不存在
return null;
}
@Override
public List getAll() {
return FileIoUtil.readFromFile(FILE_NAME);
}
@Override
public List getByCondition(InMainQueryModel imqm) {
//先读取所有,遍历,用反逻辑卫条件把不符合条件的过滤掉,剩下的放入结果集,最后返回结果集
//卫条件: 精确查询(==或equals方法)、字符串模糊查询(contains方法)、范围查询(小于小的,大于大的) ---用model去调用相应方法
List list = getAll();
if(imqm==null){
return list;
}
List results = new ArrayList();
for(InMainModel model:list){
//卫条件:id,inUserId,inDate,inDate2
//id--精确查询
if(imqm.getUuid()!=null && imqm.getUuid().trim().length()>0){
if(!model.getUuid().equals( imqm.getUuid().trim())){
continue;
}
}
//inUserId---精确查询
if(imqm.getInUserUuid()!=null && imqm.getInUserUuid().trim().length()>0){
if(!model.getInUserUuid().equals( imqm.getInUserUuid().trim())){
continue;
}
}
//inDate 范围查询之:小于小的
if(imqm.getInDate()>0){
if(model.getInDate()continue;
}
}
//inDate2 范围查询之:大于大的
if(imqm.getInDate2()>0){
if(model.getInDate()>imqm.getInDate2()){
continue;
}
}
results.add(model);
}
return results;
}
}
然后就是逻辑层
package cn.hncu.bookStore.in.business.ebo;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import cn.hncu.bookStore.book.business.factory.BookEbiFactory;
import cn.hncu.bookStore.common.uuid.constance.UuidModelConstance;
import cn.hncu.bookStore.common.uuid.dao.dao.UuidDAO;
import cn.hncu.bookStore.common.uuid.dao.factory.UuidDaoFactory;
import cn.hncu.bookStore.in.business.ebi.InMainEbi;
import cn.hncu.bookStore.in.dao.dao.InDetailDAO;
import cn.hncu.bookStore.in.dao.dao.InMainDAO;
import cn.hncu.bookStore.in.dao.factory.InDetailDaoFactory;
import cn.hncu.bookStore.in.dao.factory.InMainDaoFactory;
import cn.hncu.bookStore.in.vo.InDetailModel;
import cn.hncu.bookStore.in.vo.InDetailQueryModel;
import cn.hncu.bookStore.in.vo.InMainModel;
import cn.hncu.bookStore.in.vo.InMainQueryModel;
import cn.hncu.bookStore.store.dao.dao.StockDAO;
import cn.hncu.bookStore.store.dao.factory.StockDaoFactory;
import cn.hncu.bookStore.store.vo.StockModel;
import cn.hncu.bookStore.store.vo.StockQueryModel;
public class InMainEbo implements InMainEbi{
//注入uuidDao
private UuidDAO uuidDao = UuidDaoFactory.getUuidDAO();
//注入inMainDao
private InMainDAO inMainDao = InMainDaoFactory.getInMainDao();
//注入inDetailDao
private InDetailDAO inDetailDao = InDetailDaoFactory.getInDetailDao();
@Override
public boolean create(InMainModel inMain, List details) {
//需要:InMainModel(id,inDate,inUserId) InDetail(id,inId,bookId,sumNum,sumMoney)
//1.InMainModel需要:id,inDate,inUserId
// 在表现层已经收集并封装了inUserId, 还缺(需要补):id,inDate
String inMainUuid = uuidDao.getUuid(UuidModelConstance.IN_MAIN);
long inDate = new Date().getTime();
inMain.setUuid(inMainUuid);
inMain.setInDate(inDate);
boolean boo = inMainDao.create(inMain);
if(!boo){
return false;
}
//2.InDetail需要: id,inId,bookId,sumNum,sumMoney
//在表现层已经收集并封装了bookId、sumNum, 还缺(需要补):id,inId,sumMoney
//分别为明细列表中的每一条明细补数据
for(InDetailModel detail: details){
String detailId = uuidDao.getUuid(UuidModelConstance.IN_DETAIL);
detail.setUuid(detailId);
detail.setInUuid(inMainUuid);
//访问book模块的逻辑层(一般每个模块的dao只给自己模块的逻辑层访问)
double price = BookEbiFactory.getBookEbi().getSingle(detail.getBookId()).getInPrice();
detail.setSumMoney( detail.getSumNum()*price );
boolean boo2 = inDetailDao.create(detail);
if(!boo2){
return false;
}
}
//程序能运行到这里,说明前面存储明细成功了,那么可以更新库存了
//////////////////////更新库存///////////////////////
boolean boo2 = putInStock(details);//入库的业务逻辑
return boo2;
}
private StockDAO stockDao = StockDaoFactory.getStockDAO();
private boolean putInStock(List details) {
for(InDetailModel detail:details){
//思路:先到库中查找对应bookUuid的记录,如果存在则更新,否则新创建一条记录存储
String bookUuid = detail.getBookId();
int sumNum = detail.getSumNum();
//查询
StockQueryModel sqm = new StockQueryModel();
sqm.setBookUuid(bookUuid);
List list= stockDao.getByCondition(sqm);
StockModel stock = null;
if(list!=null && list.size()>0){
stock = list.get(0);
}
if(stock==null){//库存中不存在,则新创建一个
stock = new StockModel();
String uuid = uuidDao.getUuid(UuidModelConstance.STOCK);
stock.setUuid(uuid);
stock.setBookUuid(bookUuid);
stock.setSumNum(sumNum);
boolean boo3 = stockDao.create(stock);
if(!boo3){
return false;
}
}else{//库存中存在,则更新
stock.setSumNum(stock.getSumNum()+sumNum);
boolean boo4 = stockDao.update(stock);
if(!boo4){
return false;
}
}
}
return true;
}
@Override
public Map> getAll() {
//HashMap是无序,若要有序得用TreeMap ----所有Tree的集合都是有序
//Map> map = new HashMap>();
Map> map = new TreeMap>(); //空参,由元素自己决定排序的先后
//map集合数据的封装方式: map.put(key, value);
//1先获取所有的InMainModel --- List
//2遍历List,针对每一个InMainModel,找到它所对应的那些InDetail ----List
//3针对每一对 < InMainModel,List > 封装(entry)到map ---map.put(K,V)
List inMains = inMainDao.getAll();
for(InMainModel inMain: inMains){
//组织一个查询值对象,调用条件查即查出我们要的结果
InDetailQueryModel idqm = new InDetailQueryModel();
idqm.setInUuid( inMain.getUuid() );
List details = inDetailDao.getByCondition(idqm);
map.put(inMain, details);
}
return map;
}
@Override
public Map> getByCondition(
InMainQueryModel imqm, InDetailQueryModel idqm) {
Map> map = new TreeMap>();
//1用imqm去把符合要求的inMains查出来
List inMains = inMainDao.getByCondition(imqm);
//2遍历所有的inMain, 针对每一个inMain, 封装一个idqm去查明细。
//且把结果存储到map中
for(InMainModel inMain: inMains){
idqm.setInUuid(inMain.getUuid());//★★有这句才能保证查出来的明细是属于当前进货单inMain的
List details = inDetailDao.getByCondition(idqm);
if(details!=null && details.size()>0){
map.put(inMain, details);
}
}
return map;
}
}