package com.fs.his.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.BeanCopyUtils;
import com.fs.common.exception.CustomException;
import com.fs.company.vo.OptionVO;
import com.fs.his.domain.FsPackageSolarTerm;
import com.fs.his.mapper.FsPackageSolarTermMapper;
import com.fs.his.param.FsPackageSolarTermParam;
import com.fs.his.service.IFsPackageSolarTermService;
import com.fs.his.vo.FsPackageSolarTermVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
public class FsPackageSolarTermServiceImpl extends ServiceImpl<FsPackageSolarTermMapper, FsPackageSolarTerm> implements IFsPackageSolarTermService {

    /**
     * 查询节气列表
     * @param param 参数
     * @return List
     */
    @Override
    public List<FsPackageSolarTermVO> selectFsPackageSolarTermListVO(FsPackageSolarTermParam param) {
        return baseMapper.selectFsPackageSolarTermListVO(param);
    }

    /**
     * 根据ID查询节气详情
     * @param solarTermId id
     * @return FsPackageSolarTermVO
     */
    @Override
    public FsPackageSolarTermVO selectFsPackageSolarTermVO(Long solarTermId) {
        FsPackageSolarTerm solarTerm = baseMapper.selectById(solarTermId);
        if (Objects.isNull(solarTerm)) {
            throw new CustomException("节气不存在");
        }

        return BeanCopyUtils.copy(solarTerm, FsPackageSolarTermVO.class);
    }

    /**
     * 添加节气
     * @param param 参数
     * @return  受影响行数
     */
    @Override
    public int insertFsPackageSolarTerm(FsPackageSolarTerm param) {
        param.setCreateTime(LocalDateTime.now());
        return baseMapper.insert(param);
    }

    /**
     * 修改节气
     * @param param 参数
     * @return  受影响行数
     */
    @Override
    public int updateFsPackageSolarTerm(FsPackageSolarTerm param) {
        if (Objects.isNull(param.getId())) {
            throw new CustomException("节气ID不能为空");
        }
        param.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(param);
    }

    /**
     * 删除节气
     * @param solarTermIds  ids
     * @return  受影响行数
     */
    @Override
    public int deleteFsPackageSolarTermByIds(Long[] solarTermIds) {
        List<FsPackageSolarTerm> fsPackageSolarTerms = baseMapper.selectBatchIds(Arrays.asList(solarTermIds));
        if (Objects.nonNull(fsPackageSolarTerms) && !fsPackageSolarTerms.isEmpty()) {
            fsPackageSolarTerms.forEach(fsPackageSolarTerm -> {
                fsPackageSolarTerm.setIsDel(1);
                baseMapper.updateById(fsPackageSolarTerm);
            });
            return 1;
        }
        return 0;
    }

    /**
     * 获取节气选项
     */
    @Override
    public List<OptionVO> getOptions() {
        return baseMapper.getOptions();
    }
}
