package com.rj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rj.entity.IndustryTags;
import com.rj.mapper.IndustryTagsMapper;
import com.rj.service.IIndustryTagsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 行业标签表 服务实现类
 * </p>
 *
 * @author Auto Generated
 * @since 2025-01-29
 */
@Slf4j
@Service
public class IndustryTagsServiceImpl extends ServiceImpl<IndustryTagsMapper, IndustryTags> implements IIndustryTagsService {

    @Override
    public Map<String, Object> getIndustryTagsList(Integer current, Integer size, String industry,
                                                   String tagType, String tagName) {
        Map<String, Object> result = new HashMap<>();
        try {
            Page<IndustryTags> page = new Page<>(current, size);
            LambdaQueryWrapper<IndustryTags> queryWrapper = buildQueryWrapper(industry, tagType, tagName);

            Page<IndustryTags> industryTagsPage = this.page(page, queryWrapper);

            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", industryTagsPage.getRecords());
            result.put("total", industryTagsPage.getTotal());
            result.put("current", industryTagsPage.getCurrent());
            result.put("size", industryTagsPage.getSize());
            result.put("pages", industryTagsPage.getPages());
        } catch (Exception e) {
            log.error("查询行业标签列表异常", e);
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
        }
        return result;
    }

    @Override
    public Map<String, Object> getIndustryTagsByType(String tagType, String industry) {
        Map<String, Object> result = new HashMap<>();
        try {
            LambdaQueryWrapper<IndustryTags> queryWrapper = new LambdaQueryWrapper<>();
            
            // 必须指定标签类型
            if (!StringUtils.hasText(tagType)) {
                result.put("success", false);
                result.put("message", "标签分类不能为空");
                return result;
            }
            
            queryWrapper.eq(IndustryTags::getTagType, tagType);
            
            // 如果指定了行业，则添加行业条件
            if (StringUtils.hasText(industry)) {
                queryWrapper.eq(IndustryTags::getIndustry, industry);
            }
            
            // 按创建时间倒序排列
            queryWrapper.orderByDesc(IndustryTags::getCreateTime);

            List<IndustryTags> industryTagsList = this.list(queryWrapper);

            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", industryTagsList);
            result.put("count", industryTagsList.size());
        } catch (Exception e) {
            log.error("根据类型查询行业标签异常", e);
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
        }
        return result;
    }

    @Override
    public Map<String, Object> getIndustryTagsById(String id) {
        Map<String, Object> result = new HashMap<>();
        try {
            IndustryTags industryTags = this.getById(id);
            if (industryTags != null) {
                result.put("success", true);
                result.put("message", "查询成功");
                result.put("data", industryTags);
            } else {
                result.put("success", false);
                result.put("message", "行业标签不存在");
            }
        } catch (Exception e) {
            log.error("查询行业标签异常", e);
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
        }
        return result;
    }

    @Override
    public Map<String, Object> addIndustryTags(IndustryTags industryTags) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 验证必填字段
            if (!StringUtils.hasText(industryTags.getIndustry())) {
                result.put("success", false);
                result.put("message", "所属行业不能为空");
                return result;
            }
            if (!StringUtils.hasText(industryTags.getTagType())) {
                result.put("success", false);
                result.put("message", "标签分类不能为空");
                return result;
            }
            if (!StringUtils.hasText(industryTags.getTagName())) {
                result.put("success", false);
                result.put("message", "标签名称不能为空");
                return result;
            }
            if (!StringUtils.hasText(industryTags.getTagDetail())) {
                result.put("success", false);
                result.put("message", "标签详情不能为空");
                return result;
            }

            LocalDateTime now = LocalDateTime.now();
            if (industryTags.getId() == null || industryTags.getId().trim().isEmpty()) {
                industryTags.setId(UUID.randomUUID().toString());
            }
            industryTags.setCreateTime(now);
            industryTags.setUpdateTime(now);

            boolean success = this.save(industryTags);
            if (success) {
                result.put("success", true);
                result.put("message", "行业标签添加成功");
                result.put("data", industryTags);
            } else {
                result.put("success", false);
                result.put("message", "行业标签添加失败");
            }
        } catch (Exception e) {
            log.error("添加行业标签异常", e);
            result.put("success", false);
            result.put("message", "添加异常：" + e.getMessage());
        }
        return result;
    }

    @Override
    public Map<String, Object> updateIndustryTags(IndustryTags industryTags) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (industryTags.getId() == null || industryTags.getId().trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "行业标签ID不能为空");
                return result;
            }

            // 验证必填字段
            if (!StringUtils.hasText(industryTags.getIndustry())) {
                result.put("success", false);
                result.put("message", "所属行业不能为空");
                return result;
            }
            if (!StringUtils.hasText(industryTags.getTagType())) {
                result.put("success", false);
                result.put("message", "标签分类不能为空");
                return result;
            }
            if (!StringUtils.hasText(industryTags.getTagName())) {
                result.put("success", false);
                result.put("message", "标签名称不能为空");
                return result;
            }
            if (!StringUtils.hasText(industryTags.getTagDetail())) {
                result.put("success", false);
                result.put("message", "标签详情不能为空");
                return result;
            }

            industryTags.setUpdateTime(LocalDateTime.now());
            boolean success = this.updateById(industryTags);

            if (success) {
                result.put("success", true);
                result.put("message", "行业标签信息更新成功");
                result.put("data", industryTags);
            } else {
                result.put("success", false);
                result.put("message", "行业标签信息更新失败");
            }
        } catch (Exception e) {
            log.error("更新行业标签异常", e);
            result.put("success", false);
            result.put("message", "更新异常：" + e.getMessage());
        }
        return result;
    }

    @Override
    public Map<String, Object> deleteIndustryTags(String id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = this.removeById(id);
            if (success) {
                result.put("success", true);
                result.put("message", "行业标签删除成功");
            } else {
                result.put("success", false);
                result.put("message", "行业标签删除失败");
            }
        } catch (Exception e) {
            log.error("删除行业标签异常", e);
            result.put("success", false);
            result.put("message", "删除异常：" + e.getMessage());
        }
        return result;
    }

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<IndustryTags> buildQueryWrapper(String industry, String tagType, String tagName) {
        LambdaQueryWrapper<IndustryTags> queryWrapper = new LambdaQueryWrapper<>();

        // 添加所属行业查询条件
        if (StringUtils.hasText(industry)) {
            queryWrapper.eq(IndustryTags::getIndustry, industry.trim());
        }

        // 添加标签分类查询条件
        if (StringUtils.hasText(tagType)) {
            queryWrapper.eq(IndustryTags::getTagType, tagType.trim());
        }

        // 添加标签名称模糊查询条件
        if (StringUtils.hasText(tagName)) {
            queryWrapper.like(IndustryTags::getTagName, tagName.trim());
        }

        // 按创建时间倒序排列
        queryWrapper.orderByDesc(IndustryTags::getCreateTime);

        return queryWrapper;
    }
}

