package com.izouma.nineth.web;

import com.izouma.nineth.domain.AuctionRecord;
import com.izouma.nineth.dto.auction.AuctionRecordDTO;
import com.izouma.nineth.service.AuctionRecordService;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.AuctionRecordRepo;
import com.izouma.nineth.utils.ObjUtils;
import com.izouma.nineth.utils.SecurityUtils;
import com.izouma.nineth.utils.excel.ExcelUtils;
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.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/auctionRecord")
@AllArgsConstructor
public class AuctionRecordController extends BaseController {
    private AuctionRecordService auctionRecordService;
    private AuctionRecordRepo    auctionRecordRepo;

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


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

    @PostMapping("/recordInfos")
    public Page<AuctionRecordDTO> recordInfos(@RequestBody PageQuery pageQuery) {
        return auctionRecordService.recordInfos(pageQuery);
    }

    @PostMapping("/userRecord")
    public List<AuctionRecordDTO> userRecord(@RequestParam String type) {
        return auctionRecordService.userRecord(SecurityUtils.getAuthenticatedUser().getId(), type);
    }


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

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

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

    @GetMapping("/hasPayDeposit")
    public AuctionRecord hasPayDeposit(Long auctionId) {
        return auctionRecordRepo
                .findTopByAuctionIdAndUserIdOrderByIdDesc(auctionId, SecurityUtils.getAuthenticatedUser()
                        .getId());
    }

    @PostMapping("/create")
    public AuctionRecord created(Long auctionId, BigDecimal amount) {
        return auctionRecordService.create(SecurityUtils.getAuthenticatedUser().getId(), auctionId, amount);
    }
}

