package com.crm.custom.service.impl;

import com.crm.custom.dao.mapper.GoldenCustomBalanceRecordMapper;
import com.crm.custom.dao.repository.GoldenCustomBalanceRecordRepository;
import com.crm.custom.service.GoldenCustomBalanceRecordService;
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.exception.ServiceException;
import com.crm.rely.backend.model.dto.golden.goods.GoldenCustomBalanceRecordListDto;
import com.crm.rely.backend.model.entity.golden.order.GoldenBalanceSearchEntity;
import com.crm.rely.backend.model.pojo.table.GoldenCustomBalanceRecordTable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoldenCustomBalanceRecordServiceImpl implements GoldenCustomBalanceRecordService {

    @Autowired
    private GoldenCustomBalanceRecordRepository repository;
    @Autowired
    private GoldenCustomBalanceRecordMapper mapper;

   
    @Override
    public ResultWithPagerDto<GoldenCustomBalanceRecordListDto> searchPageList(GoldenBalanceSearchEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<GoldenCustomBalanceRecordTable> tables = mapper.pageList(entity);
        if (tables == null || tables.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<GoldenCustomBalanceRecordListDto> dtos = new ArrayList<>(tables.size());

        for (GoldenCustomBalanceRecordTable table : tables) {
            GoldenCustomBalanceRecordListDto dto = transformSearchListDto(table);
            dtos.add(dto);
        }

        return ResultWithPagerDto.success(pageDto, dtos);
    }

    private GoldenCustomBalanceRecordListDto transformSearchListDto(GoldenCustomBalanceRecordTable table) {
        GoldenCustomBalanceRecordListDto dto = new GoldenCustomBalanceRecordListDto();
        BeanUtils.copyProperties(table, dto);
        return dto;
    }


}
