package com.cslg.ppa.service;

import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cslg.ppa.common.core.auth.Response;
import com.cslg.ppa.dto.ArticleInfoIdDTO;
import com.cslg.ppa.dto.GetArticleInfoDTO;
import com.cslg.ppa.dto.SelectArticleInfoDTO;
import com.cslg.ppa.dto.UpdateArticleInfoDTO;
import com.cslg.ppa.entity.ArticleInfo;
import com.cslg.ppa.entity.BaseEntity;
import com.cslg.ppa.entity.commom.Records;
import com.cslg.ppa.mapper.ArticleInfoMapper;
import com.cslg.ppa.service.commom.DifyService;
import com.cslg.ppa.vo.SelectArticleInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ArticleInfoService extends ServiceImpl<ArticleInfoMapper, ArticleInfo> {

    @Autowired
    private ArticleInfoMapper articleInfoMapper;

    public void batchAddArticleInfo(List<GetArticleInfoDTO> articleInfoDTOS) {
        if (!CollectionUtils.isEmpty(articleInfoDTOS)) {
            for (GetArticleInfoDTO articleInfoDTO : articleInfoDTOS) {
                if (StringUtils.isNotEmpty(articleInfoDTO.getTitle())) {
                    this.addArticleInfo(articleInfoDTO);
                }
            }
        }
    }

    //添加资讯
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Throwable.class)
    public void addArticleInfo(GetArticleInfoDTO infoDTO) {
        ArticleInfo articleInfo = new ArticleInfo();
        articleInfo.setTitle(infoDTO.getTitle());
        articleInfo.setDigest(infoDTO.getDigest());
        articleInfo.setPctDigest(infoDTO.getPctDigest());
        articleInfo.setCategoryId(infoDTO.getCategoryId());
        articleInfo.setPublicTime(infoDTO.getPublicTime());
        articleInfo.setArticleUrl(infoDTO.getArticleUrl());
        articleInfo.setSourceId(infoDTO.getSourceId());
        articleInfo.setWxArticleIcon(infoDTO.getWxArticleIcon());
        articleInfo.insert();
    }

    /**
     * 查询资讯列表
     *
     * @param vo
     * @return
     */
    public String selectArticleInfoList(SelectArticleInfoDTO vo) {
        List<SelectArticleInfoVO> articleInfoVOS = articleInfoMapper.selectArticleInfoList(vo);
        Long count = articleInfoMapper.selectArticleInfoCount(vo);
        Records records = new Records();
        records.setTotal(count);
        records.setCurrent(vo.getPageNum());
        records.setSize(vo.getPageSize());
        records.setData(articleInfoVOS);
        return Response.success(records);
    }

    /**
     * 编辑资讯信息
     * @param infoDTO
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public Integer updateArticleInfo(UpdateArticleInfoDTO infoDTO) {
        Integer articleId = infoDTO.getArticleId();
        ArticleInfo articleInfo = articleInfoMapper.selectById(articleId);
        articleInfo.setDigest(infoDTO.getDigest());
        articleInfo.setCategoryId(infoDTO.getCategoryId());
        articleInfo.updateById();
        return articleInfo.getId();
    }

    /**
     * 删除资讯
     * @param vo
     */
    public void deleteArticleInfo(ArticleInfoIdDTO vo) {
        List<Integer> articleIds = vo.getArticleIds();
        if (!CollectionUtils.isEmpty(articleIds)) {
            articleInfoMapper.deleteBatchIds(articleIds);
        }
    }
}
