Skip to content

Commit

Permalink
完成实时库存查看
Browse files Browse the repository at this point in the history
  • Loading branch information
reedmi committed Mar 17, 2015
1 parent f759ed4 commit da1c930
Show file tree
Hide file tree
Showing 19 changed files with 287 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.originspark.drp.models.User;
import com.originspark.drp.service.projects.inventories.InventoryService;
import com.originspark.drp.service.projects.invoices.StockInInvoiceService;
import com.originspark.drp.service.projects.invoices.StockOutInvoiceService;
import com.originspark.drp.service.resources.VendorService;
Expand Down Expand Up @@ -40,6 +41,9 @@ public class BaseController extends HandlerInterceptorAdapter {
@Autowired
protected StockOutInvoiceService stockOutInvoiceService;

@Autowired
protected InventoryService inventoryService;

private static final ThreadLocal<HttpServletRequest> REQUEST = new ThreadLocal<HttpServletRequest>();

private static final ThreadLocal<HttpServletResponse> RESPONSE = new ThreadLocal<HttpServletResponse>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,30 @@
package com.originspark.drp.controllers.projects.inventories;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originspark.drp.controllers.BaseController;
import com.originspark.drp.util.FileUtil;
import com.originspark.drp.util.poi.exporter.MonthendInventoryGenerator;
import com.originspark.drp.models.User;
import com.originspark.drp.web.models.projects.inventories.CurrentInventoryUI;
import com.originspark.drp.web.models.projects.inventories.Ware;

@Controller
@RequestMapping("project")
@RequestMapping("inventories")
public class InventoryController extends BaseController {
/*@RequestMapping(value = "/{id}/inventories/current", method = RequestMethod.GET)

@RequestMapping(value = "/current", method = RequestMethod.GET)
@ResponseBody
@AuthRoleGroup(type={RoleEnum.WAREKEEPER,RoleEnum.PROJECTMANAGER,RoleEnum.LEADER})
public String currentInventories(@PathVariable Long id){
Project project = projectService.findById(id);
if(project == null){
return failure("你所查询的项目不存在");
}
List<CurrentInventoryUI> inventories = new ArrayList<CurrentInventoryUI>();
//如果是项目
if(project.getProject() == null){
for(Project system : project.getSystems()){
inventories.addAll(projectService.getCurrentInventories(system.getId()));
}
return ok(inventories);
}
inventories.addAll(projectService.getCurrentInventories(id));
return ok(inventories);
public String currentInventories(@RequestParam int start, @RequestParam int limit) {
List<CurrentInventoryUI> data = inventoryService.pagedCurrentInventories(start, limit);
Long count = inventoryService.pagedCurrentInventoriesCount();
return ok(data, count);
}

/*
@ResponseBody
@RequestMapping(value = "/{id}/inventories/monthend", method = RequestMethod.GET)
@AuthRoleGroup(type={RoleEnum.PROJECTMANAGER,RoleEnum.LEADER})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ public class VendorController extends BaseController {
public String create(@RequestBody Vendor vendor) {

String contactMan = vendor.getContactMan();
if (contactMan == null || contactMan.trim().equals("")) {
if (StringUtils.isEmpty(contactMan)) {
return failure("联系人不能为空");
}

String name = vendor.getName();
if (!StringUtils.isEmpty(name) && vendorService.findByName(name) != null) {
if (vendorService.have(vendor)) {
return failure("该供应商已经存在,不可重复添加");
}

Expand Down Expand Up @@ -84,14 +83,12 @@ public String update(@PathVariable Long id, @RequestBody Vendor vendor,HttpServl
return failure("您要更新的供应商不存在");
}

String name = vendor.getName();
if (name == null || name.trim().equals("")) {
logger.warn(">更新失败:商品名称不能为空");
return failure("供应商名称不能为空");
String contactMan = vendor.getContactMan();
if (StringUtils.isEmpty(contactMan)) {
return failure("联系人不能为空");
}

if (vendorService.findByName(name) != null) {
logger.warn(">更新失败:该供应商已经存在,不可重复添加");
if (vendorService.have(vendor)) {
return failure("该供应商已经存在,不可重复添加");
}

Expand All @@ -100,11 +97,9 @@ public String update(@PathVariable Long id, @RequestBody Vendor vendor,HttpServl
existingVendor.setAddress(vendor.getAddress());
existingVendor.setPhone(vendor.getPhone());
existingVendor.setNote(vendor.getNote());

existingVendor.setUpdatedBy(SessionUtil.getCurrentUserName(request));

existingVendor.setUpdatedBy(getCurrentUser().getName());
vendorService.update(existingVendor);
logger.info(">更新成功:"+existingVendor.toString());

return ok("更新成功");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ public String deleteBatch(HttpServletRequest request) {

@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
@ResponseBody
public String update(@PathVariable Long id, @RequestBody Ware ware,HttpServletRequest request) {
public String update(@PathVariable Long id, @RequestBody Ware ware) {

Ware existingWare = wareService.findById(Ware.class, id);

//validation
if (existingWare == null) {
return failure("您要更新的商品不存在");
Expand Down Expand Up @@ -130,8 +130,8 @@ public String update(@PathVariable Long id, @RequestBody Ware ware,HttpServletRe
existingWare.setUnit(ware.getUnit());
existingWare.setNote(ware.getNote());
existingWare.setVendor(ware.getVendor());

existingWare.setUpdatedBy(SessionUtil.getCurrentUserName(request));
existingWare.setCategory(ware.getCategory());
existingWare.setUpdatedBy(SessionUtil.getCurrentUserName(request()));

wareService.update(existingWare);
logger.info(">更新成功:"+existingWare.toString());
Expand All @@ -141,7 +141,6 @@ public String update(@PathVariable Long id, @RequestBody Ware ware,HttpServletRe
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String list(@RequestParam int start, @RequestParam int limit, @RequestParam(required = false) Object filter, HttpServletRequest request) {

List<FilterRequest> filters = new ArrayList<FilterRequest>();

if (filter != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
package com.originspark.drp.models.projects.costs;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.originspark.drp.models.AbstractModel;


@MappedSuperclass
public abstract class AbstractCost extends AbstractModel{
public abstract class AbstractCost extends AbstractModel {

private static SimpleDateFormat forYearMonthFormatter = new SimpleDateFormat("yyyy-MM");

/**
* 该字段和对应invoice的时间字段保持一致,为冗余字段,
* 只为简化盘点计算,forYearMonth同理
* 日期:年-月-日
*/
@Temporal(TemporalType.DATE)
private Date forDate;

/**
* 日期:年-月
*/
@JsonIgnore
@Column(columnDefinition = "char(7)", nullable = false)
private String forYearMonth;

/**
* 单价
Expand Down Expand Up @@ -60,20 +81,39 @@ public BigDecimal getTotal() {
public void setTotal(BigDecimal total) {
this.total = total;
}


public Date getForDate() {
return forDate;
}

public void setForDate(Date forDate) {
this.forDate = forDate;
if (forDate != null) {
setForYearMonth(forYearMonthFormatter.format(forDate));
}
}

public String getForYearMonth() {
return forYearMonth;
}

public void setForYearMonth(String forYearMonth) {
this.forYearMonth = forYearMonth;
}

@Override
public String toString() {
return super.toString()+", unitPrice="+unitPrice+", quantity="+quantity;
}

@PrePersist
public void prePersist(){
if(getUnitPrice() == null || getQuantity() == null){
setTotal(BigDecimal.ZERO);
}
setTotal(getUnitPrice().multiply(getQuantity()));
}

@PreUpdate
public void PreUpdate(){
if(getUnitPrice() == null || getQuantity() == null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.originspark.drp.models.AbstractModel;
import com.sun.istack.internal.NotNull;

/**
* 单据
Expand All @@ -33,7 +32,6 @@ public abstract class AbstractInvoice extends AbstractModel{
private Date forDate;

@JsonIgnore
@NotNull
@Column(columnDefinition = "char(7)", nullable = false)
private String forYearMonth;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.originspark.drp.service.projects.inventories;

import java.util.List;

import com.originspark.drp.dao.BaseDAO;
import com.originspark.drp.web.models.projects.inventories.CurrentInventoryUI;

public interface InventoryService extends BaseDAO {

List<CurrentInventoryUI> pagedCurrentInventories(int start, int limit);

Long pagedCurrentInventoriesCount();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.originspark.drp.service.projects.inventories;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.originspark.drp.dao.BaseDAOSupport;
import com.originspark.drp.web.models.projects.inventories.CurrentInventoryUI;

@Transactional
@Service
public class InventoryServiceBean extends BaseDAOSupport implements InventoryService {

// 实时入库量统计
final String CURRENT_IN_SQL = "(SELECT DISTINCT(ware) as wid, sum(quantity) as incount, SUM(total) as outcome FROM cost_stock_in GROUP BY ware) as t1";
// 实时出库量统计
final String CURRENT_OUT_SQL = "(SELECT DISTINCT(ware) as wid, sum(quantity) as outcount, SUM(total) as income FROM cost_stock_out GROUP BY ware) as t2";
// 实时库存量统计
final String CURRENT_SUM_SQL = "SELECT t3.name, t3.model, t3.unit, t3.brand, t1.outcome, t1.incount, t2.income, t2.outcount FROM "
+ CURRENT_IN_SQL + " LEFT JOIN " + CURRENT_OUT_SQL + " ON t1.wid = t2.wid"
+ " JOIN wares as t3 WHERE t1.wid = t3.id";
// 实时库存量总数计算
final String CURRENT_COUNT = "SELECT COUNT(DISTINCT(ware)) FROM cost_stock_in";


@SuppressWarnings("unchecked")
@Override
public List<CurrentInventoryUI> pagedCurrentInventories(int start, int limit) {
Query query = em.createNativeQuery(CURRENT_SUM_SQL);
List<Object[]> res = query.setFirstResult(start).setMaxResults(limit).getResultList();
List<CurrentInventoryUI> currentInventories = new ArrayList<CurrentInventoryUI>();

Object[] objAry;
for (int i = 0, length = res.size(); i < length; i++) {
CurrentInventoryUI inventory = new CurrentInventoryUI();
objAry = res.get(i);
inventory.setName(objAry[0] + "");
inventory.setModel(objAry[1] + "");
inventory.setUnit(objAry[2] + "");
inventory.setBrand(objAry[3] + "");
inventory.setOutcome(objAry[4] == null ? BigDecimal.ZERO : (BigDecimal) objAry[4]);
inventory.setIncount(objAry[5] == null ? 0L : ((BigDecimal)objAry[5]).longValue());
inventory.setIncome(objAry[6] == null ? BigDecimal.ZERO : (BigDecimal) objAry[6]);
inventory.setOutcount(objAry[7] == null ? 0L : ((BigDecimal)objAry[7]).longValue());
currentInventories.add(inventory);
}

return currentInventories;
}

@Override
public Long pagedCurrentInventoriesCount() {
Query query = em.createNativeQuery(CURRENT_COUNT);
BigInteger count = (BigInteger)query.getSingleResult();
return count.longValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.originspark.drp.dao.BaseDAO;
import com.originspark.drp.models.resources.Vendor;
import com.originspark.drp.util.json.FilterRequest;

public interface VendorService extends BaseDAO<Vendor>{

List<Vendor> pagedDataSet(int start, int limit, List<FilterRequest> filters);

Long pagedDataCount(List<FilterRequest> filters);

Map<String,String> validate();

Vendor findByName(String name);


boolean have(Vendor vendor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.originspark.drp.dao.BaseDAOSupport;
import com.originspark.drp.models.resources.Vendor;
import com.originspark.drp.models.resources.Ware;
import com.originspark.drp.models.resources.Vendor.COLUMNS;
import com.originspark.drp.util.json.FilterRequest;

Expand Down Expand Up @@ -123,4 +124,18 @@ public Vendor findByName(String name) {
return vendors.get(0);
}

@Override
public boolean have(Vendor vendor) {
String jpql = "from Vendor where name =:name and contactMan =:contactMan";
TypedQuery<Vendor> query = em.createQuery(jpql, Vendor.class)
.setParameter("name", vendor.getName())
.setParameter("contactMan", vendor.getContactMan());

List<Vendor> vendors = query.getResultList();
if(vendors.isEmpty()){
return false;
}
return true;
}

}
Loading

0 comments on commit da1c930

Please sign in to comment.