| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package com.izouma.awesomeAdmin.web;
- import com.izouma.awesomeAdmin.domain.SaleBatch;
- import com.izouma.awesomeAdmin.domain.SalesBatchExtension;
- import com.izouma.awesomeAdmin.dto.SaleBatchDto;
- import com.izouma.awesomeAdmin.repo.SalesBatchExtensionRepo;
- import com.izouma.awesomeAdmin.service.SaleBatchExtensionService;
- import com.izouma.awesomeAdmin.service.SaleBatchService;
- import com.izouma.awesomeAdmin.exception.BusinessException;
- import com.izouma.awesomeAdmin.repo.SaleBatchRepo;
- import com.izouma.awesomeAdmin.utils.Translator;
- import com.izouma.awesomeAdmin.utils.excel.ExcelUtils;
- import lombok.AllArgsConstructor;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.PageImpl;
- import org.springframework.security.access.prepost.PreAuthorize;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- @RestController
- @RequestMapping("/saleBatch")
- @AllArgsConstructor
- public class SaleBatchController extends BaseController {
- private SaleBatchService saleBatchService;
- private SaleBatchRepo saleBatchRepo;
- private SalesBatchExtensionRepo extensionRepo;
- private SaleBatchExtensionService extensionService;
- /*@PreAuthorize("hasRole('ADMIN')")
- @PostMapping("/save")
- public SaleBatch save(@RequestBody SaleBatch record) {
- if (record.getId() == null) {
- return saleBatchService.create(record);
- } else {
- return saleBatchService.update(record);
- }
- }*/
- @PreAuthorize("hasRole('ADMIN')")
- @PostMapping("/save")
- public SaleBatch saveOne(@RequestBody SaleBatch record) {
- if (record.getId() == null) {
- return saleBatchService.create1(record);
- } else {
- return saleBatchService.update1(record);
- }
- }
- @PreAuthorize("hasRole('ADMIN')")
- @PostMapping("/saveExtensions")
- public SaleBatchDto saveExtensions(@RequestBody List<SalesBatchExtension> extensions) {
- return saleBatchService.addExtensions(extensions);
- }
- @PostMapping("/all")
- public Page<SaleBatch> all() {
- PageImpl<SaleBatch> saleBatches = new PageImpl<>(saleBatchService.all());
- List<SaleBatch> content = saleBatches.getContent();
- List<SalesBatchExtension> list = extensionRepo.findAll();
- Map<Long, List<SalesBatchExtension>> map = new HashMap<>();
- list.forEach(c -> {
- if (map.containsKey(c.getBatchId())) {
- map.get(c.getBatchId()).add(c);
- } else {
- List<SalesBatchExtension> salesBatchExtensions = new ArrayList<>();
- salesBatchExtensions.add(c);
- map.put(c.getBatchId(), salesBatchExtensions);
- }
- });
- content.forEach(c -> c.setExtensions(map.get(c.getId())));
- return new PageImpl<>(content);
- }
- /*@PostMapping("/all")
- public Page<SaleBatch> all() {
- return new PageImpl<>(saleBatchService.all());
- }*/
- /*@GetMapping("/get/{id}")
- public SaleBatch get(@PathVariable Long id) {
- return saleBatchRepo.findById(id).orElseThrow(new BusinessException(Translator.toLocale("record.not_found")));
- }*/
- @GetMapping("/get/{id}")
- public SaleBatch getOne(@PathVariable Long id) {
- SaleBatch saleBatch = saleBatchRepo.findById(id).orElseThrow(new BusinessException(Translator.toLocale("record.not_found")));
- List<SalesBatchExtension> extensions = extensionRepo.findAllByBatchId(saleBatch.getId());
- saleBatch.setExtensions(extensions);
- return saleBatch;
- }
- @PostMapping("/del/{id}")
- public void deOne(@PathVariable Long id) {
- saleBatchService.del(id);
- }
- /*@PostMapping("/del/{id}")
- public void del(@PathVariable Long id) {
- saleBatchRepo.deleteById(id);
- }*/
- @GetMapping("/excel")
- @ResponseBody
- public void excel(HttpServletResponse response) throws IOException {
- List<SaleBatch> data = all().getContent();
- ExcelUtils.export(response, data);
- }
- }
|