package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.CustomGroupInterestFreeMapper;
import com.crm.manager.dao.repository.CustomGroupInterestFreeRepository;
import com.crm.manager.service.CustomGroupInterestFreeService;
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.free.CustomGroupInterestFreeListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.group.free.CustomGroupInterestFreeAddEntity;
import com.crm.rely.backend.core.entity.custom.group.free.CustomGroupInterestFreeDeleteEntitys;
import com.crm.rely.backend.core.entity.custom.group.free.CustomGroupInterestFreeSearchEntity;
import com.crm.rely.backend.core.entity.custom.group.free.CustomGroupInterestFreeUpdateEntity;
import com.crm.rely.backend.core.pojo.table.CustomGroupInterestFreeTable;
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;

@Service
public class CustomGroupInterestFreeServiceImpl implements CustomGroupInterestFreeService {

    @Autowired
    private CustomGroupInterestFreeRepository repository;
    @Autowired
    private CustomGroupInterestFreeMapper mapper;

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(CustomGroupInterestFreeDeleteEntitys entity) throws ServiceException {
        List<CustomGroupInterestFreeTable> 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(CustomGroupInterestFreeUpdateEntity entity) throws ServiceException {
        CustomGroupInterestFreeTable table = repository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        BeanUtils.copyProperties(entity, table);
        repository.save(table);
    }

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

        for (CustomGroupInterestFreeTable CustomGroupInterestFreeTable : tables) {
            CustomGroupInterestFreeListDto CustomGroupInterestFreeListDto = transformSearchListDto(CustomGroupInterestFreeTable);
            CustomGroupInterestFreeListDtos.add(CustomGroupInterestFreeListDto);
        }

        return ResultWithPagerDto.success(pageDto, CustomGroupInterestFreeListDtos);
    }

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

        return transformSearchListDto(table);
    }

    private CustomGroupInterestFreeListDto transformSearchListDto(CustomGroupInterestFreeTable CustomGroupInterestFreeTable) {
        CustomGroupInterestFreeListDto CustomGroupInterestFreeListDto = new CustomGroupInterestFreeListDto();
        BeanUtils.copyProperties(CustomGroupInterestFreeTable, CustomGroupInterestFreeListDto);
        return CustomGroupInterestFreeListDto;
    }


}
