package com.izouma.zhirongip.web.resource;

import com.izouma.zhirongip.domain.resource.IntellectualProperty;
import com.izouma.zhirongip.dto.IntellectualPropertyDTO;
import com.izouma.zhirongip.dto.PageQuery;
import com.izouma.zhirongip.exception.BusinessException;
import com.izouma.zhirongip.repo.resource.IntellectualPropertyRepo;
import com.izouma.zhirongip.service.resource.IntellectualPropertyService;
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;

@RestController
@RequestMapping("/intellectualProperty")
@AllArgsConstructor
@Slf4j
public class IntellectualPropertyController extends BaseController {
    private final IntellectualPropertyService intellectualPropertyService;
    private final IntellectualPropertyRepo    intellectualPropertyRepo;

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


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

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

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

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

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

