package com.crm.pay.service.impl;

import com.crm.pay.dao.mapper.CustomFundHistoryMapper;
import com.crm.pay.dao.repository.CustomFundHistoryRepository;
import com.crm.pay.service.CustomFundHistoryService;
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.exception.ServiceException;
import com.crm.rely.backend.model.dto.custom.fund.history.CustomFundHistoryDto;
import com.crm.rely.backend.model.entity.custom.fund.history.CustomFundHistoryAddEntity;
import com.crm.rely.backend.model.entity.custom.fund.history.CustomFundHistorySearchListEntity;
import com.crm.rely.backend.model.pojo.table.CustomFundHistoryTable;
import com.crm.rely.backend.model.pojo.view.CustomFundHistoryView;
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 CustomFundHistoryServiceImpl implements CustomFundHistoryService {

    @Autowired
    private CustomFundHistoryRepository repository;

    @Autowired
    private CustomFundHistoryMapper mapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(CustomFundHistoryAddEntity entity) throws ServiceException {
        CustomFundHistoryTable customFundHistoryTable = new CustomFundHistoryTable();
        BeanUtils.copyProperties(entity, customFundHistoryTable);
        repository.save(customFundHistoryTable);
    }


    @Override
    public ResultWithPagerDto<CustomFundHistoryDto> searchPageList(CustomFundHistorySearchListEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }

        BaseResultDto<List<CustomFundHistoryDto>> listBaseResultDto = searchList(entity);

        return ResultWithPagerDto.success(pageDto, listBaseResultDto.getData());
    }

    @Override
    public BaseResultDto<List<CustomFundHistoryDto>> searchList(CustomFundHistorySearchListEntity entity) throws ServiceException {

        List<CustomFundHistoryView> views = mapper.searchList(entity);

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

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<CustomFundHistoryDto> dtos = new ArrayList<>(views.size());

        for (CustomFundHistoryView view : views) {
            CustomFundHistoryDto transform = transform(view);
            dtos.add(transform);
        }

        return BaseResultDto.success(dtos);
    }


    private CustomFundHistoryDto transform(CustomFundHistoryView view) {
        CustomFundHistoryDto dto = new CustomFundHistoryDto();
        BeanUtils.copyProperties(view, dto);
        return dto;
    }
}
