package org.dromara.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.domain.partyNews.PartyNews;
import org.dromara.domain.partyNews.bo.PartyNewsBo;
import org.dromara.domain.partyNews.vo.PartyNewsVo;
import org.dromara.mapper.PartyNewsMapper;
import org.dromara.service.IPartyNewsService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import static org.dromara.common.core.constant.Constants.STARS_PARTY_NEWS;
import static org.dromara.common.core.constant.Constants.STARS_PARTY_NEWS_COUNT;

/**
 * 党建资讯Service业务层处理
 *
 * @author boman
 * @date 2025-04-11
 */
@RequiredArgsConstructor
@Service
public class PartyNewsServiceImpl implements IPartyNewsService {

    private final PartyNewsMapper baseMapper;

    /**
     * 查询党建资讯
     *
     * @param partyId 主键
     * @return 党建资讯
     */
    @Override
    public PartyNewsVo queryById(Long partyId){
        PartyNewsVo partyNewsVo = baseMapper.selectVoById(partyId);
        //观看次数+1
        Long viewCount = partyNewsVo.getViewCount();
        partyNewsVo.setViewCount(viewCount + 1);
        PartyNews update = MapstructUtils.convert(partyNewsVo, PartyNews.class);
        baseMapper.updateById(update);
        String partyKey = STARS_PARTY_NEWS + LoginHelper.getUserId();
        List<Long> partyIdList = RedisUtils.getCacheList(partyKey);
        partyNewsVo.setIsStars("N");
        Long partyIdNews = partyNewsVo.getPartyId();
        if (partyIdList.contains(partyIdNews)) {
            partyNewsVo.setIsStars("Y");
        }
        //插入收藏数量
        partyNewsVo.setStarsCount(0);
        Object startCount = RedisUtils.getCacheObject(STARS_PARTY_NEWS_COUNT + partyId);
        if (ObjectUtils.isNotEmpty(startCount)) {
            partyNewsVo.setStarsCount(Integer.parseInt(startCount.toString()));
        }
        return partyNewsVo;
    }

    /**
     * 分页查询党建资讯列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 党建资讯分页列表
     */
    @Override
    public TableDataInfo<PartyNewsVo> queryPageList(PartyNewsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PartyNews> lqw = buildQueryWrapper(bo);
        Page<PartyNewsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        if (result.getRecords() != null && !result.getRecords().isEmpty()) {
            String partyKey = STARS_PARTY_NEWS + LoginHelper.getUserId();
            List<Long> partyIdList = RedisUtils.getCacheList(partyKey);
            for (PartyNewsVo news : result.getRecords()) {
                news.setIsStars("N");
                Long partyId = news.getPartyId();
                if (partyIdList.contains(partyId)) {
                    news.setIsStars("Y");
                }
                //插入收藏数量
                news.setStarsCount(0);
                Object startCount = RedisUtils.getCacheObject(STARS_PARTY_NEWS_COUNT + partyId);
                if (ObjectUtils.isNotEmpty(startCount)) {
                    news.setStarsCount(Integer.parseInt(startCount.toString()));
                }
            }
        }

        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的党建资讯列表
     *
     * @param bo 查询条件
     * @return 党建资讯列表
     */
    @Override
    public List<PartyNewsVo> queryList(PartyNewsBo bo) {
        LambdaQueryWrapper<PartyNews> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PartyNews> buildQueryWrapper(PartyNewsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PartyNews> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(PartyNews::getPartyId);
        lqw.eq(StringUtils.isNotBlank(bo.getPartyTitle()), PartyNews::getPartyTitle, bo.getPartyTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getPartyType()), PartyNews::getPartyType, bo.getPartyType());
        lqw.eq(StringUtils.isNotBlank(bo.getPartyContent()), PartyNews::getPartyContent, bo.getPartyContent());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), PartyNews::getStatus, bo.getStatus());
        lqw.eq(bo.getPublishTime() != null, PartyNews::getPublishTime, bo.getPublishTime());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthor()), PartyNews::getAuthor, bo.getAuthor());
        lqw.eq(StringUtils.isNotBlank(bo.getSource()), PartyNews::getSource, bo.getSource());
        lqw.eq(StringUtils.isNotBlank(bo.getCoverImage()), PartyNews::getCoverImage, bo.getCoverImage());
        lqw.eq(bo.getViewCount() != null, PartyNews::getViewCount, bo.getViewCount());
        lqw.eq(StringUtils.isNotBlank(bo.getIsTop()), PartyNews::getIsTop, bo.getIsTop());
        return lqw;
    }

    /**
     * 新增党建资讯
     *
     * @param bo 党建资讯
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(PartyNewsBo bo) {
        PartyNews add = MapstructUtils.convert(bo, PartyNews.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPartyId(add.getPartyId());
        }
        return flag;
    }

    /**
     * 修改党建资讯
     *
     * @param bo 党建资讯
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(PartyNewsBo bo) {
        PartyNews update = MapstructUtils.convert(bo, PartyNews.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PartyNews entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除党建资讯信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
