package com.izouma.jiashanxia.web;

import com.izouma.jiashanxia.domain.OrderInfo;
import com.izouma.jiashanxia.dto.OrderInfoVO;
import com.izouma.jiashanxia.enums.OrderInfoStatus;
import com.izouma.jiashanxia.enums.PayMethod;
import com.izouma.jiashanxia.service.OrderInfoService;
import com.izouma.jiashanxia.dto.PageQuery;
import com.izouma.jiashanxia.exception.BusinessException;
import com.izouma.jiashanxia.repo.OrderInfoRepo;
import com.izouma.jiashanxia.utils.ObjUtils;
import com.izouma.jiashanxia.utils.SecurityUtils;
import com.izouma.jiashanxia.utils.excel.ExcelUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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("/orderInfo")
@AllArgsConstructor
public class OrderInfoController extends BaseController {
    private OrderInfoService orderInfoService;
    private OrderInfoRepo    orderInfoRepo;

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


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

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/backAll")
    public Page<OrderInfo> backAll(@RequestBody PageQuery pageQuery) {
        return orderInfoService.backAll(pageQuery);
    }

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

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

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

    @PostMapping("/create")
    @ApiOperation("创建订单")
    public OrderInfo create(@RequestParam Long packageId, @RequestParam PayMethod payMethod) {
        return orderInfoService.createOrder(SecurityUtils.getAuthenticatedUser().getId(), packageId, payMethod);
    }

    @PostMapping("/cancel")
    @ApiOperation("取消订单")
    public OrderInfo cancel(Long orderId) {
        return orderInfoService.cancelOrder(orderId);
    }

    @PostMapping("/my")
    public Page<OrderInfoVO> my(Pageable pageable, @RequestParam(required = false) List<OrderInfoStatus> status) {
        return orderInfoService.my(pageable, SecurityUtils.getAuthenticatedUser().getId(), status);
    }

    @GetMapping("/count")
    public long count() {
        return orderInfoRepo.countByUserId(SecurityUtils.getAuthenticatedUser().getId());
    }
}

