package com.izouma.dingdong.web.backstage;

import com.izouma.dingdong.domain.OrderRefundApply;
import com.izouma.dingdong.enums.RefundReason;
import com.izouma.dingdong.enums.Solution;
import com.izouma.dingdong.service.OrderRefundApplyService;
import com.izouma.dingdong.service.user.UserCouponService;
import com.izouma.dingdong.web.BaseController;
import com.izouma.dingdong.domain.backstage.Complaint;
import com.izouma.dingdong.service.backstage.ComplaintService;
import com.izouma.dingdong.dto.PageQuery;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.backstage.ComplaintRepo;
import com.izouma.dingdong.utils.ObjUtils;
import com.izouma.dingdong.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("/complaint")
@AllArgsConstructor
public class ComplaintController extends BaseController {
    private ComplaintService complaintService;
    private ComplaintRepo complaintRepo;
    private OrderRefundApplyService orderRefundApplyService;
    private UserCouponService userCouponService;

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


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

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

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

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

    @GetMapping("/refund")
    public void refund(Long id, BigDecimal merchantLiability, BigDecimal riderLiability, String remark) {
        Complaint complaint = complaintRepo.findById(id).orElseThrow(new BusinessException("无记录"));
        OrderRefundApply apply = orderRefundApplyService.apply(complaint.getOrderId(), RefundReason.USER_COMPLAINTS, remark,null);
        orderRefundApplyService.audit(apply.getId(), false, false);
        orderRefundApplyService.platformAudit(apply.getId(), true, merchantLiability, riderLiability, true);
        complaint.setSolution(Solution.REFUND);
        complaint.setResolve(true);
        complaintRepo.save(complaint);
    }

    @GetMapping("/issue")
    public void issue(Long id, Long userId, String couponId) {
        Complaint complaint = complaintRepo.findById(id).orElseThrow(new BusinessException("无记录"));
        userCouponService.issue(userId, couponId);
        complaint.setSolution(Solution.ISSUE_COUPONS);
        complaint.setResolve(true);
        complaintRepo.save(complaint);
    }
}

