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

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.manage.WebsitMPurchaseBean;
import com.gree.mall.manager.bean.material.vender.WebsitPurchaseCheckVO;
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.manage.WebsitMPurchaseLogic;
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 = "供应商核实采购API", tags ={"供应商核实采购API"} )
@RequestMapping(value = "/vender/check/purchase", produces = "application/json; charset=utf-8")
public class VenderCheckPurchaseController {

    @Resource
    WebsitMPurchaseLogic websitMPurchaseLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "核实采购-列表")
    public ResponseHelper<IPage<WebsitPurchaseCheckVO>> page(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitPurchaseCheckVO> page = websitMPurchaseLogic.checkPage(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitPurchaseCheckVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("核实采购-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitPurchaseCheckVO> baseVOIPage = websitMPurchaseLogic.checkPage(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "核实采购-详情")
    public ResponseHelper<WebsitMPurchaseBean> detail(
            @ApiParam(value = "purchaseId", required = true) @RequestParam String purchaseId
    ) throws RemoteServiceException {
        WebsitMPurchaseBean bean = websitMPurchaseLogic.detail(purchaseId);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/update")
    @ApiOperation(value = "核实采购-修改")
    public ResponseHelper update(@RequestBody WebsitMPurchaseBean bean) throws RemoteServiceException {
        websitMPurchaseLogic.checkUpdate(bean);
        return ResponseHelper.success();
    }

    @PostMapping("/revoke")
    @ApiOperation(value = "核实采购-撤消")
    public ResponseHelper revoke(@ApiParam(value = "采购申请单id",required = true) @RequestParam String purchaseId)
            throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_MATERIAL_PURCHASE + purchaseId);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再尝试");
        }
        try {
            websitMPurchaseLogic.revoke(purchaseId, 2);
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

    @PostMapping("/verify")
    @ApiOperation(value = "核实采购-核实")
    public ResponseHelper verify(@ApiParam(value = "采购申请单id",required = true) @RequestParam String purchaseId)
            throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_MATERIAL_PURCHASE + purchaseId);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再尝试");
        }
        try {
            websitMPurchaseLogic.verify(purchaseId);
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

}
