package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysPushMassageMapper;
import com.crm.manager.dao.repository.SysPushMassageRepository;
import com.crm.manager.service.SysPushMassageService;
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.push.massage.SysPushMassageSearchDto;
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.push.massage.*;
import com.crm.rely.backend.core.pojo.table.SysPushMassageTable;
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 SysPushMassageServiceImpl implements SysPushMassageService {

    @Autowired
    private SysPushMassageRepository sysPushMassageRepository;

    @Autowired
    private SysPushMassageMapper sysPushMassageMapper;

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

        SysPushMassageTable sysPushMassageTable = new SysPushMassageTable();
        BeanUtils.copyProperties(entity, sysPushMassageTable);
        sysPushMassageRepository.save(sysPushMassageTable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysPushMassageDeleteEntity entity) throws ServiceException {
        List<SysPushMassageTable> sysPushMassageTables = sysPushMassageRepository.findAllByIdIn(entity.getIds());

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

        sysPushMassageRepository.delete(sysPushMassageTables);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysPushMassageUpdateEntity entity) throws ServiceException {
        SysPushMassageTable sysPushMassageTable = sysPushMassageRepository.findFirstById(entity.getId());

        BeanUtils.copyProperties(entity, sysPushMassageTable);

        sysPushMassageRepository.save(sysPushMassageTable);
    }

    @Override
    public ResultWithPagerDto<SysPushMassageSearchDto> searchPageList(SysPushMassageSearchPageEntity entity) throws ServiceException {
        Integer count = sysPushMassageMapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<SysPushMassageTable> sysPushMassageTables = sysPushMassageMapper.searchPageList(entity);

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

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<SysPushMassageSearchDto> sysPushMassageSearchDtos = new ArrayList<>(sysPushMassageTables.size());

        for (SysPushMassageTable sysPushMassageTable : sysPushMassageTables) {
            SysPushMassageSearchDto sysPushMassageSearchDto = transform(sysPushMassageTable);
            sysPushMassageSearchDtos.add(sysPushMassageSearchDto);
        }

        return ResultWithPagerDto.success(pageDto, sysPushMassageSearchDtos);
    }

    @Override
    public SysPushMassageSearchDto searchSingle(SingleLongEntity entity) throws ServiceException {
        SysPushMassageTable sysPushMassageTable = sysPushMassageRepository.getFirstById(entity.getId());

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

    @Override
    public BaseResultDto<Map<String, SysPushMassageSearchDto>> searchList(SysPushMassageSearchEntity entity) throws ServiceException {
        List<SysPushMassageTable> sysPushMassageTables = sysPushMassageMapper.searchList(entity);

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

            return ResultWithPagerDto.success();
        }
        Map<String, SysPushMassageSearchDto> stringSysPushMassageSearchDtoHashMap = new HashMap<>(sysPushMassageTables.size());

        for (SysPushMassageTable sysPushMassageTable : sysPushMassageTables) {
            SysPushMassageSearchDto sysPushMassageSearchDto = transform(sysPushMassageTable);
            stringSysPushMassageSearchDtoHashMap.put(String.valueOf(sysPushMassageSearchDto.getId()), sysPushMassageSearchDto);
        }

        return BaseResultDto.success(stringSysPushMassageSearchDtoHashMap);
    }


    private SysPushMassageSearchDto transform(SysPushMassageTable sysPushMassageTable) {
        SysPushMassageSearchDto sysPushMassageSearchDto = new SysPushMassageSearchDto();
        BeanUtils.copyProperties(sysPushMassageTable, sysPushMassageSearchDto);
        return sysPushMassageSearchDto;
    }
}
