package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SalesRewardRobotMapper;
import com.crm.manager.dao.repository.SalesRewardRobotRepository;
import com.crm.manager.service.SalesRewardRobotService;
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.sales.SalesRewardRobotListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.sales.SalesRewardRobotAddEntity;
import com.crm.rely.backend.core.entity.sales.SalesRewardRobotDeleteEntitys;
import com.crm.rely.backend.core.entity.sales.SalesRewardRobotSearchEntity;
import com.crm.rely.backend.core.entity.sales.SalesRewardRobotUpdateEntity;
import com.crm.rely.backend.core.pojo.table.SalesRewardRobotTable;
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 SalesRewardRobotServiceImpl implements SalesRewardRobotService {

    @Autowired
    private SalesRewardRobotRepository repository;
    @Autowired
    private SalesRewardRobotMapper mapper;

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SalesRewardRobotDeleteEntitys entity) throws ServiceException {
        List<SalesRewardRobotTable> 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(SalesRewardRobotUpdateEntity entity) throws ServiceException {
        SalesRewardRobotTable 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<SalesRewardRobotListDto> searchPageList(SalesRewardRobotSearchEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<SalesRewardRobotTable> tables = mapper.pageList(entity);

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

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<SalesRewardRobotListDto> salesRewardRobotListDtos = new ArrayList<>(tables.size());

        for (SalesRewardRobotTable SalesRewardRobotTable : tables) {
            SalesRewardRobotListDto salesRewardRobotListDto = transformSearchListDto(SalesRewardRobotTable);
            salesRewardRobotListDtos.add(salesRewardRobotListDto);
        }

        return ResultWithPagerDto.success(pageDto, salesRewardRobotListDtos);
    }

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

        return transformSearchListDto(table);
    }

    private SalesRewardRobotListDto transformSearchListDto(SalesRewardRobotTable salesRewardRobotTable) {
        SalesRewardRobotListDto salesRewardRobotListDto = new SalesRewardRobotListDto();
        BeanUtils.copyProperties(salesRewardRobotTable, salesRewardRobotListDto);
        return salesRewardRobotListDto;
    }


}
