package com.bonus.canteen.core.nutrition.service.impl;

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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.bonus.canteen.core.nutrition.domain.HealthArticleChronic;
import com.bonus.canteen.core.nutrition.mapper.HealthArticleChronicMapper;
import com.bonus.canteen.core.nutrition.mapper.HealthChronicMapper;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.nutrition.mapper.HealthPopularScienceMapper;
import com.bonus.canteen.core.nutrition.domain.HealthPopularScience;
import com.bonus.canteen.core.nutrition.service.IHealthPopularScienceService;

/**
 * 营养科普Service业务层处理
 * 
 * @author xsheng
 * @date 2025-05-13
 */
@Service
public class HealthPopularScienceServiceImpl implements IHealthPopularScienceService {
    @Autowired
    private HealthPopularScienceMapper healthPopularScienceMapper;
    @Autowired
    HealthArticleChronicMapper healthArticleChronicMapper;
    @Autowired
    HealthChronicMapper healthChronicMapper;

    /**
     * 查询营养科普
     * 
     * @param articleId 营养科普主键
     * @return 营养科普
     */
    @Override
    public HealthPopularScience selectHealthPopularScienceById(Long articleId) {
        return healthPopularScienceMapper.selectHealthPopularScienceById(articleId);
    }

    /**
     * 查询营养科普列表
     * 
     * @param healthPopularScience 营养科普
     * @return 营养科普
     */
    @Override
    public List<HealthPopularScience> selectHealthPopularScienceList(HealthPopularScience healthPopularScience) {
        return healthPopularScienceMapper.selectHealthPopularScienceList(healthPopularScience);
    }

    /**
     * 新增营养科普
     * 
     * @param healthPopularScience 营养科普
     * @return 结果
     */
    @Override
    public int insertHealthPopularScience(HealthPopularScience healthPopularScience) {
        healthPopularScience.setCreateTime(DateUtils.getNowDate());
        healthPopularScience.setCreateBy(SecurityUtils.getUsername());
        try {
            int count = healthPopularScienceMapper.insertHealthPopularScience(healthPopularScience);
            handleHealthArticles(healthPopularScience);
            return count;
        } catch (Exception e) {
            throw new ServiceException("新增失败");
        }
    }

    private void handleHealthArticles(HealthPopularScience healthPopularScience) {
        List<Long> chronicIdList = StrUtil.isNotBlank(healthPopularScience.getChronicIds()) ?
                Arrays.stream(healthPopularScience.getChronicIds().split(",")).map(Long::parseLong).collect(Collectors.toList()) : CollUtil.newArrayList(new Long[0]);
        HealthArticleChronic vo = new HealthArticleChronic();
        for (Long chronicId : chronicIdList) {
            vo.setArticleId(healthPopularScience.getArticleId());
            vo.setChronicId(chronicId);
            vo.setChronicName(healthChronicMapper.selectHealthChronicById(chronicId).getChronicName());
            vo.setCreateBy(SecurityUtils.getUsername());
            vo.setCreateTime(DateUtils.getNowDate());
            vo.setUpdateBy(SecurityUtils.getUsername());
            vo.setUpdateTime(DateUtils.getNowDate());
            healthArticleChronicMapper.insertHealthArticleChronic(vo);
        }
    }

    /**
     * 修改营养科普
     * 
     * @param healthPopularScience 营养科普
     * @return 结果
     */
    @Override
    public int updateHealthPopularScience(HealthPopularScience healthPopularScience) {
        healthPopularScience.setUpdateTime(DateUtils.getNowDate());
        healthPopularScience.setUpdateBy(SecurityUtils.getUsername());
        try {
            int count = healthPopularScienceMapper.updateHealthPopularScience(healthPopularScience);
            healthArticleChronicMapper.deleteHealthArticleChronicByArticleId(healthPopularScience.getArticleId());
            handleHealthArticles(healthPopularScience);
            return count;
        } catch (Exception e) {
            throw new ServiceException("更新失败");
        }
    }

    /**
     * 批量删除营养科普
     * 
     * @param articleIds 需要删除的营养科普主键
     * @return 结果
     */
    @Override
    public int deleteHealthPopularScienceByIds(Long[] articleIds) {
        for (Long articleId : articleIds) {
            healthArticleChronicMapper.deleteHealthArticleChronicByArticleId(articleId);
        }
        return healthPopularScienceMapper.deleteHealthPopularScienceByIds(articleIds);
    }

    /**
     * 删除营养科普信息
     * 
     * @param articleId 营养科普主键
     * @return 结果
     */
    @Override
    public int deleteHealthPopularScienceById(Long articleId) {
        healthArticleChronicMapper.deleteHealthArticleChronicByArticleId(articleId);
        return healthPopularScienceMapper.deleteHealthPopularScienceById(articleId);
    }
}
