package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.CustomAllocationRosterAreaMapper;
import com.crm.manager.dao.repository.CustomAllocationRosterAreaRepository;
import com.crm.manager.service.CustomAllocationRosterAreaService;
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.custom.allocation.roster.area.CustomAllocationRosterAreaListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.allocation.roster.area.CustomAllocationRosterAreaAddEntity;
import com.crm.rely.backend.core.entity.custom.allocation.roster.area.CustomAllocationRosterAreaDeleteEntitys;
import com.crm.rely.backend.core.entity.custom.allocation.roster.area.CustomAllocationRosterAreaSearchEntity;
import com.crm.rely.backend.core.entity.custom.allocation.roster.area.CustomAllocationRosterAreaUpdateEntity;
import com.crm.rely.backend.core.pojo.table.CustomAllocationRosterAreaTable;
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.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class CustomAllocationRosterAreaServiceImpl implements CustomAllocationRosterAreaService {

    @Autowired
    private CustomAllocationRosterAreaRepository repository;
    @Autowired
    private CustomAllocationRosterAreaMapper mapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(CustomAllocationRosterAreaAddEntity entity) throws ServiceException {
        CustomAllocationRosterAreaTable table = new CustomAllocationRosterAreaTable();
        BeanUtils.copyProperties(entity, table);
        table.setCountry(String.join(",", entity.getCountry()));
        repository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(CustomAllocationRosterAreaDeleteEntitys entity) throws ServiceException {
        List<CustomAllocationRosterAreaTable> 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(CustomAllocationRosterAreaUpdateEntity entity) throws ServiceException {
        CustomAllocationRosterAreaTable table = repository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        BeanUtils.copyProperties(entity, table);
        table.setCountry(String.join(",", entity.getCountry()));
        repository.save(table);
    }

    @Override
    public ResultWithPagerDto<CustomAllocationRosterAreaListDto> searchPageList(CustomAllocationRosterAreaSearchEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<CustomAllocationRosterAreaTable> tables = mapper.pageList(entity);
        if (tables == null || tables.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<CustomAllocationRosterAreaListDto> CustomAllocationRosterAreaListDtos = new ArrayList<>(tables.size());

        for (CustomAllocationRosterAreaTable CustomAllocationRosterAreaTable : tables) {
            CustomAllocationRosterAreaListDto CustomAllocationRosterAreaListDto = transformSearchListDto(CustomAllocationRosterAreaTable);
            CustomAllocationRosterAreaListDtos.add(CustomAllocationRosterAreaListDto);
        }

        return ResultWithPagerDto.success(pageDto, CustomAllocationRosterAreaListDtos);
    }

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

        return transformSearchListDto(table);
    }

    @Override
    public List<CustomAllocationRosterAreaListDto> getAllList(CustomAllocationRosterAreaSearchEntity entity) {
        entity.setPage(null);
        List<CustomAllocationRosterAreaTable> tables = mapper.pageList(entity);
        List<CustomAllocationRosterAreaListDto> dtos = new ArrayList<>(tables.size());

        for (CustomAllocationRosterAreaTable table : tables) {
            CustomAllocationRosterAreaListDto customAllocationRosterAreaListDto = transformSearchListDto(table);
            dtos.add(customAllocationRosterAreaListDto);
        }

        return dtos;
    }

    private CustomAllocationRosterAreaListDto transformSearchListDto(CustomAllocationRosterAreaTable customAllocationRosterAreaTable) {
        CustomAllocationRosterAreaListDto dto = new CustomAllocationRosterAreaListDto();
        BeanUtils.copyProperties(customAllocationRosterAreaTable, dto);
        dto.setCountry(Arrays.asList(customAllocationRosterAreaTable.getCountry().split(",")));
        return dto;
    }


}
