package com.cool.store.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.cool.store.constants.RedisConstant;
import com.cool.store.dto.content.ContentAddDto;
import com.cool.store.dto.content.ContentQueryListDto;
import com.cool.store.dto.content.ContentUpdateDto;
import com.cool.store.entity.HyContentInfoDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ApiException;
import com.cool.store.mapper.HyContentInfoMapper;
import com.cool.store.service.ContentService;
import com.cool.store.utils.RedisUtilPool;
import com.cool.store.vo.HyContentInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private HyContentInfoMapper contentInfoMapper;

    @Autowired
    private RedisUtilPool redisUtilPool;

    /**
     *
     * @param dto
     * @return contentId 新增动态id
     */
    @Override
    public String addNews(ContentAddDto dto) throws ApiException {
        //增加不允许重复标题的逻辑
        Boolean isDuplicated = contentInfoMapper.whetherTitleDuplicated(dto.getContentTitle());
        if (isDuplicated) {
            throw new ApiException(ErrorCodeEnum.CONTENT_DUPLICATED);
        }
        HyContentInfoDO hyContentInfoDO = new HyContentInfoDO();
        BeanUtil.copyProperties(dto, hyContentInfoDO);
        hyContentInfoDO.setUpdateUserId(dto.getCreateUserId());
        return Integer.toString(contentInfoMapper.insertSelective(hyContentInfoDO));
    }

    /**
     * 删除动态
     * @param contentId
     */
    @Override
    public void deleteContent(String contentId) {
        contentInfoMapper.deleteSelective(contentId);
    }

    /**
     * 更新动态信息
     * @param dto
     */
    @Override
    public void updateContent(ContentUpdateDto dto) throws ApiException {
        HyContentInfoDO hyContentInfoDO = new HyContentInfoDO();
        BeanUtil.copyProperties(dto, hyContentInfoDO);
        hyContentInfoDO.setId(Long.parseLong(dto.getContentId()));
        contentInfoMapper.updateByPrimaryKeySelective(hyContentInfoDO);
    }

    /**
     * 查询动态列表
     */
    @Override
    public List<HyContentInfoVO> queryContentList(ContentQueryListDto dto) {
        return contentInfoMapper.queryContentList(dto);
    }

    /**
     * 查询动态列表 C 端用
     */
    @Override
    public List<HyContentInfoVO> queryContentListToC(ContentQueryListDto dto) {
        return contentInfoMapper.queryContentListForC(dto);
    }

    /**
     * 查询动态详情
     * @param contentId
     * @return
     */
    @Override
    public HyContentInfoDO queryContentInfo(String contentId) {
        return contentInfoMapper.queryContentInfo(contentId);
    }

    /**
     * 标题是否重复
     */
    @Override
    public Boolean queryTitles(String title) {
        List<String> titles = (List<String>) JSONObject.parseObject(redisUtilPool.getString(RedisConstant.CONTENT_TITLES), List.class);
        if (titles != null && titles.size() != 0) {
            return titles.contains(title);
        }
        titles = contentInfoMapper.queryTitles();
        redisUtilPool.setString(RedisConstant.CONTENT_TITLES, JSONObject.toJSONString(titles), 60);
        return titles.contains(title);
    }

}
