package com.crm.custom.service.impl;

import com.crm.custom.dao.mapper.GoldenRewardQuestionMapper;
import com.crm.custom.dao.repository.GoldenRewardQuestionRepository;
import com.crm.custom.service.GoldenRewardQuestionService;
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.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.dto.golden.question.GoldenRewardQuestionListDto;
import com.crm.rely.backend.model.entity.golden.question.GoldenRewardQuestionAddEntity;
import com.crm.rely.backend.model.entity.golden.question.GoldenRewardQuestionDeleteEntitys;
import com.crm.rely.backend.model.entity.golden.question.GoldenRewardQuestionSearchEntity;
import com.crm.rely.backend.model.entity.golden.question.GoldenRewardQuestionUpdateEntity;
import com.crm.rely.backend.model.pojo.table.GoldenRewardQuestionTable;
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 GoldenRewardQuestionServiceImpl implements GoldenRewardQuestionService {

    @Autowired
    private GoldenRewardQuestionRepository repository;
    @Autowired
    private GoldenRewardQuestionMapper mapper;

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(GoldenRewardQuestionDeleteEntitys entity) throws ServiceException {
        List<GoldenRewardQuestionTable> 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(GoldenRewardQuestionUpdateEntity entity) throws ServiceException {
        GoldenRewardQuestionTable 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<GoldenRewardQuestionListDto> searchPageList(GoldenRewardQuestionSearchEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<GoldenRewardQuestionTable> tables = mapper.pageList(entity);
        if (tables == null || tables.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<GoldenRewardQuestionListDto> dtos = new ArrayList<>(tables.size());

        for (GoldenRewardQuestionTable table : tables) {
            GoldenRewardQuestionListDto dto = transformSearchListDto(table);
            dtos.add(dto);
        }

        return ResultWithPagerDto.success(pageDto, dtos);
    }

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

        return transformSearchListDto(table);
    }

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


}
