夜光序言:
存乎一心,可见朝阳,亦可见暮雨。
正文:
package com.ken.wms.common.service.Impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ken.wms.common.service.Interface.CustomerManageService;
import com.ken.wms.common.util.ExcelUtil;
import com.ken.wms.dao.CustomerMapper;
import com.ken.wms.dao.StockOutMapper;
import com.ken.wms.domain.Customer;
import com.ken.wms.domain.StockOutDO;
import com.ken.wms.exception.CustomerManageServiceException;
import com.ken.wms.util.aop.UserOperation;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 客户信息管理 service 实现类
*
* @author Ken / Yeguang / Genius Team
*/
@Service
public class CustomerManageServiceImpl implements CustomerManageService {
@Autowired
private CustomerMapper customerMapper;
@Autowired
private ExcelUtil excelUtil;
@Autowired
private StockOutMapper stockOutMapper;
/**
* 返回指定customer id 的客户记录
*
* @param customerId 客户ID
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectById(Integer customerId) throws CustomerManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<Customer> customers = new ArrayList<>();
long total = 0;
// 查询
Customer customer;
try {
customer = customerMapper.selectById(customerId);
} catch (PersistenceException e) {
System.out.println("exception catch");
e.printStackTrace();
throw new CustomerManageServiceException(e);
}
if (customer != null) {
customers.add(customer);
total = 1;
}
resultSet.put("data", customers);
resultSet.put("total", total);
return resultSet;
}
/**
* 返回指定 customer name 的客户记录 支持查询分页以及模糊查询
*
* @param offset 分页的偏移值
* @param limit 分页的大小
* @param customerName 客户的名称
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByName(int offset, int limit, String customerName) throws CustomerManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<Customer> customers;
long total = 0;
boolean isPagination = true;
// validate
if (offset < 0 || limit < 0)
isPagination = false;
// query
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
customers = customerMapper.selectApproximateByName(customerName);
if (customers != null) {
PageInfo<Customer> pageInfo = new PageInfo<>(customers);
total = pageInfo.getTotal();
} else
customers = new ArrayList<>();
} else {
customers = customerMapper.selectApproximateByName(customerName);
if (customers != null)
total = customers.size();
else
customers = new ArrayList<>();
}
} catch (PersistenceException e) {
throw new CustomerManageServiceException(e);
}
resultSet.put("data", customers);
resultSet.put("total", total);
return resultSet;
}
/**
* 返回指定 customer Name 的客户记录 支持模糊查询
*
* @param customerName 客户名称
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByName(String customerName) throws CustomerManageServiceException {
return selectByName(-1, -1, customerName);
}
/**
* 分页查询客户的记录
*
* @param offset 分页的偏移值
* @param limit 分页的大小
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectAll(int offset, int limit) throws CustomerManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<Customer> customers;
long total = 0;
boolean isPagination = true;
// validate
if (offset < 0 || limit < 0)
isPagination = false;
// query
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
customers = customerMapper.selectAll();
if (customers != null) {
PageInfo<Customer> pageInfo = new PageInfo<>(customers);
total = pageInfo.getTotal();
} else
customers = new ArrayList<>();
} else {
customers = customerMapper.selectAll();
if (customers != null)
total = customers.size();
else
customers = new ArrayList<>();
}
} catch (PersistenceException e) {
throw new CustomerManageServiceException(e);
}
resultSet.put("data", customers);
resultSet.put("total", total);
return resultSet;
}
/**
* 查询所有客户的记录
*
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectAll() throws CustomerManageServiceException {
return selectAll(-1, -1);
}
/**
* 检查客户信息是否满足要求
*
* @param customer 客户信息实体
* @return 返回是否满足要求
*/
private boolean customerCheck(Customer customer) {
return customer.getName() != null && customer.getPersonInCharge() != null && customer.getTel() != null
&& customer.getEmail() != null && customer.getAddress() != null;
}
/**
* 添加客户信息
*
* @param customer 客户信息
* @return 返回一个boolean值,值为true代表更新成功,否则代表失败
*/
@UserOperation(value = "添加客户信息")
@Override
public boolean addCustomer(Customer customer) throws CustomerManageServiceException {
// 插入新的记录
if (customer != null) {
// 验证
if (customerCheck(customer)) {
try {
if (null == customerMapper.selectByName(customer.getName())) {
customerMapper.insert(customer);
return true;
}
} catch (PersistenceException e) {
throw new CustomerManageServiceException(e);
}
}
}
return false;
}
/**
* 更新客户信息
*
* @param customer 客户信息
* @return 返回一个boolean值,值为true代表更新成功,否则代表失败
*/
@UserOperation(value = "修改客户信息")
@Override
public boolean updateCustomer(Customer customer) throws CustomerManageServiceException {
// 更新记录
if (customer != null) {
// 检验
if (customerCheck(customer)) {
try {
// 检查重名
Customer customerFromDB = customerMapper.selectByName(customer.getName());
if (customerFromDB == null || customerFromDB.getId().equals(customer.getId())) {
customerMapper.update(customer);
return true;
}
} catch (PersistenceException e) {
throw new CustomerManageServiceException(e);
}
}
}
return false;
}
/**
* 删除客户信息
*
* @param customerId 客户ID
* @return 返回一个boolean值,值为true代表更新成功,否则代表失败
*/
@UserOperation(value = "删除客户信息")
@Override
public boolean deleteCustomer(Integer customerId) throws CustomerManageServiceException {
try {
// 查询该客户是否有出库记录
List<StockOutDO> records = stockOutMapper.selectByCustomerId(customerId);
if (records != null && records.size() > 0) {
return false;
} else {
// 删除该条客户记录
customerMapper.deleteById(customerId);
return true;
}
} catch (PersistenceException e) {
throw new CustomerManageServiceException(e);
}
}
/**
* 从文件中导入客户信息
*
* @param file 导入信息的文件
* @return 返回一个Map,其中:key为total代表导入的总记录数,key为available代表有效导入的记录数
*/
@UserOperation(value = "导入客户信息")
@Override
public Map<String, Object> importCustomer(MultipartFile file) throws CustomerManageServiceException {
// 初始化结果集
Map<String, Object> result = new HashMap<>();
int total = 0;
int available = 0;
// 从 Excel 文件中读取
List<Object> customers = excelUtil.excelReader(Customer.class, file);
if (customers != null) {
total = customers.size();
// 验证每一条记录
try {
Customer customer;
List<Customer> availableList = new ArrayList<>();
for (Object object : customers) {
customer = (Customer) object;
if (customerCheck(customer)) {
if (customerMapper.selectByName(customer.getName()) == null)
availableList.add(customer);
}
}
// 保存到数据库
available = availableList.size();
if (available > 0) {
customerMapper.insertBatch(availableList);
}
} catch (PersistenceException e) {
throw new CustomerManageServiceException(e);
}
}
result.put("total", total);
result.put("available", available);
return result;
}
/**
* 导出客户信息到文件中
*
* @param customers 包含若干条 customer 信息的 List
* @return Excel 文件
*/
@UserOperation(value = "导出客户信息")
@Override
public File exportCustomer(List<Customer> customers) {
if (customers == null)
return null;
return excelUtil.excelWriter(Customer.class, customers);
}
}
package com.ken.wms.common.service.Impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ken.wms.common.service.Interface.GoodsManageService;
import com.ken.wms.common.util.ExcelUtil;
import com.ken.wms.dao.GoodsMapper;
import com.ken.wms.dao.StockInMapper;
import com.ken.wms.dao.StockOutMapper;
import com.ken.wms.dao.StorageMapper;
import com.ken.wms.domain.Goods;
import com.ken.wms.domain.StockInDO;
import com.ken.wms.domain.StockOutDO;
import com.ken.wms.domain.Storage;
import com.ken.wms.exception.GoodsManageServiceException;
import com.ken.wms.util.aop.UserOperation;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 货物信息管理Service 实现类
*
* @author Ken / Yeguang / Genius Team
*/
@Service
public class GoodsManageServiceImpl implements GoodsManageService {
@Autowired
private GoodsMapper goodsMapper;
@Autowired
private StockInMapper stockInMapper;
@Autowired
private StockOutMapper stockOutMapper;
@Autowired
private StorageMapper storageMapper;
@Autowired
private ExcelUtil excelUtil;
/**
* 返回指定goods ID 的货物记录
*
* @param goodsId 货物ID
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectById(Integer goodsId) throws GoodsManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<Goods> goodsList = new ArrayList<>();
long total = 0;
// 查询
Goods goods;
try {
goods = goodsMapper.selectById(goodsId);
} catch (PersistenceException e) {
throw new GoodsManageServiceException(e);
}
if (goods != null) {
goodsList.add(goods);
total = 1;
}
resultSet.put("data", goodsList);
resultSet.put("total", total);
return resultSet;
}
/**
* 返回指定 goods name 的货物记录 支持查询分页以及模糊查询
*
* @param offset 分页的偏移值
* @param limit 分页的大小
* @param goodsName 货物的名称
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByName(int offset, int limit, String goodsName) throws GoodsManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<Goods> goodsList;
long total = 0;
boolean isPagination = true;
// validate
if (offset < 0 || limit < 0)
isPagination = false;
// query
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
goodsList = goodsMapper.selectApproximateByName(goodsName);
if (goodsList != null) {
PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
total = pageInfo.getTotal();
} else
goodsList = new ArrayList<>();
} else {
goodsList = goodsMapper.selectApproximateByName(goodsName);
if (goodsList != null)
total = goodsList.size();
else
goodsList = new ArrayList<>();
}
} catch (PersistenceException e) {
throw new GoodsManageServiceException(e);
}
resultSet.put("data", goodsList);
resultSet.put("total", total);
return resultSet;
}
/**
* 返回指定 goods name 的货物记录 支持模糊查询
*
* @param goodsName 货物名称
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByName(String goodsName) throws GoodsManageServiceException {
return selectByName(-1, -1, goodsName);
}
/**
* 分页查询货物记录
*
* @param offset 分页的偏移值
* @param limit 分页的大小
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectAll(int offset, int limit) throws GoodsManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<Goods> goodsList;
long total = 0;
boolean isPagination = true;
// validate
if (offset < 0 || limit < 0)
isPagination = false;
// query
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
goodsList = goodsMapper.selectAll();
if (goodsList != null) {
PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
total = pageInfo.getTotal();
} else
goodsList = new ArrayList<>();
} else {
goodsList = goodsMapper.selectAll();
if (goodsList != null)
total = goodsList.size();
else
goodsList = new ArrayList<>();
}
} catch (PersistenceException e) {
throw new GoodsManageServiceException(e);
}
resultSet.put("data", goodsList);
resultSet.put("total", total);
return resultSet;
}
/**
* 查询所有的货物记录
*
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectAll() throws GoodsManageServiceException {
return selectAll(-1, -1);
}
/**
* 检查货物信息是否满足要求
*
* @param goods 货物信息
* @return 若货物信息满足要求则返回true,否则返回false
*/
private boolean goodsCheck(Goods goods) {
if (goods != null) {
if (goods.getName() != null && goods.getValue() != null) {
return true;
}
}
return false;
}
/**
* 添加货物记录
*
* @param goods 货物信息
* @return 返回一个boolean值,值为true代表更新成功,否则代表失败
*/
@UserOperation(value = "添加货物信息")
@Override
public boolean addGoods(Goods goods) throws GoodsManageServiceException {
try {
// 插入新的记录
if (goods != null) {
// 验证
if (goodsCheck(goods)) {
goodsMapper.insert(goods);
return true;
}
}
return false;
} catch (PersistenceException e) {
throw new GoodsManageServiceException(e);
}
}
/**
* 更新货物记录
*
* @param goods 货物信息
* @return 返回一个boolean值,值为true代表更新成功,否则代表失败
*/
@UserOperation(value = "修改货物信息")
@Override
public boolean updateGoods(Goods goods) throws GoodsManageServiceException {
try {
// 更新记录
if (goods != null) {
// 检验
if (goodsCheck(goods)) {
goodsMapper.update(goods);
return true;
}
}
return false;
} catch (PersistenceException e) {
throw new GoodsManageServiceException(e);
}
}
/**
* 删除货物记录
*
* @param goodsId 货物ID
* @return 返回一个boolean值,值为true代表更新成功,否则代表失败
*/
@UserOperation(value = "删除货物信息")
@Override
public boolean deleteGoods(Integer goodsId) throws GoodsManageServiceException {
try {
// 检查该货物是否有入库信息
List<StockInDO> stockInDORecord = stockInMapper.selectByGoodID(goodsId);
if (stockInDORecord != null && !stockInDORecord.isEmpty())
return false;
// 检查该货物是否有出库信息
List<StockOutDO> stockOutDORecord = stockOutMapper.selectByGoodId(goodsId);
if (stockOutDORecord != null && !stockOutDORecord.isEmpty())
return false;
// 检查该货物是否有存储信息
List<Storage> storageRecord = storageMapper.selectByGoodsIDAndRepositoryID(goodsId, null);
if (storageRecord != null && !storageRecord.isEmpty())
return false;
// 删除货物记录
goodsMapper.deleteById(goodsId);
return true;
} catch (PersistenceException e) {
throw new GoodsManageServiceException(e);
}
}
/**
* 从文件中导入货物信息
*
* @param file 导入信息的文件
* @return 返回一个Map,其中:key为total代表导入的总记录数,key为available代表有效导入的记录数
*/
@UserOperation(value = "导入货物信息")
@Override
public Map<String, Object> importGoods(MultipartFile file) throws GoodsManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
int total = 0;
int available = 0;
// 从 Excel 文件中读取
List<Object> goodsList = excelUtil.excelReader(Goods.class, file);
if (goodsList != null) {
total = goodsList.size();
// 验证每一条记录
Goods goods;
List<Goods> availableList = new ArrayList<>();
for (Object object : goodsList) {
goods = (Goods) object;
if (goodsCheck(goods)) {
availableList.add(goods);
}
}
// 保存到数据库
try {
available = availableList.size();
if (available > 0) {
goodsMapper.insertBatch(availableList);
}
} catch (PersistenceException e) {
throw new GoodsManageServiceException(e);
}
}
resultSet.put("total", total);
resultSet.put("available", available);
return resultSet;
}
/**
* 导出货物信息到文件中
*
* @param goods 包含若干条 Supplier 信息的 List
* @return excel 文件
*/
@UserOperation(value = "导出货物信息")
@Override
public File exportGoods(List<Goods> goods) {
if (goods == null)
return null;
return excelUtil.excelWriter(Goods.class, goods);
}
}
package com.ken.wms.common.service.Impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ken.wms.common.service.Interface.RepositoryAdminManageService;
import com.ken.wms.common.util.ExcelUtil;
import com.ken.wms.dao.RepositoryAdminMapper;
import com.ken.wms.domain.RepositoryAdmin;
import com.ken.wms.domain.UserInfoDTO;
import com.ken.wms.exception.RepositoryAdminManageServiceException;
import com.ken.wms.exception.UserInfoServiceException;
import com.ken.wms.security.service.Interface.UserInfoService;
import com.ken.wms.util.aop.UserOperation;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.*;
/**
* 仓库管理员管理 service 实现类
*
* @author Ken/ Yeguang / Genius Team
*/
@Service
public class RepositoryAdminManageServiceImpl implements RepositoryAdminManageService {
@Autowired
private RepositoryAdminMapper repositoryAdminMapper;
@Autowired
private ExcelUtil excelUtil;
@Autowired
private UserInfoService userInfoService;
/**
* 返回指定repository id 的仓库管理员记录
*
* @param repositoryAdminID 仓库管理员ID
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByID(Integer repositoryAdminID) throws RepositoryAdminManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<RepositoryAdmin> repositoryAdmins = new ArrayList<>();
long total = 0;
// 查询
RepositoryAdmin repositoryAdmin;
try {
repositoryAdmin = repositoryAdminMapper.selectByID(repositoryAdminID);
} catch (PersistenceException e) {
throw new RepositoryAdminManageServiceException(e);
}
if (repositoryAdmin != null) {
repositoryAdmins.add(repositoryAdmin);
total = 1;
}
resultSet.put("data", repositoryAdmins);
resultSet.put("total", total);
return resultSet;
}
/**
* 返回指定 repository address 的仓库管理员记录 支持查询分页以及模糊查询
*
* @param offset 分页的偏移值
* @param limit 分页的大小
* @param name 仓库管理员的名称
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByName(int offset, int limit, String name) {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<RepositoryAdmin> repositoryAdmins;
long total = 0;
boolean isPagination = true;
// validate
if (offset < 0 || limit < 0)
isPagination = false;
// query
if (isPagination) {
PageHelper.offsetPage(offset, limit);
repositoryAdmins = repositoryAdminMapper.selectByName(name);
if (repositoryAdmins != null) {
PageInfo<RepositoryAdmin> pageInfo = new PageInfo<>(repositoryAdmins);
total = pageInfo.getTotal();
} else
repositoryAdmins = new ArrayList<>();
} else {
repositoryAdmins = repositoryAdminMapper.selectByName(name);
if (repositoryAdmins != null)
total = repositoryAdmins.size();
else
repositoryAdmins = new ArrayList<>();
}
resultSet.put("data", repositoryAdmins);
resultSet.put("total", total);
return resultSet;
}
/**
* 返回指定 repository Name 的仓库管理员记录 支持模糊查询
*
* @param name 仓库管理员名称
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByName(String name) {
return selectByName(-1, -1, name);
}
/**
* 分页查询仓库管理员的记录
*
* @param offset 分页的偏移值
* @param limit 分页的大小
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectAll(int offset, int limit) throws RepositoryAdminManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<RepositoryAdmin> repositoryAdmins;
long total = 0;
boolean isPagination = true;
// validate
if (offset < 0 || limit < 0)
isPagination = false;
// query
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
repositoryAdmins = repositoryAdminMapper.selectAll();
if (repositoryAdmins != null) {
PageInfo<RepositoryAdmin> pageInfo = new PageInfo<>(repositoryAdmins);
total = pageInfo.getTotal();
} else
repositoryAdmins = new ArrayList<>();
} else {
repositoryAdmins = repositoryAdminMapper.selectAll();
if (repositoryAdmins != null)
total = repositoryAdmins.size();
else
repositoryAdmins = new ArrayList<>();
}
} catch (PersistenceException e) {
throw new RepositoryAdminManageServiceException(e);
}
resultSet.put("data", repositoryAdmins);
resultSet.put("total", total);
return resultSet;
}
/**
* 查询所有仓库管理员的记录
*
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectAll() throws RepositoryAdminManageServiceException {
return selectAll(-1, -1);
}
/**
* 添加仓库管理员信息
*
* @param repositoryAdmin 仓库管理员信息
* @return 返回一个boolean值,值为true代表添加成功,否则代表失败
*/
@UserOperation(value = "添加仓库管理员信息")
@Override
public boolean addRepositoryAdmin(RepositoryAdmin repositoryAdmin) throws RepositoryAdminManageServiceException {
if (repositoryAdmin != null) {
if (repositoryAdminCheck(repositoryAdmin)) {
try {
// 添加仓库管理员信息到数据库中
repositoryAdminMapper.insert(repositoryAdmin);
// 获取插入数据后返回的用户ID
Integer userID = repositoryAdmin.getId();
if (userID == null)
return false;
// 为仓库管理员创建账户
UserInfoDTO userInfo = new UserInfoDTO();
userInfo.setUserID(userID);
userInfo.setUserName(repositoryAdmin.getName());
userInfo.setPassword(repositoryAdmin.getId().toString());
userInfo.setRole(new ArrayList<>(Collections.singletonList("commonsAdmin")));
// 添加新创建的仓库管理员账户信息
return userInfoService.insertUserInfo(userInfo);
} catch (PersistenceException | UserInfoServiceException e) {
throw new RepositoryAdminManageServiceException(e, "Fail to persist repository admin info");
}
}
}
return false;
}
/**
* 更新仓库管理员信息
*
* @param repositoryAdmin 仓库管理员信息
* @return 返回一个boolean值,值为true代表更新成功,否则代表失败
*/
@UserOperation(value = "修改仓库管理员信息")
@Override
public boolean updateRepositoryAdmin(RepositoryAdmin repositoryAdmin) throws RepositoryAdminManageServiceException {
if (repositoryAdmin != null) {
try {
// 检查属性
if (!repositoryAdminCheck(repositoryAdmin))
return false;
// 若有指派的仓库则检查
if (repositoryAdmin.getRepositoryBelongID() != null) {
RepositoryAdmin rAdminFromDB = repositoryAdminMapper.selectByRepositoryID(repositoryAdmin.getRepositoryBelongID());
if (rAdminFromDB != null && !Objects.equals(rAdminFromDB.getId(), repositoryAdmin.getId()))
return false;
}
// 更新
repositoryAdminMapper.update(repositoryAdmin);
return true;
} catch (PersistenceException e) {
throw new RepositoryAdminManageServiceException(e);
}
} else
return false;
}
/**
* 删除仓库管理员信息
*
* @param repositoryAdminID 仓库管理员ID
* @return 返回一个boolean值,值为true代表删除成功,否则代表失败
*/
@UserOperation(value = "删除仓库管理员信息")
@Override
public boolean deleteRepositoryAdmin(Integer repositoryAdminID) throws RepositoryAdminManageServiceException {
try {
// 判断是否已指派仓库
RepositoryAdmin repositoryAdmin = repositoryAdminMapper.selectByID(repositoryAdminID);
if (repositoryAdmin != null && repositoryAdmin.getRepositoryBelongID() == null) {
// 删除仓库管理员信息
repositoryAdminMapper.deleteByID(repositoryAdminID);
// 删除账户信息
userInfoService.deleteUserInfo(repositoryAdminID);
return true;
} else
return false;
} catch (PersistenceException | UserInfoServiceException e) {
throw new RepositoryAdminManageServiceException(e);
}
}
/**
* 为仓库管理员指派指定 ID 的仓库
*
* @param repositoryAdminID 仓库管理员ID
* @param repositoryID 所指派的仓库ID
* @return 返回一个 boolean 值,值为 true 表示仓库指派成功,否则表示失败
*/
@UserOperation(value = "指派仓库管理员")
@Override
public boolean assignRepository(Integer repositoryAdminID, Integer repositoryID) throws RepositoryAdminManageServiceException {
try {
RepositoryAdmin repositoryAdmin = repositoryAdminMapper.selectByID(repositoryAdminID);
if (repositoryAdmin != null) {
repositoryAdmin.setRepositoryBelongID(repositoryID);
return updateRepositoryAdmin(repositoryAdmin);
} else
return false;
} catch (PersistenceException e) {
throw new RepositoryAdminManageServiceException(e);
}
}
/**
* 从文件中导入仓库管理员信息
*
* @param file 导入信息的文件
* @return 返回一个Map,其中:key为total代表导入的总记录数,key为available代表有效导入的记录数
*/
@UserOperation(value = "导入仓库管理员信息")
@Override
public Map<String, Object> importRepositoryAdmin(MultipartFile file) throws RepositoryAdminManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
long total = 0;
long available = 0;
// 从文件中读取
List<Object> repositoryAdmins = excelUtil.excelReader(RepositoryAdmin.class, file);
if (repositoryAdmins != null) {
total = repositoryAdmins.size();
// 验证记录
RepositoryAdmin repositoryAdmin;
List<RepositoryAdmin> availableList = new ArrayList<>();
for (Object object : repositoryAdmins) {
repositoryAdmin = (RepositoryAdmin) object;
if (repositoryAdminCheck(repositoryAdmin))
availableList.add(repositoryAdmin);
}
try {
// 保存到数据库
available = availableList.size();
if (available > 0)
repositoryAdminMapper.insertBatch(availableList);
} catch (PersistenceException e) {
throw new RepositoryAdminManageServiceException(e);
}
}
resultSet.put("total", total);
resultSet.put("available", available);
return resultSet;
}
/**
* 导出仓库管理员信息到文件中
*
* @param repositoryAdmins 包含若干条 repository 信息的 List
* @return Excel 文件
*/
@UserOperation(value = "导出仓库管理员信息")
@Override
public File exportRepositoryAdmin(List<RepositoryAdmin> repositoryAdmins) {
File file = null;
if (repositoryAdmins != null) {
file = excelUtil.excelWriter(RepositoryAdmin.class, repositoryAdmins);
}
return file;
}
/**
* 检验 repositoryAdmin 信息是否有效
*
* @param repositoryAdmin 仓库管理员信息
* @return 返回一个 boolean 值,若仓库管理员信息中要求非空均有值,返回 true,否则返回 false
*/
private boolean repositoryAdminCheck(RepositoryAdmin repositoryAdmin) {
return repositoryAdmin.getName() != null && repositoryAdmin.getSex() != null && repositoryAdmin.getTel() != null
&& repositoryAdmin.getBirth() != null && repositoryAdmin.getBirth() != null;
}
/**
* 返回所属指定 repositoryID 的仓库管理员信息
*
* @param repositoryID 仓库ID 其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
* @return 返回一个Map,
*/
@Override
public Map<String, Object> selectByRepositoryID(Integer repositoryID) throws RepositoryAdminManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<RepositoryAdmin> repositoryAdmins = new ArrayList<>();
long total = 0;
// 查询
RepositoryAdmin repositoryAdmin;
try {
repositoryAdmin = repositoryAdminMapper.selectByRepositoryID(repositoryID);
} catch (PersistenceException e) {
throw new RepositoryAdminManageServiceException(e);
}
if (repositoryAdmin != null) {
repositoryAdmins.add(repositoryAdmin);
total = 1;
}
resultSet.put("data", repositoryAdmins);
resultSet.put("total", total);
return resultSet;
}
}
package com.ken.wms.common.service.Impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ken.wms.common.service.Interface.RepositoryService;
import com.ken.wms.common.util.ExcelUtil;
import com.ken.wms.dao.*;
import com.ken.wms.domain.*;
import com.ken.wms.exception.RepositoryManageServiceException;
import com.ken.wms.util.aop.UserOperation;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 仓库信息管理 service 实现类
*
* @author Ken / Yeguang / Genius Team
*/
@Service
public class RepositoryManageServiceImpl implements RepositoryService {
@Autowired
private RepositoryMapper repositoryMapper;
@Autowired
private ExcelUtil excelUtil;
@Autowired
private StockInMapper stockInMapper;
@Autowired
private StockOutMapper stockOutMapper;
@Autowired
private StorageMapper storageMapper;
@Autowired
private RepositoryAdminMapper repositoryAdminMapper;
/**
* 返回指定 repository ID 的仓库记录
*
* @param repositoryId 仓库ID
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectById(Integer repositoryId) throws RepositoryManageServiceException {
// 初始化結果集
Map<String, Object> resultSet = new HashMap<>();
List<Repository> repositories = new ArrayList<>();
long total = 0;
// 查詢
Repository repository;
try {
repository = repositoryMapper.selectByID(repositoryId);
} catch (PersistenceException e) {
throw new RepositoryManageServiceException(e);
}
if (repository != null) {
repositories.add(repository);
total = 1;
}
resultSet.put("data", repositories);
resultSet.put("total", total);
return resultSet;
}
/**
* 返回指定 repository address 的仓库记录 支持查询分页以及模糊查询
*
* @param offset 分页的偏移值
* @param limit 分页的大小
* @param address 仓库的地址
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByAddress(int offset, int limit, String address) throws RepositoryManageServiceException {
// 初始化結果集
Map<String, Object> resultSet = new HashMap<>();
List<Repository> repositories;
long total = 0;
boolean isPagination = true;
// validate
if (offset < 0 || limit < 0)
isPagination = false;
// query
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
repositories = repositoryMapper.selectByAddress(address);
if (repositories != null) {
PageInfo<Repository> pageInfo = new PageInfo<>(repositories);
total = pageInfo.getTotal();
} else
repositories = new ArrayList<>();
} else {
repositories = repositoryMapper.selectByAddress(address);
if (repositories != null)
total = repositories.size();
else
repositories = new ArrayList<>();
}
} catch (PersistenceException e) {
throw new RepositoryManageServiceException(e);
}
resultSet.put("data", repositories);
resultSet.put("total", total);
return resultSet;
}
/**
* 返回指定 repository address 的仓库记录 支持模糊查询
*
* @param address 仓库名称
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByAddress(String address) throws RepositoryManageServiceException {
return selectByAddress(-1, -1, address);
}
/**
* 分页查询仓库记录
*
* @param offset 分页的偏移值
* @param limit 分页的大小
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectAll(int offset, int limit) throws RepositoryManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<Repository> repositories;
long total = 0;
boolean isPagination = true;
// validate
if (offset < 0 || limit < 0)
isPagination = false;
//query
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
repositories = repositoryMapper.selectAll();
if (repositories != null) {
PageInfo<Repository> pageInfo = new PageInfo<>(repositories);
total = pageInfo.getTotal();
} else
repositories = new ArrayList<>();
} else {
repositories = repositoryMapper.selectAll();
if (repositories != null)
total = repositories.size();
else
repositories = new ArrayList<>();
}
} catch (PersistenceException e) {
throw new RepositoryManageServiceException(e);
}
resultSet.put("data", repositories);
resultSet.put("total", total);
return resultSet;
}
/**
* 查询所有的仓库记录
*
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectAll() throws RepositoryManageServiceException {
return selectAll(-1, -1);
}
/**
* 检查仓库信息是否满足
*
* @param repository 仓库信息
* @return 若仓库信息满足要求则返回true,否则返回false
*/
private boolean repositoryCheck(Repository repository) {
return repository.getAddress() != null && repository.getStatus() != null && repository.getArea() != null;
}
/**
* 添加仓库记录
*
* @param repository 仓库信息
* @return 返回一个boolean值,值为true代表更新成功,否则代表失败
*/
@UserOperation(value = "添加仓库信息")
@Override
public boolean addRepository(Repository repository) throws RepositoryManageServiceException {
// 插入一条新的记录
if (repository != null) {
try {
// 有效性验证
if (repositoryCheck(repository))
repositoryMapper.insert(repository);
if (repository.getId() != null) {
return true;
}
} catch (PersistenceException e) {
throw new RepositoryManageServiceException(e);
}
}
return false;
}
/**
* 更新仓库记录
*
* @param repository 仓库信息
* @return 返回一个boolean值,值为true代表更新成功,否则代表失败
*/
@UserOperation(value = "修改仓库信息")
@Override
public boolean updateRepository(Repository repository) throws RepositoryManageServiceException {
// 更新仓库记录
if (repository != null) {
// 有效性验证
try {
if (repositoryCheck(repository)) {
if (repository.getId() != null) {
repositoryMapper.update(repository);
return true;
}
}
} catch (PersistenceException e) {
throw new RepositoryManageServiceException(e);
}
}
return false;
}
/**
* 删除仓库记录
*
* @param repositoryId 仓库ID
* @return 返回一个boolean值,值为true代表更新成功,否则代表失败
*/
@UserOperation(value = "删除仓库信息")
@Override
public boolean deleteRepository(Integer repositoryId) throws RepositoryManageServiceException {
try {
// 检查是否存在出库记录
List<StockOutDO> stockOutDOList = stockOutMapper.selectByRepositoryID(repositoryId);
if (stockOutDOList != null && !stockOutDOList.isEmpty())
return false;
// 检查是否存在入库记录
List<StockInDO> stockInDOList = stockInMapper.selectByRepositoryID(repositoryId);
if (stockInDOList != null && !stockInDOList.isEmpty())
return false;
// 检查是否存在库存记录
List<Storage> storageRecords = storageMapper.selectAllAndRepositoryID(repositoryId);
if (storageRecords != null && !storageRecords.isEmpty())
return false;
// 检查是否已指派仓库管理员
RepositoryAdmin repositoryAdmin = repositoryAdminMapper.selectByRepositoryID(repositoryId);
if (repositoryAdmin != null)
return false;
// 删除记录
repositoryMapper.deleteByID(repositoryId);
return true;
} catch (PersistenceException e) {
throw new RepositoryManageServiceException(e);
}
}
/**
* 从文件中导入仓库信息
*
* @param file 导入信息的文件
* @return 返回一个Map,其中:key为total代表导入的总记录数,key为available代表有效导入的记录数
*/
@UserOperation(value = "导入仓库信息")
@Override
public Map<String, Object> importRepository(MultipartFile file) throws RepositoryManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
int total = 0;
int available = 0;
// 从文件中读取
List<Object> repositories = excelUtil.excelReader(Repository.class, file);
if (repositories != null) {
total = repositories.size();
// 验证每一条记录
Repository repository;
List<Repository> availableList = new ArrayList<>();
for (Object object : repositories) {
repository = (Repository) object;
if (repository.getAddress() != null && repository.getStatus() != null && repository.getArea() != null)
availableList.add(repository);
}
// 保存到数据库
try {
available = availableList.size();
if (available > 0)
repositoryMapper.insertbatch(availableList);
} catch (PersistenceException e) {
throw new RepositoryManageServiceException(e);
}
}
resultSet.put("total", total);
resultSet.put("available", available);
return resultSet;
}
/**
* 导出仓库信息到文件中
*
* @param repositories 包含若干条 Supplier 信息的 List
* @return excel 文件
*/
@UserOperation(value = "导出仓库信息")
@Override
public File exportRepository(List<Repository> repositories) {
if (repositories == null)
return null;
// 导出为文件
return excelUtil.excelWriter(Repository.class, repositories);
}
/**
* 查询所有未指派仓库管理员的仓库记录
*
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectUnassign() throws RepositoryManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<Repository> repositories;
long total = 0;
// 查询
try {
repositories = repositoryMapper.selectUnassign();
} catch (PersistenceException e) {
throw new RepositoryManageServiceException(e);
}
if (repositories != null)
total = repositories.size();
else
repositories = new ArrayList<>();
resultSet.put("data", repositories);
resultSet.put("total", total);
return resultSet;
}
}
package com.ken.wms.common.service.Impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ken.wms.common.service.Interface.StockRecordManageService;
import com.ken.wms.common.service.Interface.StorageManageService;
import com.ken.wms.dao.*;
import com.ken.wms.domain.*;
import com.ken.wms.exception.StockRecordManageServiceException;
import com.ken.wms.exception.StorageManageServiceException;
import com.ken.wms.util.aop.UserOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class StockRecordManageServiceImpl implements StockRecordManageService {
@Autowired
private SupplierMapper supplierMapper;
@Autowired
private CustomerMapper customerMapper;
@Autowired
private GoodsMapper goodsMapper;
@Autowired
private RepositoryMapper repositoryMapper;
@Autowired
private StorageManageService storageManageService;
@Autowired
private StockInMapper stockinMapper;
@Autowired
private StockOutMapper stockOutMapper;
/**
* 货物入库操作
*
* @param supplierID 供应商ID
* @param goodsID 货物ID
* @param repositoryID 入库仓库ID
* @param number 入库数量
* @return 返回一个boolean 值,若值为true表示入库成功,否则表示入库失败
*/
@UserOperation(value = "货物入库")
@Override
public boolean stockInOperation(Integer supplierID, Integer goodsID, Integer repositoryID, long number, String personInCharge) throws StockRecordManageServiceException {
// ID对应的记录是否存在
if (!(supplierValidate(supplierID) && goodsValidate(goodsID) && repositoryValidate(repositoryID)))
return false;
if (personInCharge == null)
return false;
// 检查入库数量有效性
if (number < 0)
return false;
try {
// 更新库存记录
boolean isSuccess;
isSuccess = storageManageService.storageIncrease(goodsID, repositoryID, number);
// 保存入库记录
if (isSuccess) {
StockInDO stockInDO = new StockInDO();
stockInDO.setGoodID(goodsID);
stockInDO.setSupplierID(supplierID);
stockInDO.setNumber(number);
stockInDO.setPersonInCharge(personInCharge);
stockInDO.setTime(new Date());
stockInDO.setRepositoryID(repositoryID);
stockinMapper.insert(stockInDO);
}
return isSuccess;
} catch (PersistenceException | StorageManageServiceException e) {
throw new StockRecordManageServiceException(e);
}
}
/**
* 货物出库操作
*
* @param customerID 客户ID
* @param goodsID 货物ID
* @param repositoryID 出库仓库ID
* @param number 出库数量
* @return 返回一个boolean值,若值为true表示出库成功,否则表示出库失败
*/
@UserOperation(value = "货物出库")
@Override
public boolean stockOutOperation(Integer customerID, Integer goodsID, Integer repositoryID, long number, String personInCharge) throws StockRecordManageServiceException {
// 检查ID对应的记录是否存在
if (!(customerValidate(customerID) && goodsValidate(goodsID) && repositoryValidate(repositoryID)))
return false;
// 检查出库数量范围是否有效
if (number < 0)
return false;
try {
// 更新库存信息
boolean isSuccess;
isSuccess = storageManageService.storageDecrease(goodsID, repositoryID, number);
// 保存出库记录
if (isSuccess) {
StockOutDO stockOutDO = new StockOutDO();
stockOutDO.setCustomerID(customerID);
stockOutDO.setGoodID(goodsID);
stockOutDO.setNumber(number);
stockOutDO.setPersonInCharge(personInCharge);
stockOutDO.setRepositoryID(repositoryID);
stockOutDO.setTime(new Date());
stockOutMapper.insert(stockOutDO);
}
return isSuccess;
} catch (PersistenceException | StorageManageServiceException e) {
throw new StockRecordManageServiceException(e);
}
}
/**
* 查询出入库记录
*
* @param repositoryID 仓库ID
* @param endDateStr 查询记录起始日期
* @param startDateStr 查询记录结束日期
* @param searchType 记录查询方式
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectStockRecord(Integer repositoryID, String startDateStr, String endDateStr, String searchType) throws StockRecordManageServiceException {
return selectStockRecord(repositoryID, startDateStr, endDateStr, searchType, -1, -1);
}
/**
* 分页查询出入库记录
*
* @param repositoryID 仓库ID
* @param endDateStr 查询记录起始日期
* @param startDateStr 查询记录结束日期
* @param searchType 记录查询方式
* @param offset 分页偏移值
* @param limit 分页大小
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@SuppressWarnings("unchecked")
@Override
public Map<String, Object> selectStockRecord(Integer repositoryID, String startDateStr, String endDateStr, String searchType, int offset, int limit) throws StockRecordManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
long total = 0;
// 检查传入参数
if (repositoryID == null || searchType == null)
throw new StockRecordManageServiceException("exception");
// 转换 Date 对象
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = null;
Date endDate = null;
Date newEndDate = null;
try {
if (StringUtils.isNotEmpty(startDateStr))
startDate = dateFormat.parse(startDateStr);
if (StringUtils.isNotEmpty(endDateStr))
{
endDate = dateFormat.parse(endDateStr);
newEndDate = new Date(endDate.getTime()+(24*60*60*1000)-1);
}
} catch (ParseException e) {
throw new StockRecordManageServiceException(e);
}
// 根据查询模式执行查询
List<StockRecordDTO> stockRecordDTOS = new ArrayList<>();
Map<String, Object> stockInTemp;
Map<String, Object> stockOutTemp;
List<StockInDO> stockInRecordDOS = null;
List<StockOutDO> stockOutRecordDOS = null;
switch (searchType) {
case "all": {
if (offset < 0 || limit < 0) {
stockInTemp = selectStockInRecord(repositoryID, startDate, endDate, offset, limit);
stockOutTemp = selectStockOutRecord(repositoryID, startDate, endDate, offset, limit);
stockInRecordDOS = (List<StockInDO>) stockInTemp.get("data");
stockOutRecordDOS = (List<StockOutDO>) stockOutTemp.get("data");
} else {
int stockInRecordOffset = offset / 2;
int stockOutRecordOffset = stockInRecordOffset * 2 < offset ? stockInRecordOffset + 1 : stockInRecordOffset;
int stockInRecordLimit = limit / 2;
int stockOutRecordLimit = stockInRecordLimit * 2 < limit ? stockInRecordLimit + 1 : stockInRecordLimit;
stockInTemp = selectStockInRecord(repositoryID, startDate, newEndDate, stockInRecordOffset, limit);
stockOutTemp = selectStockOutRecord(repositoryID, startDate, newEndDate, stockOutRecordOffset, limit);
stockInRecordDOS = (List<StockInDO>) stockInTemp.get("data");
stockOutRecordDOS = (List<StockOutDO>) stockOutTemp.get("data");
int stockInRecordDosSize = stockInRecordDOS.size();
int stockOutRecordDoSize = stockOutRecordDOS.size();
if (stockInRecordDosSize >= stockInRecordLimit && stockOutRecordDoSize >= stockOutRecordLimit) {
stockInRecordDOS = stockInRecordDOS.subList(0, stockInRecordLimit);
stockOutRecordDOS = stockOutRecordDOS.subList(0, stockOutRecordLimit);
} else if (stockInRecordDosSize < stockInRecordLimit && stockOutRecordDoSize > stockOutRecordLimit) {
int appendSize = (stockOutRecordDoSize - stockOutRecordLimit) > (stockInRecordLimit - stockInRecordDosSize) ?
(stockInRecordLimit - stockInRecordDosSize) : (stockOutRecordDoSize - stockOutRecordLimit);
stockOutRecordDOS = stockOutRecordDOS.subList(0, stockInRecordLimit + appendSize - 1);
} else if (stockOutRecordDoSize < stockOutRecordLimit && stockInRecordDosSize > stockInRecordLimit) {
int appendSize = (stockInRecordDosSize - stockInRecordLimit) > (stockOutRecordLimit - stockOutRecordDoSize) ?
(stockOutRecordLimit - stockOutRecordDoSize) : (stockInRecordDosSize - stockInRecordLimit);
stockInRecordDOS = stockInRecordDOS.subList(0, stockInRecordLimit + appendSize);
}
}
long stockInRecordDOSTotal = (long) stockInTemp.get("total");
long stockOutRecordDOSTotal = (long) stockOutTemp.get("total");
total = stockInRecordDOSTotal + stockOutRecordDOSTotal;
break;
}
case "stockInOnly": {
stockInTemp = selectStockInRecord(repositoryID, startDate, newEndDate, offset, limit);
total = (long) stockInTemp.get("total");
stockInRecordDOS = (List<StockInDO>) stockInTemp.get("data");
break;
}
case "stockOutOnly": {
stockOutTemp = selectStockOutRecord(repositoryID, startDate, newEndDate, offset, limit);
total = (long) stockOutTemp.get("total");
stockOutRecordDOS = (List<StockOutDO>) stockOutTemp.get("data");
break;
}
case "none": {
break;
}
}
if (stockInRecordDOS != null)
stockInRecordDOS.forEach(stockInDO -> stockRecordDTOS.add(stockInRecordConvertToStockRecordDTO(stockInDO)));
if (stockOutRecordDOS != null)
stockOutRecordDOS.forEach(stockOutDO -> stockRecordDTOS.add(stockOutDoConvertToStockRecordDTO(stockOutDO)));
resultSet.put("data", stockRecordDTOS);
resultSet.put("total", total);
return resultSet;
}
/**
* 查询入库记录
*
* @param repositoryID 入库仓库ID
* @param startDate 入库记录起始日期
* @param endDate 入库记录结束日期
* @param offset 分页偏移值
* @param limit 分页大小
* @return 返回所有符合要求的入库记录
*/
private Map<String, Object> selectStockInRecord(Integer repositoryID, Date startDate, Date endDate, int offset, int limit) throws StockRecordManageServiceException {
Map<String, Object> result = new HashMap<>();
List<StockInDO> stockInRecords;
long stockInTotal = 0;
boolean isPagination = true;
// 检查是否需要分页查询
if (offset < 0 || limit < 0)
isPagination = false;
// 查询记录
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
stockInRecords = stockinMapper.selectByRepositoryIDAndDate(repositoryID, startDate, endDate);
if (stockInRecords != null)
stockInTotal = new PageInfo<>(stockInRecords).getTotal();
else
stockInRecords = new ArrayList<>(10);
} else {
stockInRecords = stockinMapper.selectByRepositoryIDAndDate(repositoryID, startDate, endDate);
if (stockInRecords != null)
stockInTotal = stockInRecords.size();
else
stockInRecords = new ArrayList<>(10);
}
} catch (PersistenceException e) {
throw new StockRecordManageServiceException(e);
}
result.put("data", stockInRecords);
result.put("total", stockInTotal);
return result;
}
/**
* 查询出库记录
*
* @param repositoryID 出库仓库ID
* @param startDate 出库记录起始日期
* @param endDate 出库记录结束日期
* @param offset 分页偏移值
* @param limit 分页大小
* @return 返回所有符合要求的出库记录
*/
private Map<String, Object> selectStockOutRecord(Integer repositoryID, Date startDate, Date endDate, int offset, int limit) throws StockRecordManageServiceException {
Map<String, Object> result = new HashMap<>();
List<StockOutDO> stockOutRecords;
long stockOutRecordTotal = 0;
boolean isPagination = true;
// 检查是否需要分页
if (offset < 0 || limit < 0)
isPagination = false;
// 查询记录
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
stockOutRecords = stockOutMapper.selectByRepositoryIDAndDate(repositoryID, startDate, endDate);
if (stockOutRecords != null)
stockOutRecordTotal = new PageInfo<>(stockOutRecords).getTotal();
else
stockOutRecords = new ArrayList<>(10);
} else {
stockOutRecords = stockOutMapper.selectByRepositoryIDAndDate(repositoryID, startDate, endDate);
if (stockOutRecords != null)
stockOutRecordTotal = stockOutRecords.size();
else
stockOutRecords = new ArrayList<>(10);
}
} catch (PersistenceException e) {
throw new StockRecordManageServiceException(e);
}
result.put("data", stockOutRecords);
result.put("total", stockOutRecordTotal);
return result;
}
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm");
/**
* 将 StockInDO 转换为 StockRecordDTO
*
* @param stockInDO StockInDO 对象
* @return 返回 StockRecordDTO 对象
*/
private StockRecordDTO stockInRecordConvertToStockRecordDTO(StockInDO stockInDO) {
StockRecordDTO stockRecordDTO = new StockRecordDTO();
stockRecordDTO.setRecordID(stockInDO.getId());
stockRecordDTO.setSupplierOrCustomerName(stockInDO.getSupplierName());
stockRecordDTO.setGoodsName(stockInDO.getGoodName());
stockRecordDTO.setNumber(stockInDO.getNumber());
stockRecordDTO.setTime(dateFormat.format(stockInDO.getTime()));
stockRecordDTO.setRepositoryID(stockInDO.getRepositoryID());
stockRecordDTO.setPersonInCharge(stockInDO.getPersonInCharge());
stockRecordDTO.setType("入库");
return stockRecordDTO;
}
/**
* 将 StockOutDO 转换为 StockRecordDTO 对象
*
* @param stockOutDO StockOutDO 对象
* @return 返回 StockRecordDTO 对象
*/
private StockRecordDTO stockOutDoConvertToStockRecordDTO(StockOutDO stockOutDO) {
StockRecordDTO stockRecordDTO = new StockRecordDTO();
stockRecordDTO.setRecordID(stockOutDO.getId());
stockRecordDTO.setSupplierOrCustomerName(stockOutDO.getCustomerName());
stockRecordDTO.setGoodsName(stockOutDO.getCustomerName());
stockRecordDTO.setNumber(stockOutDO.getNumber());
stockRecordDTO.setTime(dateFormat.format(stockOutDO.getTime()));
stockRecordDTO.setRepositoryID(stockOutDO.getRepositoryID());
stockRecordDTO.setPersonInCharge(stockOutDO.getPersonInCharge());
stockRecordDTO.setType("出库");
return stockRecordDTO;
}
/**
* 检查货物ID对应的记录是否存在
*
* @param goodsID 货物ID
* @return 若存在则返回true,否则返回false
*/
private boolean goodsValidate(Integer goodsID) throws StockRecordManageServiceException {
try {
Goods goods = goodsMapper.selectById(goodsID);
return goods != null;
} catch (PersistenceException e) {
throw new StockRecordManageServiceException(e);
}
}
/**
* 检查仓库ID对应的记录是否存在
*
* @param repositoryID 仓库ID
* @return 若存在则返回true,否则返回false
*/
private boolean repositoryValidate(Integer repositoryID) throws StockRecordManageServiceException {
try {
Repository repository = repositoryMapper.selectByID(repositoryID);
return repository != null;
} catch (PersistenceException e) {
throw new StockRecordManageServiceException(e);
}
}
/**
* 检查供应商ID对应的记录是否存在
*
* @param supplierID 供应商ID
* @return 若存在则返回true,否则返回false
*/
private boolean supplierValidate(Integer supplierID) throws StockRecordManageServiceException {
try {
Supplier supplier = supplierMapper.selectById(supplierID);
return supplier != null;
} catch (PersistenceException e) {
throw new StockRecordManageServiceException(e);
}
}
/**
* 检查客户ID对应的记录是否存在
*
* @param cumtomerID 客户ID
* @return 若存在则返回true,否则返回false
*/
private boolean customerValidate(Integer cumtomerID) throws StockRecordManageServiceException {
try {
Customer customer = customerMapper.selectById(cumtomerID);
return customer != null;
} catch (PersistenceException e) {
throw new StockRecordManageServiceException(e);
}
}
}
package com.ken.wms.common.service.Impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ken.wms.common.service.Interface.StockRecordManageService;
import com.ken.wms.common.service.Interface.StorageManageService;
import com.ken.wms.dao.*;
import com.ken.wms.domain.*;
import com.ken.wms.exception.StockRecordManageServiceException;
import com.ken.wms.exception.StorageManageServiceException;
import com.ken.wms.util.aop.UserOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class StockRecordManageServiceImpl implements StockRecordManageService {
@Autowired
private SupplierMapper supplierMapper;
@Autowired
private CustomerMapper customerMapper;
@Autowired
private GoodsMapper goodsMapper;
@Autowired
private RepositoryMapper repositoryMapper;
@Autowired
private StorageManageService storageManageService;
@Autowired
private StockInMapper stockinMapper;
@Autowired
private StockOutMapper stockOutMapper;
/**
* 货物入库操作
*
* @param supplierID 供应商ID
* @param goodsID 货物ID
* @param repositoryID 入库仓库ID
* @param number 入库数量
* @return 返回一个boolean 值,若值为true表示入库成功,否则表示入库失败
*/
@UserOperation(value = "货物入库")
@Override
public boolean stockInOperation(Integer supplierID, Integer goodsID, Integer repositoryID, long number, String personInCharge) throws StockRecordManageServiceException {
// ID对应的记录是否存在
if (!(supplierValidate(supplierID) && goodsValidate(goodsID) && repositoryValidate(repositoryID)))
return false;
if (personInCharge == null)
return false;
// 检查入库数量有效性
if (number < 0)
return false;
try {
// 更新库存记录
boolean isSuccess;
isSuccess = storageManageService.storageIncrease(goodsID, repositoryID, number);
// 保存入库记录
if (isSuccess) {
StockInDO stockInDO = new StockInDO();
stockInDO.setGoodID(goodsID);
stockInDO.setSupplierID(supplierID);
stockInDO.setNumber(number);
stockInDO.setPersonInCharge(personInCharge);
stockInDO.setTime(new Date());
stockInDO.setRepositoryID(repositoryID);
stockinMapper.insert(stockInDO);
}
return isSuccess;
} catch (PersistenceException | StorageManageServiceException e) {
throw new StockRecordManageServiceException(e);
}
}
/**
* 货物出库操作
*
* @param customerID 客户ID
* @param goodsID 货物ID
* @param repositoryID 出库仓库ID
* @param number 出库数量
* @return 返回一个boolean值,若值为true表示出库成功,否则表示出库失败
*/
@UserOperation(value = "货物出库")
@Override
public boolean stockOutOperation(Integer customerID, Integer goodsID, Integer repositoryID, long number, String personInCharge) throws StockRecordManageServiceException {
// 检查ID对应的记录是否存在
if (!(customerValidate(customerID) && goodsValidate(goodsID) && repositoryValidate(repositoryID)))
return false;
// 检查出库数量范围是否有效
if (number < 0)
return false;
try {
// 更新库存信息
boolean isSuccess;
isSuccess = storageManageService.storageDecrease(goodsID, repositoryID, number);
// 保存出库记录
if (isSuccess) {
StockOutDO stockOutDO = new StockOutDO();
stockOutDO.setCustomerID(customerID);
stockOutDO.setGoodID(goodsID);
stockOutDO.setNumber(number);
stockOutDO.setPersonInCharge(personInCharge);
stockOutDO.setRepositoryID(repositoryID);
stockOutDO.setTime(new Date());
stockOutMapper.insert(stockOutDO);
}
return isSuccess;
} catch (PersistenceException | StorageManageServiceException e) {
throw new StockRecordManageServiceException(e);
}
}
/**
* 查询出入库记录
*
* @param repositoryID 仓库ID
* @param endDateStr 查询记录起始日期
* @param startDateStr 查询记录结束日期
* @param searchType 记录查询方式
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectStockRecord(Integer repositoryID, String startDateStr, String endDateStr, String searchType) throws StockRecordManageServiceException {
return selectStockRecord(repositoryID, startDateStr, endDateStr, searchType, -1, -1);
}
/**
* 分页查询出入库记录
*
* @param repositoryID 仓库ID
* @param endDateStr 查询记录起始日期
* @param startDateStr 查询记录结束日期
* @param searchType 记录查询方式
* @param offset 分页偏移值
* @param limit 分页大小
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@SuppressWarnings("unchecked")
@Override
public Map<String, Object> selectStockRecord(Integer repositoryID, String startDateStr, String endDateStr, String searchType, int offset, int limit) throws StockRecordManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
long total = 0;
// 检查传入参数
if (repositoryID == null || searchType == null)
throw new StockRecordManageServiceException("exception");
// 转换 Date 对象
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = null;
Date endDate = null;
Date newEndDate = null;
try {
if (StringUtils.isNotEmpty(startDateStr))
startDate = dateFormat.parse(startDateStr);
if (StringUtils.isNotEmpty(endDateStr))
{
endDate = dateFormat.parse(endDateStr);
newEndDate = new Date(endDate.getTime()+(24*60*60*1000)-1);
}
} catch (ParseException e) {
throw new StockRecordManageServiceException(e);
}
// 根据查询模式执行查询
List<StockRecordDTO> stockRecordDTOS = new ArrayList<>();
Map<String, Object> stockInTemp;
Map<String, Object> stockOutTemp;
List<StockInDO> stockInRecordDOS = null;
List<StockOutDO> stockOutRecordDOS = null;
switch (searchType) {
case "all": {
if (offset < 0 || limit < 0) {
stockInTemp = selectStockInRecord(repositoryID, startDate, endDate, offset, limit);
stockOutTemp = selectStockOutRecord(repositoryID, startDate, endDate, offset, limit);
stockInRecordDOS = (List<StockInDO>) stockInTemp.get("data");
stockOutRecordDOS = (List<StockOutDO>) stockOutTemp.get("data");
} else {
int stockInRecordOffset = offset / 2;
int stockOutRecordOffset = stockInRecordOffset * 2 < offset ? stockInRecordOffset + 1 : stockInRecordOffset;
int stockInRecordLimit = limit / 2;
int stockOutRecordLimit = stockInRecordLimit * 2 < limit ? stockInRecordLimit + 1 : stockInRecordLimit;
stockInTemp = selectStockInRecord(repositoryID, startDate, newEndDate, stockInRecordOffset, limit);
stockOutTemp = selectStockOutRecord(repositoryID, startDate, newEndDate, stockOutRecordOffset, limit);
stockInRecordDOS = (List<StockInDO>) stockInTemp.get("data");
stockOutRecordDOS = (List<StockOutDO>) stockOutTemp.get("data");
int stockInRecordDosSize = stockInRecordDOS.size();
int stockOutRecordDoSize = stockOutRecordDOS.size();
if (stockInRecordDosSize >= stockInRecordLimit && stockOutRecordDoSize >= stockOutRecordLimit) {
stockInRecordDOS = stockInRecordDOS.subList(0, stockInRecordLimit);
stockOutRecordDOS = stockOutRecordDOS.subList(0, stockOutRecordLimit);
} else if (stockInRecordDosSize < stockInRecordLimit && stockOutRecordDoSize > stockOutRecordLimit) {
int appendSize = (stockOutRecordDoSize - stockOutRecordLimit) > (stockInRecordLimit - stockInRecordDosSize) ?
(stockInRecordLimit - stockInRecordDosSize) : (stockOutRecordDoSize - stockOutRecordLimit);
stockOutRecordDOS = stockOutRecordDOS.subList(0, stockInRecordLimit + appendSize - 1);
} else if (stockOutRecordDoSize < stockOutRecordLimit && stockInRecordDosSize > stockInRecordLimit) {
int appendSize = (stockInRecordDosSize - stockInRecordLimit) > (stockOutRecordLimit - stockOutRecordDoSize) ?
(stockOutRecordLimit - stockOutRecordDoSize) : (stockInRecordDosSize - stockInRecordLimit);
stockInRecordDOS = stockInRecordDOS.subList(0, stockInRecordLimit + appendSize);
}
}
long stockInRecordDOSTotal = (long) stockInTemp.get("total");
long stockOutRecordDOSTotal = (long) stockOutTemp.get("total");
total = stockInRecordDOSTotal + stockOutRecordDOSTotal;
break;
}
case "stockInOnly": {
stockInTemp = selectStockInRecord(repositoryID, startDate, newEndDate, offset, limit);
total = (long) stockInTemp.get("total");
stockInRecordDOS = (List<StockInDO>) stockInTemp.get("data");
break;
}
case "stockOutOnly": {
stockOutTemp = selectStockOutRecord(repositoryID, startDate, newEndDate, offset, limit);
total = (long) stockOutTemp.get("total");
stockOutRecordDOS = (List<StockOutDO>) stockOutTemp.get("data");
break;
}
case "none": {
break;
}
}
if (stockInRecordDOS != null)
stockInRecordDOS.forEach(stockInDO -> stockRecordDTOS.add(stockInRecordConvertToStockRecordDTO(stockInDO)));
if (stockOutRecordDOS != null)
stockOutRecordDOS.forEach(stockOutDO -> stockRecordDTOS.add(stockOutDoConvertToStockRecordDTO(stockOutDO)));
resultSet.put("data", stockRecordDTOS);
resultSet.put("total", total);
return resultSet;
}
/**
* 查询入库记录
*
* @param repositoryID 入库仓库ID
* @param startDate 入库记录起始日期
* @param endDate 入库记录结束日期
* @param offset 分页偏移值
* @param limit 分页大小
* @return 返回所有符合要求的入库记录
*/
private Map<String, Object> selectStockInRecord(Integer repositoryID, Date startDate, Date endDate, int offset, int limit) throws StockRecordManageServiceException {
Map<String, Object> result = new HashMap<>();
List<StockInDO> stockInRecords;
long stockInTotal = 0;
boolean isPagination = true;
// 检查是否需要分页查询
if (offset < 0 || limit < 0)
isPagination = false;
// 查询记录
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
stockInRecords = stockinMapper.selectByRepositoryIDAndDate(repositoryID, startDate, endDate);
if (stockInRecords != null)
stockInTotal = new PageInfo<>(stockInRecords).getTotal();
else
stockInRecords = new ArrayList<>(10);
} else {
stockInRecords = stockinMapper.selectByRepositoryIDAndDate(repositoryID, startDate, endDate);
if (stockInRecords != null)
stockInTotal = stockInRecords.size();
else
stockInRecords = new ArrayList<>(10);
}
} catch (PersistenceException e) {
throw new StockRecordManageServiceException(e);
}
result.put("data", stockInRecords);
result.put("total", stockInTotal);
return result;
}
/**
* 查询出库记录
*
* @param repositoryID 出库仓库ID
* @param startDate 出库记录起始日期
* @param endDate 出库记录结束日期
* @param offset 分页偏移值
* @param limit 分页大小
* @return 返回所有符合要求的出库记录
*/
private Map<String, Object> selectStockOutRecord(Integer repositoryID, Date startDate, Date endDate, int offset, int limit) throws StockRecordManageServiceException {
Map<String, Object> result = new HashMap<>();
List<StockOutDO> stockOutRecords;
long stockOutRecordTotal = 0;
boolean isPagination = true;
// 检查是否需要分页
if (offset < 0 || limit < 0)
isPagination = false;
// 查询记录
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
stockOutRecords = stockOutMapper.selectByRepositoryIDAndDate(repositoryID, startDate, endDate);
if (stockOutRecords != null)
stockOutRecordTotal = new PageInfo<>(stockOutRecords).getTotal();
else
stockOutRecords = new ArrayList<>(10);
} else {
stockOutRecords = stockOutMapper.selectByRepositoryIDAndDate(repositoryID, startDate, endDate);
if (stockOutRecords != null)
stockOutRecordTotal = stockOutRecords.size();
else
stockOutRecords = new ArrayList<>(10);
}
} catch (PersistenceException e) {
throw new StockRecordManageServiceException(e);
}
result.put("data", stockOutRecords);
result.put("total", stockOutRecordTotal);
return result;
}
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm");
/**
* 将 StockInDO 转换为 StockRecordDTO
*
* @param stockInDO StockInDO 对象
* @return 返回 StockRecordDTO 对象
*/
private StockRecordDTO stockInRecordConvertToStockRecordDTO(StockInDO stockInDO) {
StockRecordDTO stockRecordDTO = new StockRecordDTO();
stockRecordDTO.setRecordID(stockInDO.getId());
stockRecordDTO.setSupplierOrCustomerName(stockInDO.getSupplierName());
stockRecordDTO.setGoodsName(stockInDO.getGoodName());
stockRecordDTO.setNumber(stockInDO.getNumber());
stockRecordDTO.setTime(dateFormat.format(stockInDO.getTime()));
stockRecordDTO.setRepositoryID(stockInDO.getRepositoryID());
stockRecordDTO.setPersonInCharge(stockInDO.getPersonInCharge());
stockRecordDTO.setType("入库");
return stockRecordDTO;
}
/**
* 将 StockOutDO 转换为 StockRecordDTO 对象
*
* @param stockOutDO StockOutDO 对象
* @return 返回 StockRecordDTO 对象
*/
private StockRecordDTO stockOutDoConvertToStockRecordDTO(StockOutDO stockOutDO) {
StockRecordDTO stockRecordDTO = new StockRecordDTO();
stockRecordDTO.setRecordID(stockOutDO.getId());
stockRecordDTO.setSupplierOrCustomerName(stockOutDO.getCustomerName());
stockRecordDTO.setGoodsName(stockOutDO.getCustomerName());
stockRecordDTO.setNumber(stockOutDO.getNumber());
stockRecordDTO.setTime(dateFormat.format(stockOutDO.getTime()));
stockRecordDTO.setRepositoryID(stockOutDO.getRepositoryID());
stockRecordDTO.setPersonInCharge(stockOutDO.getPersonInCharge());
stockRecordDTO.setType("出库");
return stockRecordDTO;
}
/**
* 检查货物ID对应的记录是否存在
*
* @param goodsID 货物ID
* @return 若存在则返回true,否则返回false
*/
private boolean goodsValidate(Integer goodsID) throws StockRecordManageServiceException {
try {
Goods goods = goodsMapper.selectById(goodsID);
return goods != null;
} catch (PersistenceException e) {
throw new StockRecordManageServiceException(e);
}
}
/**
* 检查仓库ID对应的记录是否存在
*
* @param repositoryID 仓库ID
* @return 若存在则返回true,否则返回false
*/
private boolean repositoryValidate(Integer repositoryID) throws StockRecordManageServiceException {
try {
Repository repository = repositoryMapper.selectByID(repositoryID);
return repository != null;
} catch (PersistenceException e) {
throw new StockRecordManageServiceException(e);
}
}
/**
* 检查供应商ID对应的记录是否存在
*
* @param supplierID 供应商ID
* @return 若存在则返回true,否则返回false
*/
private boolean supplierValidate(Integer supplierID) throws StockRecordManageServiceException {
try {
Supplier supplier = supplierMapper.selectById(supplierID);
return supplier != null;
} catch (PersistenceException e) {
throw new StockRecordManageServiceException(e);
}
}
/**
* 检查客户ID对应的记录是否存在
*
* @param cumtomerID 客户ID
* @return 若存在则返回true,否则返回false
*/
private boolean customerValidate(Integer cumtomerID) throws StockRecordManageServiceException {
try {
Customer customer = customerMapper.selectById(cumtomerID);
return customer != null;
} catch (PersistenceException e) {
throw new StockRecordManageServiceException(e);
}
}
}
package com.ken.wms.common.service.Impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ken.wms.common.service.Interface.StorageManageService;
import com.ken.wms.common.util.ExcelUtil;
import com.ken.wms.dao.GoodsMapper;
import com.ken.wms.dao.RepositoryMapper;
import com.ken.wms.dao.StorageMapper;
import com.ken.wms.domain.Goods;
import com.ken.wms.domain.Repository;
import com.ken.wms.domain.Storage;
import com.ken.wms.exception.StorageManageServiceException;
import com.ken.wms.util.aop.UserOperation;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 库存信息管理 service 实现类
*
* @author Ken /Yeguang / Genius Team
*/
@Service
public class StorageManageServiceImpl implements StorageManageService {
@Autowired
private StorageMapper storageMapper;
@Autowired
private GoodsMapper goodsMapper;
@Autowired
private RepositoryMapper repositoryMapper;
@Autowired
private ExcelUtil excelUtil;
/**
* 返回所有的库存记录
*
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectAll(Integer repository) throws StorageManageServiceException {
return selectAll(repository, -1, -1);
}
/**
* 分页返回所有的库存记录
*
* @param offset 分页偏移值
* @param limit 分页大小
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectAll(Integer repositoryID, int offset, int limit) throws StorageManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<Storage> storageList;
long total = 0;
boolean isPagination = true;
// validate
if (offset < 0 || limit < 0)
isPagination = false;
// query
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
storageList = storageMapper.selectAllAndRepositoryID(repositoryID);
if (storageList != null) {
PageInfo<Storage> pageInfo = new PageInfo<>(storageList);
total = pageInfo.getTotal();
} else
storageList = new ArrayList<>();
} else {
storageList = storageMapper.selectAllAndRepositoryID(repositoryID);
if (storageList != null)
total = storageList.size();
else
storageList = new ArrayList<>();
}
} catch (PersistenceException e) {
throw new StorageManageServiceException(e);
}
resultSet.put("data", storageList);
resultSet.put("total", total);
return resultSet;
}
/**
* 返回指定货物ID的库存记录
*
* @param goodsID 指定的货物ID
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByGoodsID(Integer goodsID, Integer repository) throws StorageManageServiceException {
return selectByGoodsID(goodsID, repository, -1, -1);
}
/**
* 分页返回指定的货物库存记录
*
* @param goodsID 指定的货物ID
* @param offset 分页偏移值
* @param limit 分页大小
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByGoodsID(Integer goodsID, Integer repositoryID, int offset, int limit) throws StorageManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<Storage> storageList;
long total = 0;
boolean isPagination = true;
// validate
if (offset < 0 || limit < 0)
isPagination = false;
// query
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
storageList = storageMapper.selectByGoodsIDAndRepositoryID(goodsID, repositoryID);
if (storageList != null) {
PageInfo<Storage> pageInfo = new PageInfo<>(storageList);
total = pageInfo.getTotal();
} else
storageList = new ArrayList<>();
} else {
storageList = storageMapper.selectByGoodsIDAndRepositoryID(goodsID, repositoryID);
if (storageList != null)
total = storageList.size();
else
storageList = new ArrayList<>();
}
} catch (PersistenceException e) {
throw new StorageManageServiceException(e);
}
resultSet.put("data", storageList);
resultSet.put("total", total);
return resultSet;
}
/**
* 返回指定货物名称的库存记录
*
* @param goodsName 货物名称
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByGoodsName(String goodsName, Integer repository) throws StorageManageServiceException {
return selectByGoodsName(goodsName, repository, -1, -1);
}
/**
* 分页返回指定货物名称的库存记录
*
* @param goodsName 货物名称
* @param offset 分页偏移值
* @param limit 分页大小
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByGoodsName(String goodsName, Integer repositoryID, int offset, int limit) throws StorageManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<Storage> storageList;
long total = 0;
boolean isPagination = true;
// validate
if (offset < 0 || limit < 0)
isPagination = false;
// query
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
storageList = storageMapper.selectByGoodsNameAndRepositoryID(goodsName, repositoryID);
if (storageList != null) {
PageInfo<Storage> pageInfo = new PageInfo<>(storageList);
total = pageInfo.getTotal();
} else
storageList = new ArrayList<>();
} else {
storageList = storageMapper.selectByGoodsNameAndRepositoryID(goodsName, repositoryID);
if (storageList != null)
total = storageList.size();
else
storageList = new ArrayList<>();
}
} catch (PersistenceException e) {
throw new StorageManageServiceException(e);
}
resultSet.put("data", storageList);
resultSet.put("total", total);
return resultSet;
}
/**
* 返回指定货物类型的库存记录
*
* @param goodsType 指定的货物类型
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByGoodsType(String goodsType, Integer repositoryID) throws StorageManageServiceException {
return selectByGoodsType(goodsType, repositoryID, -1, -1);
}
/**
* 分页返回指定货物类型的库存记录
*
* @param goodsType 指定的货物类型
* @param offset 分页偏移值
* @param limit 分页大小
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByGoodsType(String goodsType, Integer repositoryID, int offset, int limit) throws StorageManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<Storage> storageList;
long total = 0;
boolean isPaginatin = true;
// validate
if (offset < 0 || limit < 0)
isPaginatin = false;
// query
try {
if (isPaginatin) {
PageHelper.offsetPage(offset, limit);
storageList = storageMapper.selectByGoodsTypeAndRepositoryID(goodsType, repositoryID);
if (storageList != null) {
PageInfo<Storage> pageInfo = new PageInfo<>(storageList);
total = pageInfo.getTotal();
} else
storageList = new ArrayList<>();
} else {
storageList = storageMapper.selectByGoodsTypeAndRepositoryID(goodsType, repositoryID);
if (storageList != null)
total = storageList.size();
else
storageList = new ArrayList<>();
}
} catch (PersistenceException e) {
throw new StorageManageServiceException(e);
}
resultSet.put("data", storageList);
resultSet.put("total", total);
return resultSet;
}
/**
* 添加一条库存记录
*
* @param goodsID 指定的货物ID
* @param repositoryID 指定的仓库ID
* @param number 库存数量
* @return 返回一个boolean值,值为true代表更新成功,否则代表失败
*/
@UserOperation(value = "添加库存记录")
@Override
public boolean addNewStorage(Integer goodsID, Integer repositoryID, long number) throws StorageManageServiceException {
try {
boolean isAvailable = true;
// validate
Goods goods = goodsMapper.selectById(goodsID);
Repository repository = repositoryMapper.selectByID(repositoryID);
if (goods == null)
isAvailable = false;
if (repository == null)
isAvailable = false;
if (number < 0)
isAvailable = false;
List<Storage> storageList = storageMapper.selectByGoodsIDAndRepositoryID(goodsID, repositoryID);
if (!(storageList != null && storageList.isEmpty()))
isAvailable = false;
if (isAvailable) {
// insert
Storage storage = new Storage();
storage.setGoodsID(goodsID);
storage.setRepositoryID(repositoryID);
storage.setNumber(number);
storageMapper.insert(storage);
}
return isAvailable;
} catch (PersistenceException e) {
throw new StorageManageServiceException(e);
}
}
/**
* 更新一条库存记录
*
* @param goodsID 指定的货物ID
* @param repositoryID 指定的仓库ID
* @param number 更新的库存数量
* @return 返回一个boolean值,值为true代表更新成功,否则代表失败
*/
@UserOperation(value = "修改库存记录")
@Override
public boolean updateStorage(Integer goodsID, Integer repositoryID, long number) throws StorageManageServiceException {
try {
boolean isUpdate = false;
// validate
List<Storage> storageList = storageMapper.selectByGoodsIDAndRepositoryID(goodsID, repositoryID);
if (storageList != null && !storageList.isEmpty()) {
if (number >= 0) {
// update
Storage storage = storageList.get(0);
storage.setNumber(number);
storageMapper.update(storage);
isUpdate = true;
}
}
return isUpdate;
} catch (PersistenceException e) {
throw new StorageManageServiceException(e);
}
}
/**
* 删除一条库存记录
* 货物ID与仓库ID可唯一确定一条库存记录
*
* @param goodsID 指定的货物ID
* @param repositoryID 指定的仓库ID
* @return 返回一个boolean值,值为true代表更新成功,否则代表失败
*/
@UserOperation(value = "删除库存记录")
@Override
public boolean deleteStorage(Integer goodsID, Integer repositoryID) throws StorageManageServiceException {
try {
boolean isDelete = false;
// validate
List<Storage> storageList = storageMapper.selectByGoodsIDAndRepositoryID(goodsID, repositoryID);
if (storageList != null && !storageList.isEmpty()) {
// delete
storageMapper.deleteByRepositoryIDAndGoodsID(goodsID, repositoryID);
isDelete = true;
}
return isDelete;
} catch (PersistenceException e) {
throw new StorageManageServiceException(e);
}
}
/**
* 导入库存记录
*
* @param file 保存有的库存记录的文件
* @return 返回一个Map,其中:key为total代表导入的总记录数,key为available代表有效导入的记录数
*/
@UserOperation(value = "导入库存记录")
@Override
public Map<String, Object> importStorage(MultipartFile file) throws StorageManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
int total = 0;
int available = 0;
// 从文件中读取
List<Object> storageList = excelUtil.excelReader(Storage.class, file);
if (storageList != null) {
total = storageList.size();
try {
Storage storage;
boolean isAvailable;
List<Storage> availableList = new ArrayList<>();
Goods goods;
Repository repository;
for (Object object : storageList) {
isAvailable = true;
storage = (Storage) object;
// validate
goods = goodsMapper.selectById(storage.getGoodsID());
repository = repositoryMapper.selectByID(storage.getRepositoryID());
if (goods == null)
isAvailable = false;
if (repository == null)
isAvailable = false;
if (storage.getNumber() < 0)
isAvailable = false;
List<Storage> temp = storageMapper.selectByGoodsIDAndRepositoryID(storage.getGoodsID(), storage.getRepositoryID());
if (!(temp != null && temp.isEmpty()))
isAvailable = false;
if (isAvailable) {
availableList.add(storage);
}
}
// 保存到数据库
available = availableList.size();
System.out.println(available);
if (available > 0)
storageMapper.insertBatch(availableList);
} catch (PersistenceException e) {
throw new StorageManageServiceException(e);
}
}
resultSet.put("total", total);
resultSet.put("available", available);
return resultSet;
}
/**
* 导出库存记录
*
* @param storageList 保存有库存记录的List
* @return excel 文件
*/
@UserOperation(value = "导出库存记录")
@Override
public File exportStorage(List<Storage> storageList) {
if (storageList == null)
return null;
return excelUtil.excelWriter(Storage.class, storageList);
}
/**
* 为指定的货物库存记录增加指定数目
*
* @param goodsID 货物ID
* @param repositoryID 仓库ID
* @param number 增加的数量
* @return 返回一个 boolean 值,若值为true表示数目增加成功,否则表示增加失败
*/
@Override
public boolean storageIncrease(Integer goodsID, Integer repositoryID, long number) throws StorageManageServiceException {
// 检查货物库存增加数目的有效性
if (number < 0)
return false;
synchronized (this) {
// 检查对应的库存记录是否存在
Storage storage = getStorage(goodsID, repositoryID);
if (storage != null) {
long newStorage = storage.getNumber() + number;
updateStorage(goodsID, repositoryID, newStorage);
} else {
addNewStorage(goodsID, repositoryID, number);
}
}
return true;
}
/**
* 为指定的货物库存记录减少指定的数目
*
* @param goodsID 货物ID
* @param repositoryID 仓库ID
* @param number 减少的数量
* @return 返回一个 boolean 值,若值为 true 表示数目减少成功,否则表示减少失败
*/
@Override
public boolean storageDecrease(Integer goodsID, Integer repositoryID, long number) throws StorageManageServiceException {
synchronized (this) {
// 检查对应的库存记录是否存在
Storage storage = getStorage(goodsID, repositoryID);
if (null != storage) {
// 检查库存减少数目的范围是否合理
if (number < 0 || storage.getNumber() < number)
return false;
long newStorage = storage.getNumber() - number;
updateStorage(goodsID, repositoryID, newStorage);
return true;
} else
return false;
}
}
/**
* 获取指定货物ID,仓库ID对应的库存记录
*
* @param goodsID 货物ID
* @param repositoryID 仓库ID
* @return 若存在则返回对应的记录,否则返回null
*/
private Storage getStorage(Integer goodsID, Integer repositoryID) {
Storage storage = null;
List<Storage> storageList = storageMapper.selectByGoodsIDAndRepositoryID(goodsID, repositoryID);
if (!storageList.isEmpty())
storage = storageList.get(0);
return storage;
}
}
package com.ken.wms.common.service.Impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ken.wms.common.service.Interface.SupplierManageService;
import com.ken.wms.common.util.ExcelUtil;
import com.ken.wms.dao.StockInMapper;
import com.ken.wms.dao.SupplierMapper;
import com.ken.wms.domain.StockInDO;
import com.ken.wms.domain.Supplier;
import com.ken.wms.exception.SupplierManageServiceException;
import com.ken.wms.util.aop.UserOperation;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 供应商信息管理 service 实现类
*
* @author Ken/Yeguang / Genius Team
*/
@Service
public class SupplierManageServiceImpl implements SupplierManageService {
@Autowired
private SupplierMapper supplierMapper;
@Autowired
private StockInMapper stockInMapper;
@Autowired
private ExcelUtil excelUtil;
/**
* 返回指定supplierID 的供应商记录
*
* @param supplierId 供应商ID
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectById(Integer supplierId) throws SupplierManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<Supplier> suppliers = new ArrayList<>();
long total = 0;
// 查询
Supplier supplier;
try {
supplier = supplierMapper.selectById(supplierId);
} catch (PersistenceException e) {
throw new SupplierManageServiceException(e);
}
if (supplier != null) {
suppliers.add(supplier);
total = 1;
}
resultSet.put("data", suppliers);
resultSet.put("total", total);
return resultSet;
}
/**
* 返回指定 supplierName 的供应商记录 支持查询分页以及模糊查询
*
* @param offset 分页的偏移值
* @param limit 分页德大小
* @param supplierName 供应商德名称
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByName(int offset, int limit, String supplierName) throws SupplierManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<Supplier> suppliers;
long total = 0;
boolean isPagination = true;
// validate
if (offset < 0 || limit < 0)
isPagination = false;
// query
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
suppliers = supplierMapper.selectApproximateByName(supplierName);
if (suppliers != null) {
PageInfo<Supplier> pageInfo = new PageInfo<>(suppliers);
total = pageInfo.getTotal();
} else
suppliers = new ArrayList<>();
} else {
suppliers = supplierMapper.selectApproximateByName(supplierName);
if (suppliers != null)
total = suppliers.size();
else
suppliers = new ArrayList<>();
}
} catch (PersistenceException e) {
throw new SupplierManageServiceException(e);
}
resultSet.put("data", suppliers);
resultSet.put("total", total);
return resultSet;
}
/**
* 返回指定 supplierName 的供应商记录 支持模糊查询
*
* @param supplierName supplierName 供应商德名称
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectByName(String supplierName) throws SupplierManageServiceException {
return selectByName(-1, -1, supplierName);
}
/**
* 分页查询供应商记录
*
* @param offset 分页的偏移值
* @param limit 分页的大小
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectAll(int offset, int limit) throws SupplierManageServiceException {
// 初始化结果集
Map<String, Object> resultSet = new HashMap<>();
List<Supplier> suppliers;
long total = 0;
boolean isPagination = true;
// validate
if (offset < 0 || limit < 0)
isPagination = false;
// query
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
suppliers = supplierMapper.selectAll();
if (suppliers != null) {
PageInfo<Supplier> pageInfo = new PageInfo<>(suppliers);
total = pageInfo.getTotal();
} else
suppliers = new ArrayList<>();
} else {
suppliers = supplierMapper.selectAll();
if (suppliers != null)
total = suppliers.size();
else
suppliers = new ArrayList<>();
}
} catch (PersistenceException e) {
throw new SupplierManageServiceException(e);
}
resultSet.put("data", suppliers);
resultSet.put("total", total);
return resultSet;
}
/**
* 查询所有的供应商记录
*
* @return 结果的一个Map,其中: key为 data 的代表记录数据;key 为 total 代表结果记录的数量
*/
@Override
public Map<String, Object> selectAll() throws SupplierManageServiceException {
return selectAll(-1, -1);
}
/**
* 检验供应商信息是否满足要求
*
* @param supplier 供应商信息
* @return 若供应商上的属性均有满足要求则返回true,否则返回false
*/
private boolean supplierCheck(Supplier supplier) {
// 检查是否已填写属性
return supplier.getName() != null && supplier.getPersonInCharge() != null
&& supplier.getTel() != null && supplier.getEmail() != null && supplier.getAddress() != null;
}
/**
* 添加供应商记录
*
* @param supplier 供应商信息
* @return 返回添加结果
*/
@UserOperation(value = "添加供应商信息")
@Override
public boolean addSupplier(Supplier supplier) throws SupplierManageServiceException {
// 插入新的记录
if (supplier != null) {
try {
if (supplierCheck(supplier)) {
// 检查重名
if (null == supplierMapper.selectBuName(supplier.getName())) {
supplierMapper.insert(supplier);
if (supplier.getId() != null) {
return true;
}
}
}
} catch (PersistenceException e) {
throw new SupplierManageServiceException(e);
}
}
return false;
}
/**
* 更新供应商记录
*
* @param supplier 供应商信息
* @return 返回一个boolean值,值为true代表更新成功,否则代表失败
*/
@UserOperation(value = "修改供应商信息")
@Override
public boolean updateSupplier(Supplier supplier) throws SupplierManageServiceException {
// 更新记录
if (supplier != null) {
// 检验
try {
if (supplierCheck(supplier)) {
if (supplier.getId() != null) {
// 检查重名
Supplier supplierFromDB = supplierMapper.selectBuName(supplier.getName());
if (supplierFromDB == null || supplier.getId().equals(supplierFromDB.getId())) {
supplierMapper.update(supplier);
return true;
}
}
}
} catch (PersistenceException e) {
throw new SupplierManageServiceException(e);
}
}
return false;
}
/**
* 删除供应商记录
*
* @param supplierId 供应商ID
* @return 返回一个boolean值,值为true代表更新成功,否则代表失败
*/
@UserOperation(value = "删除供应商信息")
@Override
public boolean deleteSupplier(Integer supplierId) {
// 查询该供应商是否有入库记录
List<StockInDO> records = stockInMapper.selectBySupplierId(supplierId);
if (records == null || records.size() > 0)
return false;
// 删除该条供应商记录
supplierMapper.deleteById(supplierId);
return true;
}
/**
* 从文件中导入供应商信息
*
* @param file 导入信息的文件
* @return 返回一个Map,其中:key为total代表导入的总记录数,key为available代表有效导入的记录数
*/
@UserOperation(value = "导入供应商信息")
@Override
public Map<String, Object> importSupplier(MultipartFile file) {
// 初始化结果集
Map<String, Object> result = new HashMap<>();
int total = 0;
int available = 0;
// 从 Excel 文件中读取
List<Object> suppliers = excelUtil.excelReader(Supplier.class, file);
if (suppliers != null) {
total = suppliers.size();
// 验证每一条供应商记录
Supplier supplier;
List<Supplier> availableList = new ArrayList<>();
for (Object object : suppliers) {
supplier = (Supplier) object;
if (supplierCheck(supplier)) {
// 检查重名
if (null == supplierMapper.selectBuName(supplier.getName()))
availableList.add(supplier);
}
}
// 保存到数据库
available = availableList.size();
if (available > 0) {
supplierMapper.insertBatch(availableList);
}
}
result.put("total", total);
result.put("available", available);
return result;
}
/**
* 导出供应商信息到文件中
*
* @param suppliers 包含若干条 Supplier 信息的 List
* @return excel 文件
*/
@UserOperation(value = "导出供应商信息")
@Override
public File exportSupplier(List<Supplier> suppliers) {
if (suppliers == null)
return null;
return excelUtil.excelWriter(Supplier.class, suppliers);
}
}
package com.ken.wms.common.service.Impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ken.wms.common.service.Interface.SystemLogService;
import com.ken.wms.dao.AccessRecordMapper;
import com.ken.wms.dao.UserOperationRecordMapper;
import com.ken.wms.domain.AccessRecordDO;
import com.ken.wms.domain.AccessRecordDTO;
import com.ken.wms.domain.UserOperationRecordDO;
import com.ken.wms.domain.UserOperationRecordDTO;
import com.ken.wms.exception.SystemLogServiceException;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 系统操作日志 Service 实现
*
* @author Ken / Yeguang / Genius Team
* @since
*/
@Service
public class SystemLogServiceImpl implements SystemLogService {
@Autowired
private AccessRecordMapper accessRecordMapper;
@Autowired
private UserOperationRecordMapper userOperationRecordMapper;
/**
* 插入用户登入登出记录
*
* @param userID 用户ID
* @param userName 用户名
* @param accessIP 登陆IP
* @param accessType 记录类型
*/
@Override
public void insertAccessRecord(Integer userID, String userName, String accessIP, String accessType) throws SystemLogServiceException {
// 创建 AccessRecordDO 对象
AccessRecordDO accessRecordDO = new AccessRecordDO();
accessRecordDO.setUserID(userID);
accessRecordDO.setUserName(userName);
accessRecordDO.setAccessTime(new Date());
accessRecordDO.setAccessIP(accessIP);
accessRecordDO.setAccessType(accessType);
// 持久化 AccessRecordDO 对象到数据库
try {
accessRecordMapper.insertAccessRecord(accessRecordDO);
} catch (PersistenceException e) {
throw new SystemLogServiceException(e, "Fail to persist AccessRecordDO Object");
}
}
/**
* 选择指定用户ID、记录类型或日期范围的登入登出记录
*
* @param userID 用户ID
* @param accessType 记录类型
* @param startDateStr 记录起始日期
* @param endDateStr 记录结束日期
* @return 返回一个Map, 其中键值为 data 的值为所有符合条件的记录, 而键值为 total 的值为符合条件的记录总条数
*/
@Override
public Map<String, Object> selectAccessRecord(Integer userID, String accessType, String startDateStr, String endDateStr) throws SystemLogServiceException {
return selectAccessRecord(userID, accessType, startDateStr, endDateStr, -1, -1);
}
/**
* 分页查询指定用户ID、记录类型或日期范围的登入登出记录
*
* @param userID 用户ID
* @param accessType 记录类型
* @param startDateStr 记录起始日期
* @param endDateStr 记录结束日期
* @param offset 分页偏移值
* @param limit 分页大小
* @return 返回一个Map, 其中键值为 data 的值为所有符合条件的记录, 而键值为 total 的值为符合条件的记录总条数
*/
@Override
public Map<String, Object> selectAccessRecord(Integer userID, String accessType, String startDateStr, String endDateStr, int offset, int limit) throws SystemLogServiceException {
// 准备结果集
Map<String, Object> resultSet = new HashMap<>();
List<AccessRecordDTO> accessRecordDTOS = new ArrayList<>();
long total = 0;
boolean isPagination = true;
// 检查是否需要分页查询
if (offset < 0 || limit < 0)
isPagination = false;
// 转换 Date 对象
Date startDate = null;
Date endDate = null;
Date newEndDate = null;
try {
if (StringUtils.isNotEmpty(startDateStr))
startDate = dateFormatSimple.parse(startDateStr);
if (StringUtils.isNotEmpty(endDateStr))
{
endDate = dateFormatSimple.parse(endDateStr);
newEndDate = new Date(endDate.getTime()+(24*60*60*1000)-1);
}
} catch (ParseException e) {
throw new SystemLogServiceException(e, "Fail to convert string to Date Object");
}
// 转换 accessType
switch (accessType) {
case "loginOnly":
accessType = SystemLogService.ACCESS_TYPE_LOGIN;
break;
case "logoutOnly":
accessType = SystemLogService.ACCESS_TYPE_LOGOUT;
break;
default:
accessType = "all";
break;
}
// 执行查询操作
List<AccessRecordDO> accessRecordDOS;
try {
if (isPagination) {
PageHelper.offsetPage(offset, limit);
accessRecordDOS = accessRecordMapper.selectAccessRecords(userID, accessType, startDate, newEndDate);
if (accessRecordDOS != null) {
accessRecordDOS.forEach(accessRecordDO -> accessRecordDTOS.add(convertAccessRecordDOToAccessRecordDTO(accessRecordDO)));
total = new PageInfo<>(accessRecordDOS).getTotal();
}
} else {
accessRecordDOS = accessRecordMapper.selectAccessRecords(userID, accessType, startDate, endDate);
if (accessRecordDOS != null) {
accessRecordDOS.forEach(accessRecordDO -> accessRecordDTOS.add(convertAccessRecordDOToAccessRecordDTO(accessRecordDO)));
total = accessRecordDOS.size();
}
}
} catch (PersistenceException e) {
throw new SystemLogServiceException(e);
}
resultSet.put("data", accessRecordDTOS);
resultSet.put("total", total);
return resultSet;
}
/**
* 插入用户操作记录
*
* @param userID 执行操作的用户ID
* @param userName 执行操作的用户名
* @param operationName 操作的名称
* @param operationResult 操作的记过
*/
@Override
public void insertUserOperationRecord(Integer userID, String userName, String operationName, String operationResult) throws SystemLogServiceException {
// 创建 UserOperationRecordDO 对象
UserOperationRecordDO userOperationRecordDO = new UserOperationRecordDO();
userOperationRecordDO.setUserID(userID);
userOperationRecordDO.setUserName(userName);
userOperationRecordDO.setOperationName(operationName);
userOperationRecordDO.setOperationResult(operationResult);
userOperationRecordDO.setOperationTime(new Date());
// 将数据持久化到数据库
try {
userOperationRecordMapper.insertUserOperationRecord(userOperationRecordDO);
} catch (PersistenceException e) {
throw new SystemLogServiceException(e, "Fail to persist usrOperationRecordDo Object");
}
}
/**
* 查询指定用户ID或日期范围的用户操作记录
*
* @param userID 用户ID
* @param startDateStr 记录的起始日期
* @param endDateStr 记录的结束日期
* @return 返回一个Map, 其中键值为 data 的值为所有符合条件的记录, 而键值为 total 的值为符合条件的记录总条数
*/
@Override
public Map<String, Object> selectUserOperationRecord(Integer userID, String startDateStr, String endDateStr) throws SystemLogServiceException {
return selectUserOperationRecord(userID, startDateStr, endDateStr, -1, -1);
}
/**
* 分页查询指定用户ID或日期范围的用户操作记录
*
* @param userID 用户ID
* @param startDateStr 记录的起始日期
* @param endDateStr 记录的结束日期
* @param offset 分页的偏移值
* @param limit 分页的大小
* @return 返回一个Map, 其中键值为 data 的值为所有符合条件的记录, 而键值为 total 的值为符合条件的记录总条数
*/
@Override
public Map<String, Object> selectUserOperationRecord(Integer userID, String startDateStr, String endDateStr, int offset, int limit) throws SystemLogServiceException {
// 准备结果集
Map<String, Object> resultSet = new HashMap<>();
List<UserOperationRecordDTO> userOperationRecordDTOS = new ArrayList<>();
long total = 0;
boolean isPaginarion = true;
// 检查是否需要分页
if (offset < 0 && limit < 0)
isPaginarion = false;
// Date 转换
Date startDate = null;
Date endDate = null;
Date newEndDate = null;
try {
if (StringUtils.isNotEmpty(startDateStr))
startDate = dateFormatSimple.parse(startDateStr);
if (StringUtils.isNotEmpty(endDateStr))
{
endDate = dateFormatSimple.parse(endDateStr);
newEndDate = new Date(endDate.getTime()+(24*60*60*1000)-1);
}
} catch (ParseException e) {
throw new SystemLogServiceException(e, "Fail to convert String format date to Date Object");
}
// 执行查询操作
List<UserOperationRecordDO> userOperationRecordDOS;
try {
if (isPaginarion) {
PageHelper.offsetPage(offset, limit);
userOperationRecordDOS = userOperationRecordMapper.selectUserOperationRecord(userID, startDate, newEndDate);
if (userOperationRecordDOS != null) {
userOperationRecordDOS.forEach(userOperationRecordDO -> userOperationRecordDTOS.add(convertUserOperationRecordDOToUserOperationRecordDTO(userOperationRecordDO)));
total = new PageInfo<>(userOperationRecordDOS).getTotal();
}
} else {
userOperationRecordDOS = userOperationRecordMapper.selectUserOperationRecord(userID, startDate, endDate);
if (userOperationRecordDOS != null)
userOperationRecordDOS.forEach(userOperationRecordDO -> userOperationRecordDTOS.add(convertUserOperationRecordDOToUserOperationRecordDTO(userOperationRecordDO)));
}
} catch (PersistenceException e) {
throw new SystemLogServiceException(e);
}
resultSet.put("data", userOperationRecordDTOS);
resultSet.put("total", total);
return resultSet;
}
/**
* Date 具体格式
*/
private DateFormat dateFormatDetail = new SimpleDateFormat("yyyy-MM-dd hh:mm");
private DateFormat dateFormatSimple = new SimpleDateFormat("yyyy-MM-dd");
/**
* 将 AccessRecordDO 对象转换为 AccessRecordDTO 对象
*
* @param accessRecordDO AccessRecordDO 对象
* @return 返回 AccessRecordDTO 对象
*/
private AccessRecordDTO convertAccessRecordDOToAccessRecordDTO(AccessRecordDO accessRecordDO) {
AccessRecordDTO accessRecordDTO = new AccessRecordDTO();
accessRecordDTO.setId(accessRecordDO.getId());
accessRecordDTO.setUserID(accessRecordDO.getUserID());
accessRecordDTO.setUserName(accessRecordDO.getUserName());
accessRecordDTO.setAccessIP(accessRecordDO.getAccessIP());
accessRecordDTO.setAccessType(accessRecordDO.getAccessType().equals(SystemLogService.ACCESS_TYPE_LOGIN) ? "登入" : "登出");
accessRecordDTO.setAccessTime(dateFormatDetail.format(accessRecordDO.getAccessTime()));
return accessRecordDTO;
}
/**
* 将 UserOperationRecordDO 对象转换为 UserOperationRecordDTO 对象
*
* @param userOperationRecordDO UserOperationRecordDO 对象
* @return 返回 UserOperationRecordDTO 对象
*/
private UserOperationRecordDTO convertUserOperationRecordDOToUserOperationRecordDTO(UserOperationRecordDO userOperationRecordDO) {
UserOperationRecordDTO userOperationRecordDTO = new UserOperationRecordDTO();
userOperationRecordDTO.setId(userOperationRecordDO.getId());
userOperationRecordDTO.setUserID(userOperationRecordDO.getUserID());
userOperationRecordDTO.setUserName(userOperationRecordDO.getUserName());
userOperationRecordDTO.setOperationName(userOperationRecordDO.getOperationName());
userOperationRecordDTO.setOperationResult(userOperationRecordDO.getOperationResult());
userOperationRecordDTO.setOperationTime(dateFormatDetail.format(userOperationRecordDO.getOperationTime()));
return userOperationRecordDTO;
}
}