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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.bean.order.LeaseOrderBean;
import com.gree.mall.manager.bean.order.LeaseOrderVO;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.enums.LeaseOrderStatusEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.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
    private RedisLockRegistry redisLockRegistry;

    @PostMapping("/list/page")
    @ApiOperation(value = "订单列表")
    public ResponseHelper<IPage<LeaseOrderVO>> page(
            @ApiParam(value = "订单Id") @RequestParam(required = false) String orderId,
            @ApiParam(value = "商品名称") @RequestParam(required = false) String goodsName,
            @ApiParam(value = "会员昵称") @RequestParam(required = false) String userName,
            @ApiParam(value = "手机号") @RequestParam(required = false) String userMobile,
            @ApiParam(value = "订单状态") @RequestParam(required = false) LeaseOrderStatusEnum status,
            @ApiParam(value = "创建开始时间") @RequestParam(required = false) Date startDate,
            @ApiParam(value = "创建结束时间") @RequestParam(required = false) Date endDate,
            @ApiParam(value = "支付开始时间") @RequestParam(required = false) Date startPayDate,
            @ApiParam(value = "支付结束时间") @RequestParam(required = false) Date endPayDate,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize,
            HttpServletRequest request
    ) throws Exception {
        IPage<LeaseOrderVO> page = leaseOrderLogic.page(orderId, goodsName, userName,
                userMobile, status, startDate, endDate, startPayDate, endPayDate, pageNum, pageSize , request);
        return ResponseHelper.success(page);
    }

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

    @PostMapping("/add")
    @ApiOperation(value = "新增")
    public ResponseHelper add(
            @RequestBody LeaseOrderBean bean
    ) throws Exception {
        leaseOrderLogic.add(bean);
        return ResponseHelper.success();
    }

    @PostMapping("/cancel")
    @ApiOperation(value = "取消订单")
    public ResponseHelper cancel(
            @ApiParam(value = "订单id") @RequestParam String id
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + ":" + id);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再尝试");
        }
        try {
            leaseOrderLogic.cancel(id);
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

    @PostMapping("/confirm")
    @ApiOperation(value = "确认订单")
    public ResponseHelper confirm(
            @RequestBody LeaseOrderBean bean
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + ":" + bean.getId());
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再尝试");
        }
        try {
            leaseOrderLogic.confirm(bean);
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

    @PostMapping("/recover")
    @ApiOperation(value = "确定回收")
    public ResponseHelper recover(
            @ApiParam(value = "订单id") @RequestParam String id,
            @ApiParam(value = "实际到期日期") @RequestParam Date RealEndDate
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + ":" + id);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再尝试");
        }
        try {
            leaseOrderLogic.recover(id, RealEndDate);
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

    @PostMapping("/renewal")
    @ApiOperation(value = "续期")
    public ResponseHelper renewal(
            @ApiParam(value = "订单id") @RequestParam String id,
            @ApiParam(value = "实际到期日期") @RequestParam Date RealEndDate
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + ":" + id);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再尝试");
        }
        try {
            leaseOrderLogic.renewal(id, RealEndDate);
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

    @PostMapping("/compute/amount")
    @ApiOperation(value = "计算金额")
    public ResponseHelper<BigDecimal> computeAmount(
            @ApiParam(value = "是否续租 true=是 false=否") @RequestParam Boolean isRenewal,
            @ApiParam(value = "开始日期") @RequestParam Date startDate,
            @ApiParam(value = "结束日期") @RequestParam Date endDate,
            @ApiParam(value = "数量") @RequestParam BigDecimal qty,
            @ApiParam(value = "价格") @RequestParam BigDecimal price
    ) throws Exception {
        BigDecimal amount = leaseOrderLogic.computeAmount(isRenewal, startDate, endDate, qty, price);
        return ResponseHelper.success(amount);
    }
}
