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.material.base.MaterialCategoryTree;
import com.gree.mall.manager.bean.material.base.MaterialCategoryVO;
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.MaterialCategoryLogic;
import com.gree.mall.manager.plus.entity.WebsitGoodsCategory;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
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 java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "辅材分类API", tags ={"辅材分类API"} )
@RequestMapping(value = "/material/category", produces = "application/json; charset=utf-8")
public class MaterialCategoryController {

    @Resource
    MaterialCategoryLogic materialCategoryLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "商品分类-列表")
    public ResponseHelper<IPage<MaterialCategoryVO>> page(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<MaterialCategoryVO> page = materialCategoryLogic.page(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<MaterialCategoryVO>() {});
    }

    @PostMapping("/tree")
    @ApiOperation(value = "商品分类-树形")
    public ResponseHelper<List<MaterialCategoryTree>> tree(
            @ApiParam(value = "状态 ON=有效 OFF=无效") @RequestParam(required = false) String state
    ) {
        List<MaterialCategoryTree> trees = materialCategoryLogic.materialCategoryTree(state);
        return ResponseHelper.success(trees);
    }

    @PostMapping("/add")
    @ApiOperation(value = "商品分类-新增")
    public ResponseHelper add(@RequestBody WebsitGoodsCategory category) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.PARTS_BASE_MANAGE);
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                materialCategoryLogic.add(category);
                return ResponseHelper.success();
            }
        } catch (Exception e) {
            log.error("新增商品分类失败！", e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.error("新增失败");
    }

    @PostMapping("/update")
    @ApiOperation(value = "商品分类-修改")
    public ResponseHelper update(@RequestBody WebsitGoodsCategory category) throws RemoteServiceException {
        materialCategoryLogic.update(category);
        return ResponseHelper.success();
    }
}
