package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysInterestSymbolAmountMapper;
import com.crm.manager.dao.repository.SysInterestSymbolAmountRepository;
import com.crm.manager.service.SysInterestSymbolAmountService;
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.symbol.amount.SysInterestSymbolAmountDto;
import com.crm.rely.backend.core.entity.system.interest.symbol.amount.SysInterestSymbolAmountAddEntity;
import com.crm.rely.backend.core.entity.system.interest.symbol.amount.SysInterestSymbolAmountDeleteEntity;
import com.crm.rely.backend.core.entity.system.interest.symbol.amount.SysInterestSymbolAmountSearchPageEntity;
import com.crm.rely.backend.core.entity.system.interest.symbol.amount.SysInterestSymbolAmountUpdateEntity;
import com.crm.rely.backend.core.pojo.table.SysInterestSymbolAmountTable;
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 SysInterestSymbolAmountServiceImpl implements SysInterestSymbolAmountService {

    @Autowired
    private SysInterestSymbolAmountRepository repository;

    @Autowired
    private SysInterestSymbolAmountMapper mapper;

    @Override
    public SysInterestSymbolAmountDto searchSingle(Long id) throws ServiceException {
        SysInterestSymbolAmountTable table = repository.getFirstById(id);

        if (table == null) {

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }

        SysInterestSymbolAmountDto dto = transform(table);
        return dto;
    }

    @Override
    public ResultWithPagerDto<SysInterestSymbolAmountDto> pageSearchList(SysInterestSymbolAmountSearchPageEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<SysInterestSymbolAmountTable> tables = mapper.pageList(entity);

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

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<SysInterestSymbolAmountDto> dtos = transform(tables);
        return ResultWithPagerDto.success(pageDto, dtos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysInterestSymbolAmountAddEntity entity) throws ServiceException {

        if (validCode(entity.getCode())) {
            throw ServiceException.exception("symbol is exist");
        }
        SysInterestSymbolAmountTable table = new SysInterestSymbolAmountTable();
        BeanUtils.copyProperties(entity, table);

        repository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysInterestSymbolAmountUpdateEntity entity) throws ServiceException {
        SysInterestSymbolAmountTable table = repository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        if (!table.getCode().equals(entity.getCode())) {
            if (validCode(entity.getCode())) {
                throw ServiceException.exception("symbol is exist");
            }
        }
        BeanUtils.copyProperties(entity, table);

        repository.save(table);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysInterestSymbolAmountDeleteEntity entity) throws ServiceException {
        List<SysInterestSymbolAmountTable> tables = repository.findAllByIdIn(entity.getIds());
        if (tables == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        repository.delete(tables);
    }

    private SysInterestSymbolAmountDto transform(SysInterestSymbolAmountTable table) {
        if (table == null) {
            return null;
        }
        SysInterestSymbolAmountDto dto = new SysInterestSymbolAmountDto();
        BeanUtils.copyProperties(table, dto);
        return dto;
    }

    private List<SysInterestSymbolAmountDto> transform(List<SysInterestSymbolAmountTable> tables) {
        if (tables == null || tables.size() <= 0) {
            return null;
        }
        List<SysInterestSymbolAmountDto> dtos = new ArrayList<>(tables.size());
        for (SysInterestSymbolAmountTable table : tables) {
            SysInterestSymbolAmountDto dto = transform(table);
            dtos.add(dto);
        }
        return dtos;
    }

    private boolean validCode(String code) {
        return repository.existsByCode(code);
    }
}
