package com.izouma.wenlvju.web.performance;

import com.izouma.wenlvju.domain.performance.ProgrammeAward;
import com.izouma.wenlvju.dto.PageQuery;
import com.izouma.wenlvju.dto.ProgrammeAwardDTO;
import com.izouma.wenlvju.exception.BusinessException;
import com.izouma.wenlvju.repo.performance.ProgrammeAwardRepo;
import com.izouma.wenlvju.service.performance.ProgrammeAwardService;
import com.izouma.wenlvju.utils.ObjUtils;
import com.izouma.wenlvju.utils.excel.ExcelUtils;
import com.izouma.wenlvju.web.BaseController;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/programmeAward")
@AllArgsConstructor
public class ProgrammeAwardController extends BaseController {
    private ProgrammeAwardService programmeAwardService;
    private ProgrammeAwardRepo    programmeAwardRepo;

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


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

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

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

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

    @PostMapping("/getAll")
    public List<ProgrammeAwardDTO> getAll(@RequestParam Long performanceId) {
        return programmeAwardService.getAll1(performanceId);
    }

    @PostMapping("/getAll2")
    public List<ProgrammeAwardDTO> getAll2(@RequestParam Long performanceId) {
        return programmeAwardService.getAll2(performanceId);
    }

    @PostMapping("/batchSave")
    public void batchSave(@RequestBody List<ProgrammeAwardDTO> dtos) {
        programmeAwardService.batchSave(dtos);
    }

    @PostMapping("/saveProgramme")
    public void saveProgramme(@RequestBody List<ProgrammeAwardDTO> dtos) {
        programmeAwardService.saveProgramme1(dtos);
    }

}

