package cn.sh.stc.sict.theme.common.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.sh.stc.sict.cloud.common.core.constant.Constant;
import cn.sh.stc.sict.cloud.common.core.util.NumberUtil;
import cn.sh.stc.sict.theme.common.dao.HphyArticleMapper;
import cn.sh.stc.sict.theme.common.dto.ArticleDTO;
import cn.sh.stc.sict.theme.common.model.HphyArticle;
import cn.sh.stc.sict.theme.common.service.HphyArticleService;
import cn.sh.stc.sict.theme.common.service.HphyAttachmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("hphyArticleService")
@AllArgsConstructor
public class HphyArticleServiceImpl extends ServiceImpl<HphyArticleMapper, HphyArticle> implements HphyArticleService {

    private final HphyArticleMapper hphyArticleMapper;
    private final HphyAttachmentService hphyAttachmentService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(ArticleDTO dto) {
        if (NumberUtil.isNullOrZero(dto.getArticle().getIsPublished())) {
            dto.getArticle().setIsPublished(Constant.BYTE_NO);
        }

        this.saveOrUpdate(dto.getArticle());

        if (CollUtil.isNotEmpty(dto.getAttachmentList())) {
            hphyAttachmentService.saveOrupdate(dto.getArticle().getId(), dto.getAttachmentList());
        }

        return true;
    }
}
