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

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.ExcelData;
import com.gree.mall.manager.bean.material.base.WorkerGoodsBean;
import com.gree.mall.manager.bean.material.base.WorkerGoodsVO;
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.base.WorkerGoodsLogic;
import com.gree.mall.manager.utils.CommonUtils;
import com.gree.mall.manager.utils.excel.ExcelUtils;
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 = "/worker/goods", produces = "application/json; charset=utf-8")
public class WorkerGoodsController {

    @Resource
    WorkerGoodsLogic workerGoodsLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "师傅辅材管理-列表")
    public ResponseHelper<IPage<WorkerGoodsVO>> page(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WorkerGoodsVO> page = workerGoodsLogic.page(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WorkerGoodsVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("师傅辅材管理-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WorkerGoodsVO> baseVOIPage = workerGoodsLogic.page(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @GetMapping("/rela/export")
    @ApiOperation("师傅辅材管理-相关关联辅材")
    public void downLoadExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ExcelData excelData = workerGoodsLogic.relaExport();
        ExcelUtils.exportExcel(request,response, CommonUtils.getCurDateFileName("师傅辅材关联明细", "xlsx"),excelData);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "师傅辅材管理-详情")
    public ResponseHelper<WorkerGoodsBean> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        return ResponseHelper.success(workerGoodsLogic.detail(id));
    }

    @PostMapping("/add")
    @ApiOperation(value = "师傅辅材管理-添加")
    public ResponseHelper add(
            @RequestBody WorkerGoodsBean bean
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.MATERIAL_WORKER_INCR);
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                workerGoodsLogic.add(bean);
            }
        } catch(Exception e) {
            log.error("【师傅辅材添加】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "师傅辅材管理-编辑")
    public ResponseHelper edit(
            @RequestBody WorkerGoodsBean bean
    ) throws Exception {
        workerGoodsLogic.edit(bean);
        return ResponseHelper.success();
    }
}
