package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysCustomBlacklistMapper;
import com.crm.manager.dao.repository.SysCustomBlacklistRepository;
import com.crm.manager.service.SysCustomBlacklistService;
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.system.blacklist.SysCustomBlacklistDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.blacklist.SysCustomBlacklistAddEntity;
import com.crm.rely.backend.core.entity.system.blacklist.SysCustomBlacklistDeleteEntitys;
import com.crm.rely.backend.core.entity.system.blacklist.SysCustomBlacklistSearchListEntity;
import com.crm.rely.backend.core.entity.system.blacklist.SysCustomBlacklistUpdateEntity;
import com.crm.rely.backend.core.pojo.table.SysCustomBlacklistTable;
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;

/**
 * @Author: houn
 * @Date: 2020/10/18 21:38
 * @Description:
 */
@Service
public class SysCustomBlacklistServiceImpl implements SysCustomBlacklistService {

    @Autowired
    private SysCustomBlacklistRepository repository;

    @Autowired
    private SysCustomBlacklistMapper mapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysCustomBlacklistAddEntity entity) throws ServiceException {

        SysCustomBlacklistTable table = new SysCustomBlacklistTable();
        BeanUtils.copyProperties(entity, table);
        repository.save(table);
    }

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

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

        repository.delete(tables);
    }

    @Override
    public ResultWithPagerDto<SysCustomBlacklistDto> searchPageList(SysCustomBlacklistSearchListEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<SysCustomBlacklistTable> sysBankTables = mapper.pageList(entity);

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

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<SysCustomBlacklistDto> dtos = new ArrayList<>(sysBankTables.size());

        for (SysCustomBlacklistTable table : sysBankTables) {
            SysCustomBlacklistDto sysConfigSearchDto = transformSearchListDto(table);
            dtos.add(sysConfigSearchDto);
        }

        return ResultWithPagerDto.success(pageDto, dtos);
    }

    @Override
    public SysCustomBlacklistDto searchSingle(SingleLongEntity entity) throws ServiceException {
        SysCustomBlacklistTable table = repository.getFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        SysCustomBlacklistDto sysBankSearchListDto = transformSearchListDto(table);
        return sysBankSearchListDto;
    }


    private SysCustomBlacklistDto transformSearchListDto(SysCustomBlacklistTable sysBankTable) {
        SysCustomBlacklistDto dto = new SysCustomBlacklistDto();
        BeanUtils.copyProperties(sysBankTable, dto);
        return dto;
    }

    private SysCustomBlacklistDto transformListDto(SysCustomBlacklistTable sysBankTable) {
        SysCustomBlacklistDto sysBankListDto = new SysCustomBlacklistDto();
        BeanUtils.copyProperties(sysBankTable, sysBankListDto);
        return sysBankListDto;
    }
}
