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

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

    @Resource
    WebsitMPurchaseLogic websitMPurchaseLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

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

    @PostMapping("/list/export")
    @ApiOperation("网点采购申请-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitPurchaseApplyVO> baseVOIPage = websitMPurchaseLogic.page(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("/add")
    @ApiOperation(value = "网点采购申请-添加")
    public ResponseHelper add(
            @RequestBody WebsitMPurchaseBean bean
    ) throws Exception {
        websitMPurchaseLogic.add(bean);
        return ResponseHelper.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "网点采购申请-编辑")
    public ResponseHelper edit(
            @RequestBody WebsitMPurchaseBean bean
    ) throws Exception {
        websitMPurchaseLogic.edit(bean);

        return ResponseHelper.success();
    }

    @PostMapping("/batch/del")
    @ApiOperation(value = "网点采购申请-批量删除")
    public ResponseHelper batchDel(@RequestBody List<String> purchaseIds)
            throws RemoteServiceException {
        websitMPurchaseLogic.batchDel(purchaseIds);
        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, 1);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/submit")
    @ApiOperation(value = "网点采购申请-提交")
    public ResponseHelper submit(@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.submit(purchaseId);
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

}
