package com.izouma.dingdong.web;

import com.izouma.dingdong.domain.OrderRefundApply;
import com.izouma.dingdong.dto.PageQuery;
import com.izouma.dingdong.enums.RefundReason;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.OrderRefundApplyRepo;
import com.izouma.dingdong.service.OrderRefundApplyService;
import com.izouma.dingdong.service.merchant.MerchantService;
import com.izouma.dingdong.utils.ObjUtils;
import com.izouma.dingdong.utils.SecurityUtils;
import com.izouma.dingdong.utils.excel.ExcelUtils;
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.math.BigDecimal;
import java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("/orderRefundApply")
public class OrderRefundApplyController extends BaseController {
    /*generatedStart*/
    private OrderRefundApplyService orderRefundApplyService;

    private OrderRefundApplyRepo orderRefundApplyRepo;

    private MerchantService merchantService;
    /*generatedEnd*/

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


    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/all")
    public Page<OrderRefundApply> all(PageQuery pageQuery) {
        return orderRefundApplyRepo.findAll(toSpecification(pageQuery, OrderRefundApply.class), toPageRequest(pageQuery));
    }

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

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

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

    @GetMapping("/apply")
    @ApiOperation("申请退款")
    public void apply(@RequestParam Long orderId, RefundReason reason, String remark, String img) {
        orderRefundApplyService.apply(orderId, reason, remark, img);
    }

    @ApiOperation("商家是否同意退款")
    @GetMapping("/audit")
    public void consent(@RequestParam Long id, @RequestParam Boolean consent) {
        //需要商家同意的订单，都为骑手已经接到货
        orderRefundApplyService.audit(id, consent, true);
    }

    @GetMapping("/cancel")
    @ApiOperation("取消退款")
    public void cancel(Long orderId) {
        orderRefundApplyService.cancelRefundApply(orderId);
    }


    @GetMapping("/merchant")
    @ApiOperation("商家退款订单")
    public List<OrderRefundApply> cancel() {
        return orderRefundApplyRepo.findAllByMerchantId(merchantService.findMerchantId(SecurityUtils.getAuthenticatedUser()
                .getId()));
    }

    @ApiOperation("平台是否同意退款")
    @GetMapping("/platformAudit")
    public void platformAudit(Long id, Boolean agree, BigDecimal merchantLiability, BigDecimal riderLiability) {
        orderRefundApplyService.platformAudit(id, agree, merchantLiability, riderLiability, false);
    }

    @ApiOperation("已打款")
    @GetMapping("/payment")
    public void payment(@RequestParam Long id) {
        orderRefundApplyService.payment(id);
    }
}

