package com.izouma.jmrh.web;

import com.izouma.jmrh.domain.ProjectManagement;
import com.izouma.jmrh.service.ProjectManagementService;
import com.izouma.jmrh.dto.PageQuery;
import com.izouma.jmrh.exception.BusinessException;
import com.izouma.jmrh.repo.ProjectManagementRepo;
import com.izouma.jmrh.utils.ObjUtils;
import com.izouma.jmrh.utils.excel.ExcelUtils;
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 javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/projectManagement")
@AllArgsConstructor
@Slf4j
public class ProjectManagementController extends BaseController {
    private ProjectManagementService projectManagementService;
    private ProjectManagementRepo    projectManagementRepo;

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


    //@PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/all")
    public Page<ProjectManagement> all(PageQuery pageQuery) {
        return projectManagementRepo
                .findAll(toSpecification(pageQuery, ProjectManagement.class), toPageRequest(pageQuery));
    }

    @GetMapping("/get/{id}")
    public ProjectManagement get(@PathVariable Long id) {
        return projectManagementRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

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

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

    @PostMapping("/review")
    public void review(@RequestParam Long id, @RequestParam String review) {
        ProjectManagement projectManagement = projectManagementRepo.findById(id)
                .orElseThrow(new BusinessException("无记录"));
        projectManagement.setReview(review);
        projectManagement.setReviewStatus("已评审");
        projectManagementRepo.save(projectManagement);
    }

    @PostMapping("/audit")
    public void audit(@RequestParam Long id, @RequestParam String status) {
        ProjectManagement projectManagement = projectManagementRepo.findById(id)
                .orElseThrow(new BusinessException("无记录"));
        projectManagement.setPassStatus(status);
        projectManagementRepo.save(projectManagement);
    }
}

