package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysSalesLoginMapper;
import com.crm.manager.service.SysSalesLoginService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.sys.SysSalesLoginDto;
import com.crm.rely.backend.core.entity.sys.SysSalesLoginAddEntity;
import com.crm.rely.backend.core.entity.sys.SysSalesLoginSearchListEntity;
import com.crm.rely.backend.core.entity.sys.SysSalesLoginUpdateEntity;
import com.crm.rely.backend.core.pojo.table.SysSalesLoginTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.FormatPage;
import com.google.common.collect.Lists;
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.List;

@Service
public class SysSalesLoginServiceImpl implements SysSalesLoginService {

    @Autowired
    private SysSalesLoginMapper sysSalesLoginMapper;

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

        SysSalesLoginTable table = sysSalesLoginMapper.findByLogin(entity.getLogin());
        if (table != null) {
            throw ServiceException.exception(Constants.RECORD_EXISTS);
        }

        table = new SysSalesLoginTable();
        BeanUtils.copyProperties(entity, table);
        sysSalesLoginMapper.save(table);
    }

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

        if (entity.getLogin() == null) {return;}

        SysSalesLoginTable table = sysSalesLoginMapper.findByLogin(entity.getLogin());
        if (table != null) {
            BeanUtils.copyProperties(entity, table);
            table.setModifyTime(entity.getAddTime());
            sysSalesLoginMapper.update(table);
        }else {
            table = new SysSalesLoginTable();
            BeanUtils.copyProperties(entity, table);
            sysSalesLoginMapper.save(table);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysSalesLoginUpdateEntity entity) throws ServiceException {
        SysSalesLoginTable table = getTableById(entity.getId());

        if (!entity.getLogin().equals(table.getLogin())) {
            SysSalesLoginTable t = sysSalesLoginMapper.findByLogin(entity.getLogin());
            if (t != null) {
                throw ServiceException.exception(Constants.RECORD_EXISTS);
            }
        }

        BeanUtils.copyProperties(entity, table);
        sysSalesLoginMapper.update(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) throws ServiceException {
        getTableById(id);
        sysSalesLoginMapper.removeById(id);
    }

    @Override
    public SysSalesLoginDto searchSingle(Long id) throws ServiceException {

        SysSalesLoginTable table = sysSalesLoginMapper.findById(id);
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        return convertTableToDto(table);
    }

    @Override
    public BaseResultWithPagerDto<List<SysSalesLoginDto>> searchList(SysSalesLoginSearchListEntity entity) throws ServiceException {

        Integer count = sysSalesLoginMapper.countSearch(entity);
        PageDto pageDto = FormatPage.format(entity.getPage(), count);
        if (count == null || count == 0) {
            return new BaseResultWithPagerDto<>(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, pageDto);
        }

        List<SysSalesLoginTable> tables = sysSalesLoginMapper.listSearch(entity);
        List<SysSalesLoginDto> dtos = Lists.newArrayList();
        tables.forEach(table -> {
            dtos.add(convertTableToDto(table));
        });

        return new BaseResultWithPagerDto<>(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, pageDto, dtos);
    }

    private SysSalesLoginDto convertTableToDto(SysSalesLoginTable table) {
        SysSalesLoginDto dto = new SysSalesLoginDto();
        BeanUtils.copyProperties(table, dto);
        return dto;
    }

    private SysSalesLoginTable getTableById(Long id) throws ServiceException {
        SysSalesLoginTable table = sysSalesLoginMapper.findById(id);
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        return table;
    }
}
