package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysQaMapper;
import com.crm.manager.dao.repository.SysQaRepository;
import com.crm.manager.service.SysQaService;
import com.crm.manager.service.UserRoleQaService;
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.system.qa.SysQaDto;
import com.crm.rely.backend.core.dto.user.role.qa.UserRoleQaDto;
import com.crm.rely.backend.core.entity.system.qa.SysQaAddEntity;
import com.crm.rely.backend.core.entity.system.qa.SysQaDeleteEntity;
import com.crm.rely.backend.core.entity.system.qa.SysQaSearchPageEntity;
import com.crm.rely.backend.core.entity.system.qa.SysQaUpdateEntity;
import com.crm.rely.backend.core.pojo.table.SysQaTable;
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 java.util.ArrayList;
import java.util.List;

@Service
public class SysQaServiceImpl implements SysQaService {

    @Autowired
    private SysQaRepository repository;

    @Autowired
    private SysQaMapper mapper;

    @Autowired
    private UserRoleQaService userRoleQaService;

    @Override
    public SysQaDto searchSingle(Long id) throws ServiceException {
        SysQaTable table = repository.getFirstById(id);

        if (table == null) {

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }

        SysQaDto dto = transform(table);
        List<UserRoleQaDto> roleQaDtos = userRoleQaService.getByQaId(dto.getId());

        dto.setRoleIds(roleQaDtos);
        return dto;
    }

    @Override
    public ResultWithPagerDto<SysQaDto> pageSearchList(SysQaSearchPageEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<SysQaTable> tables = mapper.pageList(entity);

        if (tables == null || tables.size() <= 0) {

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<SysQaDto> dtos = transform(tables);
        return ResultWithPagerDto.success(pageDto, dtos);
    }

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

        SysQaTable table = new SysQaTable();
        BeanUtils.copyProperties(entity, table);

        repository.save(table);

        userRoleQaService.save(entity.getRoleIds(), table.getId());
    }

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


        SysQaTable table = repository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        BeanUtils.copyProperties(entity, table);

        repository.save(table);

        userRoleQaService.save(entity.getRoleIds(), table.getId());

    }

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

        repository.delete(tables);

        userRoleQaService.delete(entity.getIds());
    }

    private SysQaDto transform(SysQaTable table) {
        if (table == null) {
            return null;
        }
        SysQaDto dto = new SysQaDto();
        BeanUtils.copyProperties(table, dto);
        return dto;
    }

    private List<SysQaDto> transform(List<SysQaTable> tables) {
        if (tables == null || tables.size() <= 0) {
            return null;
        }
        List<SysQaDto> dtos = new ArrayList<>(tables.size());
        for (SysQaTable table : tables) {
            table.setContent(null);
            SysQaDto dto = transform(table);
            dtos.add(dto);
        }
        return dtos;
    }
}
