package com.crm.manager.service.impl;

import com.crm.manager.dao.repository.UserRoleQaRepository;
import com.crm.manager.service.UserRoleQaService;
import com.crm.rely.backend.core.dto.user.role.qa.UserRoleQaDto;
import com.crm.rely.backend.core.entity.user.role.qa.UserRoleQaSaveEntity;
import com.crm.rely.backend.core.pojo.table.UserRoleQaTable;
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;

/**
 * @description:
 * @author: houn
 * @create: 2024-03-20 19:59
 **/
@Service
public class UserRoleQaServiceImpl implements UserRoleQaService {

    @Autowired
    private UserRoleQaRepository repository;

    @Override
    public List<UserRoleQaDto> getByQaId(Long activityId) throws ServiceException {
        List<UserRoleQaTable> byQaId = repository.getAllByQaId(activityId);
        if (byQaId != null && byQaId.size() > 0) {
            List<UserRoleQaDto> roleQaDtos = new ArrayList<>(byQaId.size());
            for (UserRoleQaTable userRoleQaTable : byQaId) {
                UserRoleQaDto userRoleQaDto = new UserRoleQaDto();
                userRoleQaDto.setQaId(userRoleQaTable.getQaId());
                userRoleQaDto.setId(userRoleQaTable.getId());
                userRoleQaDto.setRoleId(userRoleQaTable.getRoleId());
                roleQaDtos.add(userRoleQaDto);
            }
            return roleQaDtos;
        }
        return null;
    }

    @Override
    public List<UserRoleQaTable> getByQaIdIn(List<Long> qaIds) throws ServiceException {
        List<UserRoleQaTable> userRoleQaTables = repository.getAllByQaIdIn(qaIds);

        if (userRoleQaTables == null || userRoleQaTables.size() <= 0) {
            return null;
        }
        return userRoleQaTables;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(List<UserRoleQaSaveEntity> entities, Long qaId) throws ServiceException {
        if (entities == null || entities.size() <= 0) {
            List<UserRoleQaTable> userRoleQaTables = repository.findAllByQaId(qaId);
            if (userRoleQaTables != null && userRoleQaTables.size() > 0) {
                repository.delete(userRoleQaTables);
            }
            return;
        }

        List<UserRoleQaTable> roleQaTables = new ArrayList<>(entities.size());
        List<UserRoleQaTable> allByQaId = repository.findAllByQaId(qaId);
        List<UserRoleQaTable> userRoleQaTables;
        //判断活动标签是否存在 如果存在 进行判断是否修改和删除 如果不存在则直接进入添加银行实体环节
        if (allByQaId != null || allByQaId.size() > 0) {
            userRoleQaTables = new ArrayList<>(allByQaId.size());
            for (UserRoleQaTable userRoleQaTable : allByQaId) {
                //标识数据库数据是否需要删除
                boolean isDelete = true;
                int size = entities.size();
                for (int i = 0; i < size; i++) {
                    if (entities.get(i).getId() != null && userRoleQaTable.getId().equals(entities.get(i).getId())) {
                        BeanUtils.copyProperties(entities.get(i), userRoleQaTable);
                        userRoleQaTable.setModifyIp(entities.get(i).getIp());
                        userRoleQaTable.setModifyTime(entities.get(i).getTime());
                        userRoleQaTable.setModifyUser(entities.get(i).getUserId());
                        roleQaTables.add(userRoleQaTable);
                        isDelete = false;
                        //移除需要修改的实体 并结束循环
                        entities.remove(i);
                        break;
                    }
                }
                //循环判断中 原数据库数据不在这次修改或者添加范围 则这个记录是需要删除 添加到删除记录中
                if (isDelete) {
                    userRoleQaTables.add(userRoleQaTable);
                }
            }
        } else {
            userRoleQaTables = null;
        }
        //剩下的这些为需要添加的实体
        for (UserRoleQaSaveEntity userRoleQaSaveEntity : entities) {
            userRoleQaSaveEntity.setId(null);
            UserRoleQaTable userRoleQaTable = new UserRoleQaTable();
            BeanUtils.copyProperties(userRoleQaSaveEntity, userRoleQaTable);
            userRoleQaTable.setQaId(qaId);
            userRoleQaTable.setAddIp(userRoleQaSaveEntity.getIp());
            userRoleQaTable.setAddTime(userRoleQaSaveEntity.getTime());
            userRoleQaTable.setAddUser(userRoleQaSaveEntity.getUserId());
            roleQaTables.add(userRoleQaTable);
        }

        if (roleQaTables != null && roleQaTables.size() > 0) {
            repository.save(roleQaTables);
        }

        if (userRoleQaTables != null && userRoleQaTables.size() > 0) {
            repository.delete(userRoleQaTables);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> qaIds) throws ServiceException {
        if (qaIds != null && qaIds.size() > 0) {
            List<UserRoleQaTable> userRoleQaTables =
                    repository.findAllByQaIdIn(qaIds);
            if (userRoleQaTables != null && userRoleQaTables.size() > 0) {
                repository.delete(userRoleQaTables);
            }
        }
    }

}
