物料导入
							parent
							
								
									e1c7bc68c7
								
							
						
					
					
						commit
						68a277643a
					
				| 
						 | 
					@ -17,10 +17,13 @@ package com.youchain.basicdata.domain;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.youchain.base.BaseEntity;
 | 
					import com.youchain.base.BaseEntity;
 | 
				
			||||||
import com.youchain.modules.system.domain.Dept;
 | 
					import com.youchain.modules.system.domain.Dept;
 | 
				
			||||||
 | 
					import lombok.AllArgsConstructor;
 | 
				
			||||||
 | 
					import lombok.Builder;
 | 
				
			||||||
import lombok.Data;
 | 
					import lombok.Data;
 | 
				
			||||||
import cn.hutool.core.bean.BeanUtil;
 | 
					import cn.hutool.core.bean.BeanUtil;
 | 
				
			||||||
import io.swagger.annotations.ApiModelProperty;
 | 
					import io.swagger.annotations.ApiModelProperty;
 | 
				
			||||||
import cn.hutool.core.bean.copier.CopyOptions;
 | 
					import cn.hutool.core.bean.copier.CopyOptions;
 | 
				
			||||||
 | 
					import lombok.NoArgsConstructor;
 | 
				
			||||||
import javax.persistence.*;
 | 
					import javax.persistence.*;
 | 
				
			||||||
import javax.validation.constraints.*;
 | 
					import javax.validation.constraints.*;
 | 
				
			||||||
import java.io.Serializable;
 | 
					import java.io.Serializable;
 | 
				
			||||||
| 
						 | 
					@ -31,8 +34,11 @@ import java.io.Serializable;
 | 
				
			||||||
 * @author houjianlan
 | 
					 * @author houjianlan
 | 
				
			||||||
 * @date 2023-08-07
 | 
					 * @date 2023-08-07
 | 
				
			||||||
 **/
 | 
					 **/
 | 
				
			||||||
@Entity
 | 
					 | 
				
			||||||
@Data
 | 
					@Data
 | 
				
			||||||
 | 
					@Builder
 | 
				
			||||||
 | 
					@AllArgsConstructor
 | 
				
			||||||
 | 
					@NoArgsConstructor
 | 
				
			||||||
 | 
					@Entity
 | 
				
			||||||
@Table(name="base_item")
 | 
					@Table(name="base_item")
 | 
				
			||||||
public class Item extends BaseEntity implements Serializable {
 | 
					public class Item extends BaseEntity implements Serializable {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,31 +1,37 @@
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
*  Copyright 2019-2020 Zheng Jie
 | 
					 *  Copyright 2019-2020 Zheng Jie
 | 
				
			||||||
*
 | 
					 *
 | 
				
			||||||
*  Licensed under the Apache License, Version 2.0 (the "License");
 | 
					 *  Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||||
*  you may not use this file except in compliance with the License.
 | 
					 *  you may not use this file except in compliance with the License.
 | 
				
			||||||
*  You may obtain a copy of the License at
 | 
					 *  You may obtain a copy of the License at
 | 
				
			||||||
*
 | 
					 *
 | 
				
			||||||
*  http://www.apache.org/licenses/LICENSE-2.0
 | 
					 *  http://www.apache.org/licenses/LICENSE-2.0
 | 
				
			||||||
*
 | 
					 *
 | 
				
			||||||
*  Unless required by applicable law or agreed to in writing, software
 | 
					 *  Unless required by applicable law or agreed to in writing, software
 | 
				
			||||||
*  distributed under the License is distributed on an "AS IS" BASIS,
 | 
					 *  distributed under the License is distributed on an "AS IS" BASIS,
 | 
				
			||||||
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||||
*  See the License for the specific language governing permissions and
 | 
					 *  See the License for the specific language governing permissions and
 | 
				
			||||||
*  limitations under the License.
 | 
					 *  limitations under the License.
 | 
				
			||||||
*/
 | 
					 */
 | 
				
			||||||
package com.youchain.basicdata.repository;
 | 
					package com.youchain.basicdata.repository;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.youchain.basicdata.domain.Item;
 | 
					import com.youchain.basicdata.domain.Item;
 | 
				
			||||||
 | 
					import com.youchain.basicdata.domain.Point;
 | 
				
			||||||
import org.springframework.data.jpa.repository.JpaRepository;
 | 
					import org.springframework.data.jpa.repository.JpaRepository;
 | 
				
			||||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 | 
					import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 | 
				
			||||||
import org.springframework.data.jpa.repository.Query;
 | 
					import org.springframework.data.jpa.repository.Query;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
* @website https://eladmin.vip
 | 
					 * @author houjianlan
 | 
				
			||||||
* @author houjianlan
 | 
					 * @website https://eladmin.vip
 | 
				
			||||||
* @date 2023-08-16
 | 
					 * @date 2023-08-16
 | 
				
			||||||
**/
 | 
					 **/
 | 
				
			||||||
public interface ItemRepository extends JpaRepository<Item, Long>, JpaSpecificationExecutor<Item> {
 | 
					public interface ItemRepository extends JpaRepository<Item, Long>, JpaSpecificationExecutor<Item> {
 | 
				
			||||||
    @Query(" from Item i where i.code = :code and i.enabled = true")
 | 
					    @Query("from Item i where i.code = :code and i.enabled = true")
 | 
				
			||||||
    Item findByCode(String code);
 | 
					    Item findByCode(String code);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Query("from Item i where i.code in :codes and i.enabled = true")
 | 
				
			||||||
 | 
					    List<Item> findByCodes(List<String> codes);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,13 @@
 | 
				
			||||||
 | 
					package com.youchain.basicdata.requset;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import cn.idev.excel.annotation.ExcelProperty;
 | 
				
			||||||
 | 
					import lombok.Data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Data
 | 
				
			||||||
 | 
					public class ItemImportReq {
 | 
				
			||||||
 | 
					    @ExcelProperty("零件号")
 | 
				
			||||||
 | 
					    private String code;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @ExcelProperty("规格")
 | 
				
			||||||
 | 
					    private String specs;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -20,6 +20,7 @@ import com.youchain.annotation.Log;
 | 
				
			||||||
import com.youchain.basicdata.domain.Item;
 | 
					import com.youchain.basicdata.domain.Item;
 | 
				
			||||||
import com.youchain.basicdata.service.ItemService;
 | 
					import com.youchain.basicdata.service.ItemService;
 | 
				
			||||||
import com.youchain.basicdata.service.dto.ItemQueryCriteria;
 | 
					import com.youchain.basicdata.service.dto.ItemQueryCriteria;
 | 
				
			||||||
 | 
					import com.youchain.exception.handler.ApiResult;
 | 
				
			||||||
import com.youchain.utils.UserUtils;
 | 
					import com.youchain.utils.UserUtils;
 | 
				
			||||||
import lombok.extern.slf4j.Slf4j;
 | 
					import lombok.extern.slf4j.Slf4j;
 | 
				
			||||||
import org.springframework.data.domain.Pageable;
 | 
					import org.springframework.data.domain.Pageable;
 | 
				
			||||||
| 
						 | 
					@ -80,13 +81,16 @@ public class ItemController {
 | 
				
			||||||
        return new ResponseEntity<>(itemService.create(resources),HttpStatus.CREATED);
 | 
					        return new ResponseEntity<>(itemService.create(resources),HttpStatus.CREATED);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @PostMapping(value = "/import_data")
 | 
					    @Log("导入物料")
 | 
				
			||||||
 | 
					    @PostMapping(value = "/importItem")
 | 
				
			||||||
    @ApiOperation("导入物料")
 | 
					    @ApiOperation("导入物料")
 | 
				
			||||||
    @Transactional
 | 
					    public ResponseEntity<Object> importItem(@RequestParam("file") MultipartFile multipartFile) {
 | 
				
			||||||
    @AnonymousAccess
 | 
					        try {
 | 
				
			||||||
    public ResponseEntity<Object> createSysAppUpdate( @RequestParam("file") MultipartFile multipartFile) {
 | 
					            String result = itemService.importItem(multipartFile);
 | 
				
			||||||
        return null;
 | 
					            return new ResponseEntity<>(ApiResult.success(result, null), HttpStatus.OK);
 | 
				
			||||||
 | 
					        }catch (Exception e){
 | 
				
			||||||
 | 
					            return new ResponseEntity<>(ApiResult.fail(e.getMessage()), HttpStatus.BAD_REQUEST);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @PutMapping
 | 
					    @PutMapping
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -16,9 +16,11 @@
 | 
				
			||||||
package com.youchain.basicdata.service;
 | 
					package com.youchain.basicdata.service;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.youchain.basicdata.domain.Item;
 | 
					import com.youchain.basicdata.domain.Item;
 | 
				
			||||||
 | 
					import com.youchain.basicdata.domain.Point;
 | 
				
			||||||
import com.youchain.basicdata.service.dto.ItemDto;
 | 
					import com.youchain.basicdata.service.dto.ItemDto;
 | 
				
			||||||
import com.youchain.basicdata.service.dto.ItemQueryCriteria;
 | 
					import com.youchain.basicdata.service.dto.ItemQueryCriteria;
 | 
				
			||||||
import org.springframework.data.domain.Pageable;
 | 
					import org.springframework.data.domain.Pageable;
 | 
				
			||||||
 | 
					import org.springframework.web.multipart.MultipartFile;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.Map;
 | 
					import java.util.Map;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
| 
						 | 
					@ -91,4 +93,20 @@ public interface ItemService {
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    Item validateItem(String itemCode);
 | 
					    Item validateItem(String itemCode);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 批量查询物料
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param codes 点位集合
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    Map<String, Item> findByCodes(List<String> codes);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 导入物料
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param multipartFile 文件
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    String importItem(MultipartFile multipartFile);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -15,30 +15,30 @@
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
package com.youchain.basicdata.service.impl;
 | 
					package com.youchain.basicdata.service.impl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import cn.idev.excel.FastExcel;
 | 
				
			||||||
import com.youchain.basicdata.domain.Item;
 | 
					import com.youchain.basicdata.domain.Item;
 | 
				
			||||||
 | 
					import com.youchain.basicdata.requset.ItemImportReq;
 | 
				
			||||||
import com.youchain.exception.BadRequestException;
 | 
					import com.youchain.exception.BadRequestException;
 | 
				
			||||||
import com.youchain.utils.FileUtil;
 | 
					import com.youchain.modules.system.domain.Dept;
 | 
				
			||||||
import com.youchain.utils.PageUtil;
 | 
					import com.youchain.utils.*;
 | 
				
			||||||
import com.youchain.utils.QueryHelp;
 | 
					 | 
				
			||||||
import com.youchain.utils.ValidationUtil;
 | 
					 | 
				
			||||||
import lombok.RequiredArgsConstructor;
 | 
					import lombok.RequiredArgsConstructor;
 | 
				
			||||||
import com.youchain.basicdata.repository.ItemRepository;
 | 
					import com.youchain.basicdata.repository.ItemRepository;
 | 
				
			||||||
import com.youchain.basicdata.service.ItemService;
 | 
					import com.youchain.basicdata.service.ItemService;
 | 
				
			||||||
import com.youchain.basicdata.service.dto.ItemDto;
 | 
					import com.youchain.basicdata.service.dto.ItemDto;
 | 
				
			||||||
import com.youchain.basicdata.service.dto.ItemQueryCriteria;
 | 
					import com.youchain.basicdata.service.dto.ItemQueryCriteria;
 | 
				
			||||||
import com.youchain.basicdata.service.mapstruct.ItemMapper;
 | 
					import com.youchain.basicdata.service.mapstruct.ItemMapper;
 | 
				
			||||||
 | 
					import org.apache.commons.collections4.CollectionUtils;
 | 
				
			||||||
import org.springframework.stereotype.Service;
 | 
					import org.springframework.stereotype.Service;
 | 
				
			||||||
import org.springframework.transaction.annotation.Transactional;
 | 
					import org.springframework.transaction.annotation.Transactional;
 | 
				
			||||||
import org.springframework.data.domain.Page;
 | 
					import org.springframework.data.domain.Page;
 | 
				
			||||||
import org.springframework.data.domain.Pageable;
 | 
					import org.springframework.data.domain.Pageable;
 | 
				
			||||||
 | 
					import org.springframework.web.multipart.MultipartFile;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.*;
 | 
				
			||||||
import java.util.Map;
 | 
					 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import javax.persistence.EntityManager;
 | 
					import javax.persistence.EntityManager;
 | 
				
			||||||
import javax.servlet.http.HttpServletResponse;
 | 
					import javax.servlet.http.HttpServletResponse;
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.stream.Collectors;
 | 
				
			||||||
import java.util.LinkedHashMap;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @author houjianlan
 | 
					 * @author houjianlan
 | 
				
			||||||
| 
						 | 
					@ -52,7 +52,6 @@ public class ItemServiceImpl implements ItemService {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private final ItemRepository itemRepository;
 | 
					    private final ItemRepository itemRepository;
 | 
				
			||||||
    private final ItemMapper itemMapper;
 | 
					    private final ItemMapper itemMapper;
 | 
				
			||||||
    private final EntityManager entityMapper;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public Map<String, Object> queryAll(ItemQueryCriteria criteria, Pageable pageable) {
 | 
					    public Map<String, Object> queryAll(ItemQueryCriteria criteria, Pageable pageable) {
 | 
				
			||||||
| 
						 | 
					@ -142,5 +141,92 @@ public class ItemServiceImpl implements ItemService {
 | 
				
			||||||
        return item;
 | 
					        return item;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public Map<String, Item> findByCodes(List<String> codes) {
 | 
				
			||||||
 | 
					        List<Item> itemList = itemRepository.findByCodes(codes);
 | 
				
			||||||
 | 
					        Map<String, Item> itemMap = new HashMap<>();
 | 
				
			||||||
 | 
					        for (Item item : itemList) {
 | 
				
			||||||
 | 
					            itemMap.put(item.getCode(), item);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return itemMap;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    @Transactional(rollbackFor = Exception.class)
 | 
				
			||||||
 | 
					    public String importItem(MultipartFile multipartFile) {
 | 
				
			||||||
 | 
					        List<ItemImportReq> dataList;
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            dataList = FastExcel.read(multipartFile.getInputStream()).head(ItemImportReq.class)
 | 
				
			||||||
 | 
					                    .sheet()
 | 
				
			||||||
 | 
					                    .doReadSync();
 | 
				
			||||||
 | 
					        } catch (IOException e) {
 | 
				
			||||||
 | 
					            throw new BadRequestException("数据格式存在问题,无法读取");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if (CollectionUtils.isEmpty(dataList)) {
 | 
				
			||||||
 | 
					            throw new BadRequestException("数据为空");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //获取文件中所有物料编码
 | 
				
			||||||
 | 
					        List<String> codes = dataList.stream()
 | 
				
			||||||
 | 
					                .map(ItemImportReq::getCode)
 | 
				
			||||||
 | 
					                .distinct()
 | 
				
			||||||
 | 
					                .collect(Collectors.toList());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //获取已存在的物料
 | 
				
			||||||
 | 
					        Map<String, Item> existingItemMap = this.findByCodes(codes);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //新增物料集合
 | 
				
			||||||
 | 
					        List<Item> itemsToCreate = new ArrayList<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //修改物料集合
 | 
				
			||||||
 | 
					        List<Item> itemsToUpdate = new ArrayList<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (ItemImportReq itemImportReq : dataList) {
 | 
				
			||||||
 | 
					            String code = itemImportReq.getCode();
 | 
				
			||||||
 | 
					            String specs = itemImportReq.getSpecs();
 | 
				
			||||||
 | 
					            //判断是否已存在容器
 | 
				
			||||||
 | 
					            if (existingItemMap.containsKey(code)) {
 | 
				
			||||||
 | 
					                //修改物料
 | 
				
			||||||
 | 
					                Item item = existingItemMap.get(code);
 | 
				
			||||||
 | 
					                itemsToUpdate.add(updateItem(item, specs));
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                //新增容器
 | 
				
			||||||
 | 
					                Item item = createItem(code, specs, UserUtils.getDept());
 | 
				
			||||||
 | 
					                itemsToCreate.add(item);
 | 
				
			||||||
 | 
					                existingItemMap.put(code, item);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //批量新增容器
 | 
				
			||||||
 | 
					        if (!itemsToCreate.isEmpty()) {
 | 
				
			||||||
 | 
					            itemRepository.saveAll(itemsToCreate);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //批量更新容器
 | 
				
			||||||
 | 
					        if (!itemsToUpdate.isEmpty()) {
 | 
				
			||||||
 | 
					            itemRepository.saveAll(itemsToUpdate);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return ("导入成功:" + " 新增(" + itemsToCreate.size() + ")修改(" + itemsToUpdate.size() + ")");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private Item createItem(String code, String specs, Dept dept) {
 | 
				
			||||||
 | 
					        return Item.builder()
 | 
				
			||||||
 | 
					                .code(code)
 | 
				
			||||||
 | 
					                .name(code)
 | 
				
			||||||
 | 
					                .specs(specs)
 | 
				
			||||||
 | 
					                .enabled(true)
 | 
				
			||||||
 | 
					                .goodType("大件专用")
 | 
				
			||||||
 | 
					                .unit("个")
 | 
				
			||||||
 | 
					                .dept(dept)
 | 
				
			||||||
 | 
					                .build();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private Item updateItem(Item item, String specs) {
 | 
				
			||||||
 | 
					        item.setSpecs(specs);
 | 
				
			||||||
 | 
					        return item;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue