package com.izouma.wenlvju.web;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.izouma.wenlvju.domain.RateExpertAudit;
import com.izouma.wenlvju.service.RateExpertAuditService;
import com.izouma.wenlvju.dto.PageQuery;
import com.izouma.wenlvju.exception.BusinessException;
import com.izouma.wenlvju.repo.RateExpertAuditRepo;
import com.izouma.wenlvju.utils.ObjUtils;
import com.izouma.wenlvju.utils.SecurityUtils;
import com.izouma.wenlvju.utils.excel.ExcelUtils;
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("/rateExpertAudit")
@AllArgsConstructor
public class RateExpertAuditController extends BaseController {
    private RateExpertAuditService rateExpertAuditService;
    private RateExpertAuditRepo    rateExpertAuditRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public RateExpertAudit save(@RequestBody RateExpertAudit record) {
        if (record.getId() != null) {
            RateExpertAudit orig = rateExpertAuditRepo.findById(record.getId())
                    .orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return rateExpertAuditRepo.save(orig);
        }
        List<RateExpertAudit> byRateId = rateExpertAuditRepo.findAllByRateId(record.getRateId());
        if (CollUtil.isNotEmpty(byRateId)) {
            throw new BusinessException("已评分");
        }
//        record.setUserId(SecurityUtils.getAuthenticatedUser().getId());
        return rateExpertAuditRepo.save(record);
    }

    @PreAuthorize("hasAnyRole('ADMIN','DISTRICT','DISTRICT_STAFF','EXPERT')")
    @PostMapping("/batchSave")
    public void batchSave(String audits) {
        List<RateExpertAudit> rateExpertAudits = JSONObject.parseArray(audits, RateExpertAudit.class);
        rateExpertAuditService.batchSave(rateExpertAudits);
    }

    @PostMapping("/grade")
    public void grade(Long rateId) {
        rateExpertAuditService.grade(rateId, SecurityUtils.getAuthenticatedUser().getId());
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<RateExpertAudit> all(@RequestBody PageQuery pageQuery) {
        pageQuery.setSort("createdAt,desc");
        pageQuery.getQuery().remove("size");
        return rateExpertAuditService.all(pageQuery);
    }

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

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        rateExpertAuditRepo.softDelete(id);
    }

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

