package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.CustomLoginDeleteExcludeMapper;
import com.crm.manager.dao.mapper.CustomLoginMarkDeleteMapper;
import com.crm.manager.dao.repository.CustomLoginDeleteExcludeRepository;
import com.crm.manager.dao.repository.CustomLoginRepository;
import com.crm.manager.service.CustomLoginDeleteExcludeService;
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.custom.login.CustomLoginDeleteExcludeListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.login.delete.CustomLoginDeleteExcludeAddEntity;
import com.crm.rely.backend.core.entity.custom.login.delete.CustomLoginDeleteExcludeDeleteEntitys;
import com.crm.rely.backend.core.entity.custom.login.delete.CustomLoginDeleteExcludeSearchEntity;
import com.crm.rely.backend.core.entity.custom.login.delete.CustomLoginDeleteExcludeUpdateEntity;
import com.crm.rely.backend.core.pojo.table.CustomLoginDeleteExcludeTable;
import com.crm.rely.backend.core.pojo.table.CustomLoginTable;
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;
import java.util.stream.Collectors;

@Service
public class CustomLoginDeleteExcludeServiceImpl implements CustomLoginDeleteExcludeService {

    @Autowired
    private CustomLoginDeleteExcludeRepository repository;
    @Autowired
    private CustomLoginDeleteExcludeMapper mapper;
    @Autowired
    private CustomLoginMarkDeleteMapper customLoginMarkDeleteMapper;
    @Autowired
    private CustomLoginRepository customLoginRepository;

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

        List<Long> loginList = null;
        String platform = entity.getPlatform();
        if (entity.getLogin() == null && entity.getCId() == null) {
            throw ServiceException.exception(Constants.PARAMETER_ERROR);
        }else{
            if (entity.getLogin() != null) {
                CustomLoginDeleteExcludeTable loginTable = repository.getFirstByLoginAndPlatform(entity.getLogin(),entity.getPlatform());
                if (loginTable != null) {
                  return;
                }

                loginList = new ArrayList<>();
                loginList.add(entity.getLogin());
            }
            if(entity.getCId() != null){
                CustomLoginDeleteExcludeTable cidTable = repository.findByCId(entity.getCId());
                if (cidTable != null) {
                    throw ServiceException.exception("cId已设置");
                }

                List<CustomLoginTable> login = customLoginRepository.findByCId(entity.getCId());
                if(login != null && login.size()>0){
                    platform = login.get(0).getPlatform();
                    loginList = login.stream().map(CustomLoginTable::getLogin).collect(Collectors.toList());
                }
            }
        }

        CustomLoginDeleteExcludeTable table = new CustomLoginDeleteExcludeTable();
        BeanUtils.copyProperties(entity, table);
        repository.save(table);

        if(loginList != null&&loginList.size()>0){
            customLoginMarkDeleteMapper.delCustomloginMarkDelete(loginList,platform);
            customLoginMarkDeleteMapper.batchUpdateCustomlogin(loginList,false, null,platform);
        }


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(CustomLoginDeleteExcludeDeleteEntitys entity) throws ServiceException {
        List<CustomLoginDeleteExcludeTable> 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(CustomLoginDeleteExcludeUpdateEntity entity) throws ServiceException {
        List<Long> loginList = null;
        String platform = entity.getPlatform();
        if (entity.getLogin() == null && entity.getCId() == null) {
            throw ServiceException.exception(Constants.PARAMETER_ERROR);
        }else{
            if (entity.getLogin() != null) {
                CustomLoginDeleteExcludeTable loginTable = repository.findByLogin(entity.getLogin());
                if (loginTable != null && loginTable.getId() != entity.getId()) {
                    throw ServiceException.exception("账户已设置");
                }

                loginList = new ArrayList<>();
                loginList.add(entity.getLogin());
            }
            if(entity.getCId() != null){
                CustomLoginDeleteExcludeTable cidTable = repository.findByCId(entity.getCId());
                if (cidTable != null && cidTable.getId() != entity.getId()) {
                    throw ServiceException.exception("cId已设置");
                }

                List<CustomLoginTable> login = customLoginRepository.findByCId(entity.getCId());
                if(login != null && login.size()>0){
                    platform = login.get(0).getPlatform();
                    loginList = login.stream().map(CustomLoginTable::getLogin).collect(Collectors.toList());
                }
            }
        }

        CustomLoginDeleteExcludeTable table = repository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        BeanUtils.copyProperties(entity, table);
        repository.save(table);

        if(loginList != null&&loginList.size()>0){
            customLoginMarkDeleteMapper.delCustomloginMarkDelete(loginList,platform);
            customLoginMarkDeleteMapper.batchUpdateCustomlogin(loginList,false, null,platform);
        }
    }

    @Override
    public ResultWithPagerDto<CustomLoginDeleteExcludeListDto> searchPageList(CustomLoginDeleteExcludeSearchEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<CustomLoginDeleteExcludeTable> tables = mapper.pageList(entity);
        if (tables == null || tables.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<CustomLoginDeleteExcludeListDto> CustomLoginDeleteExcludeListDtos = new ArrayList<>(tables.size());

        for (CustomLoginDeleteExcludeTable CustomLoginDeleteExcludeTable : tables) {
            CustomLoginDeleteExcludeListDto CustomLoginDeleteExcludeListDto = transformSearchListDto(CustomLoginDeleteExcludeTable);
            CustomLoginDeleteExcludeListDtos.add(CustomLoginDeleteExcludeListDto);
        }

        return ResultWithPagerDto.success(pageDto, CustomLoginDeleteExcludeListDtos);
    }

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

        return transformSearchListDto(table);
    }

    private CustomLoginDeleteExcludeListDto transformSearchListDto(CustomLoginDeleteExcludeTable CustomLoginDeleteExcludeTable) {
        CustomLoginDeleteExcludeListDto CustomLoginDeleteExcludeListDto = new CustomLoginDeleteExcludeListDto();
        BeanUtils.copyProperties(CustomLoginDeleteExcludeTable, CustomLoginDeleteExcludeListDto);
        return CustomLoginDeleteExcludeListDto;
    }


}
