package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysReasonsRefusalMapper;
import com.crm.manager.dao.repository.SysReasonsRefusalRepository;
import com.crm.manager.service.SysReasonsRefusalService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
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.reasons.refusal.SysReasonsRefusalSearchDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.reasons.refusal.*;
import com.crm.rely.backend.core.pojo.table.SysReasonsRefusalTable;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: houn
 * @Date: 2020/9/11 1:15
 * @Description:
 */
@Service
public class SysReasonsRefusalServiceImpl implements SysReasonsRefusalService {

    @Autowired
    private SysReasonsRefusalRepository sysReasonsRefusalRepository;

    @Autowired
    private SysReasonsRefusalMapper sysReasonsRefusalMapper;

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

        SysReasonsRefusalTable sysReasonsRefusalTable = new SysReasonsRefusalTable();
        BeanUtils.copyProperties(entity, sysReasonsRefusalTable);
        sysReasonsRefusalRepository.save(sysReasonsRefusalTable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysReasonsRefusalDeleteEntity entity) throws ServiceException {
        List<SysReasonsRefusalTable> sysReasonsRefusalTables =
                sysReasonsRefusalRepository.findAllByIdIn(entity.getIds());

        if (sysReasonsRefusalTables == null || sysReasonsRefusalTables.size() <= 0) {
            return;
        }

        sysReasonsRefusalRepository.delete(sysReasonsRefusalTables);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysReasonsRefusalUpdateEntity entity) throws ServiceException {
        SysReasonsRefusalTable sysReasonsRefusalTable = sysReasonsRefusalRepository.findFirstById(entity.getId());

        BeanUtils.copyProperties(entity, sysReasonsRefusalTable);

        sysReasonsRefusalRepository.save(sysReasonsRefusalTable);
    }

    @Override
    public ResultWithPagerDto<SysReasonsRefusalSearchDto> searchPageList(SysReasonsRefusalSearchPageEntity entity) throws ServiceException {
        Integer count = sysReasonsRefusalMapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<SysReasonsRefusalTable> sysReasonsRefusalTables = sysReasonsRefusalMapper.searchPageList(entity);

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

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<SysReasonsRefusalSearchDto> sysReasonsRefusalSearchDtos = new ArrayList<>(sysReasonsRefusalTables.size());

        for (SysReasonsRefusalTable sysReasonsRefusalTable : sysReasonsRefusalTables) {
            SysReasonsRefusalSearchDto sysReasonsRefusalSearchDto = transform(sysReasonsRefusalTable);
            sysReasonsRefusalSearchDtos.add(sysReasonsRefusalSearchDto);
        }

        return ResultWithPagerDto.success(pageDto, sysReasonsRefusalSearchDtos);
    }

    @Override
    public SysReasonsRefusalSearchDto searchSingle(SingleLongEntity entity) throws ServiceException {
        SysReasonsRefusalTable sysReasonsRefusalTable = sysReasonsRefusalRepository.getFirstById(entity.getId());

        if (sysReasonsRefusalTable == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        return transform(sysReasonsRefusalTable);
    }

    @Override
    public List<SysReasonsRefusalSearchDto> searchList(List<Long> ids) throws ServiceException {
        List<SysReasonsRefusalTable> sysReasonsRefusalTables = sysReasonsRefusalRepository.getFirstByIdIn(ids);

        List<SysReasonsRefusalSearchDto> dtos = new ArrayList<>();

        for (SysReasonsRefusalTable sysReasonsRefusalTable : sysReasonsRefusalTables) {
            SysReasonsRefusalSearchDto sysReasonsRefusalSearchDto = transform(sysReasonsRefusalTable);
            dtos.add(sysReasonsRefusalSearchDto);
        }
        return dtos;
    }

    @Override
    public BaseResultDto<Map<String, SysReasonsRefusalSearchDto>> searchList(SysReasonsRefusalSearchEntity entity) throws ServiceException {
        List<SysReasonsRefusalTable> sysReasonsRefusalTables = sysReasonsRefusalMapper.searchList(entity);

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

            return ResultWithPagerDto.success();
        }
        Map<String, SysReasonsRefusalSearchDto> sysReasonsRefusalSearchDtos =
                new HashMap<>(sysReasonsRefusalTables.size());

        for (SysReasonsRefusalTable sysReasonsRefusalTable : sysReasonsRefusalTables) {
            SysReasonsRefusalSearchDto sysReasonsRefusalSearchDto = transform(sysReasonsRefusalTable);
            sysReasonsRefusalSearchDtos.put(String.valueOf(sysReasonsRefusalSearchDto.getId()),
                    sysReasonsRefusalSearchDto);
        }

        return BaseResultDto.success(sysReasonsRefusalSearchDtos);
    }


    private SysReasonsRefusalSearchDto transform(SysReasonsRefusalTable sysReasonsRefusalTable) {
        SysReasonsRefusalSearchDto sysReasonsRefusalSearchDto = new SysReasonsRefusalSearchDto();
        BeanUtils.copyProperties(sysReasonsRefusalTable, sysReasonsRefusalSearchDto);
        return sysReasonsRefusalSearchDto;
    }
}
