package com.chinasoft.clothesmanager.service.impl;
import com.chinasoft.clothesmanager.dao.ClothesExWarehousingMapper;
import com.chinasoft.clothesmanager.dao.ClothesMapper;
import com.chinasoft.clothesmanager.dao.ClothesSizeMapper;
import com.chinasoft.clothesmanager.dao.ClothesTypeMapper;
import com.chinasoft.clothesmanager.dao.ClothesWarehousingMapper;
import com.chinasoft.clothesmanager.entity.Clothes;
import com.chinasoft.clothesmanager.entity.ClothesDto;
import com.chinasoft.clothesmanager.entity.ClothesExWarehousing;
import com.chinasoft.clothesmanager.entity.ClothesVo;
import com.chinasoft.clothesmanager.service.ClothesService;
import com.chinasoft.clothesmanager.util.ExcelFileHandleUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
*
*/
@Service
public class ClothesServiceImpl implements ClothesService {
private Logger logger = LoggerFactory.getLogger(ClothesServiceImpl.class);
@Autowired
ClothesMapper clothesMapper;
@Autowired
ClothesSizeMapper clothesSizeMapper;
@Autowired
ClothesTypeMapper clothesTypeMapper;
@Autowired
ClothesWarehousingMapper clothesWarehousingMapper;
@Autowired
ClothesExWarehousingMapper clothesExWarehousingMapper;
/**
*
* @param clothesName
* @param clothesColor
* @return
* @throws Exception
*/
@Override
public List<ClothesVo> getClothes(String clothesName, String clothesColor) throws Exception {
return clothesMapper.selectByNameAndColor(clothesName, clothesColor);
}
/**
* 衣服入库
* @param clothes
* @throws Exception
*/
@Override
public void insertClothes(Clothes clothes) throws Exception {
/*clothesMapper.insert(clothes);*/
}
/**
* 衣服入库excel
* @param file
* @throws Exception
*/
@Override
public void addClothes(MultipartFile file, Integer userId) throws Exception {
List<ClothesDto> clotheslList = getExcelList(file);
//查询当前是数据库是否有当前excel传来的数据,如果有则更新数据,没有则插入数据
List<ClothesVo> clothesVos = clothesMapper.selectClochesByExcel(clotheslList);
boolean flag = false;
if (clothesVos != null && clothesVos.size() > 0) {
flag = true;
}
//判断哪些数据需要新增哪些数据需要修改
mergeList(clotheslList, clothesVos, flag);
clothesMapper.insert(clothesVos);
clothesWarehousingMapper.insert(clotheslList, userId);
}
/**
* 将excel生成的衣服list与数据库查询的衣服list进行比对,相同的数据更新衣服数量,不相同的数据则添加衣服
* @param clotheslList excel转化后的list
* @param clothesVos 数据库查询出的相同的衣服list
* @param flag 标志位 true代表数据库里有相同的衣服 false代表没有相同的衣服
*/
public void mergeList(List<ClothesDto> clotheslList, List<ClothesVo> clothesVos, boolean flag) {
String clothesName;
String clothesColor;
String clothesTypeName;
String clothesSizeName;
BigDecimal clothesPrice;
Integer num;
Iterator<ClothesDto> iteratorOfExcel = clotheslList.iterator();
Iterator<ClothesVo> iteratorOfSys = null;
ClothesDto nextClothesDto = null;
ClothesVo nextClothesVo = null;
while (iteratorOfExcel.hasNext()) {
nextClothesDto = iteratorOfExcel.next();
clothesName = nextClothesDto.getClothesName();
clothesColor = nextClothesDto.getClothesColor();
clothesTypeName = nextClothesDto.getClothesTypeName();
clothesSizeName = nextClothesDto.getClothesSizeName();
clothesPrice = nextClothesDto.getClothesPrice();
num = nextClothesDto.getNum();
if (StringUtils.isEmpty(clothesName) || StringUtils.isEmpty(clothesColor) || StringUtils.isEmpty(clothesTypeName) ||
StringUtils.isEmpty(clothesSizeName) || clothesPrice == null || num == null) {
iteratorOfExcel.remove();
continue;
}
if (!flag) {
addClothesVo(clothesVos, nextClothesDto);
}
else {
iteratorOfSys = clothesVos.iterator();
boolean flagOfResult = true;
while (iteratorOfSys.hasNext()) {
nextClothesVo = iteratorOfSys.next();
if (clothesName.equals(nextClothesVo.getClothesName()) && clothesColor.equals(nextClothesVo.getClothesColor()) &&
clothesSizeName.equals(nextClothesVo.getClothesSizeName()) && clothesTypeName.equals(nextClothesVo.getClothesTypeName())) {
nextClothesVo.setNum(nextClothesVo.getNum() + nextClothesDto.getNum());
nextClothesVo.setClothesPrice(nextClothesDto.getClothesPrice());
flagOfResult = false;
break;
}
}
if (flagOfResult) {
addClothesVo(clothesVos, nextClothesDto);
}
}
}
}
@Override
public void exClothes(Map<String, List<ClothesExWarehousing>> exClothes) {
List<ClothesExWarehousing> clothesExWarehousings = exClothes.get("exClothes");
clothesMapper.updateByPrimaryKey(clothesExWarehousings);
clothesExWarehousingMapper.insert(clothesExWarehousings);
}
public void addClothesVo(List<ClothesVo> clothesVos, ClothesDto nextClothesDto) {
ClothesVo nextClothesVo = new ClothesVo();
nextClothesVo.setNum(nextClothesDto.getNum());
nextClothesVo.setClothesPrice(nextClothesDto.getClothesPrice());
nextClothesVo.setClothesColor(nextClothesDto.getClothesColor());
nextClothesVo.setClothesName(nextClothesDto.getClothesName());
nextClothesVo.setClothesTypeName(nextClothesDto.getClothesTypeName());
nextClothesVo.setClothesSizeName(nextClothesDto.getClothesSizeName());
clothesVos.add(nextClothesVo);
}
/**
* 将excel转化为list
*
* @param file 文件
* @return list
*/
public List<ClothesDto> getExcelList(MultipartFile file) {
// TODO: 2020/3/10 需要校验文件
Field[] declaredFields = ClothesDto.class.getDeclaredFields();
String[] fieldNames = new String[declaredFields.length];
for (int i = 0; i < declaredFields.length; i++) {
fieldNames[i] = declaredFields[i].getName();
}
List<?> objects = ExcelFileHandleUtil.parseFromExcel(file, ClothesDto.class, fieldNames);
List<ClothesDto> clotheslList = new ArrayList<>();
//如果。。。
if (objects == null || objects.size() <= 0) {
logger.error("没有数据");
}
for (Object excel : objects) {
if (excel instanceof ClothesDto) {
clotheslList.add((ClothesDto) excel);
}
}
return clotheslList;
}
}
wa re house service
最新推荐文章于 2021-09-02 11:43:05 发布