package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysLoginTypeMapper;
import com.crm.manager.dao.repository.SysLoginTypeRepository;
import com.crm.manager.service.SysLoginTypeService;
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.logintype.SysLoginTypeListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.logintype.SysLoginTypeAddEntity;
import com.crm.rely.backend.core.entity.system.logintype.SysLoginTypeDeleteEntitys;
import com.crm.rely.backend.core.entity.system.logintype.SysLoginTypeSearchEntity;
import com.crm.rely.backend.core.entity.system.logintype.SysLoginTypeUpdateEntity;
import com.crm.rely.backend.core.pojo.table.SysLoginTypeTable;
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.Arrays;
import java.util.List;

@Service
public class SysLoginTypeServiceImpl implements SysLoginTypeService {

    @Autowired
    private SysLoginTypeRepository repository;
    @Autowired
    private SysLoginTypeMapper mapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysLoginTypeAddEntity entity) throws ServiceException {
        SysLoginTypeTable typeTable = repository.findByComPointAndHide(entity.getComPoint(), entity.getHide());
        if (typeTable != null) {
            throw ServiceException.exception("加点已设置");
        }

        SysLoginTypeTable table = new SysLoginTypeTable();
        BeanUtils.copyProperties(entity, table);
        table.setLoginTypes(String.join(",", entity.getLoginTypes()));
        repository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysLoginTypeDeleteEntitys entity) throws ServiceException {
        List<SysLoginTypeTable> 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(SysLoginTypeUpdateEntity entity) throws ServiceException {
        SysLoginTypeTable typeTable = repository.findByComPointAndHide(entity.getComPoint(), entity.getHide());
        if (typeTable != null && typeTable.getId() != entity.getId()) {
            throw ServiceException.exception("加点已设置");
        }

        SysLoginTypeTable table = repository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        BeanUtils.copyProperties(entity, table);
        table.setLoginTypes(String.join(",", entity.getLoginTypes()));
        repository.save(table);
    }

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

        for (SysLoginTypeTable sysLoginTypeTable : tables) {
            SysLoginTypeListDto sysLoginTypeListDto = transformSearchListDto(sysLoginTypeTable);
            sysLoginTypeListDtos.add(sysLoginTypeListDto);
        }

        return ResultWithPagerDto.success(pageDto, sysLoginTypeListDtos);
    }

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

        return transformSearchListDto(table);
    }

    private SysLoginTypeListDto transformSearchListDto(SysLoginTypeTable sysLoginTypeTable) {
        SysLoginTypeListDto sysLoginTypeListDto = new SysLoginTypeListDto();
        BeanUtils.copyProperties(sysLoginTypeTable, sysLoginTypeListDto);
        sysLoginTypeListDto.setLoginTypes(Arrays.asList(sysLoginTypeTable.getLoginTypes().split(",")));
        return sysLoginTypeListDto;
    }


}
