package com.izouma.zhirongip.web.demand;

import com.izouma.zhirongip.domain.demand.CopyrightDemand;
import com.izouma.zhirongip.dto.PageQuery;
import com.izouma.zhirongip.exception.BusinessException;
import com.izouma.zhirongip.repo.SettingRepo;
import com.izouma.zhirongip.repo.demand.CopyrightDemandRepo;
import com.izouma.zhirongip.service.demand.CopyrightDemandService;
import com.izouma.zhirongip.utils.ObjUtils;
import com.izouma.zhirongip.utils.SecurityUtils;
import com.izouma.zhirongip.utils.excel.ExcelUtils;
import com.izouma.zhirongip.web.BaseController;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
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("/copyrightDemand")
@AllArgsConstructor
public class CopyrightDemandController extends BaseController {
    private final CopyrightDemandService copyrightDemandService;
    private final CopyrightDemandRepo    copyrightDemandRepo;
    private final SettingRepo            settingRepo;


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


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

    @GetMapping("/get/{id}")
    public CopyrightDemand get(@PathVariable Long id) {
        CopyrightDemand record = copyrightDemandRepo.findById(id).orElseThrow(new BusinessException("无记录"));
        settingRepo.findById(record.getWorkType())
                .ifPresent(setting -> record.setWorkName(setting.getName()));
        if(record.getModeId()!=null){
            settingRepo.findById(record.getModeId())
                    .ifPresent(setting -> record.setMode(setting.getName()));
        }
        return record;
    }

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

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

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("审核")
    @PostMapping("/audit")
    public void audit(@RequestParam Long id, @RequestParam boolean pass, String remark) {
        copyrightDemandService.audit(id, pass, remark);
    }
}

