package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysInterestLoginDayNumberMapper;
import com.crm.manager.dao.repository.SysInterestLoginDayNumberRepository;
import com.crm.manager.service.SysInterestLoginDayNumberService;
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.interest.SysInterestLoginDayNumberDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.interest.SysInterestLoginDayNumberAddEntity;
import com.crm.rely.backend.core.entity.system.interest.SysInterestLoginDayNumberDeleteEntitys;
import com.crm.rely.backend.core.entity.system.interest.SysInterestLoginDayNumberSearchEntity;
import com.crm.rely.backend.core.entity.system.interest.SysInterestLoginDayNumberUpdateEntity;
import com.crm.rely.backend.core.pojo.table.SysInterestLoginDayNumberTable;
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 SysInterestLoginDayNumberServiceImpl implements SysInterestLoginDayNumberService {

    @Autowired
    private SysInterestLoginDayNumberRepository repository;
    @Autowired
    private SysInterestLoginDayNumberMapper mapper;

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysInterestLoginDayNumberDeleteEntitys entity) throws ServiceException {
        List<SysInterestLoginDayNumberTable> 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(SysInterestLoginDayNumberUpdateEntity entity) throws ServiceException {
        SysInterestLoginDayNumberTable 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<SysInterestLoginDayNumberDto> searchPageList(SysInterestLoginDayNumberSearchEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<SysInterestLoginDayNumberTable> tables = mapper.pageList(entity);
        if (tables == null || tables.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<SysInterestLoginDayNumberDto> sysInterestLoginDayNumberListDtos = new ArrayList<>(tables.size());

        for (SysInterestLoginDayNumberTable SysInterestLoginDayNumberTable : tables) {
            SysInterestLoginDayNumberDto dto = transformSearchListDto(SysInterestLoginDayNumberTable);
            sysInterestLoginDayNumberListDtos.add(dto);
        }

        return ResultWithPagerDto.success(pageDto, sysInterestLoginDayNumberListDtos);
    }

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

        return transformSearchListDto(table);
    }

    private SysInterestLoginDayNumberDto transformSearchListDto(SysInterestLoginDayNumberTable sysInterestLoginDayNumberTable) {
        SysInterestLoginDayNumberDto dto = new SysInterestLoginDayNumberDto();
        BeanUtils.copyProperties(sysInterestLoginDayNumberTable, dto);
        return dto;
    }


}
