package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysGoogleGroupEmailMapper;
import com.crm.manager.dao.repository.SysGoogleGroupEmailRepository;
import com.crm.manager.service.SysGoogleGroupEmailService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.system.google.SysGoogleGroupEmailListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.google.SysGoogleGroupEmailAddEntity;
import com.crm.rely.backend.core.entity.system.google.SysGoogleGroupEmailDeleteEntitys;
import com.crm.rely.backend.core.entity.system.google.SysGoogleGroupEmailSearchEntity;
import com.crm.rely.backend.core.entity.system.google.SysGoogleGroupEmailUpdateEntity;
import com.crm.rely.backend.core.pojo.table.SysGoogleGroupEmailTable;
import com.crm.rely.backend.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysGoogleGroupEmailServiceImpl implements SysGoogleGroupEmailService {

    @Autowired
    private SysGoogleGroupEmailRepository repository;
    @Autowired
    private SysGoogleGroupEmailMapper mapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysGoogleGroupEmailAddEntity entity) throws ServiceException {
        SysGoogleGroupEmailTable table = new SysGoogleGroupEmailTable();
        BeanUtils.copyProperties(entity, table);
        repository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysGoogleGroupEmailDeleteEntitys entity) throws ServiceException {
        List<SysGoogleGroupEmailTable> tables = repository.findAllByIdIn(entity.getIds());
        if (tables == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        repository.delete(tables);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysGoogleGroupEmailUpdateEntity entity) throws ServiceException {
        SysGoogleGroupEmailTable table = repository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        BeanUtils.copyProperties(entity, table);
        repository.save(table);
    }

    @Override
    public ResultWithPagerDto<SysGoogleGroupEmailListDto> searchPageList(SysGoogleGroupEmailSearchEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<SysGoogleGroupEmailTable> tables = mapper.pageList(entity);
        if (tables == null || tables.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<SysGoogleGroupEmailListDto> SysGoogleGroupEmailListDtos = new ArrayList<>(tables.size());

        for (SysGoogleGroupEmailTable SysGoogleGroupEmailTable : tables) {
            SysGoogleGroupEmailListDto SysGoogleGroupEmailListDto = transformSearchListDto(SysGoogleGroupEmailTable);
            SysGoogleGroupEmailListDtos.add(SysGoogleGroupEmailListDto);
        }

        return ResultWithPagerDto.success(pageDto, SysGoogleGroupEmailListDtos);
    }

    @Override
    public SysGoogleGroupEmailListDto searchSingle(SingleLongEntity entity) throws ServiceException {
        SysGoogleGroupEmailTable table = repository.getFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        return transformSearchListDto(table);
    }

    @Override
    public BaseResultDto getAllList(SysGoogleGroupEmailSearchEntity entity) throws ServiceException {
        entity.setPage(null);
        List<SysGoogleGroupEmailTable> tables = mapper.pageList(entity);
        if (tables == null || tables.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<SysGoogleGroupEmailListDto> dos = new ArrayList<>(tables.size());

        for (SysGoogleGroupEmailTable SysGoogleGroupEmailTable : tables) {
            SysGoogleGroupEmailListDto SysGoogleGroupEmailListDto = transformSearchListDto(SysGoogleGroupEmailTable);
            dos.add(SysGoogleGroupEmailListDto);
        }
        return BaseResultDto.success(dos);
    }

    private SysGoogleGroupEmailListDto transformSearchListDto(SysGoogleGroupEmailTable SysGoogleGroupEmailTable) {
        SysGoogleGroupEmailListDto SysGoogleGroupEmailListDto = new SysGoogleGroupEmailListDto();
        BeanUtils.copyProperties(SysGoogleGroupEmailTable, SysGoogleGroupEmailListDto);
        return SysGoogleGroupEmailListDto;
    }


}
