|
@@ -0,0 +1,116 @@
|
|
|
+/*
|
|
|
+* Copyright 2019-2020 Zheng Jie
|
|
|
+*
|
|
|
+* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
+* you may not use this file except in compliance with the License.
|
|
|
+* You may obtain a copy of the License at
|
|
|
+*
|
|
|
+* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
+*
|
|
|
+* Unless required by applicable law or agreed to in writing, software
|
|
|
+* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
+* See the License for the specific language governing permissions and
|
|
|
+* limitations under the License.
|
|
|
+*/
|
|
|
+package me.zhengjie.modules.dm.foodCate.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.lang.Snowflake;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import me.zhengjie.modules.dm.foodCate.domain.DmFoodCate;
|
|
|
+import me.zhengjie.modules.dm.foodCate.repository.DmFoodCateRepository;
|
|
|
+import me.zhengjie.modules.dm.foodCate.service.DmFoodCateService;
|
|
|
+import me.zhengjie.modules.dm.foodCate.service.dto.DmFoodCateDto;
|
|
|
+import me.zhengjie.modules.dm.foodCate.service.dto.DmFoodCateQueryCriteria;
|
|
|
+import me.zhengjie.modules.dm.foodCate.service.mapstruct.DmFoodCateMapper;
|
|
|
+import me.zhengjie.utils.FileUtil;
|
|
|
+import me.zhengjie.utils.PageUtil;
|
|
|
+import me.zhengjie.utils.QueryHelp;
|
|
|
+import me.zhengjie.utils.ValidationUtil;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+* @website https://el-admin.vip
|
|
|
+* @description 服务实现
|
|
|
+* @author sunmz
|
|
|
+* @date 2021-09-15
|
|
|
+**/
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class DmFoodCateServiceImpl implements DmFoodCateService {
|
|
|
+
|
|
|
+ private final DmFoodCateRepository dmFoodRepository;
|
|
|
+ private final DmFoodCateMapper dmFoodMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String,Object> queryAll(DmFoodCateQueryCriteria criteria, Pageable pageable){
|
|
|
+ Page<DmFoodCate> page = dmFoodRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
|
|
|
+ return PageUtil.toPage(page.map(dmFoodMapper::toDto));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<DmFoodCateDto> queryAll(DmFoodCateQueryCriteria criteria){
|
|
|
+ return dmFoodMapper.toDto(dmFoodRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public DmFoodCateDto findById(Integer id) {
|
|
|
+ DmFoodCate dmFood = dmFoodRepository.findById(id).orElseGet(DmFoodCate::new);
|
|
|
+ ValidationUtil.isNull(dmFood.getId(),"DmFood","id",id);
|
|
|
+ return dmFoodMapper.toDto(dmFood);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public DmFoodCateDto create(DmFoodCate resources) {
|
|
|
+// Snowflake snowflake = IdUtil.createSnowflake(1, 1);
|
|
|
+// resources.setId((int) snowflake.nextId());
|
|
|
+ return dmFoodMapper.toDto(dmFoodRepository.save(resources));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void update(DmFoodCate resources) {
|
|
|
+ DmFoodCate dmFood = dmFoodRepository.findById(resources.getId()).orElseGet(DmFoodCate::new);
|
|
|
+ ValidationUtil.isNull( dmFood.getId(),"DmFood","id",resources.getId());
|
|
|
+ dmFood.copy(resources);
|
|
|
+ dmFoodRepository.save(dmFood);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteAll(Integer[] ids) {
|
|
|
+ for (Integer id : ids) {
|
|
|
+ dmFoodRepository.deleteById(id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void download(List<DmFoodCateDto> all, HttpServletResponse response) throws IOException {
|
|
|
+ List<Map<String, Object>> list = new ArrayList<>();
|
|
|
+ for (DmFoodCateDto dmFood : all) {
|
|
|
+ Map<String,Object> map = new LinkedHashMap<>();
|
|
|
+ map.put("编号", dmFood.getId());
|
|
|
+ map.put("名称", dmFood.getCateName());
|
|
|
+ map.put("上级编号", dmFood.getPid());
|
|
|
+ map.put("图片", dmFood.getPicture());
|
|
|
+ map.put("创建者", dmFood.getCreateBy());
|
|
|
+ map.put("更新者", dmFood.getUpdatedBy());
|
|
|
+ map.put("创建日期", dmFood.getCreateTime());
|
|
|
+ map.put("更新时间", dmFood.getUpdateTime());
|
|
|
+ list.add(map);
|
|
|
+ }
|
|
|
+ FileUtil.downloadExcel(list, response);
|
|
|
+ }
|
|
|
+}
|