package com.crm.custom.service.impl;

import com.crm.custom.dao.mapper.GoldenCommonQuestionMapper;
import com.crm.custom.dao.repository.GoldenCommonQuestionRepository;
import com.crm.custom.service.GoldenCommonQuestionService;
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.GoldenCommonQuestionListDto;
import com.crm.rely.backend.model.entity.golden.question.GoldenCommonQuestionAddEntity;
import com.crm.rely.backend.model.entity.golden.question.GoldenCommonQuestionDeleteEntitys;
import com.crm.rely.backend.model.entity.golden.question.GoldenCommonQuestionSearchEntity;
import com.crm.rely.backend.model.entity.golden.question.GoldenCommonQuestionUpdateEntity;
import com.crm.rely.backend.model.pojo.table.GoldenCommonQuestionTable;
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 GoldenCommonQuestionServiceImpl implements GoldenCommonQuestionService {

    @Autowired
    private GoldenCommonQuestionRepository repository;
    @Autowired
    private GoldenCommonQuestionMapper mapper;

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

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

        for (GoldenCommonQuestionTable table : tables) {
            GoldenCommonQuestionListDto dto = transformSearchListDto(table);
            dtos.add(dto);
        }

        return ResultWithPagerDto.success(pageDto, dtos);
    }

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

        return transformSearchListDto(table);
    }

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


}
