package com.gree.mall.manager.controller.material.manage;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.PayDetail;
import com.gree.mall.manager.bean.material.manage.WebsitTradeVO;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.material.manage.WebsitTradeLogic;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@RestController
@Api(value = "交易管理API", tags ={"交易管理API"} )
@Validated
@RequestMapping(value = "/websit/trade", produces = "application/json; charset=utf-8")
public class WebsitTradeController {

    @Resource
    WebsitTradeLogic websitTradeLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "交易记录-列表")
    public ResponseHelper<IPage<WebsitTradeVO>> page(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitTradeVO> page = websitTradeLogic.page(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitTradeVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("交易记录-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitTradeVO> baseVOIPage = websitTradeLogic.page(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/trade/refund")
    @ApiOperation(value = "交易记录-退款")
    public ResponseHelper<PayDetail> tradeRefund(
            @ApiParam(value = "销售记录id", required = true) @RequestParam String id,
            @ApiParam(value = "退款金额", required = true) @RequestParam BigDecimal retAmount,
            HttpServletRequest request
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.TRADE_RECORD_LOCK + id);
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                websitTradeLogic.tradeRefund(id, retAmount, request);
            }
        } catch(Exception e) {
            log.error("【退款处理】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }
}
