package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysSourceMapper;
import com.crm.manager.dao.repository.SysSourceRepository;
import com.crm.manager.service.SysSourceService;
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.source.SysSourceDto;
import com.crm.rely.backend.core.entity.system.source.SysSourceAddEntity;
import com.crm.rely.backend.core.entity.system.source.SysSourceDeleteEntity;
import com.crm.rely.backend.core.entity.system.source.SysSourceSearchPageEntity;
import com.crm.rely.backend.core.entity.system.source.SysSourceUpdateEntity;
import com.crm.rely.backend.core.pojo.table.SysSourceTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.UUIDUtil;
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 SysSourceServiceImpl implements SysSourceService {

    @Autowired
    private SysSourceRepository repository;

    @Autowired
    private SysSourceMapper mapper;

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

        if (table == null) {

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }

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

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

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

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

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

        SysSourceTable table = new SysSourceTable();
        BeanUtils.copyProperties(entity, table);

        table.setCode(UUIDUtil.getUUID());
        table.setNumber(0);
        repository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysSourceUpdateEntity entity) throws ServiceException {
        SysSourceTable table = repository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        BeanUtils.copyProperties(entity, table);

        repository.save(table);

    }

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

        repository.delete(tables);
    }

    @Override
    public List<SysSourceDto> getAllSource() throws ServiceException {

        List<SysSourceTable> tables = repository.getAllByIdIsNotNullOrderByIdDesc();
        return transform(tables);
    }

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

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