package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysIpLimitMapper;
import com.crm.manager.dao.repository.SysIpLimitRepository;
import com.crm.manager.service.SysIpLimitService;
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.ip.SysIpLimitDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.ip.SysIpLimitAddEntity;
import com.crm.rely.backend.core.entity.system.ip.SysIpLimitDeleteEntity;
import com.crm.rely.backend.core.entity.system.ip.SysIpLimitSearchEntity;
import com.crm.rely.backend.core.entity.system.ip.SysIpLimitUpdateEntity;
import com.crm.rely.backend.core.pojo.table.SysIpLimitTable;
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 SysIpLimitServiceImpl implements SysIpLimitService {

    @Autowired
    private SysIpLimitRepository repository;

    @Autowired
    private SysIpLimitMapper mapper;

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

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysIpLimitUpdateEntity entity) throws ServiceException {
        SysIpLimitTable table = repository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        if (!table.getIp().equals(entity.getIp())) {
            if (validatedIpExist(entity.getIp())) {
                throw ServiceException.exception(Constants.IP_CODE_IS_EXIST_ERROR);
            }
        }

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

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

        repository.delete(tables);
    }

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

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

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

        for (SysIpLimitTable sysBankTable : sysBankTables) {
            SysIpLimitDto sysConfigSearchDto = transformListDto(sysBankTable);
            sysIpLimitDtos.add(sysConfigSearchDto);
        }

        return ResultWithPagerDto.success(pageDto, sysIpLimitDtos);
    }

    @Override
    public SysIpLimitDto searchSingle(SingleLongEntity entity) throws ServiceException {
        SysIpLimitTable table = repository.getFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        SysIpLimitDto sysIpLimitDto = transformListDto(table);
        return sysIpLimitDto;
    }

    @Override
    public List<SysIpLimitDto> getList() throws ServiceException {
        List<SysIpLimitTable> sysBankTables = repository.getAllByIdIsNotNull();
        if (sysBankTables == null) {
            return null;
        }

        List<SysIpLimitDto> dtos = new ArrayList<>(sysBankTables.size());

        for (SysIpLimitTable table : sysBankTables) {
            SysIpLimitDto sysBankListDto = transformListDto(table);
            dtos.add(sysBankListDto);
        }

        return dtos;
    }

    @Override
    public boolean validatedIpExist(String ip) throws ServiceException {
        return repository.existsByIp(ip);
    }


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