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.CardAppCommonProblemTypeDto;
import com.crm.rely.backend.core.entity.base.BaseDeleteEntities;
import com.crm.rely.backend.core.entity.ucard.CardAppCommonProblemTypeAddEntity;
import com.crm.rely.backend.core.entity.ucard.CardAppCommonProblemTypeDropdown;
import com.crm.rely.backend.core.entity.ucard.CardAppCommonProblemTypePageEntity;
import com.crm.rely.backend.core.entity.ucard.CardAppCommonProblemTypeUpdateEntity;
import com.crm.rely.backend.core.pojo.table.ucard.CardAppCommonProblemTypeTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.ucard.dao.mapper.CardAppCommonProblemTypeMapper;
import com.crm.ucard.dao.repository.CardAppCommonProblemTypeRepository;
import com.crm.ucard.service.UcardAppCommonProblemTypeService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
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 UcardAppCommonProblemTypeServiceImpl implements UcardAppCommonProblemTypeService {

    @Autowired
    private CardAppCommonProblemTypeMapper cardAppCommonProblemTypeMapper;
    @Autowired
    private CardAppCommonProblemTypeRepository cardAppCommonProblemTypeRepository;

    /**
     * app常见问题类型列表
     */
    @Override
    public ResultWithPagerDto<CardAppCommonProblemTypeDto> getCardAppCommonProblemTypePageList(CardAppCommonProblemTypePageEntity entity) throws ServiceException {
        try {
            // 获取总数
            Integer count = cardAppCommonProblemTypeMapper.countList(entity);
            PageDto pageDto = PageDto.format(entity, count);
            if (count == null || count <= 0) {
                return ResultWithPagerDto.success(pageDto);
            }
            List<CardAppCommonProblemTypeDto> dtos = cardAppCommonProblemTypeMapper.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 addCardAppCommonProblemType(CardAppCommonProblemTypeAddEntity entity) throws ServiceException {
        CardAppCommonProblemTypeTable typeTable = cardAppCommonProblemTypeRepository.findFirstByLanguage(entity.getLanguage());
        if (typeTable != null && typeTable.getType().equals(entity.getType())){
            return BaseResultDto.error("该语言已存在该问题类型");
        }
        CardAppCommonProblemTypeTable table = new CardAppCommonProblemTypeTable();
        BeanUtils.copyProperties(entity, table);
        cardAppCommonProblemTypeRepository.save(table);
        return BaseResultDto.success();
    }

    /**
     * 修改app常见问题类型
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResultDto updateCardAppCommonProblemType(CardAppCommonProblemTypeUpdateEntity entity) throws ServiceException {
        CardAppCommonProblemTypeTable typeTable = cardAppCommonProblemTypeRepository.findFirstByLanguageAndType(entity.getLanguage(), entity.getType());
        if (typeTable != null){
            return BaseResultDto.error("该语言已存在该问题类型");
        }
        CardAppCommonProblemTypeTable table = cardAppCommonProblemTypeRepository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        BeanUtils.copyProperties(entity, table);
        cardAppCommonProblemTypeRepository.save(table);
        return BaseResultDto.success();
    }

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

    /**
     * app常见问题类型下拉列表
     */
    @Override
    public BaseResultDto cardAppCommonProblemTypeDropdown(CardAppCommonProblemTypeDropdown entity) throws ServiceException {
        List<CardAppCommonProblemTypeTable> tables = cardAppCommonProblemTypeRepository.findAllByLanguage(entity.getLanguage());
        if (CollectionUtils.isEmpty(tables)){
            return BaseResultDto.success();
        }
        List<CardAppCommonProblemTypeDto> dtos = Lists.newArrayList();
        for (CardAppCommonProblemTypeTable table : tables) {
            CardAppCommonProblemTypeDto dto = new CardAppCommonProblemTypeDto();
            BeanUtils.copyProperties(table, dto);
            dtos.add(dto);
        }
        return BaseResultDto.success(dtos);
    }


}
