wa re house service

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;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值