// LabelController.java
package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Label;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.R;
import com.yami.shop.service.LabelService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 标签 Controller
 *
 * @author fbj
 * @since 2025-09-XX
 */
@Slf4j
@RestController
@RequestMapping("/platform/label")
@AllArgsConstructor
public class LabelController {

    private final LabelService labelService;

    /**
     * 分页获取标签列表
     */
    @GetMapping("/page")
    public R<IPage<Label>> page(@RequestParam(defaultValue = "1") Integer current,
                                @RequestParam(defaultValue = "10") Integer size,
                                Label label) {
        PageParam<Label> pageParam = new PageParam<>();
        pageParam.setCurrent(current);
        pageParam.setSize(size);

        LambdaQueryWrapper<Label> queryWrapper = new LambdaQueryWrapper<>();
        if (label.getName() != null && !label.getName().trim().isEmpty()) {
            queryWrapper.like(Label::getName, label.getName().trim());
        }
        queryWrapper.eq(Label::getIsDelete, 0);

        IPage<Label> page = labelService.page(pageParam, queryWrapper);
        return R.SUCCESS(page);
    }

    /**
     * 获取全部标签
     */
    @GetMapping("/listAll")
    public R<List<Label>> listAll() {
        List<Label> list = labelService.list(new LambdaQueryWrapper<Label>().eq(Label::getIsDelete, 0));
        return R.SUCCESS(list);
    }

    /**
     * 根据ID获取标签
     */
    @GetMapping("/{id}")
    public R<Label> getById(@PathVariable Long id) {
        Label label = labelService.getById(id);
        return R.SUCCESS(label);
    }

    /**
     * 添加标签
     */
    @PostMapping
    public R save(@Valid @RequestBody Label label) {
        labelService.save(label);
        return R.SUCCESS();
    }

    /**
     * 更新标签
     */
    @PutMapping
    public R update(@Valid @RequestBody Label label) {
        labelService.updateById(label);
        return R.SUCCESS();
    }

    /**
     * 删除标签
     */
    @DeleteMapping
    public R delete(@RequestBody Long[] ids) {
        labelService.removeByIds(Arrays.asList(ids));
        return R.SUCCESS();
    }
}
