package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.ActivityRequiteRebateMapper;
import com.crm.manager.dao.repository.ActivityRequiteRebateRepository;
import com.crm.manager.service.ActivityRequiteRebateService;
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.activity.requite.ActivityRequiteRebateDto;
import com.crm.rely.backend.core.entity.activity.requite.ActivityRequiteRebateAddEntity;
import com.crm.rely.backend.core.entity.activity.requite.ActivityRequiteRebateDeleteEntitys;
import com.crm.rely.backend.core.entity.activity.requite.ActivityRequiteRebateSearchEntity;
import com.crm.rely.backend.core.entity.activity.requite.ActivityRequiteRebateUpdateEntity;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.pojo.table.ActivityRequiteRebateTable;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author gao
 * @date 2025/6/24 16:32
 */
@Service
public class ActivityRequiteRebateServiceImpl implements ActivityRequiteRebateService {

    @Autowired
    private ActivityRequiteRebateRepository repository;

    @Resource
    private ActivityRequiteRebateMapper mapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ActivityRequiteRebateAddEntity entity) throws ServiceException {
        if (entity.getIbNo() == null && entity.getRebate() == null) {
            throw ServiceException.exception(Constants.PARAMETER_ERROR);
        }

        ActivityRequiteRebateTable table = new ActivityRequiteRebateTable();
        BeanUtils.copyProperties(entity, table);
        repository.save(table);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(ActivityRequiteRebateDeleteEntitys entity) throws ServiceException {
        List<ActivityRequiteRebateTable> tables = repository.findAllByIdIn(entity.getIds());
        if (tables == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        repository.delete(tables);
    }

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

        if (entity.getIbNo() == null && entity.getRebate() == null) {
            throw ServiceException.exception(Constants.PARAMETER_ERROR);
        }

        ActivityRequiteRebateTable table = repository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        BeanUtils.copyProperties(entity, table);
        repository.save(table);

    }

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

        for (ActivityRequiteRebateTable sysActivityRebateTable : tables) {
            ActivityRequiteRebateDto sysActivityRebateListDto = transformSearchListDto(sysActivityRebateTable);
            sysActivityRebateListDtos.add(sysActivityRebateListDto);
        }

        return ResultWithPagerDto.success(pageDto, sysActivityRebateListDtos);
    }

    @Override
    public ActivityRequiteRebateDto searchSingle(SingleLongEntity entity) throws ServiceException {
        ActivityRequiteRebateTable table = repository.getFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        return transformSearchListDto(table);
    }

    private ActivityRequiteRebateDto transformSearchListDto(ActivityRequiteRebateTable sysActivityRebateTable) {
        ActivityRequiteRebateDto sysActivityRebateDto = new ActivityRequiteRebateDto();
        BeanUtils.copyProperties(sysActivityRebateTable, sysActivityRebateDto);
        return sysActivityRebateDto;
    }
}
