package com.cwkj.jxc.action;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import com.cwkj.exception.BizException;
import com.cwkj.jxc.biz.IProductService;
import com.cwkj.jxc.biz.ParamService;
import com.cwkj.jxc.biz.WareHouseService;
import com.cwkj.jxc.entity.Param;
import com.cwkj.jxc.entity.Product;
import com.cwkj.jxc.entity.ProductInitItem;
import com.cwkj.jxc.entity.WareHouse;
import com.cwkj.util.BaseAction;
import com.cwkj.util.BeanFactory;
import com.cwkj.util.CreateNumber;
import com.cwkj.util.ExcelHelper;
import com.cwkj.util.Pagination;
import com.cwkj.util.RegularValidate;
import com.opensymphony.xwork2.ModelDriven;
public class ProductAction extends BaseAction implements ModelDriven<Product> {
public static final String PRODUCT_CATEGORY="商品类别";
public static final String WAREHOUSE_INFO="仓库信息";
public static final String UNIT_TYPE="计量单位";
public static final int PAGE_SIZE=5;
private static final long serialVersionUID = 1L;
private Product product;
private ParamService service;
private IProductService iProductService;
private List<Param> params;//商品类型
private List<Param> warehouses;//仓库
private List<Param> units;//单位
private Param param;
private String jsonData;
private InputStream stream;
private Pagination<Product> pagination;
private int currentPage = 1;
private WareHouseService wareHouseService;
private String editName;
private String ids;
private File productExcel;//导入的文件
private List<WareHouse> wareHouse;
public WareHouseService getWareHouseService() {
return wareHouseService;
}
public void setWareHouseService(WareHouseService wareHouseService) {
this.wareHouseService = wareHouseService;
}
public File getProductExcel() {
return productExcel;
}
public void setProductExcel(File productExcel) {
this.productExcel = productExcel;
}
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
public String getEditName() {
return editName;
}
public void setEditName(String editName) {
this.editName = editName;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public Pagination<Product> getPagination() {
return pagination;
}
public void setPagination(Pagination<Product> pagination) {
this.pagination = pagination;
}
public InputStream getStream() {
return stream;
}
public void setStream(InputStream stream) {
this.stream = stream;
}
public String getJsonData() {
return jsonData;
}
public void setJsonData(String jsonData) {
this.jsonData = jsonData;
}
public IProductService getiProductService() {
return iProductService;
}
public void setiProductService(IProductService iProductService) {
this.iProductService = iProductService;
}
public Param getParam() {
return param;
}
public void setParam(Param param) {
this.param = param;
}
public List<Param> getWarehouses() {
return warehouses;
}
public void setWarehouses(List<Param> warehouses) {
this.warehouses = warehouses;
}
public List<Param> getUnits() {
return units;
}
public void setUnits(List<Param> units) {
this.units = units;
}
public List<Param> getParams() {
return params;
}
public void setParams(List<Param> params) {
this.params = params;
}
{
service=(ParamService) BeanFactory.getInstance(ParamService.class);
iProductService=(IProductService) BeanFactory.getInstance(IProductService.class);
wareHouseService=(WareHouseService)BeanFactory.getInstance(WareHouseService.class);
}
public ParamService getService() {
return service;
}
public void setService(ParamService service) {
this.service = service;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Product getModel() {
if(product == null){
product = new Product();
}
return product;
}
/**
* 生成id
* @return
*/
public String list(){
requestMap.put("No",CreateNumber.fillZero(5,String.valueOf(iProductService.noFlag()),"sp"));
pagination=new Pagination<Product>(iProductService.selectProductCount(product),PAGE_SIZE,product, currentPage);
requestMap.put("productList",iProductService.pagProduct(pagination));
return "productList";
}
/**
* 添加产品
*/
public String addProduct() {
Integer i=null;
try {
checkAddInfo();
i=iProductService.addProduct(product);
if(null==i||"".equals(i)){
throw new BizException("添加失败");
}else{
jsonData="true";
}
} catch (BizException e) {
jsonData=e.getMessage();
}
return SUCCESS;
}
private void checkAddInfo() {
boolean fg=false;
for (int j = 0; j < product.getProductInitItems().size(); j++) {
if(0==product.getProductInitItems().get(j).getWareHouse().getId()||0.0==product.getProductInitItems().get(j).getTotalPrice()){
fg=true;
}
}
if(fg)throw new BizException("请检查仓库信息");
else{
List<ProductInitItem> list =product.getProductInitItems();
// Set<String> set=new HashSet<String>();
Set<Integer> set = new HashSet<Integer>();
for (ProductInitItem pi : list) {
set.add(pi.getWareHouse().getId());
}
if(set.size()<list.size()){
throw new BizException("仓库名不允许重复");
}
}
}
/**
* 查询option下拉选
*/
public String categoryParamyList(){
// String type="商品类别";
params=service.findParams(PRODUCT_CATEGORY);
return "categoryList";
}
public String warehouseParamyList(){
// String type="仓库信息";
warehouses=service.findParams(WAREHOUSE_INFO);
return SUCCESS;
}
public String unitParamyList(){
// String type="计量单位";
units=service.findParams(UNIT_TYPE);
return "unitList";
}
/**
* 添加param
*/
public String addParam(){
try {
service.addParam(param);
jsonData="true";
} catch (BizException e) {
jsonData="false";
e.printStackTrace();
}
return SUCCESS;
}
/**
* checkParam,名字是否唯一
*/
public String checkParam(){
stream=iProductService.isParamExist(param)?new ByteArrayInputStream("true".getBytes()):new ByteArrayInputStream("false".getBytes());
return "checkflag";
}
public String categoryList(){
params=service.findParams(param.getType());
return SUCCESS;
}
/**
* 检查商品名称
* @return
*/
public String checkProductName(){
if(StringUtils.isEmpty(editName)){
stream=iProductService.isProductNameExist(product.getName())?new ByteArrayInputStream("true".getBytes()):new ByteArrayInputStream("false".getBytes());
}else
if(editName.equals(product.getName())){
stream=new ByteArrayInputStream("true".getBytes());
}else{
stream=iProductService.isProductNameExist(product.getName())?new ByteArrayInputStream("true".getBytes()):new ByteArrayInputStream("false".getBytes());
}
return "checkflag";
}
/**
* 获得下一个商品号
*/
public String nextId(){
jsonData=CreateNumber.fillZero(5,String.valueOf(iProductService.noFlag()),"sp");
return SUCCESS;
}
/**
* 分页list
* @return
*/
public String pagList(){
pagination=new Pagination<Product>(iProductService.selectProductCount(product),PAGE_SIZE,product, currentPage);
requestMap.put("productList",iProductService.pagProduct(pagination));
return "list";
}
/**
* 修改
* @return
*/
public String editProductUI(){
Product p=iProductService.findProductAndItem(product.getId());
requestMap.put("product",p);
System.out.println(p);
return "editForm";
}
/**
* 修改
* @return
*/
public String editProduct(){
try {
iProductService.editProduct(product);
jsonData="true";
} catch (BizException e) {
jsonData="false";
e.printStackTrace();
}
return SUCCESS;
}
/**
* 删除单个
* @return
*/
public String delete(){
try {
iProductService.deleteProductById(product.getId());
jsonData="true";
} catch (BizException e) {
jsonData="false";
e.printStackTrace();
}
return SUCCESS;
}
/**
* 删除多个
* @return
*/
public String deleteMultiple(){
System.out.println(ids);
try {
iProductService.deleteProductByIds(ids);
jsonData="true";
} catch (BizException e) {
jsonData="false";
e.printStackTrace();
}
return SUCCESS;
}
/**
* 导入excel
* @return
*/
@SuppressWarnings({"unchecked"})
public String importExcel(){
params=service.findParams(PRODUCT_CATEGORY);
wareHouse=wareHouseService.findWareHouseInfos();
units=service.findParams(UNIT_TYPE);
FileInputStream inputStream=null;
try {
inputStream=new FileInputStream(productExcel);
List<List> rowList = ExcelHelper.getInstanse().excelFileConvertToList(inputStream);
//删除了表头
rowList.remove(0);
//统计前边的必填项是否为null了,为null小于10报错
List<Product> plist=new ArrayList<Product>();
//以下可以封装
validateExcel(rowList,plist);
iProductService.addProduct(plist);
jsonData="true";
} catch (FileNotFoundException e) {
jsonData=e.getMessage();
} catch (Exception e) {
jsonData=e.getMessage();
}finally{
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
System.out.println("FileInputStream关闭失败");
e.printStackTrace();
}
}
}
return SUCCESS;
}
/**
* 验证导入的excel
* @param rowList
* @param p
* @param plist
*/
private void validateExcel(List<List> rowList,
List<Product> plist) {
List cellObject=null;
List cellObject2=null;
for (List list : rowList) {
cellObject=list.subList(0, 9);
cellObject2=list.subList(11,list.size());
//前边必填项未填满的
if(cellObject.contains("")){
Map <Object,Integer> map = new HashMap <Object,Integer>();
for(Object o :cellObject){
map.put(o, map.get(o)==null?1:map.get(o)+1);
}
if(map.get("")<9){
throw new RuntimeException("商品信息必填项不能为空");
//多个初始仓库的情况
}else if(map.get("")==9&&!cellObject2.contains("")){
boolean flag=false;
//商品必填项全部没有的情况下
if(plist.isEmpty()){
throw new RuntimeException("商品信息不能为空");
}
for (int i = 0; i < wareHouse.size(); i++) {
if(wareHouse.get(i).getName().equals(list.get(11))){
//验证productinitItem数据
int qty=0;
double cost=0;
double totalPrice=0;
if(RegularValidate.isNumber(list.get(12).toString())){
Product p=new Product();
p.setIsEnable(RegularValidate.positiveInteger(list.get(12).toString()));
qty=(int)Double.parseDouble((list.get(12).toString()));
}else{
throw new RuntimeException("期初数量填写错误!");
}
if(RegularValidate.isNumber(list.get(13).toString())){
cost=Double.parseDouble((list.get(13).toString()));
}else{
throw new RuntimeException("单位成本填写错误!");
}
if(RegularValidate.isNumber(list.get(14).toString())){
totalPrice=Double.parseDouble((list.get(14).toString()));
}else{
throw new RuntimeException("期初总价填写错误!");
}
//比较仓库是否存在
boolean repetition=false;
for (ProductInitItem pii : plist.get(plist.size()-1).getProductInitItems()) {
if(pii.getWareHouse().getName().equals(list.get(11))){
repetition=true;
}
}
if(repetition) throw new RuntimeException("同一商品不能添加重复仓库!");
plist.get(plist.size()-1).getProductInitItems().add(new ProductInitItem(null, new WareHouse(wareHouse.get(i).getId(),list.get(11).toString()),qty,cost,totalPrice,null));
flag=false;
break;
}else{
flag=true;
}
}
if(flag)throw new RuntimeException("初始仓库不存在!");
}
}else if(cellObject2.contains("")){
throw new RuntimeException("初始仓库信息不能为空");
}else{
/**
* esle 组装成对象,set属性失败报错,
* 商品信息比较equals组装的,是否存在该信息
* 最后list比较仓库和类别存入id,
*/
Product p=new Product();
p.setName(list.get(0).toString());
p.setSpec(list.get(1).toString());
boolean flag = false;
for (int i = 0; i < params.size(); i++) {
if(params.get(i).getName().equals(list.get(2))){
p.setCategory(new Param(params.get(i).getId(), list.get(2).toString(),PRODUCT_CATEGORY));
flag = false;
break;
}else{
flag = true;
}
}
if(flag)throw new RuntimeException("不存在的商品类别");
for (int i = 0; i < wareHouse.size(); i++) {
if(wareHouse.get(i).getName().equals(list.get(3))){
p.setWarehouse(new WareHouse(wareHouse.get(i).getId(),list.get(3).toString()));
flag = false;
break;
}else{
flag = true;
}
}
if(flag)throw new RuntimeException("不存在的首选仓库");
for (int i = 0; i < units.size(); i++) {
if(units.get(i).getName().equals(list.get(4))){
p.setUnit(list.get(4).toString());
flag = false;
break;
}else{
flag = true;
}
}
if(flag)throw new RuntimeException("不存在的计量单位");
if(RegularValidate.isNumber(list.get(5).toString())){
p.setPurchasePrice(Double.valueOf(list.get(5).toString()));
}else{
throw new RuntimeException("采购价请输入正确的数字");
}
if(RegularValidate.isNumber(list.get(6).toString())){
p.setSalePrice((Double.valueOf(list.get(6).toString())));
}else{
throw new RuntimeException("预计销售价请输入正确的数字");
}
if(RegularValidate.proNatureReg(String.valueOf(list.get(7)))){
p.setProNature((int)Double.parseDouble(list.get(7).toString()));
}else{
throw new RuntimeException("非法的商品性质");
}
if(RegularValidate.trueOrFalse(list.get(8).toString())){
p.setIsEnable(RegularValidate.trueOrFalse(list.get(8).toString()));
}else{
throw new RuntimeException("是否启用填写错误");
}
List<ProductInitItem> pi=new ArrayList<ProductInitItem>();
for (int i = 0; i < wareHouse.size(); i++) {
if(wareHouse.get(i).getName().equals(list.get(11))){
//验证productinitItem数据
Integer qty=0;
double cost=0;
double totalPrice=0;
if(RegularValidate.isNumber(list.get(12).toString())){
qty=(int)Double.parseDouble((list.get(12).toString()));
}else{
throw new RuntimeException("期初数量填写错误!");
}
if(RegularValidate.isNumber(list.get(13).toString())){
cost=Double.parseDouble((list.get(13).toString()));
}else{
throw new RuntimeException("单位成本填写错误!");
}
if(RegularValidate.isNumber(list.get(14).toString())){
totalPrice=Double.parseDouble((list.get(14).toString()));
}else{
throw new RuntimeException("期初总价填写错误!");
}
// p.setProductInitItems(new List<ProductInitItem>().add(new ProductInitItem(null, wareHouse, null, cost, totalPrice, p)));
pi.add(new ProductInitItem(null, new WareHouse(wareHouse.get(i).getId(),list.get(11).toString()),qty,cost,totalPrice,null));
p.setProductInitItems(pi);
List<Product> intrList=iProductService.selectByImport();
if(intrList.contains(p)){
throw new RuntimeException("已存在的产品编号和名称");
}
plist.add(p);
flag=false;
break;
}else{
flag=true;
}
}
if(flag)throw new RuntimeException("不存在的初始仓库");
}
}
}
}
poi导入excel并验证
最新推荐文章于 2024-07-09 13:36:06 发布