package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.CustomGroupTypeMapper;
import com.crm.manager.dao.repository.CustomGroupTypeRepository;
import com.crm.manager.service.CustomGroupTypeService;
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.custom.group.type.CustomGroupTypeDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.group.type.CustomGroupTypeAddEntity;
import com.crm.rely.backend.core.entity.custom.group.type.CustomGroupTypeDeleteEntity;
import com.crm.rely.backend.core.entity.custom.group.type.CustomGroupTypeSearchEntity;
import com.crm.rely.backend.core.entity.custom.group.type.CustomGroupTypeUpdateEntity;
import com.crm.rely.backend.core.pojo.table.CustomGroupTypeTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.FormatPage;
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;

/**
 * Created by max on 2020/7/8.
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Service
public class CustomGroupTypeServiceImpl implements CustomGroupTypeService {

    @Autowired
    private CustomGroupTypeRepository customGroupRepository;

    @Autowired
    private CustomGroupTypeMapper customGroupTypeMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(CustomGroupTypeAddEntity entity) throws Exception {
        CustomGroupTypeTable customGroupTypeTable = new CustomGroupTypeTable();

        BeanUtils.copyProperties(entity, customGroupTypeTable);

        customGroupRepository.save(customGroupTypeTable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CustomGroupTypeUpdateEntity entity) throws Exception {
        CustomGroupTypeTable customGroupTypeTable = customGroupRepository.findOne(entity.getId());
        if (customGroupTypeTable == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        BeanUtils.copyProperties(entity, customGroupTypeTable);

        customGroupRepository.save(customGroupTypeTable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(CustomGroupTypeDeleteEntity entity) throws Exception {
        List<CustomGroupTypeTable> customGroupTypeTables = customGroupRepository.findAllByIdIn(entity.getIds());
        if (customGroupTypeTables == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        customGroupRepository.delete(customGroupTypeTables);
    }

    @Override
    public ResultWithPagerDto<CustomGroupTypeDto> searchPageList(CustomGroupTypeSearchEntity entity) throws Exception {
       /*
            获取总数
         */
        Integer count = customGroupTypeMapper.countList(entity);
        PageDto pageDto = FormatPage.format(entity.getPage(), count);
        if (count <= 0) {
            if (count == null || count == 0) {
                return ResultWithPagerDto.success(pageDto);
            }
        }
        /*
            获取列表
         */
        List<CustomGroupTypeTable> customGroupTypeTables = customGroupTypeMapper.pageList(entity);
        if (customGroupTypeTables == null || customGroupTypeTables.size() <= 0) {

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<CustomGroupTypeDto> customGroupTypeDtos = new ArrayList<>(customGroupTypeTables.size());

        for (CustomGroupTypeTable customGroupTypeTable : customGroupTypeTables) {
            CustomGroupTypeDto rateSearchDto = transform(customGroupTypeTable);
            customGroupTypeDtos.add(rateSearchDto);
        }
        return ResultWithPagerDto.success(pageDto, customGroupTypeDtos);
    }

    @Override
    public BaseResultDto<CustomGroupTypeDto> searchSingle(SingleLongEntity entity) throws Exception {
        CustomGroupTypeTable customGroupTypeTable = customGroupRepository.getFirstById(entity.getId());
        if (customGroupTypeTable == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        CustomGroupTypeDto customGroupTypeDto = new CustomGroupTypeDto();
        BeanUtils.copyProperties(customGroupTypeTable, customGroupTypeDto);

        return BaseResultDto.success(customGroupTypeDto);
    }

    @Override
    public BaseResultDto<List<CustomGroupTypeDto>> getList() throws Exception {

        List<CustomGroupTypeTable> tables = customGroupRepository.getAllByIdIsNotNull();

        List<CustomGroupTypeDto> dtos = new ArrayList<>();
        tables.forEach((value) -> {
            CustomGroupTypeDto customGroupTypeDto = new CustomGroupTypeDto();

            BeanUtils.copyProperties(value, customGroupTypeDto);
            dtos.add(customGroupTypeDto);
        });

        return BaseResultDto.success(dtos);
    }

    private CustomGroupTypeDto transform(CustomGroupTypeTable customGroupTypeTable) {
        CustomGroupTypeDto customGroupTypeDto = new CustomGroupTypeDto();
        BeanUtils.copyProperties(customGroupTypeTable, customGroupTypeDto);
        return customGroupTypeDto;
    }

}
