package com.nailart.controller;

import com.nailart.dataobj.TagDO;
import com.nailart.service.TagInfoService;
import com.nailart.vo.RespVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 标签信息控制类
 * @Classname TagInfoController
 * @Date 2025/7/14 23:44
 * @Created by 21616
 */
@RestController
@RequestMapping("/tagInfo")
public class TagInfoController {
    private final TagInfoService tagInfoService;

    TagInfoController(TagInfoService tagInfoService) {
        this.tagInfoService = tagInfoService;
    }

    @PostMapping("/save")
    public RespVO saveTagInfo(@RequestBody TagDO tagDO) {
        RespVO respVO = new RespVO();
        if (tagDO == null) {
            respVO.setCode(500);
            respVO.setMsg("标签信息不能为空");
            return respVO;
        }
        if (!tagInfoService.save(tagDO)) {
            respVO.setCode(500);
            respVO.setMsg("保存标签信息失败");
            return respVO;
        }
        respVO.setCode(200);
        respVO.setData("保存标签信息成功");
        return respVO;
    }

    @GetMapping("/getById")
    public RespVO getTagInfoById(@RequestParam Long id) {
        RespVO respVO = new RespVO();
        if (id == null) {
            respVO.setCode(500);
            respVO.setMsg("标签id不能为空");
            return respVO;
        }
        TagDO tagInfoById = tagInfoService.getById(id);
        if (tagInfoById == null) {
            respVO.setCode(500);
            respVO.setMsg("查询标签信息失败");
            return respVO;
        }
        respVO.setCode(200);
        respVO.setData(tagInfoById);
        return respVO;


    }

    @PostMapping("/deleteById")
    public RespVO deleteTagInfoById(Long id) {

        RespVO respVO = new RespVO();
        if (id == null) {
            respVO.setCode(500);
            respVO.setMsg("标签id不能为空");
            return respVO;
        }
        boolean i = tagInfoService.removeById(id);
        if (!i) {
            respVO.setCode(500);
            respVO.setMsg("删除标签信息失败");
            return respVO;
        }

        respVO.setCode(200);
        respVO.setData("删除标签信息成功");
        return respVO;
    }

    @PostMapping("/updateById")
    public RespVO updateTagInfoById(@RequestBody TagDO tagDO) {
        RespVO respVO = new RespVO();
        if (tagDO == null) {
            respVO.setCode(500);
            respVO.setMsg("标签信息不能为空");
            return respVO;
        }
        Boolean i = tagInfoService.updateById(tagDO);
        if (!i) {
            respVO.setCode(500);
            respVO.setMsg("更新标签信息失败");
            return respVO;
        }
        respVO.setCode(200);
        respVO.setData("更新标签信息成功");

        return respVO;
    }

    @GetMapping("/getAll")
    public RespVO getAllTagInfo() {
        RespVO respVO = new RespVO();
        List<TagDO> allTagInfo = tagInfoService.list();
        if (allTagInfo == null || allTagInfo.isEmpty()) {
            respVO.setCode(500);
            respVO.setMsg("获取标签信息失败");
            return respVO;

        }
        respVO.setCode(200);
        respVO.setData(allTagInfo);
        return respVO;
    }
}
