package com.ruoyi.post.frequency.posttype.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.post.controller.req.PostTypeListReq;
import com.ruoyi.post.frequency.post.entity.Post;
import com.ruoyi.post.frequency.post.service.PostService;
import com.ruoyi.post.frequency.posttype.entity.PostType;
import com.ruoyi.post.frequency.posttype.mapper.PostTypeMapper;
import com.ruoyi.post.frequency.posttype.service.PostTypeService;
import com.ruoyi.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

/**
 * 帖子分类Service业务层处理
 *
 * @author liwenlong
 * @date 2023-10-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PostTypeServiceImpl extends ServiceImpl<PostTypeMapper, PostType>  implements PostTypeService
{
    @Autowired
    private PostTypeMapper postTypeMapper;

    /**
     * 查询帖子分类
     *
     * @param id 帖子分类主键
     * @return 帖子分类
     */
    @Override
    public PostType selectPostTypeById(Long id)
    {
        return postTypeMapper.selectPostTypeById(id);
    }

    /**
     * 查询帖子分类列表
     *
     * @param postType 帖子分类
     * @return 帖子分类
     */
    @Override
    public List<PostType> selectPostTypeList(PostType postType)
    {
        return postTypeMapper.selectPostTypeList(postType);
    }

    /**
     * 新增帖子分类
     *
     * @param postType 帖子分类
     * @return 结果
     */
    @Override
    public int insertPostType(PostType postType)
    {
        postType.setCreateTime(DateUtils.getNowDate());
        return postTypeMapper.insert(postType);
    }

    /**
     * 修改帖子分类
     *
     * @param postType 帖子分类
     * @return 结果
     */
    @Override
    public int updatePostType(PostType postType)
    {
        postType.setUpdateTime(DateUtils.getNowDate());
        return postTypeMapper.updateById(postType);
    }

    /**
     * 批量删除帖子分类
     *
     * @param ids 需要删除的帖子分类主键
     * @return 结果
     */
    @Override
    public int deletePostTypeByIds(Long[] ids,Integer delFlag)
    {
        this.lambdaUpdate().set(PostType::getDelFlag,delFlag).in(PostType::getId,ids).update();
        return ids.length;
    }

    /**
     * 删除帖子分类信息
     *
     * @param id 帖子分类主键
     * @return 结果
     */
    @Override
    public int deletePostTypeById(Long id)
    {
        this.lambdaUpdate().set(PostType::getDelFlag,PublicCommon.删除).eq(PostType::getId,id).update();
        return 1;
    }

    @Override
    public ResponseData postTypeList(PostTypeListReq req) {

        List<PostType> list = this.list(new QueryWrapper<PostType>().lambda()
                .eq(ObjectUtil.isNotEmpty(req.getUserType()),PostType::getUserType,req.getUserType())
                .eq(ObjectUtil.isNotEmpty(req.getType()),PostType::getType,req.getType())
                .eq(PostType::getDelFlag,PublicCommon.启用).orderByAsc(PostType::getSort));

        return ResponseData.success(list);
    }
}
