package com.ruoyi.generator.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.generator.domain.Community.CommunityTag;
import com.ruoyi.generator.domain.Community.CommunityTagBlock;
import com.ruoyi.generator.mapper.community.CommunityTagBlockMapper;
import com.ruoyi.generator.service.ICommunityTagService;
import com.ruoyi.generator.vo.CommunityTagVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 文章标签
 *
 * @author fangzhen
 */
@Api(tags = "文章标签")
@RestController
@RequestMapping("/community/tag")
public class CommunityTagController extends BaseController {

    @Autowired
    private ICommunityTagService communityTagService;

    @Autowired
    private CommunityTagBlockMapper communityTagBlockMapper;

    /**
     * 获取标签信息
     */
    @ApiOperation("获取标签信息")
    @GetMapping()
    @Transactional
    //@Anonymous
    public AjaxResult tags(String tagName) {
        Page<CommunityTag> page = new Page<>(1, 20);
        List<CommunityTag> communityTags = communityTagService.page(page, new QueryWrapper<CommunityTag>().and((wrapper) -> {
                    wrapper.ne("is_delete", 1)
                            .or()
                            .isNull("is_delete");
                })
                .like("tag_name", tagName)
                .orderByDesc("tag_hot")).getRecords();
        return AjaxResult.success(communityTags);
    }


    /**
     * 获取标签信息(当前用户是否已拉黑)
     */
    @ApiOperation("获取标签信息(附带拉黑信息)")
    @GetMapping("/tagsTakeBlock")
    @Transactional
    //@Anonymous
    public AjaxResult tagsTakeBlock(String tagName) {
        Page<CommunityTag> page = new Page<>(1, 10);
        List<CommunityTag> communityTags = communityTagService.page(page, new QueryWrapper<CommunityTag>().and((wrapper) -> {
                    wrapper.ne("is_delete", 1)
                            .or()
                            .isNull("is_delete");
                })
                .like("tag_name", tagName)
                .orderByDesc("tag_hot")).getRecords();

        List<CommunityTagVo> communityTagVos = new ArrayList<>();
        CommunityTagVo communityTagVo = null;
        for (CommunityTag communityTag : communityTags) {
            communityTagVo = new CommunityTagVo();
            BeanUtils.copyProperties(communityTag, communityTagVo);
            communityTagVos.add(communityTagVo);
        }

        //查询是否被当前用户拉黑
        List<Long> tagIds = communityTagVos.stream().map(CommunityTagVo::getId).collect(Collectors.toList());
        Long userId = SecurityUtils.getUserId();
        List<CommunityTagBlock> communityTagBlocks = communityTagBlockMapper.selectList(new QueryWrapper<CommunityTagBlock>().eq("user_id", userId).in("tag_id", tagIds));

        //设置是否已拉黑
        for (CommunityTagVo tagVo : communityTagVos) {
            Long id = tagVo.getId();
            for (CommunityTagBlock communityTagBlock : communityTagBlocks) {
                if (communityTagBlock.getTagId().equals(id)) {
                    tagVo.setBlock(communityTagBlock.isBlock());
                    break;
                }
            }
        }

        return AjaxResult.success(communityTagVos);
    }

    /**
     * 新增标签
     */
    @ApiOperation("新增标签")
    @PostMapping("/addTag")
    @Transactional
    //@Anonymous
    public AjaxResult addTag(String tagName) {
        if (StringUtils.isEmpty(tagName)) {
            return AjaxResult.error("参数异常!");
        }
        CommunityTag communityTag = new CommunityTag();
        Long userId = SecurityUtils.getUserId();
        communityTag.setTagName(tagName);
        communityTag.setTagHot(0L);
        communityTag.setDelete(false);
        communityTag.setCreateBy(userId);
        communityTag.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
        communityTagService.save(communityTag);
        return AjaxResult.success("添加标签成功!");
    }

    /**
     * 拉黑标签
     */
    @ApiOperation("拉黑标签")
    @PostMapping("/blockTag")
    @Transactional
    //@Anonymous
    public AjaxResult blockTag(@RequestBody CommunityTagBlock tagBlock) {
        if (Objects.isNull(tagBlock.getTagId()) || Objects.isNull(tagBlock.getUserId())) {
            return AjaxResult.error("参数异常!");
        }

        CommunityTagBlock communityTagBlock = communityTagService.blockTag(tagBlock);
        return AjaxResult.success(communityTagBlock);
    }
}