package cn.fateverse.query.service.impl;

import cn.fateverse.query.entity.DynamicEcharts;
import cn.fateverse.query.entity.dto.DynamicEchartsDto;
import cn.fateverse.query.entity.vo.DynamicEchartsVo;
import cn.fateverse.query.entity.query.DynamicEchartsQuery;
import cn.fateverse.query.mapper.DynamicEchartsMapper;
import cn.fateverse.query.service.DynamicEchartsService;
import cn.fateverse.common.core.entity.Option;
import cn.fateverse.common.core.result.page.TableDataInfo;
import cn.fateverse.common.mybatis.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 动态echarts图表 Controller
 *
 * @author clay
 * @date 2023-11-16
 */
@Slf4j
@Service
public class DynamicEchartsServiceImpl implements DynamicEchartsService {

    private final DynamicEchartsMapper dynamicEchartsMapper;

    public DynamicEchartsServiceImpl(DynamicEchartsMapper dynamicEchartsMapper) {
        this.dynamicEchartsMapper = dynamicEchartsMapper;
    }

    @Override
    public DynamicEchartsVo searchById(Long echartsId){
        DynamicEcharts dynamicEcharts = dynamicEchartsMapper.selectById(echartsId);
        return DynamicEchartsVo.toDynamicEchartsVo(dynamicEcharts);
    }

    @Override
    public TableDataInfo<DynamicEchartsVo> searchList(DynamicEchartsQuery query){
        PageUtils.startPage();
        List<DynamicEcharts> list = dynamicEchartsMapper.selectList(query);
        return PageUtils.convertDataTable(list, DynamicEchartsVo::toDynamicEchartsVo);
    }

    @Override
    public List<Option> searchOptionList(){
        DynamicEchartsQuery query = new DynamicEchartsQuery();
        List<DynamicEcharts> list = dynamicEchartsMapper.selectList(query);
        return list.stream().map(item-> Option.builder()
                                      .value(item.getEchartsId())
                                      .label(item.getEchartsName())
                                      .build()).collect(Collectors.toList());
    }

    @Override
    public List<DynamicEchartsVo> exportList(DynamicEchartsQuery query){
        List<DynamicEcharts> list = dynamicEchartsMapper.selectList(query);
        return list.stream().map(DynamicEchartsVo::toDynamicEchartsVo)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(DynamicEchartsDto dynamicEcharts){
        DynamicEcharts info = dynamicEcharts.toDynamicEcharts();
        return dynamicEchartsMapper.insert(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int edit(DynamicEchartsDto dynamicEcharts){
        DynamicEcharts info = dynamicEcharts.toDynamicEcharts();
        return dynamicEchartsMapper.update(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Long echartsId){
        return dynamicEchartsMapper.deleteById(echartsId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeBatch(List<Long> echartsIdList){
        return dynamicEchartsMapper.deleteBatchByIdList(echartsIdList);
    }

}