package com.izouma.zhirongip.web.resource;

import com.izouma.zhirongip.domain.resource.Digital;
import com.izouma.zhirongip.dto.PageQuery;
import com.izouma.zhirongip.dto.ProductDTO;
import com.izouma.zhirongip.exception.BusinessException;
import com.izouma.zhirongip.repo.resource.DigitalRepo;
import com.izouma.zhirongip.service.resource.DigitalService;
import com.izouma.zhirongip.utils.ObjUtils;
import com.izouma.zhirongip.utils.excel.ExcelUtils;
import com.izouma.zhirongip.web.BaseController;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/digital")
@AllArgsConstructor
@Slf4j
public class DigitalController extends BaseController {
    private final DigitalService digitalService;
    private final DigitalRepo    digitalRepo;

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public Digital save(@RequestBody Digital record) {
        if (record.getId() != null) {
            Digital orig = digitalRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return digitalRepo.save(orig);
        }
        return digitalRepo.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<Digital> all(@RequestBody PageQuery pageQuery) {
        return digitalService.all(pageQuery);
    }

    @GetMapping("/get/{id}")
    public Digital get(@PathVariable Long id) {
        return digitalService.get(id);
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        digitalRepo.softDelete(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<Digital> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @GetMapping("/excelProduct")
    @ResponseBody
    public void excelProduct(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<Digital> data = all(pageQuery).getContent();
        List<ProductDTO> dtos = data.stream().map(ProductDTO::new).collect(Collectors.toList());
        ExcelUtils.export(response, dtos);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/upload")
    public void uploadFile(@RequestParam("file") MultipartFile file) {
        InputStream is;
        try {
            is = file.getInputStream();
            digitalService.upload(is);
        } catch (IOException e) {
            log.error("上传失败", e);
            throw new BusinessException("上传失败", e.getMessage());
        }
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/uploadProduct")
    public void uploadProduct(@RequestParam("file") MultipartFile file) {
        InputStream is;
        try {
            is = file.getInputStream();
            digitalService.uploadProduct(is);
        } catch (IOException e) {
            log.error("上传失败", e);
            throw new BusinessException("上传失败", e.getMessage());
        }
    }
}

