package com.izouma.nineth.web;

import com.izouma.nineth.domain.AuctionOrder;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.enums.AuctionPaymentType;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.AuctionOrderRepo;
import com.izouma.nineth.service.AuctionOrderService;
import com.izouma.nineth.utils.ObjUtils;
import com.izouma.nineth.utils.excel.ExcelUtils;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
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("/auctionOrder")
@AllArgsConstructor
public class AuctionOrderController extends BaseController {
    private AuctionOrderService auctionOrderService;
    private AuctionOrderRepo    auctionOrderRepo;

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


    }

    @PostMapping("/create")
    @ApiModelProperty(value = "创建拍卖订单（支付）")
    public AuctionOrder create(Long userId, @RequestParam(required = false) Long auctionId,
                               Long addressId, Long auctionRecordId, AuctionPaymentType type) {
        return auctionOrderService.create(userId, auctionId, addressId, auctionRecordId, type);
    }

    @PostMapping("/createFixPrice")
    @ApiModelProperty(value = "创建拍卖订单（一口价）")
    public AuctionOrder createFixPrice(Long userId, @RequestParam(required = false) Long auctionId,
                                       Long addressId, AuctionPaymentType type) {
//        AuctionActivity auctionActivity = auctionActivityRepo.findById(auctionId)
//                .orElseThrow(new BusinessException("暂无"));
//        AuctionRecord auctionRecord = AuctionRecord.builder()
//                .auctionId(auctionId)
//                .type(AuctionRecordType.FIXEDPRICE)
//                .bidderPrice(auctionActivity.getFixedPrice())
//                .auctionPic(null)
//                .userId(SecurityUtils.getAuthenticatedUser().getId())
//                .avatar(SecurityUtils.getAuthenticatedUser().getAvatar())
//                .name(auctionActivity.getName())
//                .purchased(false)
//                .build();
//        AuctionRecord record = auctionRecordRepo.save(auctionRecord);
        return auctionOrderService.create(userId, auctionId, addressId, null, type);
    }

    @PostMapping("/createDeposit")
    @ApiModelProperty(value = "创建拍卖订单（保证金）")
    public AuctionOrder createDeposit(Long userId, @RequestParam(required = false) Long auctionId,
                                      Long addressId, AuctionPaymentType type) {

//        AuctionActivity auctionActivity = auctionActivityRepo.findById(auctionId)
//                .orElseThrow(new BusinessException("暂无"));
//        AuctionRecord auctionRecord = AuctionRecord.builder()
//                .auctionId(auctionId)
//                .type(AuctionRecordType.DEPOSIT)
//                .bidderPrice(auctionActivity.getDeposit())
//                .auctionPic(null)
//                .userId(SecurityUtils.getAuthenticatedUser().getId())
//                .avatar(SecurityUtils.getAuthenticatedUser().getAvatar())
//                .name(auctionActivity.getName())
//                .purchased(false)
//                .build();
//        AuctionRecord record = auctionRecordRepo.save(auctionRecord);
        return auctionOrderService.create(userId, auctionId, addressId, null, type);
    }

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

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

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

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

    @ApiOperation("订单完成")
    @GetMapping("/finish/{id}")
    public void finish(@PathVariable Long id) {
        auctionOrderService.finish(id);
    }

    @ApiOperation("订单发货")
    @GetMapping("/delivery")
    public void delivery(@RequestParam Long id, @RequestParam String courierId) {
        auctionOrderService.dispatch(id, courierId);
    }
}

