package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysCountryMapper;
import com.crm.manager.dao.repository.SysCountryRepository;
import com.crm.manager.service.SysCountryService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.CustomConstant;
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.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.country.*;
import com.crm.rely.backend.core.pojo.table.SysCountryTable;
import com.crm.rely.backend.exception.ServiceException;
import com.google.common.base.Strings;
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 SysCountryServiceImpl implements SysCountryService {

    @Autowired
    private SysCountryRepository sysCountryRepository;

    @Autowired
    private SysCountryMapper sysCountryMapper;

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysCountryDeleteEntitys entity) throws ServiceException {
        List<SysCountryTable> tables = sysCountryRepository.findAllByIdIn(entity.getIds());
        if (tables == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        //sysCountryRepository.delete(tables);
        for (SysCountryTable table : tables) {
            sysCountryRepository.delete(table);
        }
    }

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

    }

    @Override
    public ResultWithPagerDto<SysCountrySearchListDto> searchPageList(SysCountrySearchListEntity entity) throws ServiceException {
        PageDto pageDto = null;
        if(entity.getPid() == null || entity.getPid() == 0){
            entity.setPid(0l);
            Integer count = sysCountryMapper.countList(entity);
            pageDto = PageDto.format(entity, count);
            if (count == null || count <= 0) {
                return ResultWithPagerDto.success(pageDto);
            }
        }else{
            entity.setPage(null);
        }

        List<SysCountryTable> sysCountryTables = sysCountryMapper.pageList(entity);
        List<SysCountrySearchListDto> sysCountrySearchListDtos = new ArrayList<>(sysCountryTables.size());

        for (SysCountryTable sysCountryTable : sysCountryTables) {
            SysCountrySearchListDto sysCountrySearchListDto = transformSearchListDto(sysCountryTable);
            sysCountrySearchListDtos.add(sysCountrySearchListDto);
        }

        return ResultWithPagerDto.success(pageDto, sysCountrySearchListDtos);
    }

    private SysCountrySearchListDto transformSearchListDto(SysCountryTable sysCountryTable) {
        SysCountrySearchListDto sysCountrySearchListDto = new SysCountrySearchListDto();
        BeanUtils.copyProperties(sysCountryTable, sysCountrySearchListDto);
        return sysCountrySearchListDto;
    }

    @Override
    public SysCountrySearchListDto searchSingle(SingleLongEntity entity) throws ServiceException {
        SysCountrySearchListDto dto = sysCountryMapper.getFirstById(entity.getId());
        if (dto == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        return dto;
    }

    @Override
    public SysCountryTable getByCode(String code) throws ServiceException {

        //判断code不为空
        if (Strings.isNullOrEmpty(code)) {
            throw new ServiceException(CustomConstant.COUNTRY_NOT_NULL);
        }

        SysCountryTable sysCountryTable = sysCountryRepository.getByCode(code);
        if (sysCountryTable == null) {
            throw new ServiceException(CustomConstant.COUNTRY_NOT_EXIST_ERROR);
        }

        return sysCountryTable;
    }

}
