package com.crm.ucard.service.impl;

import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
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.ucard.CardAppCommonProblemDto;
import com.crm.rely.backend.core.entity.base.BaseDeleteEntities;
import com.crm.rely.backend.core.entity.ucard.CardAppCommonProblemAddEntity;
import com.crm.rely.backend.core.entity.ucard.CardAppCommonProblemPageEntity;
import com.crm.rely.backend.core.entity.ucard.CardAppCommonProblemUpdateEntity;
import com.crm.rely.backend.core.pojo.table.ucard.CardAppCommonProblemTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.ucard.dao.mapper.CardAppCommonProblemMapper;
import com.crm.ucard.dao.repository.CardAppCommonProblemRepository;
import com.crm.ucard.service.UcardAppCommonProblemService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
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.List;
/**
 * app 常见问题
 */
@Slf4j
@Service
public class UcardAppCommonProblemServiceImpl implements UcardAppCommonProblemService {

    @Autowired
    private CardAppCommonProblemMapper appCommonProblemMapper;
    @Autowired
    private CardAppCommonProblemRepository appCommonProblemRepository;

    /**
     * app常见问题列表
     */
    @Override
    public ResultWithPagerDto<CardAppCommonProblemDto> getCardAppCommonProblemPageList(CardAppCommonProblemPageEntity entity) throws ServiceException {
        try {
            // 获取总数
            Integer count = appCommonProblemMapper.countList(entity);
            PageDto pageDto = PageDto.format(entity, count);
            if (count == null || count <= 0) {
                return ResultWithPagerDto.success(pageDto);
            }
            List<CardAppCommonProblemDto> dtos = appCommonProblemMapper.pageList(entity);
            return ResultWithPagerDto.success(pageDto, dtos);
        } catch (Exception e) {
            log.error("获取app常见问题列表失败", e);
            throw new ServiceException("获取app常见问题列表失败");
        }
    }

    /**
     * 新增app常见问题
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResultDto addCardAppCommonProblem(CardAppCommonProblemAddEntity entity) throws ServiceException {
        CardAppCommonProblemTable table = new CardAppCommonProblemTable();
        BeanUtils.copyProperties(entity, table);
        appCommonProblemRepository.save(table);
        return BaseResultDto.success();
    }

    /**
     * 修改app常见问题
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResultDto updateCardAppCommonProblem(CardAppCommonProblemUpdateEntity entity) throws ServiceException {
        CardAppCommonProblemTable table = appCommonProblemRepository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        BeanUtils.copyProperties(entity, table);
        appCommonProblemRepository.save(table);
        return BaseResultDto.success();
    }

    /**
     * 批量删除app常见问题
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResultDto deleteCardAppCommonProblem(BaseDeleteEntities entity) throws ServiceException {
        List<CardAppCommonProblemTable> tables = Lists.newArrayList();
        for (Long id : entity.getIds()) {
            CardAppCommonProblemTable table = new CardAppCommonProblemTable();
            table.setId(id);
            tables.add(table);
        }
        appCommonProblemRepository.deleteInBatch(tables);
        return BaseResultDto.success();
    }


}
