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

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.material.worker.NewRefundManageBean;
import com.gree.mall.manager.bean.material.worker.NewRefundManageVO;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.material.worker.NewRefundManageLogic;
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.web.bind.annotation.*;

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

@Slf4j
@RestController
@Api(value = "新件返还管理", tags = {"新件返还管理"})
@RequestMapping(value = "/worker/new-refund", produces = "application/json; charset=utf-8")
public class NewRefundController {

    @Resource
    NewRefundManageLogic newRefundManageLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation("新件返还管理列表")
    public ResponseHelper<IPage<NewRefundManageVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<NewRefundManageVO> stockBeanIPage = newRefundManageLogic.list(zfireParamBean);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<NewRefundManageVO>() {
        });
    }

    @PostMapping("/list/export")
    @ApiOperation("新件返还管理列表导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<NewRefundManageVO> stockBeanIPage = newRefundManageLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "新件返还管理详情")
    public ResponseHelper<NewRefundManageBean> detail(
            @ApiParam(value = "申请单号", required = true) @RequestParam String applyNo
    ) throws RemoteServiceException {
        NewRefundManageBean bean = newRefundManageLogic.detail(applyNo);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/examine")
    @ApiOperation(value = "新件返还审批")
    public ResponseHelper examine(
            @ApiParam(value = "申请单号", required = true) @RequestParam String applyNo,
            @ApiParam(value = "审核结果 AGREE=通过 REJECT=驳回 REFUNDED=已返还", required = true) @RequestParam String flag,
            @ApiParam(value = "审核备注") @RequestParam(required = false) String examineRemark
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.NEW_REFUND + applyNo);
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                newRefundManageLogic.examine(applyNo, flag, examineRemark);
            }
        } catch (Exception e) {
            log.error("【新件返还审批处理】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

    @PostMapping("/refund")
    @ApiOperation(value = "新件返还确认返还")
    public ResponseHelper refund(
            @RequestParam String applyNo
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.NEW_REFUND + applyNo);
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                newRefundManageLogic.refund(applyNo);
            }
        } catch (Exception e) {
            log.error("【新件返还确认处理】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

}
