package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.CustomRemarkMapper;
import com.crm.manager.dao.repository.CustomRemarkRepository;
import com.crm.manager.service.CustomRemarkService;
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.custom.remark.CustomRemarkSearchListDto;
import com.crm.rely.backend.core.dto.finance.rate.FinanceRateSearchDto;
import com.crm.rely.backend.core.entity.custom.remark.CustomRemarkAddEntity;
import com.crm.rely.backend.core.entity.custom.remark.CustomRemarkSearchListEntity;
import com.crm.rely.backend.core.pojo.table.CustomRemarkTable;
import com.crm.rely.backend.core.pojo.table.FinanceRateTable;
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;

/**
 * @description:
 * @author: houn
 * @create: 2020-07-04 13:15
 **/
@Service
public class CustomRemarkServiceImpl implements CustomRemarkService {

    @Autowired
    private CustomRemarkRepository customRemarkRepository;

    @Autowired
    private CustomRemarkMapper customRemarkMapper;

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

        CustomRemarkTable customRemarkTable = new CustomRemarkTable();
        BeanUtils.copyProperties(entity, customRemarkTable);
        customRemarkRepository.save(customRemarkTable);
    }

    @Override
    public ResultWithPagerDto<CustomRemarkSearchListDto> searchList(CustomRemarkSearchListEntity entity) throws ServiceException {
        Integer count = customRemarkMapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<CustomRemarkTable> customRemarkTables = customRemarkMapper.pageList(entity);

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

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<CustomRemarkSearchListDto> customRemarkSearchListDtos = new ArrayList<>(customRemarkTables.size());

        for (CustomRemarkTable customRemarkTable : customRemarkTables) {
            CustomRemarkSearchListDto rateSearchDto = transform(customRemarkTable);
            customRemarkSearchListDtos.add(rateSearchDto);
        }

        return ResultWithPagerDto.success(pageDto, customRemarkSearchListDtos);
    }

    private CustomRemarkSearchListDto transform(CustomRemarkTable customRemarkTable) {
        CustomRemarkSearchListDto customRemarkSearchListDto = new CustomRemarkSearchListDto();
        BeanUtils.copyProperties(customRemarkTable, customRemarkSearchListDto);
        return customRemarkSearchListDto;
    }
}
