package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysCountryGroupTypeMapper;
import com.crm.manager.dao.repository.SysCountryGroupTypeRepository;
import com.crm.manager.service.SysCountryGroupTypeService;
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.country.group.type.SysCountryGroupTypeSearchListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.country.group.type.SysCountryGroupTypeAddEntity;
import com.crm.rely.backend.core.entity.system.country.group.type.SysCountryGroupTypeDeleteEntitys;
import com.crm.rely.backend.core.entity.system.country.group.type.SysCountryGroupTypeSearchListEntity;
import com.crm.rely.backend.core.entity.system.country.group.type.SysCountryGroupTypeUpdateEntity;
import com.crm.rely.backend.core.pojo.table.SysCountryGroupTypeTable;
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: 2022=04-12 21:38
 * @Description:
 */
@Service
public class SysCountryGroupTypeServiceImpl implements SysCountryGroupTypeService {

    @Autowired
    private SysCountryGroupTypeRepository sysBankRepository;

    @Autowired
    private SysCountryGroupTypeMapper sysBankMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysCountryGroupTypeAddEntity entity) throws ServiceException {
        if (sysBankRepository.existsByCountry(entity.getCountry())) {
            throw ServiceException.exception(Constants.COUNTRY_EXIST_ERROR);
        }
        SysCountryGroupTypeTable table = new SysCountryGroupTypeTable();
        BeanUtils.copyProperties(entity, table);
        sysBankRepository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysCountryGroupTypeUpdateEntity entity) throws ServiceException {
        SysCountryGroupTypeTable table = sysBankRepository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        if (!table.getCountry().equals(entity.getCountry()) && sysBankRepository.existsByCountry(entity.getCountry())) {
            throw ServiceException.exception(Constants.COUNTRY_EXIST_ERROR);
        }
        BeanUtils.copyProperties(entity, table);
        sysBankRepository.save(table);
    }

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

        sysBankRepository.delete(tables);
    }

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

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

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

        for (SysCountryGroupTypeTable sysBankTable : sysBankTables) {
            SysCountryGroupTypeSearchListDto sysConfigSearchDto = transformSearchListDto(sysBankTable);
            sysBankSearchListDtos.add(sysConfigSearchDto);
        }

        return ResultWithPagerDto.success(pageDto, sysBankSearchListDtos);
    }

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

    private SysCountryGroupTypeSearchListDto transformSearchListDto(SysCountryGroupTypeTable table) {
        SysCountryGroupTypeSearchListDto sysBankSearchListDto = new SysCountryGroupTypeSearchListDto();
        BeanUtils.copyProperties(table, sysBankSearchListDto);
        return sysBankSearchListDto;
    }

}
