package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.req.AuthOperaReq;
import com.yonge.cooleshow.biz.dal.dto.search.UserOrderRefundSearch;
import com.yonge.cooleshow.biz.dal.entity.UserOrderRefundBill;
import com.yonge.toolset.mybatis.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.UserOrderRefundVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.payment.base.model.RefundBill;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import com.yonge.cooleshow.biz.dal.service.UserOrderRefundService;

@RestController
@RequestMapping("/userOrderRefunds")
@Api(value = "用户退款表", tags = "用户退款表")
public class UserOrderRefundController extends BaseController {
	@Autowired
	private SysUserFeignService sysUserFeignService;
    @Autowired
    private UserOrderRefundService userOrderRefundService;

	/**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入id")
	@PreAuthorize("@pcs.hasPermissions('userOrderRefunds/detail')")
	public HttpResponseResult<UserOrderRefundVo> detail(@PathVariable("id") Long id) {
    	return succeed(userOrderRefundService.detail(id));
	}
    
    /**
     * 查询分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询分页", notes = "传入orderRefundsSearch")
	@PreAuthorize("@pcs.hasPermissions('userOrderRefunds/page')")
	public HttpResponseResult<PageInfo<UserOrderRefundVo>> page(@RequestBody UserOrderRefundSearch query) {
		IPage<UserOrderRefundVo> pages = userOrderRefundService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
	}

	@PostMapping("/doAuth")
	@ApiOperation(value = "审核", notes = "传入authOperaReq")
	@PreAuthorize("@pcs.hasPermissions('userOrderRefunds/doAuth')")
	public HttpResponseResult<Boolean> doAuth(@Valid @RequestBody AuthOperaReq authOperaReq) throws Exception {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || null == user.getId()) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		return userOrderRefundService.doAuth(authOperaReq, user);
	}

	@PostMapping("/orderRefund")
	@ApiOperation(value = "直接退款", notes = "传入authOperaReq")
	@PreAuthorize("@pcs.hasPermissions('userOrderRefunds/orderRefund')")
	public HttpResponseResult<UserOrderRefundBill> orderRefund(String orderNo, String reason) throws Exception {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || null == user.getId()) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		return userOrderRefundService.orderRefund(orderNo, reason);
	}

	@PostMapping("/orderRefundSuccessBizHandle")
	@ApiOperation(value = "调用退款后处理业务逻辑", notes = "传入authOperaReq")
	@PreAuthorize("@pcs.hasPermissions('userOrderRefunds/orderRefundSuccessBizHandle')")
	public HttpResponseResult<UserOrderRefundBill> orderRefundSuccessBizHandle(Long refundId) throws Exception {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || null == user.getId()) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		userOrderRefundService.orderRefundSuccessBizHandle(refundId);
		return HttpResponseResult.succeed();
	}

}