package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysApproveEmailMapper;
import com.crm.manager.dao.repository.SysApproveEmailRepository;
import com.crm.manager.entity.excel.SysApproveEmailExcelEntity;
import com.crm.manager.service.SysApproveEmailService;
import com.crm.rely.backend.core.constant.Constants;
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.country.SysCountrySearchListDto;
import com.crm.rely.backend.core.dto.system.email.approve.SysApproveEmailListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.email.approve.SysApproveEmailAddEntity;
import com.crm.rely.backend.core.entity.system.email.approve.SysApproveEmailDeleteEntitys;
import com.crm.rely.backend.core.entity.system.email.approve.SysApproveEmailSearchEntity;
import com.crm.rely.backend.core.entity.system.email.approve.SysApproveEmailUpdateEntity;
import com.crm.rely.backend.core.pojo.table.SysApproveEmailTable;
import com.crm.rely.backend.core.pojo.table.SysCountryTable;
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 SysApproveEmailServiceImpl implements SysApproveEmailService {

    @Autowired
    private SysApproveEmailRepository repository;
    @Autowired
    private SysApproveEmailMapper mapper;

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysApproveEmailDeleteEntitys entity) throws ServiceException {
        List<SysApproveEmailTable> 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(SysApproveEmailUpdateEntity entity) throws ServiceException {
        SysApproveEmailTable 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<SysApproveEmailListDto> searchPageList(SysApproveEmailSearchEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<SysApproveEmailTable> tables = mapper.pageList(entity);

        if (tables == null || tables.size() <= 0) {

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<SysApproveEmailListDto> sysApproveEmailListDtos = new ArrayList<>(tables.size());

        for (SysApproveEmailTable sysApproveEmailTable : tables) {
            SysApproveEmailListDto sysApproveEmailListDto = transformSearchListDto(sysApproveEmailTable);
            sysApproveEmailListDtos.add(sysApproveEmailListDto);
        }

        return ResultWithPagerDto.success(pageDto, sysApproveEmailListDtos);
    }

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

        return transformSearchListDto(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importSysApproveEmail(List<SysApproveEmailExcelEntity> emailEntities) {
        List<SysApproveEmailTable> tables = new ArrayList<>();
        for (SysApproveEmailExcelEntity entity : emailEntities) {
            SysApproveEmailTable table = new SysApproveEmailTable();
            BeanUtils.copyProperties(entity, table);
            tables.add(table);
        }
        repository.save(tables);
    }

    private SysApproveEmailListDto transformSearchListDto(SysApproveEmailTable sysApproveEmailTable) {
        SysApproveEmailListDto sysApproveEmailListDto = new SysApproveEmailListDto();
        BeanUtils.copyProperties(sysApproveEmailTable, sysApproveEmailListDto);
        return sysApproveEmailListDto;
    }


}
