package com.yonge.cooleshow.admin.controller;

import com.yonge.cooleshow.mall.common.enums.OrderCacheEnum;
import com.yonge.cooleshow.mall.common.api.CommonPage;
import com.yonge.cooleshow.mall.common.api.CommonResult;
import com.yonge.cooleshow.admin.dto.OmsOrderReturnApplyResult;
import com.yonge.cooleshow.admin.dto.OmsReturnApplyQueryParam;
import com.yonge.cooleshow.admin.dto.OmsUpdateStatusParam;
import com.yonge.cooleshow.mbg.model.OmsOrderReturnApply;
import com.yonge.cooleshow.admin.service.OmsOrderReturnApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 订单退货申请管理Controller
 * Created by macro on 2018/10/18.
 */
@Controller
@Api(tags = "OmsOrderReturnApplyController", description = "订单退货申请管理")
@RequestMapping("/returnApply")
public class OmsOrderReturnApplyController {
    @Autowired
    private OmsOrderReturnApplyService returnApplyService;

    @Autowired
    private RedissonClient redissonClient;

    private static final Logger log = LoggerFactory.getLogger(OmsOrderReturnApplyController.class);

    @ApiOperation("分页查询退货申请")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<OmsOrderReturnApply>> list(OmsReturnApplyQueryParam queryParam,
                                                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<OmsOrderReturnApply> returnApplyList = returnApplyService.list(queryParam, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(returnApplyList));
    }

    @ApiOperation("批量删除退货申请")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = returnApplyService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取退货申请详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getItem(@PathVariable Long id) {
        OmsOrderReturnApplyResult result = returnApplyService.getItem(id);
        return CommonResult.success(result);
    }

    @ApiOperation("修改退货申请状态")
    @RequestMapping(value = "/update/status/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateStatus(@PathVariable Long id, @RequestBody OmsUpdateStatusParam statusParam) {
        RLock lock = redissonClient.getLock(OrderCacheEnum.LOCK_REFUND_ORDER_MALL + ":refundOrder:" + id);
        try {
            boolean b = lock.tryLock(60, 60, TimeUnit.SECONDS);
            if (b) {
                int count = returnApplyService.updateStatus(id, statusParam);
                if (count > 0) {
                    return CommonResult.success(count);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.warn("");
        } finally {
            if (lock.getHoldCount() >0) {
                lock.unlock();
            }
        }

        return CommonResult.failed();
    }

}
