package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.TradeSwapsMapper;
import com.crm.manager.dto.export.TradeSwapsExportDto;
import com.crm.manager.service.TradeService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseReportResultDto;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.ib.trade.TradeSwapsDto;
import com.crm.rely.backend.core.dto.ib.trade.TradeSwapsSumDto;
import com.crm.rely.backend.core.entity.base.BaseIbReportListEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.pojo.view.TradeSwapsView;
import com.crm.rely.backend.util.FormatPage;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @ClassName TradeServiceImpl
 * @Description
 * @Author y
 * @Date 2023/3/23 15:41
 */
@Service
public class TradeServiceImpl implements TradeService {


    @Autowired
    private TradeSwapsMapper tradeSwapsMapper;

    @Override
    public BaseReportResultDto<List<TradeSwapsDto>, TradeSwapsSumDto> tradeSwaps(BaseIbReportListEntity entity, InfoEntity infoEntity) throws Exception {

        Integer count = tradeSwapsMapper.countSearch(entity);
        PageDto pageDto = FormatPage.format(entity.getPage(), count);
        if (count == null || count == 0) {
            return new BaseReportResultDto(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, pageDto);
        }

        List<TradeSwapsView> views = tradeSwapsMapper.pageSearch(entity);
        List<TradeSwapsDto> dtos = convertToDto(views);

        TradeSwapsView sumView = tradeSwapsMapper.sumSearch(entity);
        TradeSwapsSumDto sumDto = TradeSwapsSumDto.getInstance();
        sumDto.setAmount(sumView.getAmount());

        return new BaseReportResultDto<>(dtos, sumDto, pageDto);
    }

    @Override
    public List<TradeSwapsExportDto> tradeSwapsExport(BaseIbReportListEntity entity, InfoEntity infoEntity) throws Exception {
        entity.setPage(null);
        List<TradeSwapsView> views = tradeSwapsMapper.pageSearch(entity);
        List<TradeSwapsExportDto> dtos = convertToExportDto(views);
        return dtos;
    }

    private List<TradeSwapsDto> convertToDto(List<TradeSwapsView> views) throws Exception {
        if (CollectionUtils.isEmpty(views)) return Lists.newArrayList();
        List<TradeSwapsDto> dtos = Lists.newArrayList();
        views.forEach(view -> {
            TradeSwapsDto dto = TradeSwapsDto.getInstance();
            BeanUtils.copyProperties(view, dto);
            dtos.add(dto);
        });

        return dtos;

    }


    private List<TradeSwapsExportDto> convertToExportDto(List<TradeSwapsView> views) throws Exception {
        if (CollectionUtils.isEmpty(views)) return Lists.newArrayList();
        List<TradeSwapsExportDto> dtos = Lists.newArrayList();
        views.forEach(view -> {
            TradeSwapsExportDto dto = TradeSwapsExportDto.getInstance();
            BeanUtils.copyProperties(view, dto);
            dtos.add(dto);
        });

        return dtos;

    }
}
