package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysAgentLoginTypeMapper;
import com.crm.manager.dao.repository.SysAgentLoginTypeRepository;
import com.crm.manager.service.SysAgentLoginTypeService;
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.SysAgentLoginTypeListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.agent.logintype.SysAgentLoginTypeAddEntity;
import com.crm.rely.backend.core.entity.system.agent.logintype.SysAgentLoginTypeDeleteEntitys;
import com.crm.rely.backend.core.entity.system.agent.logintype.SysAgentLoginTypeUpdateEntity;
import com.crm.rely.backend.core.entity.system.logintype.SysLoginTypeSearchEntity;
import com.crm.rely.backend.core.pojo.table.SysAgentLoginTypeTable;
import com.crm.rely.backend.core.pojo.view.SysAgentLoginTypeListView;
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 SysAgentLoginTypeServiceImpl implements SysAgentLoginTypeService {

    @Autowired
    private SysAgentLoginTypeRepository repository;
    @Autowired
    private SysAgentLoginTypeMapper mapper;

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

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

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

        SysAgentLoginTypeTable 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 boolean existByAgentId(Long agentId) throws ServiceException {
        return mapper.existByAgentId(agentId) > 0 ? true : false;
    }

    @Override
    public ResultWithPagerDto<SysAgentLoginTypeListDto> 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<SysAgentLoginTypeListView> views = mapper.pageList(entity);
        if (views == null || views.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<SysAgentLoginTypeListDto> sysLoginTypeListDtos = new ArrayList<>(views.size());

        for (SysAgentLoginTypeListView view : views) {
            SysAgentLoginTypeListDto sysLoginTypeListDto = transformSearchListDto(view);
            sysLoginTypeListDtos.add(sysLoginTypeListDto);
        }

        return ResultWithPagerDto.success(pageDto, sysLoginTypeListDtos);
    }

    @Override
    public SysAgentLoginTypeListDto searchSingle(SingleLongEntity entity) throws ServiceException {
        SysAgentLoginTypeTable table = repository.getFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        SysAgentLoginTypeListDto sysLoginTypeListDto = new SysAgentLoginTypeListDto();
        BeanUtils.copyProperties(table, sysLoginTypeListDto);
        sysLoginTypeListDto.setLoginTypes(Arrays.asList(table.getLoginTypes().split(",")));

        return sysLoginTypeListDto;
    }

    private SysAgentLoginTypeListDto transformSearchListDto(SysAgentLoginTypeListView view) {
        SysAgentLoginTypeListDto sysLoginTypeListDto = new SysAgentLoginTypeListDto();
        BeanUtils.copyProperties(view, sysLoginTypeListDto);
        sysLoginTypeListDto.setLoginTypes(Arrays.asList(view.getLoginTypes().split(",")));
        return sysLoginTypeListDto;
    }


}
