package com.gree.mall.miniapp.controller.order;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.miniapp.bean.PayDetail;
import com.gree.mall.miniapp.bean.order.LeaseOrderBean;
import com.gree.mall.miniapp.bean.order.LeaseOrderVO;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.constant.Constant;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.logic.order.LeaseOrderLogic;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "租赁订单列表", tags ={"租赁订单列表"} )
@RequestMapping(value = "/lease/order", produces = "application/json; charset=utf-8")
public class LeaseOrderController {

    @Resource
    LeaseOrderLogic leaseOrderLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;
    @Resource
    CommonLogic commonLogic;

    @PostMapping("/add")
    @ApiOperation(value = "提交订单")
    public ResponseHelper<String> add(
            @RequestBody LeaseOrderBean bean
    ) throws Exception {
        final CurrentCompanyWechat wechat = commonLogic.getCurrentCompanyWechat();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LEASE_ORDER_LOCK + ":" + wechat.getUserId());
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再尝试");
        }
        try {
            String id = leaseOrderLogic.add(bean);

            return ResponseHelper.success(id);
        } finally {
            obtain.unlock();
        }

    }

    @PostMapping("/list/page")
    @ApiOperation(value = "订单列表")
    public ResponseHelper<IPage<LeaseOrderVO>> page(
            @ApiParam(value = "关键词") @RequestParam(required = false) String keyword,
            @ApiParam(value = "支付状态 true=已支付 false=未支付") @RequestParam(required = false) Boolean payStatus,
            @ApiParam(value = "订单状态 WAIT=待确认 WAIT_START=待开始 LEASE=租赁中 OVER=已到期，待回收 RECOVER=已到期，已回收 CANCEL=已取消") @RequestParam(required = false) String status,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<LeaseOrderVO> page = leaseOrderLogic.page(keyword, payStatus, status, pageNum, pageSize);
        return ResponseHelper.success(page);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "详情")
    public ResponseHelper<LeaseOrderBean> detail(
            @ApiParam(value = "单据编号",required = true) @RequestParam String id
    ) throws Exception {
        LeaseOrderBean bean = leaseOrderLogic.detail(id);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/pay")
    @ApiOperation(value = "支付")
    public ResponseHelper<PayDetail> pay(
            HttpServletRequest request,
            @ApiParam(value = "单据编号",required = true) @RequestParam String id,
            @ApiParam(value = "是否小程序兼容") @RequestParam (required = false) Boolean miniPay,
            @ApiParam(value = "openId") @RequestParam(required = false) String openId,
            @ApiParam(value = "续租结束日期") @RequestParam(required = false) Date endDate,
            @ApiParam(value = "备注") @RequestParam(required = false) String remark
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LEASE_ORDER_LOCK + ":" + id);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再尝试");
        }
        try {
            PayDetail detail = leaseOrderLogic.pay(request, id, miniPay, openId, endDate, remark);
            return ResponseHelper.success(detail);
        } finally {
            obtain.unlock();
        }
    }

    @PostMapping("/compute/renewal/amount")
    @ApiOperation(value = "计算续租金额")
    public ResponseHelper<BigDecimal> computeRenewalAmount(
            @ApiParam(value = "单据编号",required = true) @RequestParam String id,
            @ApiParam(value = "续租结束日期") @RequestParam(required = false) Date endDate
    ) throws Exception {
        BigDecimal amount = leaseOrderLogic.computeRenewalAmount(id, endDate);
        return ResponseHelper.success(amount);
    }

}
