package com.yonge.cooleshow.student.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.OrderRefundReq;
import com.yonge.cooleshow.biz.dal.dto.search.UserOrderRefundSearch;
import com.yonge.cooleshow.common.enums.CacheNameEnum;
import com.yonge.cooleshow.biz.dal.service.UserOrderRefundService;
import com.yonge.toolset.payment.util.DistributedLock;
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.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

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

    /**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入id")
    public HttpResponseResult<UserOrderRefundVo> detail(@PathVariable("id") Long id) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(userOrderRefundService.detail(id));
    }

    /**
     * 查询分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询分页", notes = "传入orderRefundsSearch")
    public HttpResponseResult<PageInfo<UserOrderRefundVo>> page(@RequestBody UserOrderRefundSearch query) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        query.setUserId(user.getId());
        IPage<UserOrderRefundVo> pages = userOrderRefundService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }

    @ApiOperation(value = "订单退款")
    @PostMapping("/orderRefund")
    public HttpResponseResult<Boolean> orderRefund(@RequestBody OrderRefundReq refundReq) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        refundReq.setUserId(user.getId());

        try {
            HttpResponseResult<Boolean> res = DistributedLock.of(redissonClient)
                    .runIfLockToFunction(CacheNameEnum.LOCK_EXECUTE_ORDER.getRedisKey(refundReq.getOrderNo())
                            , userOrderRefundService::orderRefund, refundReq, 10L);
            if (null != res) {
                return res;
            } else {
                return HttpResponseResult.failed("订单退款失败");
            }
        } catch (BizException e) {
            return HttpResponseResult.failed(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return HttpResponseResult.failed("订单退款失败");
        }
    }
}