package com.xjrsoft.module.dataexpert.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.module.dataexpert.dto.DataExpertSourceFieldListDto;
import com.xjrsoft.module.dataexpert.entity.DataExpertSource;
import com.xjrsoft.module.dataexpert.entity.DataExpertSourceField;
import com.xjrsoft.module.dataexpert.mapper.DataExpertSourceFieldMapper;
import com.xjrsoft.module.dataexpert.mapper.DataExpertSourceMapper;
import com.xjrsoft.module.dataexpert.service.IDataExpertSourceService;
import com.xjrsoft.module.dataexpert.vo.DataExpertSourceFieldVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @title: 数据导出-数据源设置
* @Author dzx
* @Date: 2024-04-19
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class DataExpertSourceServiceImpl extends MPJBaseServiceImpl<DataExpertSourceMapper, DataExpertSource> implements IDataExpertSourceService {
    private final DataExpertSourceMapper expertSourceMapper;
    private final DataExpertSourceFieldMapper fieldMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(DataExpertSource dataExpertSource) {
        expertSourceMapper.insert(dataExpertSource);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(DataExpertSource dataExpertSource) {
        expertSourceMapper.updateById(dataExpertSource);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        expertSourceMapper.deleteBatchIds(ids);
        return true;
    }

    @Override
    public List<DataExpertSourceFieldVo> getFieldList(DataExpertSourceFieldListDto dto) {
        List<DataExpertSourceField> list = fieldMapper.selectList(
            new QueryWrapper<DataExpertSourceField>().lambda()
            .eq(dto.getDataExpertSourceId() != null, DataExpertSourceField::getDataExpertSourceId, dto.getDataExpertSourceId())
            .eq(StrUtil.isNotEmpty(dto.getDataGroup()), DataExpertSourceField::getDataGroup, dto.getDataGroup())
            .eq(DataExpertSourceField::getDeleteMark, DeleteMark.NODELETE.getCode())
        );
        return BeanUtil.copyToList(list, DataExpertSourceFieldVo.class);
    }
}
