package com.rj.controller;

import com.rj.entity.IndustryTags;
import com.rj.service.IIndustryTagsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 行业标签表 前端控制器
 * </p>
 *
 * @author Auto Generated
 * @since 2025-01-29
 */
@RestController
@RequestMapping("/api/industryTags")
@Tag(name = "行业标签", description = "行业标签相关接口")
@Slf4j
public class IndustryTagsController {

    @Autowired
    private IIndustryTagsService industryTagsService;

    /**
     * 根据条件分页查询行业标签列表
     */
    @PostMapping("/list")
    @Operation(summary = "分页查询行业标签列表", description = "根据条件分页查询行业标签信息列表，支持按所属行业、标签分类、标签名称查询")
    public ResponseEntity<Map<String, Object>> getIndustryTagsList(
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小", example = "10")
            @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "所属行业（精确查询）")
            @RequestParam(required = false) String industry,
            @Parameter(description = "标签分类（精确查询）")
            @RequestParam(required = false) String tagType,
            @Parameter(description = "标签名称（模糊查询）")
            @RequestParam(required = false) String tagName) {
        Map<String, Object> result = industryTagsService.getIndustryTagsList(
                current, size, industry, tagType, tagName);
        
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 根据类型不分页查询行业标签列表
     */
    @GetMapping("/listByType")
    @Operation(summary = "根据类型查询行业标签列表", description = "根据标签分类查询行业标签列表（不分页），可选择性按所属行业筛选")
    public ResponseEntity<Map<String, Object>> getIndustryTagsByType(
            @Parameter(description = "标签分类", required = true)
            @RequestParam String tagType,
            @Parameter(description = "所属行业（可选，精确查询）")
            @RequestParam(required = false) String industry) {
        Map<String, Object> result = industryTagsService.getIndustryTagsByType(tagType, industry);
        
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        } else {
            String message = (String) result.get("message");
            if (message != null && message.contains("不能为空")) {
                return ResponseEntity.badRequest().body(result);
            }
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 根据ID查询行业标签
     */
    @GetMapping("/get/{id}")
    @Operation(summary = "根据ID查询行业标签", description = "根据行业标签ID获取详细信息")
    public ResponseEntity<Map<String, Object>> getIndustryTagsById(
            @Parameter(description = "行业标签ID", required = true)
            @PathVariable String id) {
        Map<String, Object> result = industryTagsService.getIndustryTagsById(id);
        
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 新增行业标签
     */
    @PostMapping("/add")
    @Operation(summary = "新增行业标签", description = "添加新的行业标签信息")
    public ResponseEntity<Map<String, Object>> addIndustryTags(
            @Parameter(description = "行业标签信息", required = true)
            @RequestBody IndustryTags industryTags) {
        Map<String, Object> result = industryTagsService.addIndustryTags(industryTags);
        
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 更新行业标签信息
     */
    @PutMapping("/update")
    @Operation(summary = "更新行业标签信息", description = "更新行业标签详细信息")
    public ResponseEntity<Map<String, Object>> updateIndustryTags(
            @Parameter(description = "行业标签信息", required = true)
            @RequestBody IndustryTags industryTags) {
        Map<String, Object> result = industryTagsService.updateIndustryTags(industryTags);
        
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        } else {
            String message = (String) result.get("message");
            if (message != null && message.contains("不能为空")) {
                return ResponseEntity.badRequest().body(result);
            }
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 根据ID删除行业标签
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除行业标签", description = "根据行业标签ID删除信息")
    public ResponseEntity<Map<String, Object>> deleteIndustryTags(
            @Parameter(description = "行业标签ID", required = true)
            @PathVariable String id) {
        Map<String, Object> result = industryTagsService.deleteIndustryTags(id);
        
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.badRequest().body(result);
        }
    }
}

