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.material.manage.WebsitPurchaseAdjustmentBean;
import com.gree.mall.manager.bean.material.manage.WebsitPurchaseAdjustmentVO;
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.WebsitPurchaseAdjustmentLogic;
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.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "网点采购调整API", tags ={"网点采购调整API"} )
@Validated
@RequestMapping(value = "/websit/purchase/adjustment", produces = "application/json; charset=utf-8")
public class WebsitPurchaseAdjustmentController {

    @Resource
    WebsitPurchaseAdjustmentLogic websitPurchaseAdjustmentLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "网点采购调整-列表")
    public ResponseHelper<IPage<WebsitPurchaseAdjustmentVO>> page(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitPurchaseAdjustmentVO> page = websitPurchaseAdjustmentLogic.page(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitPurchaseAdjustmentVO>() {});
    }

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

    @PostMapping("/detail")
    @ApiOperation(value = "网点采购调整-详情")
    public ResponseHelper<WebsitPurchaseAdjustmentBean> detail(
            @ApiParam(value = "purchaseAdjustmentId", required = true) @RequestParam String purchaseAdjustmentId
    ) throws RemoteServiceException {
        WebsitPurchaseAdjustmentBean bean = websitPurchaseAdjustmentLogic.detail(purchaseAdjustmentId);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/add")
    @ApiOperation(value = "网点采购调整-添加")
    public ResponseHelper<WebsitPurchaseAdjustmentBean> add(
            @RequestBody WebsitPurchaseAdjustmentBean bean
    ) throws Exception {
        WebsitPurchaseAdjustmentBean result;
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_MATERIAL_PURCHASE + bean.getRef());
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再尝试");
        }
        try {
            result = websitPurchaseAdjustmentLogic.add(bean);
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success(result);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改采购调整单")
    public ResponseHelper update(@RequestBody WebsitPurchaseAdjustmentBean purchaseAdjustmentBean) throws RemoteServiceException {
        websitPurchaseAdjustmentLogic.update(purchaseAdjustmentBean);
        return ResponseHelper.success();
    }

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

        return ResponseHelper.success();
    }

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

        return ResponseHelper.success();
    }

    @PostMapping("/del")
    @ApiOperation(value = "采购调整单删除")
    public ResponseHelper del(@ApiParam(value = "采购调整单id",required = true) @RequestParam String purchaseAdjustmentId)
            throws RemoteServiceException {
        websitPurchaseAdjustmentLogic.del(purchaseAdjustmentId);
        return ResponseHelper.success();
    }

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

        return ResponseHelper.success();
    }
}
