package com.bonus.material.upOrDown.tool.controller;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.material.upOrDown.tool.entity.ToolEntity;
import com.bonus.material.upOrDown.tool.service.UpOrDownToolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 30791
 * @version 1.0
 * Create by 2025/11/15 23:10
 */

@RestController
@RequestMapping("/upOrDown/tool")
@Api(value = "工具上下架", tags = "工具上下架")
public class UpOrDownToolController extends BaseController {

    @Resource
    private UpOrDownToolService toolService;

    @ApiOperation(value = "工具上架列表")
    @GetMapping("/list")
    public TableDataInfo list(ToolEntity entity) {
        try {
            startPage();
            List<ToolEntity> list = toolService.list(entity);
            return getDataTable(list);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return getDataTableError(new ArrayList<>());
    }


    /**
     * 数量工具列表
     *
     * @param entity 实体
     * @return 结果
     */
    @ApiOperation(value = "数量工具列表")
    @GetMapping("/numberToolList")
    public TableDataInfo listByNumber(ToolEntity entity) {
        try {
            startPage();
            List<ToolEntity> list = toolService.listByNumber(entity);
            return getDataTable(list);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return getDataTableError(new ArrayList<>());
    }



    /**
     * 上架数量工具
     *
     * @param entity 实体
     * @return 结果
     */
    @ApiOperation(value = "上架数量工具")
    @PostMapping("/upByNumber")
    public AjaxResult upToolByNumber(@RequestBody ToolEntity entity) {
        return toolService.upToolByNumber(entity);
    }



    /**
     * 编码工具列表
     *
     * @param entity 实体
     * @return 结果
     */
    @ApiOperation(value = "编码工具列表")
    @GetMapping("/codeToolList")
    public TableDataInfo listByCode(ToolEntity entity) {
        try {
            startPage();
            List<ToolEntity> list = toolService.listByCode(entity);
            return getDataTable(list);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return getDataTableError(new ArrayList<>());
    }

    /**
     * 上架编码工具
     *
     * @param entity 实体
     * @return 结果
     */
    @ApiOperation(value = "上架编码工具")
    @PostMapping("/upByCode")
    public AjaxResult upToolByCode(@RequestBody ToolEntity entity) {
        return toolService.upToolByCode(entity);
    }



    /**
     * 下架工具
     */
    @ApiOperation(value = "下架工具")
    @PostMapping("/down/{toolId}")
    public AjaxResult down(@PathVariable("toolId") Long toolId)
    {
        toolService.downToolById(toolId);
        return AjaxResult.success();
    }

    /**
     * 批量上架
     * @param entity
     * @return
     */
    @ApiOperation(value = "批量上架工具")
    @PostMapping("/batch/up")
    public AjaxResult upToolBatch(@RequestBody List<ToolEntity> entity) {
        if (CollectionUtils.isEmpty(entity)) {
            return AjaxResult.warn("请选择要批量上架的工具");
        }
        return toolService.upToolBatch(entity);
    }

    /**
     * 批量下架
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量下架工具")
    @PostMapping("/batch/down")
    public AjaxResult downToolBatch(@RequestBody List<Long> ids) {
        toolService.downToolBatch(ids);
        return AjaxResult.success();
    }

}
